by slinger · 3y ago · 0 comments
I wish browser vendors could work around this limitation and stick to Sass syntax. IMHO all of those options seems strange and make it difficult to read.
FWIW, it is not a limitation, it's a conscious choice. If you want to accept arbitrary selectors in option 3, you'll need unlimited lookahead, which limits the types of parser algorithms you can use, which means that _all CSS_ (not just CSS using nesting, every single web page out there) will be slower to parse, which means the web will become ever so slightly slower. And you cannot ever go back after making that choice. Is it really worth it for the few unusual cases that require workarounds, in an already not-super-common feature?
(Disclaimer: I wrote Chromium's CSS nesting support. It currently implements option 3.)
h1:has(+ p) { something: anything; } h1 { something: nothing; }
everything: has(+ p) { something: anything; }; h1 { something: nothing; }
Syntactically speaking these two lines are equivalent except for a single semicolon; the differing first token doesn't change anything about that. And you can absolutely add a custom element that looks like a property name to blur a line further. So in order to see if the first block is a rule or a property, you absolutely have to read up to the first `;`, excluding nested braces and alikes.That said, the main problem is that the property syntax is overly forgiving, not that the nested rule syntax is problematic. Something akin to JS `use strict` should be able to restrict the property syntax enough to allow the nested rule syntax without unlimited lookahead.
Also, looking at your second line: I would say the second part (starting at the semicolon before the h1), is invalidated by the semicolon. No need to lookahead or read up to the semicolon. The first part would always be evaluated first, up to the closing curly bracket. The second part would fail at the first character. I'm looking at this on a phone so maybe I've misread?
Edit: I should add, I'm not disagreeing with your point (I don't think!), just emphasising it
These options are bad, and CSS does not need this.
1. You don't have to use it if it doesn't seem useful to you.
2. Many developers (me included) use Sass and other preprocessors so they can use nesting; it's probably the single most popular feature of Sass.
3. CSS doesn't need nesting but a lot of developers want it, as it improves the developer experience for writing complex selectors.
Other people will use it and chances are you will have to deal with it sooner or later at $dayjob or whatnot.
The line in the sand here is good.
I put together a regex last time this was discussed [1] (which obviously completely alleviates all concerns): https://regex101.com/r/aOc2Pz/1
But seriously, syntax evolutions happen. If there's an issue with parsing garbage CSS why not just let that garbage run slowly, and re-optimise for the rest, prioritising for form and function.
The implied impact is based on a wrong assumption and unpublished effects.
It would be my choice of all 3, if SCSS-style nesting is not possible.
That is my biggest take away from this whole thing. The SASS/LESS syntax is so much easier to read to me.
a:hover {
color: hotpink;
:is(aside) & {
color: red;
}
}
Here, you can't just use `aside &` as this starts with a letter and will confuse the parser. Hence, wrapping with `:is()` is requires.Is it really necessary to support? I'd be cool with just simple nesting logic
This is probably something I can unlearn but until then it is very jarring to see & everywhere.