fix bug: if do c.SetNextSibling(nil) and c = c.NextSibling(). After remove the first child the var c is always nil.the next child will not be set nil

This commit is contained in:
xml 2020-01-09 17:33:21 +08:00
parent 3e38e966f6
commit 4fc27178e4
2 changed files with 21 additions and 1 deletions

View file

@ -236,10 +236,12 @@ func (n *BaseNode) RemoveChild(self, v Node) {
// RemoveChildren implements Node.RemoveChildren . // RemoveChildren implements Node.RemoveChildren .
func (n *BaseNode) RemoveChildren(self Node) { 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.SetParent(nil)
c.SetPreviousSibling(nil) c.SetPreviousSibling(nil)
next := c.NextSibling()
c.SetNextSibling(nil) c.SetNextSibling(nil)
c = next
} }
n.firstChild = nil n.firstChild = nil
n.lastChild = nil n.lastChild = nil

18
ast/ast_test.go Normal file
View file

@ -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())
}