Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle plus sign before value #79

Merged
merged 1 commit into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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';
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just better order


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 }; }
;