Appends the new nodes to the target slice, making sure no duplicate is added. There is no check to the original state of the target slice, so it may still contain duplicates. The target slice is returned because append() may create a new underlying array. If targetSet is nil, a local set is created
(target []*html.Node, nodes []*html.Node, targetSet map[*html.Node]bool)
| 129 | // a new underlying array. If targetSet is nil, a local set is created with the |
| 130 | // target if len(target) + len(nodes) is greater than minNodesForSet. |
| 131 | func appendWithoutDuplicates(target []*html.Node, nodes []*html.Node, targetSet map[*html.Node]bool) []*html.Node { |
| 132 | // if there are not that many nodes, don't use the map, faster to just use nested loops |
| 133 | // (unless a non-nil targetSet is passed, in which case the caller knows better). |
| 134 | if targetSet == nil && len(target)+len(nodes) < minNodesForSet { |
| 135 | for _, n := range nodes { |
| 136 | if !isInSlice(target, n) { |
| 137 | target = append(target, n) |
| 138 | } |
| 139 | } |
| 140 | return target |
| 141 | } |
| 142 | |
| 143 | // if a targetSet is passed, then assume it is reliable, otherwise create one |
| 144 | // and initialize it with the current target contents. |
| 145 | if targetSet == nil { |
| 146 | targetSet = make(map[*html.Node]bool, len(target)) |
| 147 | for _, n := range target { |
| 148 | targetSet[n] = true |
| 149 | } |
| 150 | } |
| 151 | for _, n := range nodes { |
| 152 | if !targetSet[n] { |
| 153 | target = append(target, n) |
| 154 | targetSet[n] = true |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return target |
| 159 | } |
| 160 | |
| 161 | // Loop through a selection, returning only those nodes that pass the predicate |
| 162 | // function. |