Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(css) fix overly greedy pseudo class matching #3853

Merged
merged 6 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Core Grammars:
- fix(reasonml) simplify syntax and align it with ocaml [jchavarri][]
- fix(swift) `warn_unqualified_access` is an attribute [Bradley Mackey][]
- enh(swift) macro attributes are highlighted as keywords [Bradley Mackey][]
- fix(css) fix overly greedy pseudo class matching [Bradley Mackey][]

Dev tool:

Expand Down
15 changes: 8 additions & 7 deletions src/languages/lib/css-shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ export const TAGS = [
'video'
];

// Sorting, then reversing makes sure longer attributes/elements like
// `font-weight` are matched fully instead of getting false positives on say `font`

export const MEDIA_FEATURES = [
'any-hover',
'any-pointer',
Expand Down Expand Up @@ -153,7 +156,7 @@ export const MEDIA_FEATURES = [
'max-width',
'min-height',
'max-height'
];
].sort().reverse();
joshgoebel marked this conversation as resolved.
Show resolved Hide resolved

// https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes
export const PSEUDO_CLASSES = [
Expand Down Expand Up @@ -216,7 +219,7 @@ export const PSEUDO_CLASSES = [
'valid',
'visited',
'where' // where()
];
].sort().reverse();

// https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-elements
export const PSEUDO_ELEMENTS = [
Expand All @@ -234,7 +237,7 @@ export const PSEUDO_ELEMENTS = [
'selection',
'slotted',
'spelling-error'
];
].sort().reverse();

export const ATTRIBUTES = [
'align-content',
Expand Down Expand Up @@ -595,9 +598,7 @@ export const ATTRIBUTES = [
'word-wrap',
'writing-mode',
'z-index'
// reverse makes sure longer attributes `font-weight` are matched fully
// instead of getting false positives on say `font`
].reverse();
].sort().reverse();

// some grammars use them all as a single group
export const PSEUDO_SELECTORS = PSEUDO_CLASSES.concat(PSEUDO_ELEMENTS);
export const PSEUDO_SELECTORS = PSEUDO_CLASSES.concat(PSEUDO_ELEMENTS).sort().reverse();
22 changes: 22 additions & 0 deletions test/markup/css/pseudo-selector.expect.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
<span class="hljs-selector-tag">li</span><span class="hljs-selector-pseudo">:not</span>(<span class="hljs-selector-class">.red</span>){}
<span class="hljs-selector-tag">li</span><span class="hljs-selector-pseudo">:not</span>(<span class="hljs-selector-class">.red</span>)<span class="hljs-selector-pseudo">:not</span>(<span class="hljs-selector-class">.green</span>){}

<span class="hljs-selector-pseudo">:first-child</span> {
<span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
}

<span class="hljs-selector-pseudo">:first-of-type</span> {
<span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
}

<span class="hljs-selector-pseudo">:last-child</span> {
<span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
}

<span class="hljs-selector-pseudo">:last-of-type</span> {
<span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
}

<span class="hljs-selector-tag">p</span><span class="hljs-selector-pseudo">::first-letter</span> {
<span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.5rem</span>;
<span class="hljs-attribute">font-weight</span>: bold;
<span class="hljs-attribute">color</span>: brown;
}
22 changes: 22 additions & 0 deletions test/markup/css/pseudo-selector.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
li:not(.red){}
li:not(.red):not(.green){}

:first-child {
padding: 0;
}

:first-of-type {
padding: 0;
}

:last-child {
padding: 0;
}

:last-of-type {
padding: 0;
}

p::first-letter {
font-size: 1.5rem;
font-weight: bold;
color: brown;
}