mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2026-10-20 00:16:32 +00:00
Merge pull request #548 from jvoisin/shortcircuit
perf: short-circuit IsFunction, IsSelection, and IsNodes
This commit is contained in:
@@ -24,19 +24,32 @@ func (s *Selection) IsMatcher(m Matcher) bool {
|
||||
// IsFunction checks the current matched set of elements against a predicate and
|
||||
// returns true if at least one of these elements matches.
|
||||
func (s *Selection) IsFunction(f func(int, *Selection) bool) bool {
|
||||
return s.FilterFunction(f).Length() > 0
|
||||
for i, n := range s.Nodes {
|
||||
if f(i, newSingleSelection(n, s.document)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IsSelection checks the current matched set of elements against a Selection object
|
||||
// and returns true if at least one of these elements matches.
|
||||
func (s *Selection) IsSelection(sel *Selection) bool {
|
||||
return s.FilterSelection(sel).Length() > 0
|
||||
if sel == nil {
|
||||
return false
|
||||
}
|
||||
return s.IsNodes(sel.Nodes...)
|
||||
}
|
||||
|
||||
// IsNodes checks the current matched set of elements against the specified nodes
|
||||
// and returns true if at least one of these elements matches.
|
||||
func (s *Selection) IsNodes(nodes ...*html.Node) bool {
|
||||
return s.FilterNodes(nodes...).Length() > 0
|
||||
for _, n := range s.Nodes {
|
||||
if isInSlice(nodes, n) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Contains returns true if the specified Node is within,
|
||||
|
||||
Reference in New Issue
Block a user