Collect NodeRefs so that they all get visited

main
Charlotte Som 2022-04-28 20:03:26 +01:00
parent dc914b9977
commit 0c75749ae5
2 changed files with 4 additions and 3 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "rewrite-html-blocks" name = "rewrite-html-blocks"
version = "1.0.0" version = "1.0.1"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View File

@ -7,6 +7,7 @@ pub fn rewrite_tags(html: &str, selector: &str, mapping: impl Fn(&NodeRef) -> St
for elem in document for elem in document
.select(selector) .select(selector)
.expect("Failed to select document with given selector") .expect("Failed to select document with given selector")
.collect::<Vec<_>>()
{ {
let node = elem.as_node(); let node = elem.as_node();
let new_contents = kuchiki::parse_fragment( let new_contents = kuchiki::parse_fragment(
@ -48,8 +49,8 @@ mod tests {
#[test] #[test]
fn remap_block() { fn remap_block() {
let html = r#"<p>hello, world! <span id="replace-me">REPLACE ME</span></p>"#; let html = r#"<p>hello, world! <span class="replace-me">REPLACE ME</span> <span class="replace-me me-too">me too!</span></p>"#;
let rewritten_html = rewrite_tags(html, "#replace-me", |_| { let rewritten_html = rewrite_tags(html, ".replace-me", |_| {
"<strong>it's a me!</strong>".to_string() "<strong>it's a me!</strong>".to_string()
}); });