Skip to content

Commit

Permalink
fix: handle plus sign before value (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Apr 3, 2019
1 parent 4ae727f commit 016a444
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 14 deletions.
56 changes: 56 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,3 +744,59 @@ test(
'calc(calc(100% / 3) * 3)',
'100%',
);

test(
'plus sign',
testValue,
'calc(+100px + +100px)',
'200px',
);

test(
'plus sign (#1)',
testValue,
'calc(+100px - +100px)',
'0px',
);

test(
'plus sign (#2)',
testValue,
'calc(200px * +1)',
'200px',
);

test(
'plus sign (#3)',
testValue,
'calc(200px / +1)',
'200px',
);

test(
'minus sign',
testValue,
'calc(-100px + -100px)',
'-200px',
);

test(
'minus sign (#2)',
testValue,
'calc(-100px - -100px)',
'0px',
);

test(
'minus sign (#3)',
testValue,
'calc(200px * -1)',
'-200px',
);

test(
'minus sign (#4)',
testValue,
'calc(200px / -1)',
'-200px',
);
30 changes: 16 additions & 14 deletions src/parser.jison
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@
"+" return 'ADD';
"-" return 'SUB';

([0-9]+("."[0-9]+)?|"."[0-9]+)px\b return 'LENGTH';
([0-9]+("."[0-9]+)?|"."[0-9]+)em\b return 'EMS';
([0-9]+("."[0-9]+)?|"."[0-9]+)ex\b return 'EXS';
([0-9]+("."[0-9]+)?|"."[0-9]+)ch\b return 'CHS';
([0-9]+("."[0-9]+)?|"."[0-9]+)rem\b return 'REMS';
([0-9]+("."[0-9]+)?|"."[0-9]+)vw\b return 'VWS';
([0-9]+("."[0-9]+)?|"."[0-9]+)vh\b return 'VHS';
([0-9]+("."[0-9]+)?|"."[0-9]+)vmin\b return 'VMINS';
([0-9]+("."[0-9]+)?|"."[0-9]+)vmax\b return 'VMAXS';
([0-9]+("."[0-9]+)?|"."[0-9]+)cm\b return 'LENGTH';
([0-9]+("."[0-9]+)?|"."[0-9]+)mm\b return 'LENGTH';
([0-9]+("."[0-9]+)?|"."[0-9]+)in\b return 'LENGTH';
([0-9]+("."[0-9]+)?|"."[0-9]+)pt\b return 'LENGTH';
([0-9]+("."[0-9]+)?|"."[0-9]+)pc\b return 'LENGTH';
([0-9]+("."[0-9]+)?|"."[0-9]+)px\b return 'LENGTH';
([0-9]+("."[0-9]+)?|"."[0-9]+)deg\b return 'ANGLE';
([0-9]+("."[0-9]+)?|"."[0-9]+)grad\b return 'ANGLE';
([0-9]+("."[0-9]+)?|"."[0-9]+)rad\b return 'ANGLE';
Expand All @@ -34,14 +42,6 @@
([0-9]+("."[0-9]+)?|"."[0-9]+)dpi\b return 'RES';
([0-9]+("."[0-9]+)?|"."[0-9]+)dpcm\b return 'RES';
([0-9]+("."[0-9]+)?|"."[0-9]+)dppx\b return 'RES';
([0-9]+("."[0-9]+)?|"."[0-9]+)em\b return 'EMS';
([0-9]+("."[0-9]+)?|"."[0-9]+)ex\b return 'EXS';
([0-9]+("."[0-9]+)?|"."[0-9]+)ch\b return 'CHS';
([0-9]+("."[0-9]+)?|"."[0-9]+)rem\b return 'REMS';
([0-9]+("."[0-9]+)?|"."[0-9]+)vw\b return 'VWS';
([0-9]+("."[0-9]+)?|"."[0-9]+)vh\b return 'VHS';
([0-9]+("."[0-9]+)?|"."[0-9]+)vmin\b return 'VMINS';
([0-9]+("."[0-9]+)?|"."[0-9]+)vmax\b return 'VMAXS';
([0-9]+("."[0-9]+)?|"."[0-9]+)\% return 'PERCENTAGE';
([0-9]+("."[0-9]+)?|"."[0-9]+)\b return 'NUMBER';

Expand Down Expand Up @@ -77,11 +77,6 @@ expression
| value { $$ = $1; }
;

value
: NUMBER { $$ = { type: 'Value', value: parseFloat($1) }; }
| SUB NUMBER { $$ = { type: 'Value', value: parseFloat($2) * -1 }; }
;

function
: FUNCTION { $$ = { type: 'Function', value: $1 }; }
;
Expand All @@ -101,5 +96,12 @@ expression
| VMINS { $$ = { type: 'VminValue', value: parseFloat($1), unit: 'vmin' }; }
| VMAXS { $$ = { type: 'VmaxValue', value: parseFloat($1), unit: 'vmax' }; }
| PERCENTAGE { $$ = { type: 'PercentageValue', value: parseFloat($1), unit: '%' }; }
| ADD css_value { var prev = $2; $$ = prev; }
| SUB css_value { var prev = $2; prev.value *= -1; $$ = prev; }
;

value
: NUMBER { $$ = { type: 'Value', value: parseFloat($1) }; }
| ADD NUMBER { $$ = { type: 'Value', value: parseFloat($2) }; }
| SUB NUMBER { $$ = { type: 'Value', value: parseFloat($2) * -1 }; }
;

0 comments on commit 016a444

Please sign in to comment.