mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2024-04-21 12:31:36 +00:00
finalize iterators, with tests and comments.
This commit is contained in:
@@ -102,7 +102,7 @@ package goquery
|
||||
// DONE query.go : Reflect (query) node: Is(), Contains(), HasClass()
|
||||
// property.go : Inspect node: Contents(), Html(), Text(), Attr(), ** Does it make sense in a static HTML tree? Val()**, Length(), Size()
|
||||
// traversal.go : Traversal: Find(), Children(), Parents...(), Next...(), Prev...(), Closest(), Siblings()
|
||||
// iteration.go : Iteration: Each(), Map()
|
||||
// DONE iteration.go : Iteration: Each(), Map()
|
||||
// type.go : Selection and Document
|
||||
|
||||
// TODO : Benchmarks
|
||||
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
package goquery
|
||||
|
||||
// Returns this (same Selection object)
|
||||
// Each() iterates over a Selection object, executing a function for each
|
||||
// matched element.
|
||||
func (this *Selection) Each(f func(int, *Selection)) *Selection {
|
||||
for i, n := range this.Nodes {
|
||||
f(i, newSingleSelection(n, this.document))
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package goquery
|
||||
|
||||
import (
|
||||
"exp/html"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -19,3 +20,22 @@ func TestEach(t *testing.T) {
|
||||
t.Errorf("Expected 6 matching nodes, found %v.", len(sel.Nodes))
|
||||
}
|
||||
}
|
||||
|
||||
func TestMap(t *testing.T) {
|
||||
sel := Doc().Find(".pvk-content")
|
||||
vals := sel.Map(func(i int, s *Selection) string {
|
||||
n := s.Get(0)
|
||||
if n.Type == html.ElementNode {
|
||||
return n.Data
|
||||
}
|
||||
return ""
|
||||
})
|
||||
for _, v := range vals {
|
||||
if v != "div" {
|
||||
t.Error("Expected Map array result to be all 'div's.")
|
||||
}
|
||||
}
|
||||
if len(vals) != 3 {
|
||||
t.Errorf("Expected Map array result to have a length of 3, found %v.", len(vals))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user