mirror of
https://github.com/PuerkitoBio/goquery
synced 2025-10-05 00:42:50 +08:00
add Get() to array.go, with tests
This commit is contained in:
18
array.go
18
array.go
@@ -1,5 +1,9 @@
|
||||
package goquery
|
||||
|
||||
import (
|
||||
"exp/html"
|
||||
)
|
||||
|
||||
// First() reduces the set of matched elements to the first in the set.
|
||||
// It returns a new Selection object.
|
||||
func (this *Selection) First() *Selection {
|
||||
@@ -16,10 +20,8 @@ func (this *Selection) Last() *Selection {
|
||||
// If a negative index is given, it counts backwards starting at the end of the set.
|
||||
// It returns a new Selection object, and an empty Selection object if the index is invalid.
|
||||
func (this *Selection) Eq(index int) *Selection {
|
||||
var l = len(this.Nodes)
|
||||
|
||||
if index < 0 {
|
||||
index += l
|
||||
index += len(this.Nodes)
|
||||
}
|
||||
return this.Slice(index, index+1)
|
||||
}
|
||||
@@ -29,3 +31,13 @@ func (this *Selection) Eq(index int) *Selection {
|
||||
func (this *Selection) Slice(start int, end int) *Selection {
|
||||
return pushStack(this, this.Nodes[start:end])
|
||||
}
|
||||
|
||||
// Get() retrieves the underlying node at the specified index.
|
||||
// Get() without parameter is not implemented, since the node array is available
|
||||
// on the Selection object.
|
||||
func (this *Selection) Get(index int) *html.Node {
|
||||
if index < 0 {
|
||||
index += len(this.Nodes) // Negative index gets from the end
|
||||
}
|
||||
return this.Nodes[index]
|
||||
}
|
||||
|
Reference in New Issue
Block a user