add Children() and ChildrenFiltered(), with tests

This commit is contained in:
Martin Angers
2012-08-29 14:15:28 -04:00
parent f696fbf5bb
commit 4cc3f30d4a
2 changed files with 43 additions and 17 deletions
+23 -17
View File
@@ -6,13 +6,18 @@ import (
//"fmt"
"net/http"
"net/url"
"strings"
)
// TODO : Ensure no node is added more than once in a selection (especially with Add...)
// TODO : Add the following methods:
// - Closest()
// - Parents()
// - Fix ChildrenFiltered, by forking Cascadia and adding a MatchSingle() method?
// - Contains() (static function?)
// - Contents() (similar to Children(), but includes text and comment nodes, so Children() should filter them out)
// - Each() should pass a Selection object over a single node, so that Attr() and such can be called
// - End()
// - Eq()
type Document struct {
Root *html.Node
@@ -68,25 +73,26 @@ func findWithContext(selector string, nodes ...*html.Node) []*html.Node {
func childrenWithContext(selector string, nodes ...*html.Node) []*html.Node {
var matches []*html.Node
//var allChildren bool
//var sel *cascadia.Selector
/*
if selector == "*" || selector == "" {
// Get all children
allChildren = true
} else {
if sel, e := cascadia.Compile(selector); e != nil {
// Selector doesn't compile, empty selection
return nil
}
var allChildren bool
var sel cascadia.Selector
var e error
selector = strings.TrimSpace(selector)
if selector == "*" || selector == "" {
// Get all children
allChildren = true
} else {
if sel, e = cascadia.Compile(selector); e != nil {
// Selector doesn't compile, empty selection
return nil
}
*/
}
for _, n := range nodes {
for _, nchild := range n.Child {
// TODO : At the moment, given Cascadia's API, cannot call Children with a selector string
//if allChildren /*|| sel(nchild)*/ {
matches = append(matches, nchild)
//}
if allChildren || sel(nchild) {
matches = append(matches, nchild)
}
}
}
return matches
+20
View File
@@ -108,3 +108,23 @@ func TestChildren(t *testing.T) {
}
}
}
func TestChildrenFiltered(t *testing.T) {
sel := doc.Find(".pvk-content").ChildrenFiltered(".hero-unit")
if len(sel.Nodes) != 1 {
t.Errorf("Expected 1 child nodes, got %v.", len(sel.Nodes))
for _, n := range sel.Nodes {
t.Logf("%+v", n)
}
}
}
func TestChildrenFilteredNone(t *testing.T) {
sel := doc.Find(".pvk-content").ChildrenFiltered("a.btn")
if len(sel.Nodes) != 0 {
t.Errorf("Expected 0 child node, got %v.", len(sel.Nodes))
for _, n := range sel.Nodes {
t.Logf("%+v", n)
}
}
}