Skip to content

Commit

Permalink
Identify CSS units and variables (#1450)
Browse files Browse the repository at this point in the history
Assume CSS unit is a word or `%` after a number, CSS variable is a word inside `var()`.

`%` is not recognized as a part of number, but the leading `-` is.

When using minus operator in `calc` function, we must type a space in both sides of `-`
(value like `calc(100%-5px)` is wrong), so if we met a pattern like `-[\d.]` in a CSS value
(not in selector, rule or variable), it must be the start of a negative number.
  • Loading branch information
RexSkz authored and mAAdhaTTah committed Dec 3, 2018
1 parent 0cc8c56 commit 5fcee96
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 6 deletions.
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\()[^)]+(?=\))/,
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.

0 comments on commit 5fcee96

Please sign in to comment.