diff --git a/doc.go b/doc.go index bd4117d..6deb00e 100644 --- a/doc.go +++ b/doc.go @@ -128,7 +128,7 @@ package goquery // x HasClass() // - Html() ? - Attributes // x Index() -// - Is() - Filtering +// x Is() - Filtering // x Last() // x Length() / Size() // x Map() @@ -145,7 +145,7 @@ package goquery // x PushStack() // - Siblings() - Tree traversal // x Slice() -// - Text() - DOM Manipulation +// x Text() - DOM Manipulation // x ToArray() // x Unique() internally only // - Val() ? - Attributes diff --git a/traversal.go b/traversal.go index e4d740a..0381304 100644 --- a/traversal.go +++ b/traversal.go @@ -6,24 +6,36 @@ import ( "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 { 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 { - 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 { var matches []*html.Node sel := cascadia.MustCompile(selector) // Match the selector on each node 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 } diff --git a/traversal_test.go b/traversal_test.go index 13f794e..a04687e 100644 --- a/traversal_test.go +++ b/traversal_test.go @@ -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) { defer func() { if e := recover(); e == nil {