New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rules for CSS Nesting 1 specification #6421
Comments
@romainmenke Thanks for the request and for using the template.
Yes, absolutely! I'm glad you created an issue. I've been meaning to open one myself since the discussion about the nesting spec in #6294 (comment). In that issue, we touched on these three nesting patterns: /* this use of the nesting selector at the top level is valid, but won't match anything */
&.foo {} /* this is valid, but probably unintentional */
{
&& {}
} /* these are equivalent */
a {
@media (orientation: landscape) { color: red; }
}
a {
@media (orientation: landscape) { & { color: red; } }
}
Rules that warn about invalid or ambiguous patterns are a perfect fit for Stylelint. We should eventually add rules to catch: .foo {
bar & {} /* ambiguous nesting */
} And ::before {
bar & {} /* pseudo elements are not allowed */
} The patterns above could fit into one of our existing or upcoming categories:
Or we may need a new category or two, e.g.
I suggest any rules are first created as community plugins (perhaps bundled into a plugin pack) while the spec stabilises and is adopted by browsers. It's easier to experiment and make changes in plugins than it is to built-in rules. The closer the plugins follow our conventions for rules and criteria for inclusion the easier it'll be to port them to built-in rules when the time is right. I'll label this issue as a discussion so that it can be a place to chat about:
Everyone's welcome to chime in. There are opportunities for us to help people write good and consistent nesting CSS. |
Another case : div {
.foo, textarea { … }
}
div {
.foo &, textarea & { … }
} This is fully valid but mutations can easily make it invalid.
Some authors might prefer to enforce a style where each selector in a list is always valid even when the list changes. div {
.foo, & textarea { … }
}
div {
.foo &, :is(textarea) & { … }
} |
Rather than enforcing |
Updated, I typed this out too quickly. |
Article from Webkit on nesting. It have an example of #6421 (comment) in it: ul {
padding-left: 1em;
:is(article) & {
padding-left: 0;
}
} |
Not a solution for everything, but the ambiguous nesting case I think it can be blocked with this:
(In other words, not allowing nested selectors to start with a letter) |
the restriction on idents might be dropped in the near future : w3c/csswg-drafts#7961 (comment) |
What is the problem you're trying to solve?
css-nesting-1
is being implemented in Chrome. The specification isn't final yet and it might change in the next few weeks.There are some issues where linters might help authors write correctly nested CSS.
At the moment we still need to wait for the final specification for all issues to surface.
What solution would you like to see?
Some stylelint rules that warn authors when they write nested selectors that are invalid or ambiguous.
Is there interest to add this to stylelint?
The text was updated successfully, but these errors were encountered: