Skip to content

Include parent rules in the specificity of nested selectors - #525

Open
Rusty Raven (kakiuwang-ui) wants to merge 3 commits into
microsoft:mainfrom
kakiuwang-ui:fix/nested-selector-specificity
Open

Rusty Raven (kakiuwang-ui) wants to merge 3 commits into
microsoft:mainfrom
kakiuwang-ui:fix/nested-selector-specificity

Conversation

@kakiuwang-ui

@kakiuwang-ui Rusty Raven (kakiuwang-ui) commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #309

Problem

For nested rules, the selector hover builds the element preview from the whole chain of parent rules, but the specificity shown next to it only counts the innermost selector:

input { #id { p { color: red; } } }  // preview: <input> … <element id="id"> … <p>
                                     // specificity: (0, 0, 1), should be (1, 0, 2) like `input #id p`
.a { &:hover {} }                    // (0, 1, 0), should be (0, 2, 0)

Fix

selectorToSpecificityMarkedString now adds the specificity of the parent rule, following CSS Nesting: the nesting selector &, explicit or implied, has the specificity of the most specific selector of the parent rule, like :is().

  • & itself is scored as the parent's specificity, so the existing pseudo-class rules apply to it: :where(&) adds nothing, and :is(&, .c) takes the more specific of the two. Each & counts: .a { & + & {} } is (0, 2, 0). In LESS several & can share one token (&-&), so each & character counts.
  • A selector without any & has an implied & in front, so the parent is added once.
  • Parents are resolved recursively. When the parent rule has a selector list, the most specific one is used: .a, #b { .c {} } is (1, 1, 0).
  • &-suffix still counts once: .a { &-b {} } is (0, 1, 0), since &-b itself scores nothing.
  • Nesting stops at a mixin, at the stylesheet, and at @scope, whose rules are relative to :where(:scope).
  • @at-root, as a block (@at-root { }, @at-root (without: media) { }) or before a selector list (@at-root .b, .c), removes the implied & for every selector in the list. An explicit & still refers to the parent, as in Sass: .a { @at-root .x & {} } compiles to .x .a and is (0, 2, 0).
  • The parent-rule walk moves out of selectorToElement into getParentRuleSet, so the preview and the specificity use the same chain. That part is a pure refactor.
  • Results are cached per hover. Without the cache, lists at every level make the recursion exponential. With 6 levels of 8-selector lists, a hover takes 0.3 ms.

Notes for review

  • Sass expands .a, #b { .c {} } into two selectors with different specificity. A single value can't show both, so I went with the CSS Nesting (:is()) rule, which gives the highest of them.
  • An explicit & inside @scope refers to the scope root and should carry its specificity. It currently adds nothing; I left that out to keep this change focused.
  • Pre-existing and not changed here: the element preview doesn't recognize @at-root .b (it shows .a … <element> … .b). The specificity now handles that form correctly.
  • I cross-checked the nested results against Dart Sass. I compiled each case, including the @at-root forms, & inside :not()/:is(), & > & three levels deep, and @media/@supports in between, then compared the hover with the specificity of the compiled flat selector. They all match.
  • Nested specificity fix #311 attempted this earlier and was closed for inactivity. This change reuses the parent-rule walk instead of a second traversal, and adds the &, selector-list, @at-root and @scope handling.

Testing

npm test: 360 passing, 0 failing, lint clean (359 on main; the new case is added to the existing nested hover test's suite).

  • src/test/css/hover.test.ts: the existing nested test asserted the old values. div { div {} } now expects (0, 0, 2), and the two .foo .bar .bar chains expect (0, 3, 0).
  • New nested specificity test in the SCSS suite: the [SCSS] Specificity values for nested rules are incorrect #309 case next to its flat equivalent, >, &:hover, .b &, & + &, &-b, a parent selector list, & inside :where() and :is(), a chain through @media, the @at-root forms (including a selector list, and an explicit & under both the inline and the block form), @mixin, @scope (both around and inside a rule), and LESS &-&.

I broke each part on purpose and checked that a test fails: no parent contribution, & scored as zero, one & per token, no implied &, an implied & added even when there's an explicit one, first instead of most specific parent, no recursion, wrong comparison order, @at-root not removing the implied & (inline and block form separately), @at-root checked per selector instead of per rule, @at-root hiding the parent from an explicit & (inline and block form separately), no @scope stop, the implied & added regardless of @at-root, and the parent specificity set after scoring instead of before.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Qualified @at-root still needs its style-rule nesting preserved

The new nesting calculation unconditionally clears implicit nesting when it encounters @at-root, but a qualified query can preserve style-rule ancestors:

.a {
  @at-root (without: media) {
    .b { color: red; }
  }
}

This compiles to a selector retaining .a .b, so hovering .b should report specificity (0, 2, 0). This head reports (0, 1, 0). The (with: rule) form also preserves style-rule ancestors.

Please interpret the @at-root query before discarding implicit nesting, and correct the test expectation that currently enshrines this behavior. Sass documents the query semantics here: https://sass-lang.com/documentation/at-rules/at-root/#beyond-style-rules

To be precise, this is incomplete coverage in the newly added nesting-specificity calculation, not a regression from a previously correct base result. The ordinary nesting improvement is useful.

Reviewed commit: 4552e09. Locations: src/services/selectorPrinting.ts, getNestingParent() around lines 682-691, and the qualified-at-root expectation in src/test/css/hover.test.ts around line 152.

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.

[SCSS] Specificity values for nested rules are incorrect

2 participants