Skip to content

Commit de1338d

Browse files
committed
Handle nested calc values. Fixes MoOx#23.
1 parent f2b7b6f commit de1338d

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

parser.jison

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
([0-9]+("."[0-9]+)?|"."[0-9]+)\% return 'PERCENTAGE';
3838
([0-9]+("."[0-9]+)?|"."[0-9]+)\b return 'NUMBER';
3939

40+
(calc) return 'NESTED_CALC';
41+
([a-z]+) return 'PREFIX';
42+
4043
"(" return 'LPAREN';
4144
")" return 'RPAREN';
4245

@@ -63,6 +66,8 @@ expression
6366
| math_expression MUL math_expression { $$ = { type: 'MathExpression', operator: $2, left: $1, right: $3 }; }
6467
| math_expression DIV math_expression { $$ = { type: 'MathExpression', operator: $2, left: $1, right: $3 }; }
6568
| LPAREN math_expression RPAREN { $$ = $2; }
69+
| NESTED_CALC LPAREN math_expression RPAREN { $$ = $3; }
70+
| SUB PREFIX SUB NESTED_CALC LPAREN math_expression RPAREN { $$ = $6; }
6671
| css_value { $$ = $1; }
6772
| value { $$ = $1; }
6873
;

src/__tests__/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ test(
7070
'a 2px b 3em c'
7171
)
7272

73+
test(
74+
'should reduce nested calc',
75+
testFixture,
76+
'calc(100% - calc(50% + 25px))',
77+
'calc(50% - 25px)'
78+
)
79+
80+
test(
81+
'should reduce prefixed nested calc',
82+
testFixture,
83+
'-webkit-calc(100% - -webkit-calc(50% + 25px))',
84+
'-webkit-calc(50% - 25px)'
85+
)
86+
7387
test(
7488
'should reduce calc with newline characters',
7589
testFixture,

0 commit comments

Comments
 (0)