From 4cf2a2a4b4db1e37a2f85d075bf1d71ac01aac65 Mon Sep 17 00:00:00 2001
From: Yusuf Efe <120668197+yuefdev@users.noreply.github.com>
Date: Mon, 19 Oct 2026 01:08:52 +0300
Subject: [PATCH] Clear the attribute cursor when moving to the root
---
navigator_root_test.go | 27 +++++++++++++++++++++++++++
query.go | 1 +
2 files changed, 28 insertions(+)
create mode 100644 navigator_root_test.go
diff --git a/navigator_root_test.go b/navigator_root_test.go
new file mode 100644
index 0000000..d162971
--- /dev/null
+++ b/navigator_root_test.go
@@ -0,0 +1,27 @@
+package htmlquery
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/antchfx/xpath"
+)
+
+func TestMoveToRootFromAttribute(t *testing.T) {
+ doc, err := Parse(strings.NewReader(`
`))
+ if err != nil {
+ t.Fatal(err)
+ }
+ iter := xpath.MustCompile("//div/@id").Select(CreateXPathNavigator(doc))
+ if !iter.MoveNext() {
+ t.Fatal("attribute not found")
+ }
+ nav := iter.Current().Copy()
+ nav.MoveToRoot()
+ if !nav.MoveToChild() {
+ t.Fatal("root still has an attribute cursor and cannot move to its child")
+ }
+ if nav.NodeType() != xpath.ElementNode || nav.LocalName() != "html" {
+ t.Fatalf("unexpected child after root navigation: %v", nav)
+ }
+}
diff --git a/query.go b/query.go
index cb8b337..4930aa1 100644
--- a/query.go
+++ b/query.go
@@ -301,6 +301,7 @@ func (h *NodeNavigator) Copy() xpath.NodeNavigator {
func (h *NodeNavigator) MoveToRoot() {
h.curr = h.root
+ h.attr = -1
}
func (h *NodeNavigator) MoveToParent() bool {