diff --git a/array.go b/array.go index 3eb3c3a..10f036f 100644 --- a/array.go +++ b/array.go @@ -24,6 +24,11 @@ func (this *Selection) Eq(index int) *Selection { if index < 0 { index += len(this.Nodes) } + + if index >= len(this.Nodes) || index < 0 { + return newEmptySelection(this.document) + } + return this.Slice(index, index+1) } diff --git a/array_test.go b/array_test.go index 13b6ca2..4237dc6 100644 --- a/array_test.go +++ b/array_test.go @@ -10,8 +10,8 @@ func TestFirst(t *testing.T) { } func TestFirstEmpty(t *testing.T) { - defer AssertPanic(t) - Doc().Find(".pvk-zzcontentzz").First() + sel := Doc().Find(".pvk-zzcontentzz").First() + AssertLength(t, sel.Nodes, 0) } func TestFirstRollback(t *testing.T) { @@ -53,6 +53,21 @@ func TestEqNegative(t *testing.T) { } } +func TestEqEmpty(t *testing.T) { + sel := Doc().Find("something_random_that_does_not_exists").Eq(0) + AssertLength(t, sel.Nodes, 0) +} + +func TestEqInvalidPositive(t *testing.T) { + sel := Doc().Find(".pvk-content").Eq(3) + AssertLength(t, sel.Nodes, 0) +} + +func TestEqInvalidNegative(t *testing.T) { + sel := Doc().Find(".pvk-content").Eq(-4) + AssertLength(t, sel.Nodes, 0) +} + func TestEqRollback(t *testing.T) { sel := Doc().Find(".pvk-content") sel2 := sel.Eq(1).End()