Skip to content

Commit 2e0eff7

Browse files
CSS: Added support for the selector function (#2201)
This adds support for CSS 4's selector function. The selectors passed to the function will now be highlighted as such.
1 parent a7d67ca commit 2e0eff7

7 files changed

+72
-7
lines changed

components/prism-css-extras.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
(function (Prism) {
22

3+
var string = /("|')(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/;
4+
var selectorInside;
5+
36
Prism.languages.css.selector = {
47
pattern: Prism.languages.css.selector,
5-
inside: {
8+
inside: selectorInside = {
69
'pseudo-element': /:(?:after|before|first-letter|first-line|selection)|::[-\w]+/,
710
'pseudo-class': /:[-\w]+/,
811
'class': /\.[-:.\w]+/,
912
'id': /#[-:.\w]+/,
1013
'attribute': {
11-
pattern: /\[(?:[^[\]"']|("|')(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1)*\]/,
14+
pattern: RegExp('\\[(?:[^[\\]"\']|' + string.source + ')*\\]'),
1215
greedy: true,
1316
inside: {
1417
'punctuation': /^\[|\]$/,
@@ -29,7 +32,7 @@
2932
lookbehind: true
3033
},
3134
'value': [
32-
/("|')(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
35+
string,
3336
{
3437
pattern: /(=\s*)[-\w\xA0-\uFFFF]+(?=\s*$)/,
3538
lookbehind: true
@@ -56,6 +59,8 @@
5659
}
5760
};
5861

62+
Prism.languages.css['atrule'].inside['selector-function-argument'].inside = selectorInside;
63+
5964
Prism.languages.insertBefore('css', 'property', {
6065
'variable': {
6166
pattern: /(^|[^-\w\xA0-\uFFFF])--[-_a-z\xA0-\uFFFF][-\w\xA0-\uFFFF]*/i,

components/prism-css-extras.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/prism-css.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
'atrule': {
88
pattern: /@[\w-]+[\s\S]*?(?:;|(?=\s*\{))/,
99
inside: {
10-
'rule': /@[\w-]+/
10+
'rule': /^@[\w-]+/,
11+
'selector-function-argument': {
12+
pattern: /(\bselector\s*\((?!\s*\))\s*)(?:[^()]|\((?:[^()]|\([^()]*\))*\))+?(?=\s*\))/,
13+
lookbehind: true,
14+
alias: 'selector'
15+
}
1116
// See rest below
1217
}
1318
},

components/prism-css.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prism.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,12 @@ Prism.languages.svg = Prism.languages.markup;
748748
'atrule': {
749749
pattern: /@[\w-]+[\s\S]*?(?:;|(?=\s*\{))/,
750750
inside: {
751-
'rule': /@[\w-]+/
751+
'rule': /^@[\w-]+/,
752+
'selector-function-argument': {
753+
pattern: /(\bselector\s*\((?!\s*\))\s*)(?:[^()]|\((?:[^()]|\([^()]*\))*\))+?(?=\s*\))/,
754+
lookbehind: true,
755+
alias: 'selector'
756+
}
752757
// See rest below
753758
}
754759
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@supports selector(::-webkit-foo) {
2+
/* style to apply when the `::-webkit-foo` selector is actually supported,
3+
* instead of just being parsed as matching nothing because of Selectors 4
4+
* § Appendix B: https://drafts.csswg.org/selectors-4/#compat */
5+
}
6+
7+
----------------------------------------------------
8+
9+
[
10+
["atrule", [
11+
["rule", "@supports"],
12+
["function", "selector"],
13+
["punctuation", "("],
14+
["selector-function-argument", [
15+
["pseudo-element", "::-webkit-foo"]
16+
]],
17+
["punctuation", ")"]
18+
]],
19+
["punctuation", "{"],
20+
["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 */"],
21+
["punctuation", "}"]
22+
]
23+
24+
----------------------------------------------------
25+
26+
Checks for the selector function in @supports rules.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
@supports selector(::-webkit-foo) {
2+
/* style to apply when the `::-webkit-foo` selector is actually supported,
3+
* instead of just being parsed as matching nothing because of Selectors 4
4+
* § Appendix B: https://drafts.csswg.org/selectors-4/#compat */
5+
}
6+
7+
----------------------------------------------------
8+
9+
[
10+
["atrule", [
11+
["rule", "@supports"],
12+
["function", "selector"],
13+
["punctuation", "("],
14+
["selector-function-argument", "::-webkit-foo"],
15+
["punctuation", ")"]
16+
]],
17+
["punctuation", "{"],
18+
["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 */"],
19+
["punctuation", "}"]
20+
]
21+
22+
----------------------------------------------------
23+
24+
Checks for the selector function in @supports rules.

0 commit comments

Comments
 (0)