You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

query.go 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package goquery
  2. import "golang.org/x/net/html"
  3. // Is checks the current matched set of elements against a selector and
  4. // returns true if at least one of these elements matches.
  5. func (s *Selection) Is(selector string) bool {
  6. if len(s.Nodes) > 0 {
  7. return s.IsMatcher(compileMatcher(selector))
  8. }
  9. return false
  10. }
  11. // IsMatcher checks the current matched set of elements against a matcher and
  12. // returns true if at least one of these elements matches.
  13. func (s *Selection) IsMatcher(m Matcher) bool {
  14. if len(s.Nodes) > 0 {
  15. if len(s.Nodes) == 1 {
  16. return m.Match(s.Nodes[0])
  17. }
  18. return len(m.Filter(s.Nodes)) > 0
  19. }
  20. return false
  21. }
  22. // IsFunction checks the current matched set of elements against a predicate and
  23. // returns true if at least one of these elements matches.
  24. func (s *Selection) IsFunction(f func(int, *Selection) bool) bool {
  25. return s.FilterFunction(f).Length() > 0
  26. }
  27. // IsSelection checks the current matched set of elements against a Selection object
  28. // and returns true if at least one of these elements matches.
  29. func (s *Selection) IsSelection(sel *Selection) bool {
  30. return s.FilterSelection(sel).Length() > 0
  31. }
  32. // IsNodes checks the current matched set of elements against the specified nodes
  33. // and returns true if at least one of these elements matches.
  34. func (s *Selection) IsNodes(nodes ...*html.Node) bool {
  35. return s.FilterNodes(nodes...).Length() > 0
  36. }
  37. // Contains returns true if the specified Node is within,
  38. // at any depth, one of the nodes in the Selection object.
  39. // It is NOT inclusive, to behave like jQuery's implementation, and
  40. // unlike Javascript's .contains, so if the contained
  41. // node is itself in the selection, it returns false.
  42. func (s *Selection) Contains(n *html.Node) bool {
  43. return sliceContains(s.Nodes, n)
  44. }