mirror of
https://github.com/PuerkitoBio/goquery
synced 2025-11-01 20:22:34 +08:00
reorganize positional methods in array.go, tests.
This commit is contained in:
@@ -21,8 +21,11 @@ func (this *Selection) Eq(index int) *Selection {
|
|||||||
if index < 0 {
|
if index < 0 {
|
||||||
index += l
|
index += l
|
||||||
}
|
}
|
||||||
if index > -1 && index < l {
|
return this.Slice(index, index+1)
|
||||||
return newSingleSelection(this.Nodes[index], this.document)
|
}
|
||||||
}
|
|
||||||
return newEmptySelection(this.document)
|
// Slice() reduces the set of matched elements to a subset specified by a range of indices.
|
||||||
|
// At the moment, negative indices are not supported.
|
||||||
|
func (this *Selection) Slice(start int, end int) *Selection {
|
||||||
|
return pushStack(this, this.Nodes[start:end])
|
||||||
}
|
}
|
||||||
68
array_test.go
Normal file
68
array_test.go
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package goquery
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFirst(t *testing.T) {
|
||||||
|
sel := doc.Find(".pvk-content").First()
|
||||||
|
if len(sel.Nodes) != 1 {
|
||||||
|
t.Errorf("Expected 1 node, found %v.", len(sel.Nodes))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFirstEmpty(t *testing.T) {
|
||||||
|
defer func() {
|
||||||
|
if e := recover(); e == nil {
|
||||||
|
t.Error("Expected a panic, First() called on empty Selection.")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
doc.Find(".pvk-zzcontentzz").First()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLast(t *testing.T) {
|
||||||
|
sel := doc.Find(".pvk-content").Last()
|
||||||
|
if len(sel.Nodes) != 1 {
|
||||||
|
t.Errorf("Expected 1 node, found %v.", len(sel.Nodes))
|
||||||
|
}
|
||||||
|
// Should contain Footer
|
||||||
|
foot := doc.Find(".footer")
|
||||||
|
if !sel.Contains(foot.Nodes[0]) {
|
||||||
|
t.Error("Last .pvk-content should contain .footer.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEq(t *testing.T) {
|
||||||
|
sel := doc.Find(".pvk-content").Eq(1)
|
||||||
|
if len(sel.Nodes) != 1 {
|
||||||
|
t.Errorf("Expected 1 node, found %v.", len(sel.Nodes))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEqNegative(t *testing.T) {
|
||||||
|
sel := doc.Find(".pvk-content").Eq(-1)
|
||||||
|
if len(sel.Nodes) != 1 {
|
||||||
|
t.Errorf("Expected 1 node, found %v.", len(sel.Nodes))
|
||||||
|
}
|
||||||
|
// Should contain Footer
|
||||||
|
foot := doc.Find(".footer")
|
||||||
|
if !sel.Contains(foot.Nodes[0]) {
|
||||||
|
t.Error("Index -1 of .pvk-content should contain .footer.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSlice(t *testing.T) {
|
||||||
|
sel := doc.Find(".pvk-content").Slice(0, 2)
|
||||||
|
if len(sel.Nodes) != 2 {
|
||||||
|
t.Errorf("Expected 2 nodes, found %v.", len(sel.Nodes))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSliceOutOfBounds(t *testing.T) {
|
||||||
|
defer func() {
|
||||||
|
if e := recover(); e == nil {
|
||||||
|
t.Error("Expected a panic, Slice() called with out of bounds indices.")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
doc.Find(".pvk-content").Slice(2, 12)
|
||||||
|
}
|
||||||
18
doc.go
18
doc.go
@@ -37,14 +37,14 @@ necessary since multiple return values cannot be used to allow a chainable inter
|
|||||||
*/
|
*/
|
||||||
package goquery
|
package goquery
|
||||||
|
|
||||||
// Positional Manipulation: First(), Last(), Eq(), Get(), Index(), Slice()
|
// array.go : Positional Manipulation: First(), Last(), Eq(), Get(), Index(), Slice()
|
||||||
// Filtering: Filter(), Not(), Has(), End()
|
// filter.go : Filtering: Filter(), Not(), Has(), End()
|
||||||
// "Expanding": Add(), AndSelf()
|
// expand.go : "Expanding": Add(), AndSelf()
|
||||||
// Reflect (query) node: Is(), Contains(), HasClass()
|
// query.go : Reflect (query) node: Is(), Contains(), HasClass()
|
||||||
// Inspect node: Contents(), Html(), Text(), Attr(), Val()
|
// property.go : Inspect node: Contents(), Html(), Text(), Attr(), Val(), Length(), Size()
|
||||||
// Selection "properties": Length(), Size()
|
// traversal.go : Traversal: Find(), Children(), Parents...(), Next...(), Prev...(), Closest(), Siblings()
|
||||||
// Traversal: Find(), Children(), Parents...(), Next...(), Prev...(), Closest(), Siblings()
|
// iteration.go : Iteration: Each(), Map()
|
||||||
// Iteration: Each(), Map()
|
// type.go : Selection and Document
|
||||||
|
|
||||||
// TODO : Benchmarks
|
// TODO : Benchmarks
|
||||||
|
|
||||||
@@ -85,7 +85,7 @@ package goquery
|
|||||||
// - PrevUntil() - Tree traversal
|
// - PrevUntil() - Tree traversal
|
||||||
// x PushStack() ? - Internals
|
// x PushStack() ? - Internals
|
||||||
// - Siblings() - Tree traversal
|
// - Siblings() - Tree traversal
|
||||||
// - Slice() - Filtering
|
// x Slice() - Filtering
|
||||||
// - Text() - DOM Manipulation
|
// - Text() - DOM Manipulation
|
||||||
// x ToArray() Is not implemented, is Selection.Nodes
|
// x ToArray() Is not implemented, is Selection.Nodes
|
||||||
// x Unique() ? Or internally only, to remove duplicates and maintain node order? - Utilities
|
// x Unique() ? Or internally only, to remove duplicates and maintain node order? - Utilities
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
package goquery
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestFirst(t *testing.T) {
|
|
||||||
sel := doc.Find(".pvk-content").First()
|
|
||||||
if len(sel.Nodes) != 1 {
|
|
||||||
t.Errorf("Expected 1 node, found %v.", len(sel.Nodes))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFirstEmpty(t *testing.T) {
|
|
||||||
sel := doc.Find(".pvk-zzcontentzz").First()
|
|
||||||
if len(sel.Nodes) != 0 {
|
|
||||||
t.Errorf("Expected 0 node, found %v.", len(sel.Nodes))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user