Skip to content

Commit cafefda

Browse files
committed
Add support for css variables.
1 parent de1338d commit cafefda

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

parser.jison

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
([0-9]+("."[0-9]+)?|"."[0-9]+)\b return 'NUMBER';
3939

4040
(calc) return 'NESTED_CALC';
41+
(var\(.*\)) return 'CSS_VAR';
4142
([a-z]+) return 'PREFIX';
4243

4344
"(" return 'LPAREN';
@@ -68,6 +69,7 @@ expression
6869
| LPAREN math_expression RPAREN { $$ = $2; }
6970
| NESTED_CALC LPAREN math_expression RPAREN { $$ = $3; }
7071
| SUB PREFIX SUB NESTED_CALC LPAREN math_expression RPAREN { $$ = $6; }
72+
| css_variable { $$ = $1; }
7173
| css_value { $$ = $1; }
7274
| value { $$ = $1; }
7375
;
@@ -77,6 +79,10 @@ expression
7779
| SUB NUMBER { $$ = { type: 'Value', value: parseFloat($2) * -1 }; }
7880
;
7981

82+
css_variable
83+
: CSS_VAR { $$ = { type: 'CssVariable', value: $1 }; }
84+
;
85+
8086
css_value
8187
: LENGTH { $$ = { type: 'LengthValue', value: parseFloat($1), unit: /[a-z]+/.exec($1)[0] }; }
8288
| ANGLE { $$ = { type: 'AngleValue', value: parseFloat($1), unit: /[a-z]+/.exec($1)[0] }; }

src/__tests__/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ test(
8484
'-webkit-calc(50% - 25px)'
8585
)
8686

87+
test(
88+
'should ignore calc with css variables',
89+
testFixture,
90+
'calc(var(--mouseX) * 1px)'
91+
)
92+
8793
test(
8894
'should reduce calc with newline characters',
8995
testFixture,

src/lib/stringifier.js

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ function stringify(node, prec) {
3535
}
3636
case "Value":
3737
return round(node.value, prec)
38+
case 'CssVariable':
39+
return node.value
3840
default:
3941
return round(node.value, prec) + node.unit
4042
}

0 commit comments

Comments
 (0)