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.

103 lines
2.9 KiB

  1. package goquery
  2. import (
  3. "golang.org/x/net/html"
  4. )
  5. // First reduces the set of matched elements to the first in the set.
  6. // It returns a new Selection object, and an empty Selection object if the
  7. // the selection is empty.
  8. func (s *Selection) First() *Selection {
  9. return s.Eq(0)
  10. }
  11. // Last reduces the set of matched elements to the last in the set.
  12. // It returns a new Selection object, and an empty Selection object if
  13. // the selection is empty.
  14. func (s *Selection) Last() *Selection {
  15. return s.Eq(-1)
  16. }
  17. // Eq reduces the set of matched elements to the one at the specified index.
  18. // If a negative index is given, it counts backwards starting at the end of the
  19. // set. It returns a new Selection object, and an empty Selection object if the
  20. // index is invalid.
  21. func (s *Selection) Eq(index int) *Selection {
  22. if index < 0 {
  23. index += len(s.Nodes)
  24. }
  25. if index >= len(s.Nodes) || index < 0 {
  26. return newEmptySelection(s.document)
  27. }
  28. return s.Slice(index, index+1)
  29. }
  30. // Slice reduces the set of matched elements to a subset specified by a range
  31. // of indices.
  32. func (s *Selection) Slice(start, end int) *Selection {
  33. if start < 0 {
  34. start += len(s.Nodes)
  35. }
  36. if end < 0 {
  37. end += len(s.Nodes)
  38. }
  39. return pushStack(s, s.Nodes[start:end])
  40. }
  41. // Get retrieves the underlying node at the specified index.
  42. // Get without parameter is not implemented, since the node array is available
  43. // on the Selection object.
  44. func (s *Selection) Get(index int) *html.Node {
  45. if index < 0 {
  46. index += len(s.Nodes) // Negative index gets from the end
  47. }
  48. return s.Nodes[index]
  49. }
  50. // Index returns the position of the first element within the Selection object
  51. // relative to its sibling elements.
  52. func (s *Selection) Index() int {
  53. if len(s.Nodes) > 0 {
  54. return newSingleSelection(s.Nodes[0], s.document).PrevAll().Length()
  55. }
  56. return -1
  57. }
  58. // IndexSelector returns the position of the first element within the
  59. // Selection object relative to the elements matched by the selector, or -1 if
  60. // not found.
  61. func (s *Selection) IndexSelector(selector string) int {
  62. if len(s.Nodes) > 0 {
  63. sel := s.document.Find(selector)
  64. return indexInSlice(sel.Nodes, s.Nodes[0])
  65. }
  66. return -1
  67. }
  68. // IndexMatcher returns the position of the first element within the
  69. // Selection object relative to the elements matched by the matcher, or -1 if
  70. // not found.
  71. func (s *Selection) IndexMatcher(m Matcher) int {
  72. if len(s.Nodes) > 0 {
  73. sel := s.document.FindMatcher(m)
  74. return indexInSlice(sel.Nodes, s.Nodes[0])
  75. }
  76. return -1
  77. }
  78. // IndexOfNode returns the position of the specified node within the Selection
  79. // object, or -1 if not found.
  80. func (s *Selection) IndexOfNode(node *html.Node) int {
  81. return indexInSlice(s.Nodes, node)
  82. }
  83. // IndexOfSelection returns the position of the first node in the specified
  84. // Selection object within this Selection object, or -1 if not found.
  85. func (s *Selection) IndexOfSelection(sel *Selection) int {
  86. if sel != nil && len(sel.Nodes) > 0 {
  87. return indexInSlice(s.Nodes, sel.Nodes[0])
  88. }
  89. return -1
  90. }