Skip to content

Commit

Permalink
fix bug: if do c.SetNextSibling(nil) and c = c.NextSibling(). After r…
Browse files Browse the repository at this point in the history
…emove the first child the var c is always nil.the next child will not be set nil
  • Loading branch information
xml committed Jan 9, 2020
1 parent 3e38e96 commit 4fc2717
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 4fc2717

Please sign in to comment.