w3c / csswg-drafts

CSS Working Group Editor Drafts
https://drafts.csswg.org/
Other
4.43k stars 656 forks source link

[css-selectors] Why does `:is()` have the specificity of the most specific selector in its arguments? #3325

Open ExE-Boss opened 5 years ago

ExE-Boss commented 5 years ago

https://drafts.csswg.org/selectors-4/#matches

I believe it would make more sense if the specificity would be identical to that when the selector would be expanded into all possible combinations, which also makes polyfilling using PostCSS easier.

Loirooriol commented 5 years ago

See #1027. The expansion into all possible combinations can grow exponentially, which is bad.

fantasai commented 5 years ago

The alternative would be taking the specificity of the least-specific selector in its arguments. But yeah, what @Loirooriol said.

fantasai commented 5 years ago

Closing out, please re-open if there's something to fix.

pygy commented 4 years ago

While exponentially large Cartesian products can be problematic, the current spec also has drawbacks:

1) It lends itself to priority hacking :is(.foo, a:is(b#cant-match-but#lots-of#dummy#ids)) lets one match only .foo with a very high priority. Not sure if it is a feature or a bug... 2) it can't be accurately polyfilled

:is(.foo, #bar) {color: red;}
.foo {color: blue;}

gives a red .foo, but a manual expansion to

.foo {color: red;}
#bar {color: red;}
.foo {color: blue;}

gives a blue one. (Expanding to multiple rulesets because :is() is fault tolerant, unlike the plain selector lists).

This can be mitigated per sheet, but if the definitions are in distinct sheets it will bite people.

3) The way nesting with & is currently implemented in SASS and Postcss also involves Cartesian products. Since & defers to :is() (well, the spec still has :matches() :-) not sure where to file a PR) porting nested sheets to native nesting will be hazardous (beyond the fact that native nesting doesn't support BEM-like &-foo).

If instead you went for a Cartesian product approach, (maybe with an expansion limit for sanity), these three concerns would be addressed.

Edit: also, since SASS has been using the Cartesian products for a decade for its nested selector, and is widely deployed in the real world, I think that this concern about explosion is merely theoretical. You get O(m^n) selectors, but for m and n small enough for this not to matter.

pygy commented 2 years ago

Hi @fantasai I thought this was a lost cause... Are there recent developments that made the WG reconsider this point?