add Map() Has() and more

This commit is contained in:
Martin Angers
2012-08-30 13:43:44 -04:00
parent 07f9164340
commit c3f897ba31
6 changed files with 54 additions and 9 deletions

8
doc.go
View File

@@ -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
View File

@@ -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
}

View File

@@ -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

View File

@@ -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 newSingleSelection(this.Nodes[0], this.document)
return this.Eq(0)
}
// 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
View 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
}

View File

@@ -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 {