From 4fc27178e4a63a74f0dc6002509e01a93975a437 Mon Sep 17 00:00:00 2001 From: xml Date: Thu, 9 Jan 2020 17:33:21 +0800 Subject: [PATCH] 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 --- ast/ast.go | 4 +++- ast/ast_test.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 ast/ast_test.go 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()) +}