Skip to content

Commit

Permalink
Rename AddElement to CreateChild
Browse files Browse the repository at this point in the history
  • Loading branch information
beevik committed Jan 25, 2025
1 parent 0da9e6a commit a775de4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
17 changes: 9 additions & 8 deletions etree.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,28 +752,29 @@ func (e *Element) findTermCharDataIndex(start int) int {
}

// CreateElement creates a new element with the specified tag (i.e., name) and
// adds it as the last child token of the element e. The tag may include a
// prefix followed by a colon.
// adds it as the last child of element 'e'. The tag may include a prefix
// followed by a colon.
func (e *Element) CreateElement(tag string) *Element {
space, stag := spaceDecompose(tag)
return newElement(space, stag, e)
}

// 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.
// CreateChild 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.AddElement("organization", func(e *Element) {
// e.AddElement("person", func(e *Element) {
// org := doc.CreateChild("organization", func(e *Element) {
// e.CreateComment("Mary")
// e.CreateChild("person", func(e *Element) {
// e.CreateAttr("name", "Mary")
// e.CreateAttr("age", "30")
// e.CreateAttr("hair", "brown")
// })
// })
func (e *Element) AddElement(tag string, cont func(e *Element)) *Element {
func (e *Element) CreateChild(tag string, cont func(e *Element)) *Element {
child := e.CreateElement(tag)
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.AddElement("root", func(e *Element) {
e.AddElement("child1", func(e *Element) {
root := doc.CreateChild("root", func(e *Element) {
e.CreateChild("child1", func(e *Element) {
e.CreateComment("Grandchildren of child #1")
e.AddElement("grandchild1", func(e *Element) {
e.CreateChild("grandchild1", func(e *Element) {
e.CreateAttr("attr1", "1")
e.CreateAttr("attr2", "2")
})
e.AddElement("grandchild2", func(e *Element) {
e.CreateChild("grandchild2", func(e *Element) {
e.CreateAttr("attr1", "3")
e.CreateAttr("attr2", "4")
})
})
e.AddElement("child2", func(e *Element) {
e.CreateChild("child2", func(e *Element) {
e.CreateComment("Grandchildren of child #2")
e.AddElement("grandchild1", func(e *Element) {
e.CreateChild("grandchild1", func(e *Element) {
e.CreateAttr("attr1", "5")
e.CreateAttr("attr2", "6")
})
e.AddElement("grandchild2", func(e *Element) {
e.CreateChild("grandchild2", func(e *Element) {
e.CreateAttr("attr1", "7")
e.CreateAttr("attr2", "8")
})
Expand Down

0 comments on commit a775de4

Please sign in to comment.