kuchiki-rs / kuchiki

(朽木) HTML/XML tree manipulation library for Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Remove all elements with selector

8176135 opened this issue · comments

How to remove every element matching a css-selector?
Currently if I detach() a node in the select() loop, it just stops.
i.e.

for css_match in document.select(".someclass").unwrap() {
    css_match.as_node().detach();
    // Stops after one iteration, even if there are multiple in the document.
}

Do I have to keep running select until all instances are removed?

For a one-line fix I suspect it would be more efficient to collect the elements to remove into a Vec before removing any (compared to running select repeatedly).

Better still would be a custom iterator (for example, if an element is to be removed there is no need to look at its descendants since they would be removed from the original tree with it), but that’s more work to build.

I didn't realize you could save css_match into a Vec and then delete it that way, it works for me, thanks!