Skip to content

Commit

Permalink
Element.Create returns the created element
Browse files Browse the repository at this point in the history
  • Loading branch information
beevik committed Jan 17, 2025
1 parent 4615956 commit bf1cb66
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
8 changes: 5 additions & 3 deletions etree.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,15 +773,17 @@ type CreateContinuation func(e *Element)
// This method of creating elements is useful when building nested XML
// document from code. For example:
//
// doc.Create("organization", func(e *Element) {
// org := doc.Create("organization", func(e *Element) {
// e.Create("person", func(e *Element) {
// e.CreateAttr("name", "Mary")
// e.CreateAttr("age", "30")
// e.CreateAttr("hair", "brown")
// })
// })
func (e *Element) Create(tag string, f CreateContinuation) {
f(e.CreateElement(tag))
func (e *Element) Create(tag string, f CreateContinuation) *Element {
child := e.CreateElement(tag)
f(child)
return child
}

// AddChild adds the token 't' as the last child of the element. If token 't'
Expand Down
5 changes: 3 additions & 2 deletions etree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1632,7 +1632,7 @@ func TestSiblingElement(t *testing.T) {

func TestContinuations(t *testing.T) {
doc := NewDocument()
doc.Create("root", func(e *Element) {
root := doc.Create("root", func(e *Element) {
e.Create("child1", func(e *Element) {
e.Create("grandchild1", func(e *Element) {
e.CreateAttr("attr1", "1")
Expand All @@ -1654,9 +1654,10 @@ func TestContinuations(t *testing.T) {
})
})
})
doc.IndentTabs()
checkStrEq(t, root.Tag, "root")

// Serialize the document to a string
doc.IndentTabs()
s, err := doc.WriteToString()
if err != nil {
t.Error("etree: failed to serialize document")
Expand Down

0 comments on commit bf1cb66

Please sign in to comment.