Repository navigation
Include parent rules in the specificity of nested selectors - #525
Rusty Raven (kakiuwang-ui) wants to merge 3 commits into
Conversation
Martin Aeschlimann (aeschli)
left a comment
There was a problem hiding this comment.
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.
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:
Fix
selectorToSpecificityMarkedStringnow 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.&has an implied&in front, so the parent is added once..a, #b { .c {} }is(1, 1, 0).&-suffixstill counts once:.a { &-b {} }is(0, 1, 0), since&-bitself scores nothing.@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 .aand is(0, 2, 0).selectorToElementintogetParentRuleSet, so the preview and the specificity use the same chain. That part is a pure refactor.Notes for review
.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.&inside@scoperefers to the scope root and should carry its specificity. It currently adds nothing; I left that out to keep this change focused.@at-root .b(it shows.a … <element> … .b). The specificity now handles that form correctly.@at-rootforms,&inside:not()/:is(),& > &three levels deep, and@media/@supportsin between, then compared the hover with the specificity of the compiled flat selector. They all match.&, selector-list,@at-rootand@scopehandling.Testing
npm test: 360 passing, 0 failing, lint clean (359 onmain; the new case is added to the existingnestedhover test's suite).src/test/css/hover.test.ts: the existingnestedtest asserted the old values.div { div {} }now expects(0, 0, 2), and the two.foo .bar .barchains expect(0, 3, 0).nested specificitytest 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-rootforms (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-rootnot removing the implied&(inline and block form separately),@at-rootchecked per selector instead of per rule,@at-roothiding the parent from an explicit&(inline and block form separately), no@scopestop, the implied&added regardless of@at-root, and the parent specificity set after scoring instead of before.