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

Identify CSS units and variables #1450

Merged
merged 3 commits into from
Dec 3, 2018
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
14 changes: 13 additions & 1 deletion components/prism-css-extras.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@ Prism.languages.css.selector = {
};

Prism.languages.insertBefore('css', 'function', {
'variable': {
pattern: /(var\()[^)]+(?=\))/,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern will also include possible default values.

But we can use the naming convention of custom properties to make a simple pattern which matches all variables accessed via var or not.

A custom property [= variable] is any property whose name starts with two dashes [...].

Using that, /(^|[^-\w\xA0-\uFFFF])--[-_a-z\xA0-\uFFFF][-\w\xA0-\uFFFF]*/i should do the job.

Depending on whether we insert it before property we can also highlight the custom property declarations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gah, hit merge too quickly!

Copy link
Member

@RunDevelopment RunDevelopment Dec 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's alright, I reviewed too late.

lookbehind: true
},
'operator': {
pattern: /(\s)[+\-*\/](?=\s)/,
lookbehind: true
},
'hexcode': /#[\da-f]{3,8}/i,
'entity': /\\[\da-f]{1,8}/i,
'number': /[\d%.]+/
'unit': {
pattern: /(\d)(?:%|[a-z]+)/,
lookbehind: true
},
'number': /-?[\d.]+/
});
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.

8 changes: 4 additions & 4 deletions tests/languages/css!+css-extras/number_feature.test
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
42
3.14159
42%
3.14%
-42
-3.14

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

[
["number", "42"],
["number", "3.14159"],
["number", "42%"],
["number", "3.14%"]
["number", "-42"],
["number", "-3.14"]
]

----------------------------------------------------
Expand Down
71 changes: 71 additions & 0 deletions tests/languages/css!+css-extras/operator_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
width: calc(100% + 20px);
width: calc(100% - 20px);
width: calc(5px * 2);
width: calc(10px / 2);
height: -20px;
content: 'this - is not an operator';

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

[
["property", "width"],
["punctuation", ":"],
["function", "calc"],
["punctuation", "("],
["number", "100"],
["unit", "%"],
["operator", "+"],
["number", "20"],
["unit", "px"],
["punctuation", ")"],
["punctuation", ";"],

["property", "width"],
["punctuation", ":"],
["function", "calc"],
["punctuation", "("],
["number", "100"],
["unit", "%"],
["operator", "-"],
["number", "20"],
["unit", "px"],
["punctuation", ")"],
["punctuation", ";"],

["property", "width"],
["punctuation", ":"],
["function", "calc"],
["punctuation", "("],
["number", "5"],
["unit", "px"],
["operator", "*"],
["number", "2"],
["punctuation", ")"],
["punctuation", ";"],

["property", "width"],
["punctuation", ":"],
["function", "calc"],
["punctuation", "("],
["number", "10"],
["unit", "px"],
["operator", "/"],
["number", "2"],
["punctuation", ")"],
["punctuation", ";"],

["property", "height"],
["punctuation", ":"],
["number", "-20"],
["unit", "px"],
["punctuation", ";"],

["property", "content"],
["punctuation", ":"],
["string", "'this - is not an operator'"],
["punctuation", ";"]
]

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

Checks for operators.
21 changes: 21 additions & 0 deletions tests/languages/css!+css-extras/unit_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
100%
1rem
500ms
.5s
-3px
-0.618vw

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

[
["number", "100"], ["unit", "%"],
["number", "1"], ["unit", "rem"],
["number", "500"], ["unit", "ms"],
["number", ".5"], ["unit", "s"],
["number", "-3"], ["unit", "px"],
["number", "-0.618"], ["unit", "vw"]
]

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

Checks for units.
34 changes: 34 additions & 0 deletions tests/languages/css!+css-extras/variable_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var(--color-primary)
var(--level-3)
calc(100% - var(--margin-size) * 2)

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

[
["function", "var"],
["punctuation", "("],
["variable", "--color-primary"],
["punctuation", ")"],

["function", "var"],
["punctuation", "("],
["variable", "--level-3"],
["punctuation", ")"],

["function", "calc"],
["punctuation", "("],
["number", "100"],
["unit", "%"],
["operator", "-"],
["function", "var"],
["punctuation", "("],
["variable", "--margin-size"],
["punctuation", ")"],
["operator", "*"],
["number", "2"],
["punctuation", ")"]
]

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

Checks for variables.