Skip to content

Commit b91c6e9

Browse files
feat: relax parser on unknown units (#85)
1 parent 69a3ca0 commit b91c6e9

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/__tests__/index.js

+21
Original file line numberDiff line numberDiff line change
@@ -1029,3 +1029,24 @@ test(
10291029
'calc(1q + 10pc)',
10301030
'170.33333q'
10311031
);
1032+
1033+
test(
1034+
'unknown units',
1035+
testValue,
1036+
'calc(1unknown + 2unknown)',
1037+
'calc(1unknown + 2unknown)'
1038+
);
1039+
1040+
test(
1041+
'unknown units with known',
1042+
testValue,
1043+
'calc(1unknown + 2px)',
1044+
'calc(1unknown + 2px)'
1045+
);
1046+
1047+
test(
1048+
'unknown units with known (#1)',
1049+
testValue,
1050+
'calc(1px + 2unknown)',
1051+
'calc(1px + 2unknown)'
1052+
);

src/lib/transform.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function transformValue(value, options, result, item) {
1919
// stringify calc expression and produce an AST
2020
const contents = valueParser.stringify(node.nodes);
2121
const ast = parser.parse(contents);
22-
22+
2323
// reduce AST to its simplest form, that is, either to a single value
2424
// or a simplified calc expression
2525
const reducedAst = reducer(ast, options.precision);

src/parser.jison

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)\% return 'PERCENTAGE';
4747
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)\b return 'NUMBER';
4848

49+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)-?([a-zA-Z_]|[\240-\377]|(\\[0-9a-fA-F]{1,6}(\r\n|[ \t\r\n\f])?|\\[^\r\n\f0-9a-fA-F]))([a-zA-Z0-9_-]|[\240-\377]|(\\[0-9a-fA-F]{1,6}(\r\n|[ \t\r\n\f])?|\\[^\r\n\f0-9a-fA-F]))*\b return 'UNKNOWN_DIMENSION';
50+
4951
"(" return 'LPAREN';
5052
")" return 'RPAREN';
5153

@@ -88,6 +90,7 @@ expression
8890
| TIME { $$ = { type: 'TimeValue', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
8991
| FREQ { $$ = { type: 'FrequencyValue', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
9092
| RES { $$ = { type: 'ResolutionValue', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
93+
| UNKNOWN_DIMENSION { $$ = { type: 'UnknownDimension', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
9194
| EMS { $$ = { type: 'EmValue', value: parseFloat($1), unit: 'em' }; }
9295
| EXS { $$ = { type: 'ExValue', value: parseFloat($1), unit: 'ex' }; }
9396
| CHS { $$ = { type: 'ChValue', value: parseFloat($1), unit: 'ch' }; }

0 commit comments

Comments
 (0)