Skip to content

Commit

Permalink
Rename Element.Create to Element.CreateElementContinue
Browse files Browse the repository at this point in the history
  • Loading branch information
beevik committed Jan 21, 2025
1 parent bf1cb66 commit fd97f04
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
19 changes: 9 additions & 10 deletions etree.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,23 +764,22 @@ func (e *Element) CreateElement(tag string) *Element {
// use in the Element Create function.
type CreateContinuation func(e *Element)

// Create 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. After creating the element, Create calls the
// continuation function to perform additional actions on the created child
// 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.
//
// This method of creating elements is useful when building nested XML
// document from code. For example:
// This method of element creation is particularly useful when building nested
// XML documents from code. For example:
//
// org := doc.Create("organization", func(e *Element) {
// e.Create("person", func(e *Element) {
// org := doc.CreateElementContinue("organization", func(e *Element) {
// e.CreateElementContinue("person", func(e *Element) {
// e.CreateAttr("name", "Mary")
// e.CreateAttr("age", "30")
// e.CreateAttr("hair", "brown")
// })
// })
func (e *Element) Create(tag string, f CreateContinuation) *Element {
func (e *Element) CreateElementContinue(tag string, f CreateContinuation) *Element {
child := e.CreateElement(tag)
f(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,23 +1632,23 @@ func TestSiblingElement(t *testing.T) {

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

0 comments on commit fd97f04

Please sign in to comment.