Skip to content

Commit

Permalink
Merge pull request gocolly#166 from mumugoah/master
Browse files Browse the repository at this point in the history
Add ForEachWithBreak function to HTMLElement
  • Loading branch information
asciimoo authored Jun 14, 2018
2 parents 903f5b1 + bed8e7b commit 41323a4
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions htmlelement.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,21 @@ func (h *HTMLElement) ForEach(goquerySelector string, callback func(int, *HTMLEl
}
})
}

// ForEachWithBreak iterates over the elements matched by the first argument
// and calls the callback function on every HTMLElement match.
// It is identical to ForEach except that it is possible to break
// out of the loop by returning false in the callback function. It returns the
// current Selection object.
func (h *HTMLElement) ForEachWithBreak(goquerySelector string, callback func(int, *HTMLElement) bool) {
i := 0
h.DOM.Find(goquerySelector).EachWithBreak(func(_ int, s *goquery.Selection) bool {
for _, n := range s.Nodes {
if callback(i, NewHTMLElementFromSelectionNode(h.Response, s, n)) {
return true
}
i++
}
return false
})
}

0 comments on commit 41323a4

Please sign in to comment.