From 2017916f6940bfa0ae2cf97c2851d62cdbeb6a83 Mon Sep 17 00:00:00 2001 From: Sasha Mitchell Date: Fri, 9 Oct 2026 20:03:01 +0700 Subject: [PATCH] fix: MoveToFirst reports success when already on the first sibling The method returned true without moving. xmlquery and htmlquery return false in that case, so a caller can tell the navigator did not move. --- query.go | 5 +++++ query_test.go | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/query.go b/query.go index 12e87f3..6910013 100644 --- a/query.go +++ b/query.go @@ -148,6 +148,11 @@ func (a *NodeNavigator) MoveToChild() bool { } func (a *NodeNavigator) MoveToFirst() bool { + // Already on the first sibling. xmlquery and htmlquery return false + // here so a caller can tell the navigator did not move. + if a.cur.PrevSibling == nil { + return false + } for n := a.cur.PrevSibling; n != nil; n = n.PrevSibling { a.cur = n } diff --git a/query_test.go b/query_test.go index c56bd0a..d6d8f9e 100644 --- a/query_test.go +++ b/query_test.go @@ -22,6 +22,23 @@ func BenchmarkDisableSelectorCache(b *testing.B) { } } +func TestMoveToFirst(t *testing.T) { + doc, err := parseString(`{"a":1,"b":2}`) + require.NoError(t, err) + + nav := CreateXPathNavigator(doc) + require.True(t, nav.MoveToChild()) + require.Equal(t, "a", nav.Current().Data) + // Already on the first sibling. The move did not happen. + require.False(t, nav.MoveToFirst()) + require.Equal(t, "a", nav.Current().Data) + + require.True(t, nav.MoveToNext()) + require.Equal(t, "b", nav.Current().Data) + require.True(t, nav.MoveToFirst()) + require.Equal(t, "a", nav.Current().Data) +} + func TestNavigator(t *testing.T) { s := `{ "name":"John",