Skip to content

Commit

Permalink
feat: add support for lh & rlh units
Browse files Browse the repository at this point in the history
  • Loading branch information
René Schleusner authored and ludofischer committed Nov 13, 2024
1 parent 1ccc2ef commit 9b6d7a1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
6 changes: 5 additions & 1 deletion parser.jison
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)cqb\b return 'CQBS';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)cqmin\b return 'CQMINS';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)cqmax\b return 'CQMAXS';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)lh\b return 'LHS';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)rlh\b return 'RLHS';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)cm\b return 'LENGTH';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)mm\b return 'LENGTH';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)Q\b return 'LENGTH';
Expand Down Expand Up @@ -150,7 +152,9 @@ expression
| CQIS { $$ = { type: 'CqiValue', value: parseFloat($1), unit: 'cqi' }; }
| CQBS { $$ = { type: 'CqbValue', value: parseFloat($1), unit: 'cqb' }; }
| CQMINS { $$ = { type: 'CqminValue', value: parseFloat($1), unit: 'cqmin' }; }
| CQMAXS { $$ = { type: 'CqmaxValue', value: parseFloat($1), unit: 'cqmax' }; }
| CQMAXS { $$ = { type: 'CqmaxValue', value: parseFloat($1), unit: 'cqmax' }; }
| LHS { $$ = { type: 'LhValue', value: parseFloat($1), unit: 'lh' }; }
| RLHS { $$ = { type: 'RlhValue', value: parseFloat($1), unit: 'rlh' }; }
| PERCENTAGE { $$ = { type: 'PercentageValue', value: parseFloat($1), unit: '%' }; }
| ADD dimension { var prev = $2; $$ = prev; }
| SUB dimension { var prev = $2; prev.value *= -1; $$ = prev; }
Expand Down
2 changes: 2 additions & 0 deletions src/lib/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ function isValueType(node) {
case 'CqminValue':
case 'CqmaxValue':
case 'PercentageValue':
case 'LhValue':
case 'RlhValue':
case 'Number':
return true;
}
Expand Down
4 changes: 3 additions & 1 deletion src/parser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ export interface DimensionExpression {
| 'CqbValue'
| 'CqiValue'
| 'CqminValue'
| 'CqmaxValue';
| 'CqmaxValue'
| 'LhValue'
| 'RlhValue';
value: number;
unit: string;
}
Expand Down
20 changes: 20 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,26 @@ test(
testValue('calc(100svmax - 44.5svh)', 'calc(100svmax - 44.5svh)')
);

test(
'should add numbers with lh units',
testValue('calc(1lh + 4lh)', '5lh')
);

test(
'should add numbers with rlh units',
testValue('calc(1rlh + 4rlh)', '5rlh')
);

test(
'should not combine different lh units',
testValue('calc(1lh + 4rlh)', 'calc(1lh + 4rlh)')
);

test(
'should not combine different lh units',
testValue('calc(1lh + 20px)', 'calc(1lh + 20px)')
);

test(
'should parse fractions without leading zero',
testValue('calc(2rem - .14285em)', 'calc(2rem - 0.14285em)')
Expand Down

0 comments on commit 9b6d7a1

Please sign in to comment.