expand.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package goquery
  2. import (
  3. "github.com/andybalholm/cascadia"
  4. "golang.org/x/net/html"
  5. )
  6. // Add adds the selector string's matching nodes to those in the current
  7. // selection and returns a new Selection object.
  8. // The selector string is run in the context of the document of the current
  9. // Selection object.
  10. func (s *Selection) Add(selector string) *Selection {
  11. return s.AddNodes(findWithMatcher([]*html.Node{s.document.rootNode}, cascadia.MustCompile(selector))...)
  12. }
  13. // AddMatcher adds the matcher's matching nodes to those in the current
  14. // selection and returns a new Selection object.
  15. // The matcher is run in the context of the document of the current
  16. // Selection object.
  17. func (s *Selection) AddMatcher(m Matcher) *Selection {
  18. return s.AddNodes(findWithMatcher([]*html.Node{s.document.rootNode}, m)...)
  19. }
  20. // AddSelection adds the specified Selection object's nodes to those in the
  21. // current selection and returns a new Selection object.
  22. func (s *Selection) AddSelection(sel *Selection) *Selection {
  23. if sel == nil {
  24. return s.AddNodes()
  25. }
  26. return s.AddNodes(sel.Nodes...)
  27. }
  28. // Union is an alias for AddSelection.
  29. func (s *Selection) Union(sel *Selection) *Selection {
  30. return s.AddSelection(sel)
  31. }
  32. // AddNodes adds the specified nodes to those in the
  33. // current selection and returns a new Selection object.
  34. func (s *Selection) AddNodes(nodes ...*html.Node) *Selection {
  35. return pushStack(s, appendWithoutDuplicates(s.Nodes, nodes))
  36. }
  37. // AndSelf adds the previous set of elements on the stack to the current set.
  38. // It returns a new Selection object containing the current Selection combined
  39. // with the previous one.
  40. func (s *Selection) AndSelf() *Selection {
  41. return s.AddSelection(s.prevSel)
  42. }