mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2026-10-19 16:15:33 +00:00
Merge pull request #600 from udf2457/manipulateNodes
Clone and reverse nodes slice for safe manipulation
This commit is contained in:
+5
-4
@@ -1,6 +1,7 @@
|
||||
package goquery
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
@@ -618,11 +619,11 @@ func (s *Selection) manipulateNodes(ns []*html.Node, reverse bool,
|
||||
|
||||
// net.Html doesn't provide document fragments for insertion, so to get
|
||||
// things in the correct order with After() and Prepend(), the callback
|
||||
// needs to be called on the reverse of the nodes.
|
||||
// needs to be called on the reverse of the nodes. Clone the slice before
|
||||
// reversing so the caller's slice is not modified.
|
||||
if reverse {
|
||||
for i, j := 0, len(ns)-1; i < j; i, j = i+1, j-1 {
|
||||
ns[i], ns[j] = ns[j], ns[i]
|
||||
}
|
||||
ns = slices.Clone(ns)
|
||||
slices.Reverse(ns)
|
||||
}
|
||||
|
||||
for i, sn := range s.Nodes {
|
||||
|
||||
Reference in New Issue
Block a user