expand.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package goquery
  2. import "golang.org/x/net/html"
  3. // Add adds the selector string's matching nodes to those in the current
  4. // selection and returns a new Selection object.
  5. // The selector string is run in the context of the document of the current
  6. // Selection object.
  7. func (s *Selection) Add(selector string) *Selection {
  8. return s.AddNodes(findWithMatcher([]*html.Node{s.document.rootNode}, compileMatcher(selector))...)
  9. }
  10. // AddMatcher adds the matcher's matching nodes to those in the current
  11. // selection and returns a new Selection object.
  12. // The matcher is run in the context of the document of the current
  13. // Selection object.
  14. func (s *Selection) AddMatcher(m Matcher) *Selection {
  15. return s.AddNodes(findWithMatcher([]*html.Node{s.document.rootNode}, m)...)
  16. }
  17. // AddSelection adds the specified Selection object's nodes to those in the
  18. // current selection and returns a new Selection object.
  19. func (s *Selection) AddSelection(sel *Selection) *Selection {
  20. if sel == nil {
  21. return s.AddNodes()
  22. }
  23. return s.AddNodes(sel.Nodes...)
  24. }
  25. // Union is an alias for AddSelection.
  26. func (s *Selection) Union(sel *Selection) *Selection {
  27. return s.AddSelection(sel)
  28. }
  29. // AddNodes adds the specified nodes to those in the
  30. // current selection and returns a new Selection object.
  31. func (s *Selection) AddNodes(nodes ...*html.Node) *Selection {
  32. return pushStack(s, appendWithoutDuplicates(s.Nodes, nodes, nil))
  33. }
  34. // AndSelf adds the previous set of elements on the stack to the current set.
  35. // It returns a new Selection object containing the current Selection combined
  36. // with the previous one.
  37. // Deprecated: This function has been deprecated and is now an alias for AddBack().
  38. func (s *Selection) AndSelf() *Selection {
  39. return s.AddBack()
  40. }
  41. // AddBack adds the previous set of elements on the stack to the current set.
  42. // It returns a new Selection object containing the current Selection combined
  43. // with the previous one.
  44. func (s *Selection) AddBack() *Selection {
  45. return s.AddSelection(s.prevSel)
  46. }
  47. // AddBackFiltered reduces the previous set of elements on the stack to those that
  48. // match the selector string, and adds them to the current set.
  49. // It returns a new Selection object containing the current Selection combined
  50. // with the filtered previous one
  51. func (s *Selection) AddBackFiltered(selector string) *Selection {
  52. return s.AddSelection(s.prevSel.Filter(selector))
  53. }
  54. // AddBackMatcher reduces the previous set of elements on the stack to those that match
  55. // the mateher, and adds them to the curernt set.
  56. // It returns a new Selection object containing the current Selection combined
  57. // with the filtered previous one
  58. func (s *Selection) AddBackMatcher(m Matcher) *Selection {
  59. return s.AddSelection(s.prevSel.FilterMatcher(m))
  60. }