Skip to content

fix(compiler-sfc): scoped style with state pseudo in compound selectors - #14559

Closed
w2jiodsfa wants to merge 1 commit into
vuejs:mainfrom
w2jiodsfa:fix/scoped-pseudo-hover
Closed

w2jiodsfa wants to merge 1 commit into
vuejs:mainfrom
w2jiodsfa:fix/scoped-pseudo-hover

Conversation

@w2jiodsfa

@w2jiodsfa w2jiodsfa commented Mar 12, 2026 •

Copy link
Copy Markdown

Summary

fix #13746

When using scoped styles with state pseudo classes in compound selectors (e.g., .root:hover .a), the scoped attribute was incorrectly injected after the pseudo class instead of before it.

Before (incorrect):

.root:hover .a[data-v-xxx] { color: red; }

After (correct):

.root[data-v-xxx]:hover .a[data-v-xxx] { color: red; }

This caused the scoped style to leak to external components when they had matching class names.

How did you test this change?

  • Added test case for state pseudo in compound selectors
  • All 3511 existing tests pass

Summary by CodeRabbit

  • Bug Fixes

    • Improved scoped style handling for state pseudo-classes (:hover, :focus) in compound selectors, ensuring the scope attribute is correctly positioned in generated CSS output.
  • Tests

    • Added test coverage verifying state pseudo-class scoping patterns produce correct styled outputs.

fix #13746

When using scoped styles with state pseudo classes in compound selectors
(e.g., .root:hover .a), the scoped attribute was incorrectly injected after
the pseudo class instead of before it.

This caused the scoped style to leak to external components when they had
matching class names.

The fix injects the scoped attribute (data-v-xxx) before state pseudo
classes like :hover, :focus, :active, etc., ensuring proper scoping.
@coderabbitai

coderabbitai Bot commented Mar 12, 2026 •

Copy link
Copy Markdown

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

This change fixes scoped style handling for state pseudo-classes in compound selectors by introducing a regex pattern to identify state pseudo-classes and logic to inject the scoped attribute before them, ensuring selectors like .root:hover .a are fully scoped.

Changes

Cohort / File(s) Summary
Test Coverage
packages/compiler-sfc/__tests__/compileStyle.spec.ts
Added tests verifying scoped attribute injection for state pseudo-classes in compound selectors, covering patterns like .root:hover .a and .root:focus .a.
Scoped Selector Logic
packages/compiler-sfc/src/style/pluginScoped.ts
Introduced statePseudoRE regex to detect state pseudo-classes (:hover, :focus, etc.) and added logic to track previous nodes and inject scoped attributes before state pseudo-classes when following combinators.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • vuejs/core#12918 — Modifies rewriteSelector in the same file to adjust scoped attribute injection for selector edge cases (trailing universal selectors).

Suggested labels

ready to merge, scope: sfc, :hammer: p3-minor-bug

Suggested reviewers

  • edison1105
  • Doctor-wu

Poem

🐰 A scope that hovered, incomplete,
Now glows with attributes sweet,
State pseudo-classes find their place,
Before the : mark's embrace,
Selectors scoped from root to tail! 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and specifically describes the main fix: addressing scoped style behavior with state pseudo-classes in compound selectors.
Linked Issues check ✅ Passed The PR successfully implements the fix for issue #13746 by injecting the scoped attribute before state pseudo-classes, ensuring both parent and descendant selectors are properly scoped and preventing style leakage.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the scoping bug: the pluginScoped.ts logic handles state pseudo-class placement and test coverage validates the fix, with no unrelated modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@w2jiodsfa w2jiodsfa closed this Mar 12, 2026
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.

the scoped style is not fully scoped

1 participant