Identify CSS units and variables (#1450) 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
diff --git a/components/prism-css-extras.js b/components/prism-css-extras.js
index 3b4261f..254688b 100644
--- a/components/prism-css-extras.js
+++ b/components/prism-css-extras.js
@@ -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.]+/
});
diff --git a/components/prism-css-extras.min.js b/components/prism-css-extras.min.js
index f3d252f..34a2226 100644
--- a/components/prism-css-extras.min.js
+++ b/components/prism-css-extras.min.js
@@ -1 +1 @@
-Prism.languages.css.selector={pattern:Prism.languages.css.selector,inside:{"pseudo-element":/:(?:after|before|first-letter|first-line|selection)|::[-\w]+/,"pseudo-class":/:[-\w]+(?:\(.*\))?/,"class":/\.[-:.\w]+/,id:/#[-:.\w]+/,attribute:/\[[^\]]+\]/}},Prism.languages.insertBefore("css","function",{hexcode:/#[\da-f]{3,8}/i,entity:/\\[\da-f]{1,8}/i,number:/[\d%.]+/});
\ No newline at end of file
+Prism.languages.css.selector={pattern:Prism.languages.css.selector,inside:{"pseudo-element":/:(?:after|before|first-letter|first-line|selection)|::[-\w]+/,"pseudo-class":/:[-\w]+(?:\(.*\))?/,"class":/\.[-:.\w]+/,id:/#[-:.\w]+/,attribute:/\[[^\]]+\]/}},Prism.languages.insertBefore("css","function",{variable:{pattern:/(var\()[^)]+(?=\))/,lookbehind:!0},operator:{pattern:/(\s)[+\-*\/](?=\s)/,lookbehind:!0},hexcode:/#[\da-f]{3,8}/i,entity:/\\[\da-f]{1,8}/i,unit:{pattern:/(\d)(?:%|[a-z]+)/,lookbehind:!0},number:/-?[\d.]+/});
\ No newline at end of file
diff --git a/tests/languages/css!+css-extras/number_feature.test b/tests/languages/css!+css-extras/number_feature.test
index 24ed530..ef58e62 100644
--- a/tests/languages/css!+css-extras/number_feature.test
+++ b/tests/languages/css!+css-extras/number_feature.test
@@ -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"]
]
----------------------------------------------------
diff --git a/tests/languages/css!+css-extras/operator_feature.test b/tests/languages/css!+css-extras/operator_feature.test
new file mode 100644
index 0000000..0df7612
--- /dev/null
+++ b/tests/languages/css!+css-extras/operator_feature.test
@@ -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.
diff --git a/tests/languages/css!+css-extras/unit_feature.test b/tests/languages/css!+css-extras/unit_feature.test
new file mode 100644
index 0000000..6d5c15c
--- /dev/null
+++ b/tests/languages/css!+css-extras/unit_feature.test
@@ -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.
diff --git a/tests/languages/css!+css-extras/variable_feature.test b/tests/languages/css!+css-extras/variable_feature.test
new file mode 100644
index 0000000..d17053a
--- /dev/null
+++ b/tests/languages/css!+css-extras/variable_feature.test
@@ -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.