mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2024-04-21 12:31:36 +00:00
add Children() and ChildrenFiltered(), with tests
This commit is contained in:
+23
-17
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user