SCSS: Support parameterized pseudo-classes in selectors#4093
Open
dweep-js wants to merge 1 commit into
Open
Conversation
Fixes PrismJS#3740. Selectors like `:foo()` were not being highlighted because the selector pattern excluded all parentheses. This adds a constrained alternative that only allows balanced parens when they directly follow a pseudo-class (`:name(...)` or `::name(...)`), which fixes the reported case without regressing the @mixin/@include argument-list case Golmote flagged in the original workaround.
✅ Deploy Preview for dev-prismjs-com ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3740
Problem
SCSS selectors containing a parameterized pseudo-class were not
highlighted as selectors:
:foo()broke the match entirely — onlybarwas left as anunmatched token, while
foo :foo()fell out of theselectortoken.The equivalent selector without parens (
foo :foo bar {}) worked fine.Root cause
Prism.languages.scss.selector.patternexplicitly excludes(and)from the character class that makes up a selector:
This exclusion exists on purpose: the same pattern is reused (via
atrule's$rest: 'scss') to tokenize the inside of at-rules like@mixinand@include, and parens there belong to the argument list,not to a selector. A naive fix — just deleting
()from the exclusionclass, as originally suggested in the issue — makes the selector
pattern swallow at-rule argument lists too, which breaks highlighting
of code like:
(
prefixshould tokenize as afunction; with the naive fix it getsabsorbed into a spurious
selectortoken instead, as flagged by@Golmote in the issue thread.)
Fix
Instead of opening up
()unconditionally, this adds one new,narrowly-scoped alternative to the selector pattern: a balanced
parenthesized group, but only when it directly follows a
pseudo-class name (
:name(...)or::name(...)):This matches
:foo(),:nth-child(2n+1),:not(.active), etc., whileleaving the existing exclusion of bare
(/)intact everywhere else —so
@mixin/@includeargument lists (which are never preceded by acolon) are unaffected.
Testing
Added a new case to
tests/languages/scss/selector_feature.test:Verified the fix two ways:
scss.jschange (
git stash push -- src/languages/scss.js) while keepingthe new test case, and confirmed
selector_featurefails againstthe old pattern —
foo :foo() bar {}tokenizes asproperty/punctuation/function/punctuation/punctuation/selector("bar ")instead of one cleanselectortoken. Thisreproduces the exact bug from the issue.
(
npm run test:languages -- --language=scss, 15 passing).@mixin prefix($property, $value, $prefixes) {}counter-example against the new pattern to confirmprefixstill tokenizes correctly as afunctionand is notswallowed into a
selector— the specific regression that sankthe original one-line workaround in the issue thread.
Scope
One-line regex change in
src/languages/scss.js, plus one new testcase appended to the existing
selector_feature.test(no existingcases modified).