Skip to content

Commit

Permalink
fix: do not abort selector filtering after the first match
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Thomas committed Dec 30, 2024
1 parent 69007e1 commit eb4412b
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ impl Critters {

if filtered_selectors.is_empty() {
rules_to_remove.insert(style_rule.id());
break;
continue;
} else {
style_rule.selectors = SelectorList::new(filtered_selectors.into());
}
Expand Down Expand Up @@ -848,6 +848,40 @@ mod tests {
assert!(!stylesheet.contains(".non-critical"));
}

#[test]
fn complex() {
let critters = Critters::new(Default::default());

let html = construct_html(
r#"<style>
.red { color: red; }
.green { color: green; }
.blue { color: blue; }
.purple { color: purple; }
.link-underline > a { text-decoration: underline; }
</style>"#,
r#"<div>
<h1 class="red">This is a heading</h1>
<p class="purple link-underline">
This is some body text
<a>This should be underlined</a>
</p>
</div>"#,
);

let processed = critters.process(&html).unwrap();

let parser = kuchikiki::parse_html();
let dom = parser.one(processed);
let stylesheet = dom.select_first("style").unwrap().text_contents();

assert!(stylesheet.contains(".red"));
assert!(stylesheet.contains(".purple"));
assert!(stylesheet.contains(".link-underline>a"));
assert!(!stylesheet.contains(".green"));
assert!(!stylesheet.contains(".blue"));
}

#[test]
fn font_preload() {
let html = construct_html(
Expand Down

0 comments on commit eb4412b

Please sign in to comment.