diff --git a/ast/ast.go b/ast/ast.go index 4f1d885..3ba6447 100644 --- a/ast/ast.go +++ b/ast/ast.go @@ -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 diff --git a/ast/ast_test.go b/ast/ast_test.go new file mode 100644 index 0000000..77a80f6 --- /dev/null +++ b/ast/ast_test.go @@ -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()) +}