Skip to content

Commit

Permalink
Rename CreateElementContinue to AddElement
Browse files Browse the repository at this point in the history
  • Loading branch information
beevik committed Jan 21, 2025
1 parent f6baac9 commit 29c58a3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
20 changes: 7 additions & 13 deletions etree.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,29 +759,23 @@ func (e *Element) CreateElement(tag string) *Element {
return newElement(space, stag, e)
}

// CreateContinuation is a continuation-passing interface for building
// XML documents in a more nested manner. See the description of its
// use in the Element Create function.
type CreateContinuation func(e *Element)

// CreateElementContinue performs the same task as CreateElement but calls
// a continuation function after the child element is created, allowing
// additional actions to be performed on the child element before
// returning.
// AddElement performs the same task as CreateElement but calls a continuation
// function after the child element is created, allowing additional actions to
// be performed on the child element before returning.
//
// This method of element creation is particularly useful when building nested
// XML documents from code. For example:
//
// org := doc.CreateElementContinue("organization", func(e *Element) {
// e.CreateElementContinue("person", func(e *Element) {
// org := doc.AddElement("organization", func(e *Element) {
// e.AddElement("person", func(e *Element) {
// e.CreateAttr("name", "Mary")
// e.CreateAttr("age", "30")
// e.CreateAttr("hair", "brown")
// })
// })
func (e *Element) CreateElementContinue(tag string, f CreateContinuation) *Element {
func (e *Element) AddElement(tag string, cont func(e *Element)) *Element {
child := e.CreateElement(tag)
f(child)
cont(child)
return child
}

Expand Down
14 changes: 7 additions & 7 deletions etree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1632,25 +1632,25 @@ func TestSiblingElement(t *testing.T) {

func TestContinuations(t *testing.T) {
doc := NewDocument()
root := doc.CreateElementContinue("root", func(e *Element) {
e.CreateElementContinue("child1", func(e *Element) {
root := doc.AddElement("root", func(e *Element) {
e.AddElement("child1", func(e *Element) {
e.CreateComment("Grandchildren of child #1")
e.CreateElementContinue("grandchild1", func(e *Element) {
e.AddElement("grandchild1", func(e *Element) {
e.CreateAttr("attr1", "1")
e.CreateAttr("attr2", "2")
})
e.CreateElementContinue("grandchild2", func(e *Element) {
e.AddElement("grandchild2", func(e *Element) {
e.CreateAttr("attr1", "3")
e.CreateAttr("attr2", "4")
})
})
e.CreateElementContinue("child2", func(e *Element) {
e.AddElement("child2", func(e *Element) {
e.CreateComment("Grandchildren of child #2")
e.CreateElementContinue("grandchild1", func(e *Element) {
e.AddElement("grandchild1", func(e *Element) {
e.CreateAttr("attr1", "5")
e.CreateAttr("attr2", "6")
})
e.CreateElementContinue("grandchild2", func(e *Element) {
e.AddElement("grandchild2", func(e *Element) {
e.CreateAttr("attr1", "7")
e.CreateAttr("attr2", "8")
})
Expand Down

0 comments on commit 29c58a3

Please sign in to comment.