mirror of
https://github.com/PuerkitoBio/goquery
synced 2025-10-05 00:42:50 +08:00
fix Find() so that is searches only descendents
This commit is contained in:
4
doc.go
4
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
|
||||
|
22
traversal.go
22
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
|
||||
}
|
||||
|
@@ -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 {
|
||||
|
Reference in New Issue
Block a user