mirror of
https://github.com/PuerkitoBio/goquery
synced 2025-10-05 08:46:53 +08:00
add Map() Has() and more
This commit is contained in:
8
doc.go
8
doc.go
@@ -36,14 +36,14 @@ package goquery
|
||||
// x Find() : Complete with Selection object and Node object as selectors - Tree Traversal
|
||||
// x First() - Filtering
|
||||
// x Get() - Node (DOM) Manipulation
|
||||
// - Has() - Filtering
|
||||
// x Has() - Filtering
|
||||
// x HasClass() - Attributes
|
||||
// - Html() ? - Attributes
|
||||
// - Index() - DOM Manipulation
|
||||
// - Is() - Filtering
|
||||
// - Last() - Filtering
|
||||
// - Length() / Size() - jQUery property
|
||||
// - Map() - Filtering
|
||||
// x Last() - Filtering
|
||||
// x Length() / Size() - jQUery property
|
||||
// x Map() - Filtering
|
||||
// - Next() - Tree traversal
|
||||
// - NextAll() - Tree traversal
|
||||
// - NextUntil() - Tree traversal
|
||||
|
10
each.go
10
each.go
@@ -7,3 +7,13 @@ func (this *Selection) Each(f func(int, *Selection)) *Selection {
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
// Map() passes each element in the current matched set through a function,
|
||||
// producing a slice of string holding the returned values.
|
||||
func (this *Selection) Map(f func(int, *Selection) string) (result []string) {
|
||||
for i, n := range this.Nodes {
|
||||
result = append(result, f(i, newSingleSelection(n, this.document)))
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
16
filter.go
16
filter.go
@@ -60,8 +60,22 @@ func (this *Selection) FilterSelection(s *Selection) *Selection {
|
||||
func (this *Selection) Has(selector string) *Selection {
|
||||
sel := this.document.Find(selector)
|
||||
|
||||
return this.HasSelection(sel)
|
||||
}
|
||||
|
||||
func (this *Selection) HasNode(node *html.Node) *Selection {
|
||||
return this.FilterFunction(func(_ int, s *Selection) bool {
|
||||
// Add all nodes that contain one of the nodes selected by the Has() selector
|
||||
// Add all nodes that contain the node specified
|
||||
if s.Contains(node) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
func (this *Selection) HasSelection(sel *Selection) *Selection {
|
||||
return this.FilterFunction(func(_ int, s *Selection) bool {
|
||||
// Add all nodes that contain one of the nodes in the selection
|
||||
for _, n := range sel.Nodes {
|
||||
if s.Contains(n) {
|
||||
return true
|
||||
|
9
first.go
9
first.go
@@ -3,10 +3,13 @@ package goquery
|
||||
// First() reduces the set of matched elements to the first in the set.
|
||||
// It returns a new Selection object.
|
||||
func (this *Selection) First() *Selection {
|
||||
if len(this.Nodes) == 0 {
|
||||
return newEmptySelection(this.document)
|
||||
return this.Eq(0)
|
||||
}
|
||||
return newSingleSelection(this.Nodes[0], this.document)
|
||||
|
||||
// Last() reduces the set of matched elements to the last in the set.
|
||||
// It returns a new Selection object.
|
||||
func (this *Selection) Last() *Selection {
|
||||
return this.Eq(-1)
|
||||
}
|
||||
|
||||
// Eq() reduces the set of matched elements to the one at the specified index.
|
||||
|
9
iterators.go
Normal file
9
iterators.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package goquery
|
||||
|
||||
// Returns this (same Selection object)
|
||||
func (this *Selection) Each(f func(int, *Selection)) *Selection {
|
||||
for i, n := range this.Nodes {
|
||||
f(i, newSingleSelection(n, this.document))
|
||||
}
|
||||
return this
|
||||
}
|
@@ -7,6 +7,15 @@ import (
|
||||
type Selection struct {
|
||||
Nodes []*html.Node
|
||||
document *Document
|
||||
prevSel *Selection
|
||||
}
|
||||
|
||||
func (this *Selection) Size() int {
|
||||
return this.Length()
|
||||
}
|
||||
|
||||
func (this *Selection) Length() int {
|
||||
return len(this.Nodes)
|
||||
}
|
||||
|
||||
func newEmptySelection(doc *Document) *Selection {
|
||||
|
Reference in New Issue
Block a user