Skip to content

Commit

Permalink
feat: relax parser on unknown units (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Apr 4, 2019
1 parent 69a3ca0 commit b91c6e9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1029,3 +1029,24 @@ test(
'calc(1q + 10pc)',
'170.33333q'
);

test(
'unknown units',
testValue,
'calc(1unknown + 2unknown)',
'calc(1unknown + 2unknown)'
);

test(
'unknown units with known',
testValue,
'calc(1unknown + 2px)',
'calc(1unknown + 2px)'
);

test(
'unknown units with known (#1)',
testValue,
'calc(1px + 2unknown)',
'calc(1px + 2unknown)'
);
2 changes: 1 addition & 1 deletion src/lib/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function transformValue(value, options, result, item) {
// stringify calc expression and produce an AST
const contents = valueParser.stringify(node.nodes);
const ast = parser.parse(contents);

// reduce AST to its simplest form, that is, either to a single value
// or a simplified calc expression
const reducedAst = reducer(ast, options.precision);
Expand Down
3 changes: 3 additions & 0 deletions src/parser.jison
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)\% return 'PERCENTAGE';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)\b return 'NUMBER';

(([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';

"(" return 'LPAREN';
")" return 'RPAREN';

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

0 comments on commit b91c6e9

Please sign in to comment.