ClosestMatcher gets the first element that matches the matcher by testing the element itself and traversing up through its ancestors in the DOM tree.
(m Matcher)
| 134 | // ClosestMatcher gets the first element that matches the matcher by testing the |
| 135 | // element itself and traversing up through its ancestors in the DOM tree. |
| 136 | func (s *Selection) ClosestMatcher(m Matcher) *Selection { |
| 137 | return pushStack(s, mapNodes(s.Nodes, func(i int, n *html.Node) []*html.Node { |
| 138 | // For each node in the selection, test the node itself, then each parent |
| 139 | // until a match is found. |
| 140 | for ; n != nil; n = n.Parent { |
| 141 | if m.Match(n) { |
| 142 | return []*html.Node{n} |
| 143 | } |
| 144 | } |
| 145 | return nil |
| 146 | })) |
| 147 | } |
| 148 | |
| 149 | // ClosestNodes gets the first element that matches one of the nodes by testing the |
| 150 | // element itself and traversing up through its ancestors in the DOM tree. |