Skip to content

Commit b870d1f

Browse files
committed
Simplify :is with a single selector when possible
1 parent 94dcd6e commit b870d1f

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5614,6 +5614,24 @@ mod tests {
56145614
minify_test(":foo(bar) { color: yellow }", ":foo(bar){color:#ff0}");
56155615
minify_test("::foo(bar) { color: yellow }", "::foo(bar){color:#ff0}");
56165616
minify_test("::foo(*) { color: yellow }", "::foo(*){color:#ff0}");
5617+
5618+
minify_test(":is(.foo) { color: yellow }", ".foo{color:#ff0}");
5619+
minify_test(":is(#foo) { color: yellow }", "#foo{color:#ff0}");
5620+
minify_test("a:is(.foo) { color: yellow }", "a.foo{color:#ff0}");
5621+
minify_test("a:is([data-test]) { color: yellow }", "a[data-test]{color:#ff0}");
5622+
minify_test(".foo:is(a) { color: yellow }", ".foo:is(a){color:#ff0}");
5623+
minify_test(".foo:is(*|a) { color: yellow }", ".foo:is(a){color:#ff0}");
5624+
minify_test(".foo:is(*) { color: yellow }", ".foo:is(*){color:#ff0}");
5625+
minify_test(
5626+
"@namespace svg url(http://www.w3.org/2000/svg); .foo:is(svg|a) { color: yellow }",
5627+
"@namespace svg \"http://www.w3.org/2000/svg\";.foo:is(svg|a){color:#ff0}",
5628+
);
5629+
minify_test("a:is(.foo .bar) { color: yellow }", "a:is(.foo .bar){color:#ff0}");
5630+
minify_test(":is(.foo, .bar) { color: yellow }", ":is(.foo,.bar){color:#ff0}");
5631+
minify_test("a:is(:not(.foo)) { color: yellow }", "a:not(.foo){color:#ff0}");
5632+
minify_test("a:is(:first-child) { color: yellow }", "a:first-child{color:#ff0}");
5633+
minify_test("a:is(:has(.foo)) { color: yellow }", "a:has(.foo){color:#ff0}");
5634+
minify_test("a:is(:is(.foo)) { color: yellow }", "a.foo{color:#ff0}");
56175635
}
56185636

56195637
#[test]

src/selector.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,16 @@ impl<'a, 'i> ToCssWithContext<'a, 'i> for Component<'i, Selectors> {
11641164
Is(ref list) | Where(ref list) | Negation(ref list) | Any(_, ref list) => {
11651165
match *self {
11661166
Where(..) => dest.write_str(":where(")?,
1167-
Is(..) => {
1167+
Is(ref selectors) => {
1168+
// If there's only one simple selector, serialize it directly.
1169+
if selectors.len() == 1 {
1170+
let first = selectors.first().unwrap();
1171+
if !has_type_selector(first) && is_simple(first) {
1172+
serialize_selector(first, dest, context, false)?;
1173+
return Ok(());
1174+
}
1175+
}
1176+
11681177
let vp = dest.vendor_prefix;
11691178
if vp.intersects(VendorPrefix::WebKit | VendorPrefix::Moz) {
11701179
dest.write_char(':')?;

0 commit comments

Comments
 (0)