Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions traversal.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,12 +676,27 @@ func getChildrenWithSiblingType(parent *html.Node, st siblingType, skipNode *htm

// Internal implementation of parent nodes that return a raw slice of Nodes.
func getParentNodes(nodes []*html.Node) []*html.Node {
return mapNodes(nodes, func(i int, n *html.Node) []*html.Node {
if n.Parent != nil && n.Parent.Type == html.ElementNode {
return []*html.Node{n.Parent}
switch len(nodes) {
case 0:
return nil
case 1:
if p := nodes[0].Parent; p != nil && p.Type == html.ElementNode {
return []*html.Node{p}
}
return nil
})
}

// Sibling nodes share a parent, so dedup directly into a set instead of
// allocating a throwaway one-element slice per node through mapNodes.
var result []*html.Node
set := make(map[*html.Node]bool, len(nodes))
for _, n := range nodes {
if p := n.Parent; p != nil && p.Type == html.ElementNode && !set[p] {
set[p] = true
result = append(result, p)
}
}
return result
}

// Internal map function used by many traversing methods. Takes the source nodes
Expand Down
Loading