diff --git a/query.go b/query.go index fd05108..2e2b25e 100644 --- a/query.go +++ b/query.go @@ -220,6 +220,7 @@ func (x *NodeNavigator) Copy() xpath.NodeNavigator { func (x *NodeNavigator) MoveToRoot() { x.curr = x.root + x.attr = -1 } func (x *NodeNavigator) MoveToParent() bool { diff --git a/root_attribute_cursor_test.go b/root_attribute_cursor_test.go new file mode 100644 index 0000000..cd56e2f --- /dev/null +++ b/root_attribute_cursor_test.go @@ -0,0 +1,38 @@ +package xmlquery + +import ( + "github.com/antchfx/xpath" + "strings" + "testing" +) + +func TestMoveToRootClearsAttributeCursor(t *testing.T) { + doc, err := Parse(strings.NewReader(``)) + if err != nil { + t.Fatal(err) + } + for _, top := range []*Node{doc, FindOne(doc, "//root")} { + nav := CreateXPathNavigator(top) + if top == doc { + nav.MoveToChild() + for nav.NodeType() != xpath.ElementNode { + if !nav.MoveToNext() { + t.Fatal("missing element") + } + } + } + if !nav.MoveToNextAttribute() { + t.Fatal("missing attribute") + } + nav.MoveToRoot() + if got := nav.NodeType(); got != CreateXPathNavigator(top).NodeType() { + t.Errorf("root type=%v", got) + } + if !nav.MoveToChild() { + t.Fatal("root retained attribute cursor, cannot descend") + } + if nav.Current().Parent != top { + t.Fatal("wrong root child") + } + } +}