diff --git a/doc.go b/doc.go index 2b22967..284aa84 100644 --- a/doc.go +++ b/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 diff --git a/each.go b/each.go index 1a40ca8..47b037d 100644 --- a/each.go +++ b/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 +} diff --git a/filter.go b/filter.go index 9fea878..7fe5108 100644 --- a/filter.go +++ b/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 diff --git a/first.go b/first.go index 5f7569c..4babb05 100644 --- a/first.go +++ b/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 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. diff --git a/iterators.go b/iterators.go new file mode 100644 index 0000000..1a40ca8 --- /dev/null +++ b/iterators.go @@ -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 +} diff --git a/selection.go b/selection.go index b1d4d90..bb17658 100644 --- a/selection.go +++ b/selection.go @@ -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 {