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

CSS: Added support for the selector function #2201

Merged
merged 2 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
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'
mAAdhaTTah marked this conversation as resolved.
Show resolved Hide resolved
}
// 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"],
mAAdhaTTah marked this conversation as resolved.
Show resolved Hide resolved
["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.