Skip to content
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

[css-selectors] Allow relative selectors everywhere #8010

Closed
romainmenke opened this issue Nov 3, 2022 · 5 comments
Closed

[css-selectors] Allow relative selectors everywhere #8010

romainmenke opened this issue Nov 3, 2022 · 5 comments
Labels
css-nesting-1 Current Work selectors-4 Current Work

Comments

@romainmenke
Copy link
Member

romainmenke commented Nov 3, 2022

A significant motivation behind changing the nesting syntax was making selectors easier to copy/paste between contexts.

This changed .foo { & .bar{} } into .foo { .bar{} }
Both .foo and .bar can be copy/pasted to any other context, or could have been copy/pasted from any other context.

.foo { > .bar{} } however can not be copied to the root of the stylesheet.

/* invalid */
> .bar{}

Currently I think it is only allowed in :

  • @scope
  • nested context
  • :has()

#7854 (comment)

Can > .bar{} be equal to :scope > .bar{} in all contexts?

All the pseudo's that accept selectors will need to be defined :

  • :is(> .foo)
  • :host(> .foo)
  • ...

Maybe :scope is not suitable everywhere?

@Loirooriol
Copy link
Contributor

A top-level style rule > .bar{} doesn't seem particularly clear to me.
But this can be convenient for element.querySelector("> .foo").

@romainmenke
Copy link
Member Author

romainmenke commented Nov 4, 2022

A top-level style rule > .bar{} doesn't seem particularly clear to me.

I agree, but I think this is also true for .foo { > .bar {} }
I prefer to have everything explicit as I think the resulting stylesheet is easier to maintain and understand (even for people who are not an expert in CSS).

But the current trend is clearly in favour of implicit and terse notations.


element.querySelector("> .foo")

I hadn't yet thought about querySelector and friends. Yes those already support :scope today and this would indeed be convenient.

Another one is @supports:

html {
	> body {
		color: red;
	}
}

@supports selector(> :possibly-invalid) {
	html {
		> :possibly-invalid {
			color: green;
		}
	}
}

html {
	@supports selector(> :possibly-invalid) {
		> :possibly-invalid {
			color: green;
		}
	}
}

These supports checks fails because relative selectors are not allowed there.
This can be confusing to authors.

@tabatkins
Copy link
Member

I don't think we should support top-level relative selectors. It's not immediately clear what they're relative to. If you say "the root", then (a) virtually nothing will ever successfully match, so it doesn't seem useful, and (b) the behavior for "implicit descendant combinator" relatives will be different anyway, since they can match the root.

Hammering on (a) a little more, unless you're doing some DOM shenanigans, then neither of the sibling combinators will ever match, and the child combinator will only let you match the head and body elements which are, again ignoring DOM shenanigans, unique anyway and thus didn't really need nesting anyway.

@romainmenke
Copy link
Member Author

(b) the behavior for "implicit descendant combinator" relatives will be different anyway, since they can match the root.

I think that is the best reason not to do this.


I mainly wanted to give this some thought because part of the reason to update the nesting specification was to facility copy/paste of selectors.

I don't think the updated specification fully succeeded in that goal but that is fine.


This issue can be closed unless someone wants to spend more time on this.
Thank you for the insights!

@tabatkins
Copy link
Member

Yeah, given that the sole copy-paste scenario supported here would be from rules nested inside of html {...}, I don't think the loss is very meaningful in practice. ^_^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
css-nesting-1 Current Work selectors-4 Current Work
Projects
None yet
Development

No branches or pull requests

4 participants