Skip to content

Commit

Permalink
CSS: Added support for the selector function (PrismJS#2201)
Browse files Browse the repository at this point in the history
This adds support for CSS 4's selector function. The selectors passed to the function will now be highlighted as such.
  • Loading branch information
RunDevelopment authored and catb0t committed Feb 7, 2020
1 parent 0dac5ae commit d6a4ec6
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 7 deletions.
11 changes: 8 additions & 3 deletions components/prism-css-extras.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
(function (Prism) {

var string = /("|')(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/;
var selectorInside;

Prism.languages.css.selector = {
pattern: Prism.languages.css.selector,
inside: {
inside: selectorInside = {
'pseudo-element': /:(?:after|before|first-letter|first-line|selection)|::[-\w]+/,
'pseudo-class': /:[-\w]+/,
'class': /\.[-:.\w]+/,
'id': /#[-:.\w]+/,
'attribute': {
pattern: /\[(?:[^[\]"']|("|')(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1)*\]/,
pattern: RegExp('\\[(?:[^[\\]"\']|' + string.source + ')*\\]'),
greedy: true,
inside: {
'punctuation': /^\[|\]$/,
Expand All @@ -29,7 +32,7 @@
lookbehind: true
},
'value': [
/("|')(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
string,
{
pattern: /(=\s*)[-\w\xA0-\uFFFF]+(?=\s*$)/,
lookbehind: true
Expand All @@ -56,6 +59,8 @@
}
};

Prism.languages.css['atrule'].inside['selector-function-argument'].inside = selectorInside;

Prism.languages.insertBefore('css', 'property', {
'variable': {
pattern: /(^|[^-\w\xA0-\uFFFF])--[-_a-z\xA0-\uFFFF][-\w\xA0-\uFFFF]*/i,
Expand Down
2 changes: 1 addition & 1 deletion components/prism-css-extras.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion components/prism-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
'atrule': {
pattern: /@[\w-]+[\s\S]*?(?:;|(?=\s*\{))/,
inside: {
'rule': /@[\w-]+/
'rule': /^@[\w-]+/,
'selector-function-argument': {
pattern: /(\bselector\s*\((?!\s*\))\s*)(?:[^()]|\((?:[^()]|\([^()]*\))*\))+?(?=\s*\))/,
lookbehind: true,
alias: 'selector'
}
// See rest below
}
},
Expand Down
2 changes: 1 addition & 1 deletion components/prism-css.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,12 @@ Prism.languages.svg = Prism.languages.markup;
'atrule': {
pattern: /@[\w-]+[\s\S]*?(?:;|(?=\s*\{))/,
inside: {
'rule': /@[\w-]+/
'rule': /^@[\w-]+/,
'selector-function-argument': {
pattern: /(\bselector\s*\((?!\s*\))\s*)(?:[^()]|\((?:[^()]|\([^()]*\))*\))+?(?=\s*\))/,
lookbehind: true,
alias: 'selector'
}
// See rest below
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@supports selector(::-webkit-foo) {
/* style to apply when the `::-webkit-foo` selector is actually supported,
* instead of just being parsed as matching nothing because of Selectors 4
* § Appendix B: https://drafts.csswg.org/selectors-4/#compat */
}

----------------------------------------------------

[
["atrule", [
["rule", "@supports"],
["function", "selector"],
["punctuation", "("],
["selector-function-argument", [
["pseudo-element", "::-webkit-foo"]
]],
["punctuation", ")"]
]],
["punctuation", "{"],
["comment", "/* style to apply when the `::-webkit-foo` selector is actually supported,\r\n\t * instead of just being parsed as matching nothing because of Selectors 4\r\n\t * § Appendix B: https://drafts.csswg.org/selectors-4/#compat */"],
["punctuation", "}"]
]

----------------------------------------------------

Checks for the selector function in @supports rules.
24 changes: 24 additions & 0 deletions tests/languages/css/atrule_selector-function-argument_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@supports selector(::-webkit-foo) {
/* style to apply when the `::-webkit-foo` selector is actually supported,
* instead of just being parsed as matching nothing because of Selectors 4
* § Appendix B: https://drafts.csswg.org/selectors-4/#compat */
}

----------------------------------------------------

[
["atrule", [
["rule", "@supports"],
["function", "selector"],
["punctuation", "("],
["selector-function-argument", "::-webkit-foo"],
["punctuation", ")"]
]],
["punctuation", "{"],
["comment", "/* style to apply when the `::-webkit-foo` selector is actually supported,\r\n\t * instead of just being parsed as matching nothing because of Selectors 4\r\n\t * § Appendix B: https://drafts.csswg.org/selectors-4/#compat */"],
["punctuation", "}"]
]

----------------------------------------------------

Checks for the selector function in @supports rules.

0 comments on commit d6a4ec6

Please sign in to comment.