mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2026-10-19 16:15:33 +00:00
60 lines
1.8 KiB
Go
60 lines
1.8 KiB
Go
package goquery
|
|
|
|
import (
|
|
"slices"
|
|
|
|
"golang.org/x/net/html"
|
|
)
|
|
|
|
// Is checks the current matched set of elements against a selector and
|
|
// returns true if at least one of these elements matches.
|
|
func (s *Selection) Is(selector string) bool {
|
|
return s.IsMatcher(compileMatcher(selector))
|
|
}
|
|
|
|
// IsMatcher checks the current matched set of elements against a matcher and
|
|
// returns true if at least one of these elements matches.
|
|
func (s *Selection) IsMatcher(m Matcher) bool {
|
|
return slices.ContainsFunc(s.Nodes, m.Match)
|
|
}
|
|
|
|
// 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 {
|
|
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 {
|
|
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 {
|
|
for _, n := range s.Nodes {
|
|
if isInSlice(nodes, n) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Contains returns true if the specified Node is within,
|
|
// at any depth, one of the nodes in the Selection object.
|
|
// It is NOT inclusive, to behave like jQuery's implementation, and
|
|
// unlike Javascript's .contains, so if the contained
|
|
// node is itself in the selection, it returns false.
|
|
func (s *Selection) Contains(n *html.Node) bool {
|
|
return sliceContains(s.Nodes, n)
|
|
}
|