示例#1
0
// Find finds all Nodes that match this sequence in the tree rooted by
// n.
func (s Sequence) Find(n *html.Node) []*html.Node {
	var found []*html.Node
	h5.WalkNodes(n, func(n *html.Node) {
		if s.Match(n) {
			found = append(found, n)
		}
	})
	return found
}
示例#2
0
// Find all the nodes in a html.Node tree that match this Selector Link.
func (l Link) Find(n *html.Node) []*html.Node {
	var found []*html.Node
	switch l.Combinator {
	case Descendant:
		// walk the node tree returning any nodes the sequence matches
		h5.WalkNodes(n, func(n *html.Node) {
			if l.Sequence.Match(n) {
				found = append(found, n)
			}
		})
	case Child:
		// iterate through the children returning any nodes the sequence matches
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			if l.Sequence.Match(c) {
				found = append(found, c)
			}
		}
	case AdjacentSibling:
		// look at the two adjacent siblings if any and return any that the squence matches.
		if l.Sequence.Match(n.PrevSibling) {
			found = append(found, n.PrevSibling)
		}
		if l.Sequence.Match(n.NextSibling) {
			found = append(found, n.NextSibling)
		}
	case Sibling:
		// Look at all the siblings if any and return any that the sequence matches.
		for s := n.PrevSibling; s != nil; s = s.PrevSibling {
			if l.Sequence.Match(s) {
				found = append([]*html.Node{s}, found...)
			}
		}
		for s := n.NextSibling; s != nil; s = s.NextSibling {
			if l.Sequence.Match(s) {
				found = append(found, s)
			}
		}
	}
	return found
}