Skip to content

Commit 3f2b43a

Browse files
authored
fix: add support for container query units (#214)
Fix #199
1 parent 5d30859 commit 3f2b43a

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

Diff for: parser.jison

+12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)vh\b return 'VHS';
2626
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)vmin\b return 'VMINS';
2727
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)vmax\b return 'VMAXS';
28+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)cqw\b return 'CQWS';
29+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)cqh\b return 'CQHS';
30+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)cqi\b return 'CQIS';
31+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)cqb\b return 'CQBS';
32+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)cqmin\b return 'CQMINS';
33+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)cqmax\b return 'CQMAXS';
2834
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)cm\b return 'LENGTH';
2935
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)mm\b return 'LENGTH';
3036
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)Q\b return 'LENGTH';
@@ -99,6 +105,12 @@ expression
99105
| VWS { $$ = { type: 'VwValue', value: parseFloat($1), unit: 'vw' }; }
100106
| VMINS { $$ = { type: 'VminValue', value: parseFloat($1), unit: 'vmin' }; }
101107
| VMAXS { $$ = { type: 'VmaxValue', value: parseFloat($1), unit: 'vmax' }; }
108+
| CQWS { $$ = { type: 'CqwValue', value: parseFloat($1), unit: 'cqw' }; }
109+
| CQHS { $$ = { type: 'CqhValue', value: parseFloat($1), unit: 'cqh' }; }
110+
| CQIS { $$ = { type: 'CqiValue', value: parseFloat($1), unit: 'cqi' }; }
111+
| CQBS { $$ = { type: 'CqbValue', value: parseFloat($1), unit: 'cqb' }; }
112+
| CQMINS { $$ = { type: 'CqminValue', value: parseFloat($1), unit: 'cqmin' }; }
113+
| CQMAXS { $$ = { type: 'CqmaxValue', value: parseFloat($1), unit: 'cqmax' }; }
102114
| PERCENTAGE { $$ = { type: 'PercentageValue', value: parseFloat($1), unit: '%' }; }
103115
| ADD dimension { var prev = $2; $$ = prev; }
104116
| SUB dimension { var prev = $2; prev.value *= -1; $$ = prev; }

Diff for: src/lib/reducer.js

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ function isValueType(node) {
2020
case 'VwValue':
2121
case 'VminValue':
2222
case 'VmaxValue':
23+
case 'CqwValue':
24+
case 'CqhValue':
25+
case 'CqiValue':
26+
case 'CqbValue':
27+
case 'CqminValue':
28+
case 'CqmaxValue':
2329
case 'PercentageValue':
2430
case 'Number':
2531
return true;

Diff for: src/parser.d.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ export interface DimensionExpression {
2525
| 'VhValue'
2626
| 'VwValue'
2727
| 'VminValue'
28-
| 'VmaxValue';
28+
| 'VmaxValue'
29+
| 'CqwValue'
30+
| 'CqhValue'
31+
| 'CqbValue'
32+
| 'CqiValue'
33+
| 'CqminValue'
34+
| 'CqmaxValue';
2935
value: number;
3036
unit: string;
3137
}

Diff for: test/index.js

+35
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,41 @@ test(
257257
testValue('calc(100% + 1px)', 'calc(100% + 1px)')
258258
);
259259

260+
test(
261+
'should preserve calc with cqw units',
262+
testValue('calc(12.72727px + 8.523cqw)', 'calc(12.72727px + 8.523cqw)')
263+
);
264+
265+
test(
266+
'should add numbers with cqw units',
267+
testValue('calc(1cqw + 8cqw)', '9cqw')
268+
);
269+
270+
test(
271+
'should add numbers with cqh units',
272+
testValue('calc(1cqh + 3cqh)', '4cqh')
273+
);
274+
275+
test(
276+
'should add numbers with cqi units',
277+
testValue('calc(1cqi + 3cqi)', '4cqi')
278+
);
279+
280+
test(
281+
'should add numbers with cqb units',
282+
testValue('calc(1cqb + 3cqb)', '4cqb')
283+
);
284+
285+
test(
286+
'should add numbers with cqmin units',
287+
testValue('calc(1cqmin + 3cqmin)', '4cqmin')
288+
);
289+
290+
test(
291+
'should add numbers with cqmax units',
292+
testValue('calc(1cqmax + 3cqmax)', '4cqmax')
293+
);
294+
260295
test(
261296
'should parse fractions without leading zero',
262297
testValue('calc(2rem - .14285em)', 'calc(2rem - 0.14285em)')

0 commit comments

Comments
 (0)