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

Conversation

ludofischer
Copy link
Collaborator

Fix #132

\b also matches '.', so the regex for the number expression would match 41 in 41.7rpx. Then UNKNOWN_DIMENSION would match .7rpx and create the invalid sequence NumberExpression, UnknownDimension.

Fix #132

`\b` also matches '.', so the regex for the number expression would match
`41` in `41.7rpx`. Then `UNKNOWN_DIMENSION` would match `.7rpx`
and create the invalid sequence `NumberExpression`, `UnknownDimension`.
Copy link
Collaborator Author

@ludofischer ludofischer left a comment

Choose a reason for hiding this comment

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

This solves the issue but maybe it's better to refactor so that numbers and units are parsed as separate tokens, which is closer to what the CSS spec says.

@@ -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

@@ -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';

(([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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Floating number with unknown unit can't be parsed
2 participants