mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2024-04-21 12:31:36 +00:00
Eq now returns an empty Selector when an invalid index is given
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
+17
-2
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user