kuchiki-rs / kuchiki

(朽木) HTML/XML tree manipulation library for Rust
MIT License
470 stars 54 forks source link

Remove all elements with selector #57

Closed 8176135 closed 5 years ago

8176135 commented 5 years ago

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?

SimonSapin commented 5 years ago

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.

8176135 commented 5 years ago

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