Skip to content

Commit bf1cb66

Browse files
committed
Element.Create returns the created element
1 parent 4615956 commit bf1cb66

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

etree.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -773,15 +773,17 @@ type CreateContinuation func(e *Element)
773773
// This method of creating elements is useful when building nested XML
774774
// document from code. For example:
775775
//
776-
// doc.Create("organization", func(e *Element) {
776+
// org := doc.Create("organization", func(e *Element) {
777777
// e.Create("person", func(e *Element) {
778778
// e.CreateAttr("name", "Mary")
779779
// e.CreateAttr("age", "30")
780780
// e.CreateAttr("hair", "brown")
781781
// })
782782
// })
783-
func (e *Element) Create(tag string, f CreateContinuation) {
784-
f(e.CreateElement(tag))
783+
func (e *Element) Create(tag string, f CreateContinuation) *Element {
784+
child := e.CreateElement(tag)
785+
f(child)
786+
return child
785787
}
786788

787789
// AddChild adds the token 't' as the last child of the element. If token 't'

etree_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,7 +1632,7 @@ func TestSiblingElement(t *testing.T) {
16321632

16331633
func TestContinuations(t *testing.T) {
16341634
doc := NewDocument()
1635-
doc.Create("root", func(e *Element) {
1635+
root := doc.Create("root", func(e *Element) {
16361636
e.Create("child1", func(e *Element) {
16371637
e.Create("grandchild1", func(e *Element) {
16381638
e.CreateAttr("attr1", "1")
@@ -1654,9 +1654,10 @@ func TestContinuations(t *testing.T) {
16541654
})
16551655
})
16561656
})
1657-
doc.IndentTabs()
1657+
checkStrEq(t, root.Tag, "root")
16581658

16591659
// Serialize the document to a string
1660+
doc.IndentTabs()
16601661
s, err := doc.WriteToString()
16611662
if err != nil {
16621663
t.Error("etree: failed to serialize document")

0 commit comments

Comments
 (0)