Skip to content

Commit

Permalink
fix: add support for container query units (#214)
Browse files Browse the repository at this point in the history
Fix #199
  • Loading branch information
ludofischer authored Aug 5, 2024
1 parent 5d30859 commit 3f2b43a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
12 changes: 12 additions & 0 deletions parser.jison
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)vh\b return 'VHS';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)vmin\b return 'VMINS';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)vmax\b return 'VMAXS';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)cqw\b return 'CQWS';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)cqh\b return 'CQHS';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)cqi\b return 'CQIS';
(([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]+)?)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 @@ -99,6 +105,12 @@ expression
| VWS { $$ = { type: 'VwValue', value: parseFloat($1), unit: 'vw' }; }
| VMINS { $$ = { type: 'VminValue', value: parseFloat($1), unit: 'vmin' }; }
| VMAXS { $$ = { type: 'VmaxValue', value: parseFloat($1), unit: 'vmax' }; }
| CQWS { $$ = { type: 'CqwValue', value: parseFloat($1), unit: 'cqw' }; }
| CQHS { $$ = { type: 'CqhValue', value: parseFloat($1), unit: 'cqh' }; }
| 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' }; }
| PERCENTAGE { $$ = { type: 'PercentageValue', value: parseFloat($1), unit: '%' }; }
| ADD dimension { var prev = $2; $$ = prev; }
| SUB dimension { var prev = $2; prev.value *= -1; $$ = prev; }
Expand Down
6 changes: 6 additions & 0 deletions src/lib/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ function isValueType(node) {
case 'VwValue':
case 'VminValue':
case 'VmaxValue':
case 'CqwValue':
case 'CqhValue':
case 'CqiValue':
case 'CqbValue':
case 'CqminValue':
case 'CqmaxValue':
case 'PercentageValue':
case 'Number':
return true;
Expand Down
8 changes: 7 additions & 1 deletion src/parser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ export interface DimensionExpression {
| 'VhValue'
| 'VwValue'
| 'VminValue'
| 'VmaxValue';
| 'VmaxValue'
| 'CqwValue'
| 'CqhValue'
| 'CqbValue'
| 'CqiValue'
| 'CqminValue'
| 'CqmaxValue';
value: number;
unit: string;
}
Expand Down
35 changes: 35 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,41 @@ test(
testValue('calc(100% + 1px)', 'calc(100% + 1px)')
);

test(
'should preserve calc with cqw units',
testValue('calc(12.72727px + 8.523cqw)', 'calc(12.72727px + 8.523cqw)')
);

test(
'should add numbers with cqw units',
testValue('calc(1cqw + 8cqw)', '9cqw')
);

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

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

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

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

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

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

0 comments on commit 3f2b43a

Please sign in to comment.