Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(): Fix Parse error on PX #76

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions parser.jison
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have px as LENGTH above on line 13.

And looks like this fix will not work 10pX and 10Px. It is better to make lower case before sending to parser.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have enabled the plugin to convert PX to REM, and 50px to 1rem.

div{
  width: 50px; // 1rem
  width: calc(100% - 10PX); // calc(100% - 10PX)
  height: calc(10px - 10PX); // calc(0.2rem -10PX)
}

This CSS can be converted multiple times and needs to be uppercase at all times,and the browser supports PXPx

This issue also appeared in postcss-calc and they fixed it(7.0.4)

([0-9]+("."[0-9]*)?|"."[0-9]+)\b return 'NUMBER';

(calc) return 'NESTED_CALC';
Expand Down Expand Up @@ -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; }
;
14 changes: 14 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/lib/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function isValueType(type) {
case 'VminValue':
case 'VmaxValue':
case 'PercentageValue':
case 'PXValue':
case 'Value':
return true;
}
Expand Down