Skip to content

Include the 'of S' selector in specificity for :nth-child(odd/even/n-… - #550

Open
Hamza Paracha (hamza-paracha) wants to merge 3 commits into
microsoft:mainfrom
hamza-paracha:devin/1795141395-nth-child-odd-even-of-specificity
Open

Hamza Paracha (hamza-paracha) wants to merge 3 commits into
microsoft:mainfrom
hamza-paracha:devin/1795141395-nth-child-odd-even-of-specificity

Conversation

@hamza-paracha

@hamza-paracha Hamza Paracha (hamza-paracha) commented Nov 20, 2026 •

Copy link
Copy Markdown

Summary

The selector hover reports the wrong specificity when the <an+b> argument of :nth-child() / :nth-last-child() is a single identifier and is followed by of S. The S part is dropped:

Selector Before After (per Selectors 4)
:nth-child(odd of #x) (0, 1, 0) (1, 1, 0)
:nth-child(even of .a) (0, 1, 0) (0, 2, 0)
:nth-child(n-1 of #a) (0, 1, 0) (1, 1, 0)
:nth-child(N of #a) (0, 1, 0) (1, 1, 0)
:nth-child(+n of #a) (0, 1, 0) (1, 1, 0)

Cause: for these arguments the parser reads odd of #x as a selector (type selectors odd and of), so no SelectorList child is produced. selectorToSpecificityMarkedString already worked around this, but only for an exact, lowercase n or -n first token.

Fix: detect any identifier-form <an+b> followed by of, case-insensitively ([+-]?n, [+-]?n-<digits>, odd, even), with a regex on the argument text, then parse the rest as the selector list as before. Forms such as 2n+1 of S are unaffected because they already parse into a SelectorList.

Tests: added cases to nth-child, nth-last-child specificity. They fail without the change; npm test (compile, lint, 359 tests) passes with it.

This change was prepared with help from an AI coding assistant (Devin) and reviewed by me.

…1 of S)

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 20, 2026 02:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@hamza-paracha

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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.

Preserve comment-aware parsing before the of selector

The replacement regex in src/services/selectorPrinting.ts around line 491 regresses valid selectors containing a comment between An+B and of:

:nth-child(n /*x*/ of #id) { color: red; }

On the base, hover specificity is correctly (1, 1, 0). On this head it becomes (0, 1, 0), dropping #id. The same problem occurs with -n. The previous scanner skips comments, whereas the new whitespace regex does not.

Please keep token-aware extraction while extending support for odd, even, and the additional An+B forms. Add comment-separated regression cases for both :nth-child() and :nth-last-child().

Integration note, separate from that functional defect: when combining this with #555, the new hover assertions must expect preserved functional arguments (for example :nth-child(odd of .li)), rather than the old argument-free preview. The combined suite currently fails that assertion even though its specificity is correct.

Reviewed commit: 1220ed0.

Hamza Paracha and others added 2 commits November 23, 2026 15:22
…geservice into devin/1795141395-nth-child-odd-even-of-specificity
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@hamza-paracha

Copy link
Copy Markdown
Author

Thanks for catching that. Fixed in 39ccd75:

  • The <an+b> of S extraction now uses the CSS Scanner again instead of a whitespace regex, so comments and whitespace between An+B, of and S are skipped. It still accepts the identifier forms this PR adds (odd, even, N, n-1, and an optional leading +). S is taken from the end of the of token.
  • Added comment-separated regression cases for both :nth-child() and :nth-last-child(): n /*x*/ of #bar, -n /*x*/ of #bar, odd /*x*/ of /*y*/ #bar and even /*x*/ of .li.
  • Merged main (including Preserve functional pseudo-selector arguments in hover previews #555) and updated the new hover assertions to expect the preserved functional arguments, e.g. <element id="foo" :nth-child(odd of .li)>.

npm test passes (422 tests plus lint). With the previous regex, the new comment cases fail.

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.

3 participants