Skip to content

Commit 1cd2c5f

Browse files
fix: handle numbers with exponen composed (#83)
1 parent c4db282 commit 1cd2c5f

File tree

2 files changed

+74
-32
lines changed

2 files changed

+74
-32
lines changed

src/__tests__/index.js

+42
Original file line numberDiff line numberDiff line change
@@ -849,3 +849,45 @@ test(
849849
'calc(/*test*/100px + calc(/*test*/100px/*test*/ + /*test*/100px/*test*/))',
850850
'300px',
851851
);
852+
853+
test(
854+
'exponent composed',
855+
testValue,
856+
'calc(1.1e+1px + 1.1e+1px)',
857+
'22px',
858+
);
859+
860+
test(
861+
'exponent composed (#1)',
862+
testValue,
863+
'calc(10e+1px + 10e+1px)',
864+
'200px',
865+
);
866+
867+
test(
868+
'exponent composed (#2)',
869+
testValue,
870+
'calc(1.1e+10px + 1.1e+10px)',
871+
'22000000000px',
872+
);
873+
874+
test(
875+
'exponent composed (#3)',
876+
testValue,
877+
'calc(9e+1 * 1px)',
878+
'90px',
879+
);
880+
881+
test(
882+
'exponent composed (#4)',
883+
testValue,
884+
'calc(9e+1% + 10%)',
885+
'100%',
886+
);
887+
888+
test(
889+
'exponent composed (uppercase)',
890+
testValue,
891+
'calc(1.1E+1px + 1.1E+1px)',
892+
'22px',
893+
);

src/parser.jison

+32-32
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,33 @@
1717
"+" return 'ADD';
1818
"-" return 'SUB';
1919

20-
([0-9]+("."[0-9]+)?|"."[0-9]+)em\b return 'EMS';
21-
([0-9]+("."[0-9]+)?|"."[0-9]+)ex\b return 'EXS';
22-
([0-9]+("."[0-9]+)?|"."[0-9]+)ch\b return 'CHS';
23-
([0-9]+("."[0-9]+)?|"."[0-9]+)rem\b return 'REMS';
24-
([0-9]+("."[0-9]+)?|"."[0-9]+)vw\b return 'VWS';
25-
([0-9]+("."[0-9]+)?|"."[0-9]+)vh\b return 'VHS';
26-
([0-9]+("."[0-9]+)?|"."[0-9]+)vmin\b return 'VMINS';
27-
([0-9]+("."[0-9]+)?|"."[0-9]+)vmax\b return 'VMAXS';
28-
([0-9]+("."[0-9]+)?|"."[0-9]+)cm\b return 'LENGTH';
29-
([0-9]+("."[0-9]+)?|"."[0-9]+)mm\b return 'LENGTH';
30-
([0-9]+("."[0-9]+)?|"."[0-9]+)in\b return 'LENGTH';
31-
([0-9]+("."[0-9]+)?|"."[0-9]+)pt\b return 'LENGTH';
32-
([0-9]+("."[0-9]+)?|"."[0-9]+)pc\b return 'LENGTH';
33-
([0-9]+("."[0-9]+)?|"."[0-9]+)px\b return 'LENGTH';
34-
([0-9]+("."[0-9]+)?|"."[0-9]+)deg\b return 'ANGLE';
35-
([0-9]+("."[0-9]+)?|"."[0-9]+)grad\b return 'ANGLE';
36-
([0-9]+("."[0-9]+)?|"."[0-9]+)rad\b return 'ANGLE';
37-
([0-9]+("."[0-9]+)?|"."[0-9]+)turn\b return 'ANGLE';
38-
([0-9]+("."[0-9]+)?|"."[0-9]+)s\b return 'TIME';
39-
([0-9]+("."[0-9]+)?|"."[0-9]+)ms\b return 'TIME';
40-
([0-9]+("."[0-9]+)?|"."[0-9]+)Hz\b return 'FREQ';
41-
([0-9]+("."[0-9]+)?|"."[0-9]+)kHz\b return 'FREQ';
42-
([0-9]+("."[0-9]+)?|"."[0-9]+)dpi\b return 'RES';
43-
([0-9]+("."[0-9]+)?|"."[0-9]+)dpcm\b return 'RES';
44-
([0-9]+("."[0-9]+)?|"."[0-9]+)dppx\b return 'RES';
45-
([0-9]+("."[0-9]+)?|"."[0-9]+)\% return 'PERCENTAGE';
46-
([0-9]+("."[0-9]+)?|"."[0-9]+)\b return 'NUMBER';
20+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)em\b return 'EMS';
21+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)ex\b return 'EXS';
22+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)ch\b return 'CHS';
23+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)rem\b return 'REMS';
24+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)vw\b return 'VWS';
25+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)vh\b return 'VHS';
26+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)vmin\b return 'VMINS';
27+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)vmax\b return 'VMAXS';
28+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)cm\b return 'LENGTH';
29+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)mm\b return 'LENGTH';
30+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)in\b return 'LENGTH';
31+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)pt\b return 'LENGTH';
32+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)pc\b return 'LENGTH';
33+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)px\b return 'LENGTH';
34+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)deg\b return 'ANGLE';
35+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)grad\b return 'ANGLE';
36+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)rad\b return 'ANGLE';
37+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)turn\b return 'ANGLE';
38+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)s\b return 'TIME';
39+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)ms\b return 'TIME';
40+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)Hz\b return 'FREQ';
41+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)kHz\b return 'FREQ';
42+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)dpi\b return 'RES';
43+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)dpcm\b return 'RES';
44+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)dppx\b return 'RES';
45+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)\% return 'PERCENTAGE';
46+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)\b return 'NUMBER';
4747

4848
"(" return 'LPAREN';
4949
")" return 'RPAREN';
@@ -82,11 +82,11 @@ expression
8282
;
8383

8484
css_value
85-
: LENGTH { $$ = { type: 'LengthValue', value: parseFloat($1), unit: /[a-z]+/i.exec($1)[0] }; }
86-
| ANGLE { $$ = { type: 'AngleValue', value: parseFloat($1), unit: /[a-z]+/i.exec($1)[0] }; }
87-
| TIME { $$ = { type: 'TimeValue', value: parseFloat($1), unit: /[a-z]+/i.exec($1)[0] }; }
88-
| FREQ { $$ = { type: 'FrequencyValue', value: parseFloat($1), unit: /[a-z]+/i.exec($1)[0] }; }
89-
| RES { $$ = { type: 'ResolutionValue', value: parseFloat($1), unit: /[a-z]+/i.exec($1)[0] }; }
85+
: LENGTH { $$ = { type: 'LengthValue', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
86+
| ANGLE { $$ = { type: 'AngleValue', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
87+
| TIME { $$ = { type: 'TimeValue', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
88+
| FREQ { $$ = { type: 'FrequencyValue', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
89+
| RES { $$ = { type: 'ResolutionValue', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
9090
| EMS { $$ = { type: 'EmValue', value: parseFloat($1), unit: 'em' }; }
9191
| EXS { $$ = { type: 'ExValue', value: parseFloat($1), unit: 'ex' }; }
9292
| CHS { $$ = { type: 'ChValue', value: parseFloat($1), unit: 'ch' }; }

0 commit comments

Comments
 (0)