diff --git a/doc.go b/doc.go index 9f39690..5f29bb1 100644 --- a/doc.go +++ b/doc.go @@ -44,19 +44,19 @@ file: https://github.com/puerkitobio/goquery The various methods are split into files based on the category of behavior: * array.go : array-like positional manipulation of the selection. - - First() - - Last() - Eq() + - First() - Get() - Index...() + - Last() - Slice() * filter.go : filtering methods, that reduce the selection's set. - - Filter...() - - Not...() - - Has...() - End() + - Filter...() + - Has...() - Intersection(), which is an alias of FilterSelection() + - Not...() * expand.go : methods that expand or augment the selection's set. - Add...() @@ -70,7 +70,6 @@ The various methods are split into files based on the category of behavior: * property.go : methods that inspect and get the node's properties values. - Attr() - - Contents() - Html() - Length() - Size(), which is an alias for Length() @@ -79,6 +78,7 @@ The various methods are split into files based on the category of behavior: * traversal.go : methods to traverse the HTML document tree. - Children...() - Closest() + - Contents() - Find...() - Next...() - Parent[s]...() @@ -99,8 +99,8 @@ package goquery // DONE filter.go : Filtering: Filter(), Not(), Has(), End() // DONE expand.go : "Expanding": Add(), AndSelf() // DONE query.go : Reflect (query) node: Is(), Contains(), HasClass() -// property.go : Inspect node: Contents(), Html(), Text(), Attr(), Length(), Size() -// traversal.go : Traversal: Find(), Children(), Parents...(), Next...(), Prev...(), Closest(), Siblings() +// DONE property.go : Inspect node: Html(), Text(), Attr(), Length(), Size() +// traversal.go : Traversal: Contents(), Find(), Children(), Parents...(), Next...(), Prev...(), Closest(), Siblings() // DONE iteration.go : Iteration: Each(), Map() // DONE type.go : Selection and Document diff --git a/traversal.go b/traversal.go index 0381304..bcfe2de 100644 --- a/traversal.go +++ b/traversal.go @@ -40,47 +40,35 @@ func findWithContext(selector string, nodes ...*html.Node) []*html.Node { return matches } -// TODO : Filtered using Node and other Selection object +// TODO : Tests and doc for contents and children -// Returns a new Selection object. -func (this *Document) Children() *Selection { - return this.ChildrenFiltered("") -} - -// Returns a new Selection object. -func (this *Selection) Children() *Selection { - return this.ChildrenFiltered("") -} - -// Returns a new Selection object. -func (this *Document) ChildrenFiltered(selector string) *Selection { - return &Selection{childrenWithContext(selector, this.Root), this, nil} -} - -// Returns a new Selection object. -func (this *Selection) ChildrenFiltered(selector string) *Selection { - return &Selection{childrenWithContext(selector, this.Nodes...), this.document, nil} -} - -func childrenWithContext(selector string, nodes ...*html.Node) []*html.Node { +func (this *Selection) Contents() *Selection { var matches []*html.Node - var allChildren bool - var sel cascadia.Selector - selector = strings.TrimSpace(selector) - if selector == "*" || selector == "" { - // Get all children - allChildren = true - } else { - sel = cascadia.MustCompile(selector) + for _, n := range this.Nodes { + matches = appendWithoutDuplicates(matches, getChildren(n, false)) } + return pushStack(this, matches) +} - for _, n := range nodes { - for _, nchild := range n.Child { - if allChildren || sel(nchild) { - matches = append(matches, nchild) +func (this *Selection) Children() *Selection { + var matches []*html.Node + + for _, n := range this.Nodes { + matches = appendWithoutDuplicates(matches, getChildren(n, true)) + } + return pushStack(this, matches) +} + +// Return the immediate children of the node, filtered on element nodes only +// if requested. The result is necessarily a slice of unique nodes. +func getChildren(n *html.Node, elemOnly bool) (result []*html.Node) { + if n != nil { + for _, c := range n.Child { + if c.Type == html.ElementNode || !elemOnly { + result = append(result, c) } } } - return matches + return }