Open ExE-Boss opened 5 years ago
See #1027. The expansion into all possible combinations can grow exponentially, which is bad.
The alternative would be taking the specificity of the least-specific selector in its arguments. But yeah, what @Loirooriol said.
Closing out, please re-open if there's something to fix.
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.
Hi @fantasai I thought this was a lost cause... Are there recent developments that made the WG reconsider this point?
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.