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

fix: correctly visit elements that may match :has() during pruning #15207

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

7nik
Copy link
Contributor

@7nik 7nik commented Feb 4, 2025

Fixes #14072

:has() was matching only against descendants or siblings, but not sibling's descendants.

Also, in a case like :has(> .foo > .bar) it was matching against children instead of descendants, so I had to drop the > optimization. But, to ensure that in case like :has(> .foo) it matches only the direct children, the inner selector is prepended with a selector of new type that matches only certain element.

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.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

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

Copy link

changeset-bot bot commented Feb 4, 2025

🦋 Changeset detected

Latest commit: b41004a

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

Copy link
Contributor

github-actions bot commented Feb 4, 2025

Playground

pnpm add https://pkg.pr.new/svelte@15207

@@ -234,7 +236,7 @@ function apply_combinator(relative_selector, parent_selectors, rule, node) {

case '+':
case '~': {
const siblings = get_possible_element_siblings(node, name === '+');
const siblings = get_possible_element_preceding_siblings(node, name === '+');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed to clarify that it retreats previous siblings.


walk_children(element.fragment, { is_child: true });
sibling_elements ??= get_following_sibling_elements(element, include_self);
if (selectors.some((s) => s.combinator?.name === ' ' || s.combinator?.name === '>')) {
Copy link
Contributor Author

@7nik 7nik Feb 4, 2025

Choose a reason for hiding this comment

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

It doesn't cover s.combinator === null case - I suppose only the first selector can have no combinator.

@7nik 7nik changed the title Fix 14072 fix: correctly visit elements that may match :has() during pruning Feb 4, 2025
@7nik
Copy link
Contributor Author

7nik commented Feb 5, 2025

So, now :has() sometimes is very far from being optimal. The correct way is to call forward-going apply_selector and other functions, but currently they are backward-going. So I have an idea of making them bidirectional instead. I'll try it later.

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.

Sibling Combinator combined with :has can be improperly marked as unused
1 participant