fix Find() so that is searches only descendents

This commit is contained in:
Martin Angers
2012-09-01 20:00:44 -04:00
parent 505516529b
commit 3631dc402c
3 changed files with 26 additions and 7 deletions

4
doc.go
View File

@@ -128,7 +128,7 @@ package goquery
// x HasClass() // x HasClass()
// - Html() ? - Attributes // - Html() ? - Attributes
// x Index() // x Index()
// - Is() - Filtering // x Is() - Filtering
// x Last() // x Last()
// x Length() / Size() // x Length() / Size()
// x Map() // x Map()
@@ -145,7 +145,7 @@ package goquery
// x PushStack() // x PushStack()
// - Siblings() - Tree traversal // - Siblings() - Tree traversal
// x Slice() // x Slice()
// - Text() - DOM Manipulation // x Text() - DOM Manipulation
// x ToArray() // x ToArray()
// x Unique() internally only // x Unique() internally only
// - Val() ? - Attributes // - Val() ? - Attributes

View File

@@ -6,24 +6,36 @@ import (
"strings" "strings"
) )
// Returns a new Selection object // TODO : Maybe make the Document's Root return a Selection, Find() on the
// Document is not very useful (would need all the same funcs as Selection)
func (this *Document) Find(selector string) *Selection { func (this *Document) Find(selector string) *Selection {
return &Selection{findWithContext(selector, this.Root), this, nil} return &Selection{findWithContext(selector, this.Root), this, nil}
} }
// Returns a new Selection object // Find() gets the descendants of each element in the current set of matched
// elements, filtered by a selector. It returns a new Selection object
// containing these matched elements.
func (this *Selection) Find(selector string) *Selection { func (this *Selection) Find(selector string) *Selection {
return &Selection{findWithContext(selector, this.Nodes...), this.document, nil} return pushStack(this, findWithContext(selector, this.Nodes...))
} }
// Private internal implementation of the various Find() methods func (this *Selection) FindSelection(sel *Selection) *Selection {
return nil
}
// Private internal implementation of the Find() methods
func findWithContext(selector string, nodes ...*html.Node) []*html.Node { func findWithContext(selector string, nodes ...*html.Node) []*html.Node {
var matches []*html.Node var matches []*html.Node
sel := cascadia.MustCompile(selector) sel := cascadia.MustCompile(selector)
// Match the selector on each node // Match the selector on each node
for _, n := range nodes { for _, n := range nodes {
matches = append(matches, sel.MatchAll(n)...) // Go down one level, becausejQuery's Find() selects only within descendants
for _, c := range n.Child {
if c.Type == html.ElementNode {
matches = appendWithoutDuplicates(matches, sel.MatchAll(c))
}
}
} }
return matches return matches
} }

View File

@@ -11,6 +11,13 @@ func TestFind(t *testing.T) {
} }
} }
func TestFindNotSelf(t *testing.T) {
sel := Doc().Find("h1").Find("h1")
if len(sel.Nodes) > 0 {
t.Errorf("Expected no node, found %v.", len(sel.Nodes))
}
}
func TestFindInvalidSelector(t *testing.T) { func TestFindInvalidSelector(t *testing.T) {
defer func() { defer func() {
if e := recover(); e == nil { if e := recover(); e == nil {