From aecca3ca2a29c90ced41d5e7be3408c8cdd5d52f Mon Sep 17 00:00:00 2001 From: chenyulun390 Date: Thu, 25 Nov 2021 21:19:45 +0800 Subject: [PATCH] feat(): Fix Parse error on PX --- parser.jison | 2 ++ src/__tests__/index.js | 14 ++++++++++++++ src/lib/reducer.js | 1 + 3 files changed, 17 insertions(+) diff --git a/parser.jison b/parser.jison index d467718..b3bebd9 100644 --- a/parser.jison +++ b/parser.jison @@ -36,6 +36,7 @@ ([0-9]+("."[0-9]*)?|"."[0-9]+)vmin\b return 'VMINS'; ([0-9]+("."[0-9]*)?|"."[0-9]+)vmax\b return 'VMAXS'; ([0-9]+("."[0-9]*)?|"."[0-9]+)\% return 'PERCENTAGE'; +([0-9]+("."[0-9]*)?|"."[0-9]+)PX\b return 'PX'; ([0-9]+("."[0-9]*)?|"."[0-9]+)\b return 'NUMBER'; (calc) return 'NESTED_CALC'; @@ -101,5 +102,6 @@ expression | VMINS { $$ = { type: 'VminValue', value: parseFloat($1), unit: 'vmin' }; } | VMAXS { $$ = { type: 'VmaxValue', value: parseFloat($1), unit: 'vmax' }; } | PERCENTAGE { $$ = { type: 'PercentageValue', value: parseFloat($1), unit: '%' }; } + | PX { $$ = { type: 'PXValue', value: parseFloat($1), unit: 'PX' }; } | SUB css_value { var prev = $2; prev.value *= -1; $$ = prev; } ; diff --git a/src/__tests__/index.js b/src/__tests__/index.js index e525b6d..75d5751 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -63,6 +63,13 @@ test( 'calc(200px - 100%)' ) +test( + 'should reduce simple calc (8)', + testFixture, + 'calc(1PX + 1PX)', + '2PX' +) + test( 'should reduce additions and subtractions (1)', testFixture, @@ -77,6 +84,13 @@ test( 'calc(100% - 10px)' ) +test( + 'should reduce additions and subtractions (3)', + testFixture, + 'calc(100% + 10PX - 20PX)', + 'calc(100% - 10PX)' +) + test( 'should handle fractions', testFixture, diff --git a/src/lib/reducer.js b/src/lib/reducer.js index cea0634..8e90716 100644 --- a/src/lib/reducer.js +++ b/src/lib/reducer.js @@ -29,6 +29,7 @@ function isValueType(type) { case 'VminValue': case 'VmaxValue': case 'PercentageValue': + case 'PXValue': case 'Value': return true; }