diff --git a/parser.jison b/parser.jison index 36cb668..b46b718 100644 --- a/parser.jison +++ b/parser.jison @@ -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'; @@ -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; } diff --git a/src/lib/reducer.js b/src/lib/reducer.js index b7a6e28..92cc4a3 100644 --- a/src/lib/reducer.js +++ b/src/lib/reducer.js @@ -47,6 +47,8 @@ function isValueType(node) { case 'CqminValue': case 'CqmaxValue': case 'PercentageValue': + case 'LhValue': + case 'RlhValue': case 'Number': return true; } diff --git a/src/parser.d.ts b/src/parser.d.ts index 5ab6579..e3a19fb 100644 --- a/src/parser.d.ts +++ b/src/parser.d.ts @@ -51,7 +51,9 @@ export interface DimensionExpression { | 'CqbValue' | 'CqiValue' | 'CqminValue' - | 'CqmaxValue'; + | 'CqmaxValue' + | 'LhValue' + | 'RlhValue'; value: number; unit: string; } diff --git a/test/index.js b/test/index.js index 635155e..a2f3d16 100644 --- a/test/index.js +++ b/test/index.js @@ -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)')