Merge pull request #548 from jvoisin/shortcircuit

perf: short-circuit IsFunction, IsSelection, and IsNodes
This commit is contained in:
Martin Angers
2026-05-26 08:53:05 -04:00
committed by GitHub
+16 -3
View File
@@ -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,