diff --git a/src/editor/css_remove.rs b/src/editor/css_remove.rs index 5ac4879..da71356 100644 --- a/src/editor/css_remove.rs +++ b/src/editor/css_remove.rs @@ -7,7 +7,6 @@ impl CssRemover { fn parse(&self) -> anyhow::Result { let scrape_selector = scraper::Selector::parse(&self.0) .map_err(|err| anyhow::anyhow!("css remover ({}) parse error: {:?}", self.0, err))?; - Ok(scrape_selector) } @@ -17,16 +16,17 @@ impl CssRemover { } pub fn apply(&self, html: &str) -> anyhow::Result { - let parsed_html = scraper::Html::parse_document(html); + let mut parsed_html = scraper::Html::parse_document(html); let selected = parsed_html .select(&self.parse()?) - .map(|o| o.html()) + .map(|o| o.id()) .collect::>(); - - let mut html = parsed_html.root_element().html(); - for s in selected { - html = html.replace(&s, ""); + for selected in selected { + if let Some(mut selected_mut) = parsed_html.tree.get_mut(selected) { + selected_mut.detach(); + } } + let html = parsed_html.root_element().html(); Ok(html) } } @@ -96,3 +96,12 @@ fn multiple_selectors_inside_each_other_work() { r#"
B
"# ); } + +#[test] +fn multiple_hits_only_remove_exact() { + let html = CssRemover(".a p".to_string()).apply(r#"

TEST

TEST

"#).unwrap(); + assert_eq!( + html, + r#"

TEST

"# + ); +}