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.

39 lines
1.4 KiB

  1. package goquery
  2. // Each iterates over a Selection object, executing a function for each
  3. // matched element. It returns the current Selection object. The function
  4. // f is called for each element in the selection with the index of the
  5. // element in that selection starting at 0, and a *Selection that contains
  6. // only that element.
  7. func (s *Selection) Each(f func(int, *Selection)) *Selection {
  8. for i, n := range s.Nodes {
  9. f(i, newSingleSelection(n, s.document))
  10. }
  11. return s
  12. }
  13. // EachWithBreak iterates over a Selection object, executing a function for each
  14. // matched element. It is identical to Each except that it is possible to break
  15. // out of the loop by returning false in the callback function. It returns the
  16. // current Selection object.
  17. func (s *Selection) EachWithBreak(f func(int, *Selection) bool) *Selection {
  18. for i, n := range s.Nodes {
  19. if !f(i, newSingleSelection(n, s.document)) {
  20. return s
  21. }
  22. }
  23. return s
  24. }
  25. // Map passes each element in the current matched set through a function,
  26. // producing a slice of string holding the returned values. The function
  27. // f is called for each element in the selection with the index of the
  28. // element in that selection starting at 0, and a *Selection that contains
  29. // only that element.
  30. func (s *Selection) Map(f func(int, *Selection) string) (result []string) {
  31. for i, n := range s.Nodes {
  32. result = append(result, f(i, newSingleSelection(n, s.document)))
  33. }
  34. return result
  35. }