Skip to content

Commit

Permalink
Merge pull request #87 from qiuzhiqian/master
Browse files Browse the repository at this point in the history
fix bug: BaseNode.RemoveChildren only set the first child as nil
  • Loading branch information
yuin authored Jan 9, 2020
2 parents 3e38e96 + 4fc2717 commit 5294fa5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,12 @@ func (n *BaseNode) RemoveChild(self, v Node) {

// RemoveChildren implements Node.RemoveChildren .
func (n *BaseNode) RemoveChildren(self Node) {
for c := n.firstChild; c != nil; c = c.NextSibling() {
for c := n.firstChild; c != nil; {
c.SetParent(nil)
c.SetPreviousSibling(nil)
next := c.NextSibling()
c.SetNextSibling(nil)
c = next
}
n.firstChild = nil
n.lastChild = nil
Expand Down
18 changes: 18 additions & 0 deletions ast/ast_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ast

import "testing"

func TestRemoveChildren(t *testing.T) {
root := NewDocument()

node1 := NewDocument()

node2 := NewDocument()

root.AppendChild(root, node1)
root.AppendChild(root, node2)

root.RemoveChildren(root)

t.Logf("%+v", node2.PreviousSibling())
}

0 comments on commit 5294fa5

Please sign in to comment.