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

fix: do not consume number token if expression ends with '.' #164

Open
wants to merge 1 commit into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,15 @@ test(
testValue('calc(1px + 2unknown)', 'calc(1px + 2unknown)')
);

test('decimal unknown units', async () => {
const result = await postcss(reduceCalc()).process(
'p{width: calc(120rpx - 41.7rpx)}',
postcssOpts
);
const warnings = result.warnings();
assert.is(warnings.length, 0);
});

test(
'error with parsing',
testThrows(
Expand Down
2 changes: 1 addition & 1 deletion src/parser.jison
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)dpcm\b return 'RES';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)dppx\b return 'RES';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)\% return 'PERCENTAGE';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)\b return 'NUMBER';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)(?!".")\b return 'NUMBER';
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Using a lookahead assertion to check there is no dot before the word boundary. That's illegal CSS anyway https://drafts.csswg.org/css-syntax-3/#typedef-number-token


(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)-?([a-zA-Z_]|[\240-\377]|(\\[0-9a-fA-F]{1,6}(\r\n|[ \t\r\n\f])?|\\[^\r\n\f0-9a-fA-F]))([a-zA-Z0-9_-]|[\240-\377]|(\\[0-9a-fA-F]{1,6}(\r\n|[ \t\r\n\f])?|\\[^\r\n\f0-9a-fA-F]))*\b return 'UNKNOWN_DIMENSION';
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Alternatively we could try simplifying UNKNOWN_DIMENSION. Why does it need to match so many types of characters, even \r, \t, etc? According to the spec, the unit can only be a CSS identifier https://www.w3.org/TR/css-values-3/#css-identifier

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it is right direction, yes, I think it is wrong

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I realized that this parsing duplicates a lot of work that's already done in postcss-value-parser. It calls postcss-value-parser, stringifies the result, then parses again. Looks like the reason is that this package also wants to parse calc() in selectors, which is not in the spec. But it feels a bit like a waste to do this work all over again. There's already 3 separate CSS tokenizers in postcss, postcss-value-parser, and postcss-selector-parser. So together with postcss-calc, the tokenizer has been reimplemented 4 times in PostCSS projects.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, It was here before I was invited and fix bugs, will be great to rewrite it on one parser


Expand Down