mirror of
https://github.com/PuerkitoBio/goquery
synced 2025-10-05 08:46:53 +08:00
add Contents() and Children() with tests.
This commit is contained in:
57
traversal.go
57
traversal.go
@@ -3,7 +3,6 @@ package goquery
|
||||
import (
|
||||
"code.google.com/p/cascadia"
|
||||
"exp/html"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// TODO : Maybe make the Document's Root return a Selection, Find() on the
|
||||
@@ -40,24 +39,52 @@ func findWithContext(selector string, nodes ...*html.Node) []*html.Node {
|
||||
return matches
|
||||
}
|
||||
|
||||
// TODO : Tests and doc for contents and children
|
||||
|
||||
// Contents() gets the children of each element in the Selection,
|
||||
// including text and comment nodes. It returns a new Selection object
|
||||
// containing these elements.
|
||||
func (this *Selection) Contents() *Selection {
|
||||
var matches []*html.Node
|
||||
|
||||
for _, n := range this.Nodes {
|
||||
matches = appendWithoutDuplicates(matches, getChildren(n, false))
|
||||
}
|
||||
return pushStack(this, matches)
|
||||
return pushStack(this, getSelectionChildren(this, false))
|
||||
}
|
||||
|
||||
func (this *Selection) Children() *Selection {
|
||||
var matches []*html.Node
|
||||
|
||||
for _, n := range this.Nodes {
|
||||
matches = appendWithoutDuplicates(matches, getChildren(n, true))
|
||||
// ContentsFiltered() gets the children of each element in the Selection,
|
||||
// filtered by the specified selector. It returns a new Selection
|
||||
// object containing these elements. Since selectors only act on Element nodes,
|
||||
// this function is an alias to ChildrenFiltered() unless the selector is empty,
|
||||
// in which case it is an alias to Contents().
|
||||
func (this *Selection) ContentsFiltered(selector string) *Selection {
|
||||
if selector != "" {
|
||||
return this.ChildrenFiltered(selector)
|
||||
}
|
||||
return pushStack(this, matches)
|
||||
return this.Contents()
|
||||
}
|
||||
|
||||
// Children() gets the child elements of each element in the Selection.
|
||||
// It returns a new Selection object containing these elements.
|
||||
func (this *Selection) Children() *Selection {
|
||||
return pushStack(this, getSelectionChildren(this, true))
|
||||
}
|
||||
|
||||
// ChildrenFiltered() gets the child elements of each element in the Selection,
|
||||
// filtered by the specified selector. It returns a new
|
||||
// Selection object containing these elements.
|
||||
func (this *Selection) ChildrenFiltered(selector string) *Selection {
|
||||
// Get the Children() unfiltered
|
||||
nodes := getSelectionChildren(this, true)
|
||||
// Create a temporary Selection to filter using winnow
|
||||
sel := &Selection{nodes, this.document, nil}
|
||||
// Filter based on selector
|
||||
nodes = winnow(sel, selector, true)
|
||||
// Push on the stack and return the "real" Selection
|
||||
return pushStack(this, nodes)
|
||||
}
|
||||
|
||||
// Return the child nodes of each node in the Selection object, without
|
||||
// duplicates.
|
||||
func getSelectionChildren(s *Selection, elemOnly bool) (result []*html.Node) {
|
||||
for _, n := range s.Nodes {
|
||||
result = appendWithoutDuplicates(result, getChildren(n, elemOnly))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Return the immediate children of the node, filtered on element nodes only
|
||||
|
Reference in New Issue
Block a user