From 28528c31bb87511238d071e77a9695dd190e20fe Mon Sep 17 00:00:00 2001 From: Yusuf Efe <120668197+yuefdev@users.noreply.github.com> Date: Sun, 18 Oct 2026 18:28:50 +0300 Subject: [PATCH] Preserve the selection stack for out-of-range Eq calls Signed-off-by: Yusuf Efe <120668197+yuefdev@users.noreply.github.com> --- array.go | 2 +- array_test.go | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/array.go b/array.go index f75be617..e98a6491 100644 --- a/array.go +++ b/array.go @@ -38,7 +38,7 @@ func (s *Selection) Eq(index int) *Selection { } if index >= len(s.Nodes) || index < 0 { - return newEmptySelection(s.document) + return pushStack(s, nil) } return s.Slice(index, index+1) diff --git a/array_test.go b/array_test.go index 7857b38f..3586bee9 100644 --- a/array_test.go +++ b/array_test.go @@ -94,6 +94,17 @@ func TestEqRollback(t *testing.T) { assertEqual(t, sel, sel2) } +func TestEqOutOfRangeRollback(t *testing.T) { + for _, sel := range []*Selection{Doc().Find(".pvk-content"), Doc().Find("missing")} { + for _, index := range []int{sel.Length(), -sel.Length() - 1} { + empty := sel.Eq(index) + assertLength(t, empty.Nodes, 0) + assertEqual(t, sel, empty.End()) + assertLength(t, empty.AddBack().Nodes, sel.Length()) + } + } +} + func TestSlice(t *testing.T) { sel := Doc().Find(".pvk-content").Slice(0, 2)