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
14 changes: 12 additions & 2 deletions manipulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,20 @@ func (s *Selection) Empty() *Selection {
var nodes []*html.Node

for _, n := range s.Nodes {
for c := n.FirstChild; c != nil; c = n.FirstChild {
n.RemoveChild(c)
// Detach all children in one pass. Since each child is being
// removed from its only parent and ends up with no siblings,
// we can inline what RemoveChild does for the FirstChild case
// and skip the per-iter FirstChild/LastChild bookkeeping.
for c := n.FirstChild; c != nil; {
next := c.NextSibling
c.Parent = nil
c.PrevSibling = nil
c.NextSibling = nil
nodes = append(nodes, c)
c = next
}
n.FirstChild = nil
n.LastChild = nil
}

return pushStack(s, nodes)
Expand Down
Loading