From 48de68c2b7bcec8d5d257cba8143353fb7047bfc Mon Sep 17 00:00:00 2001 From: tnt Date: Fri, 28 Feb 2014 21:54:48 +0100 Subject: [PATCH 1/3] Eq now returns an empty Selector when an invalid index is given --- array.go | 5 +++++ array_test.go | 19 +++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) 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() From 076667ebf2ef07d7d8f7db30a8e30819194cedbe Mon Sep 17 00:00:00 2001 From: tnt Date: Fri, 28 Feb 2014 21:55:18 +0100 Subject: [PATCH 2/3] Updated docs for First() and Last() --- array.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/array.go b/array.go index 10f036f..6917c7b 100644 --- a/array.go +++ b/array.go @@ -5,13 +5,15 @@ import ( ) // First() reduces the set of matched elements to the first in the set. -// It returns a new Selection object. +// It returns a new Selection object, and an empty Selection object if the +// the selection is empty. func (this *Selection) First() *Selection { return this.Eq(0) } // Last() reduces the set of matched elements to the last in the set. -// It returns a new Selection object. +// It returns a new Selection object, and an empty Selection object if +// the selection is empty. func (this *Selection) Last() *Selection { return this.Eq(-1) } From a6cc89ff5801cc7b806f6b68832d2e71716729e0 Mon Sep 17 00:00:00 2001 From: tnt Date: Fri, 28 Feb 2014 21:55:47 +0100 Subject: [PATCH 3/3] Added test TestLastEmpty --- array_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/array_test.go b/array_test.go index 4237dc6..721affd 100644 --- a/array_test.go +++ b/array_test.go @@ -31,6 +31,11 @@ func TestLast(t *testing.T) { } } +func TestLastEmpty(t *testing.T) { + sel := Doc().Find(".pvk-zzcontentzz").Last() + AssertLength(t, sel.Nodes, 0) +} + func TestLastRollback(t *testing.T) { sel := Doc().Find(".pvk-content") sel2 := sel.Last().End()