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

:has(...) contents are not scoped #13395

Closed
Rich-Harris opened this issue Sep 25, 2024 · 1 comment · Fixed by #13568
Closed

:has(...) contents are not scoped #13395

Rich-Harris opened this issue Sep 25, 2024 · 1 comment · Fixed by #13568
Milestone

Comments

@Rich-Harris
Copy link
Member

Rich-Harris commented Sep 25, 2024

Describe the bug

This...

.x:has(hr) {
  background: palegoldenrod;
}
	
:global(.y):has(hr) {
  background: palegoldenrod;
}

...becomes this:

.x.svelte-1bfks0l:has(hr) {
  background: palegoldenrod;
}
	
.y:has(hr) {
  background: palegoldenrod;
}

That's consistent with Svelte 4, but it's not consistent with how Svelte 5 handles :is(...) and :where(...). If we scoped the selector list, then either those styles would be removed as unused, or transformed into this:

.x.svelte-1bfks0l:has(hr:where(.svelte-1bfks0l)) {
  background: palegoldenrod;
}
	
.y:has([hr](hr:where(.svelte-1bfks0l))) {
  background: palegoldenrod;
}

The fourth functional pseudo-class is :not(...).

Options:

  • leave things as they are
  • always analyse selector lists for functional pseudo-classes
  • always analyse selector lists for functional pseudo-classes of non-global selectors (i.e. :global(x).has(y) would not scope y)
  • do it for :has but not :not or vice versa

I don't know off the top of my head what the right answer is, but it would be good to have consistency.

Reproduction

link

@7nik
Copy link

7nik commented Sep 25, 2024

I'd expect it to follow a very simple rule: if the selector is not inside :global(...) or :global {...}, then it gets scoped.

dummdidumm added a commit that referenced this issue Oct 11, 2024
The main part of #13395

This implements scoping for selectors inside `:has(...)`. The approach is to first descend into the contents of a `:has(...)` selector, then in case of a match, try to match the rest of the selector ignoring the `:has(...)` part. In other words, `.x:has(y)` is essentially treated as `x y` with `y` being matched first, then walking up the selector chain taking into account combinators.

This is a breaking change because people could've used `:has(.unknown)` with `.unknown` not appearing in the HTML, and so they need to do `:has(:global(.unknown))` instead
dummdidumm added a commit that referenced this issue Oct 11, 2024
The other part of #13395

This implements scoping for selectors inside `:not(...)`. The approach is almot the same as for `:is/where(...)`.

This is a breaking change because people could've used `:not(.unknown)` with `.unknown` not appearing in the HTML, and so they need to do `:not(:global(.unknown))` instead.

While implementing it I also discovered a few bugs, which are fixed in this PR:
- `foo :is(bar baz)` wasn't properly handled. This selector can mean `foo bar baz` but it can also mean `bar foo baz` (super weird, but it is what it is). Since our current algorithm isn't suited for handling this, we just assume it matches and scope it. Worst case is we missed a prune
- `bar` in `:global(foo):is(bar)` was always marked as unused, even if it matched
dummdidumm added a commit that referenced this issue Oct 14, 2024
The main part of #13395

This implements scoping for selectors inside `:has(...)`. The approach is to first descend into the contents of a `:has(...)` selector, then in case of a match, try to match the rest of the selector ignoring the `:has(...)` part. In other words, `.x:has(y)` is essentially treated as `x y` with `y` being matched first, then walking up the selector chain taking into account combinators.

This is a breaking change because people could've used `:has(.unknown)` with `.unknown` not appearing in the HTML, and so they need to do `:has(:global(.unknown))` instead
dummdidumm added a commit that referenced this issue Oct 15, 2024
The other part of #13395

This implements scoping for selectors inside `:not(...)`. The approach is almot the same as for `:is/where(...)`.

This is a breaking change because people could've used `:not(.unknown)` with `.unknown` not appearing in the HTML, and so they need to do `:not(:global(.unknown))` instead.

While implementing it I also discovered a few bugs, which are fixed in this PR:
- `foo :is(bar baz)` wasn't properly handled. This selector can mean `foo bar baz` but it can also mean `bar foo baz` (super weird, but it is what it is). Since our current algorithm isn't suited for handling this, we just assume it matches and scope it. Worst case is we missed a prune
- `bar` in `:global(foo):is(bar)` was always marked as unused, even if it matched
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants