Skip to content

breaking: scope :not(...) selectors #13568

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

Merged
merged 6 commits into from
Oct 15, 2024
Merged

breaking: scope :not(...) selectors #13568

merged 6 commits into from
Oct 15, 2024

Conversation

dummdidumm
Copy link
Member

Targets PR #13567, but should be merged after that one to main, to make the diffs more manageable

The other part of, and therefore closes #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

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

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
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
Copy link

changeset-bot bot commented Oct 11, 2024

🦋 Changeset detected

Latest commit: 37f10e6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@trueadm
Copy link
Contributor

trueadm commented Oct 14, 2024

@dummdidumm Mind rebasing the PR before I review please?

Copy link
Contributor

@trueadm trueadm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Nice work.

@dummdidumm dummdidumm merged commit 6f03561 into main Oct 15, 2024
9 checks passed
@dummdidumm dummdidumm deleted the scope-not branch October 15, 2024 07:13
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 this pull request may close these issues.

:has(...) contents are not scoped
2 participants