Skip to content

[css-nesting-1] Clarify valid position of & in a selector #9940

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

Closed
cdoublev opened this issue Feb 13, 2024 · 4 comments
Closed

[css-nesting-1] Clarify valid position of & in a selector #9940

cdoublev opened this issue Feb 13, 2024 · 4 comments

Comments

@cdoublev
Copy link
Collaborator

CSS Nesting does not clearly define where & is allowed to appear in a selector. In Chrome/FF, these selectors are all invalid:

  • html|&
  • html&|*
  • #&id
  • .&class
  • [&]
  • [attr=&]
  • [attr=value &]

While the position of & does not make a difference in its behavior, it should only match <type-selector> and <subclass-selector> in <compound-selector>.


I would also like to suggest to define this again (cf. #7503):

An invalid selector remains invalid when some of its parts are nested.

In Chrome/FF, the nested rule in ::before, div { &:first-child {} } is valid whereas the version without nesting, ::before:first-child, div:first-child {} is not (because :first-child is not allowed to be compounded to ::before).

@cdoublev cdoublev added the css-nesting-1 Current Work label Feb 13, 2024
@romainmenke
Copy link
Member

romainmenke commented Feb 13, 2024

none of this is invalid:

  • ::before, div
  • &:first-child

The ::before:first-child branch (with selector as if resolved) can not match anything, but it isn't invalid in a way that it would also cause the div:first-child branch to become invalid.

@cdoublev
Copy link
Collaborator Author

Indeed, they could be validated without considering the selector of the parent style rule. This is not specified.

I would not mind if it was. It may be surprising that ::before:first-child, div:first-child {} is entirely ignored, whereas the nested version is not.

This is slighlty unrelated to my original question though. Maybe I should create a dedicated one.

@tabatkins
Copy link
Member

It does indeed clearly define where it's valid - it's a new type of selector (by implication, a new "simple" selector). The valid positions are thus defined by the grammar.

None of the options you listed are places where you can put a selector.

@tabatkins
Copy link
Member

In Chrome/FF, the nested rule in ::before, div { &:first-child {} } is valid whereas the version without nesting, ::before:first-child, div:first-child {} is not

That's also correct behavior. We don't "expand" the & in a nested selector, it just represents the elements matched by the parent selector. It doesn't represent pseudo-elements, so in this example it ends up matching the divs on the page. That's perfectly fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants