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

How to convert tokens to unescaped, usable values? #227

Open
romainmenke opened this issue Jan 2, 2023 · 5 comments
Open

How to convert tokens to unescaped, usable values? #227

romainmenke opened this issue Jan 2, 2023 · 5 comments

Comments

@romainmenke
Copy link

css-tree/tokenizer exposes tokens as [type, start, end].
But it does not seem to expose unescaped and usable values.

For example :

https://www.w3.org/TR/css-syntax-3/#consume-numeric-token

A dimension-token should have a value, a unit and a type flag.

10px:

  • value: 10
  • unit: px
  • type: integer

10\70 x:

  • value: 10
  • unit: px
  • type: integer

Are there helper functions for this?

@romainmenke
Copy link
Author

Another example:

Does a hash token have type id or unrestricted.
The only way to answer this is by checking if it starts with ident start code points or ident code points.

@lahmatiy
Copy link
Member

lahmatiy commented Jan 3, 2023

CSSTree tokenizer doesn't produce an object for a token for performance/memory reasons. That's not necessary in most cases. However, it provides some helper functions to decode/encode values for ident, string and url tokens. See Util functions for detail.

@romainmenke
Copy link
Author

Thank you for that info!
I am using those helper functions but they are limited.

Is there any way to collect the information outlined above with CSSTree?
Is there a different API that does produce an object per token?

This information is needed to correctly parse CSS so I assume it exists somewhere in CSSTree?

@chinchang
Copy link

Another method is passing the output from css-tree to something like lightningcss. Works well!

@romainmenke
Copy link
Author

Another method is passing the output from css-tree to something like lightningcss. Works well!

I am sure it does, I can also pass it to one of the parsing utilities I've created in @csstools

But for users this doesn't make sense.
Why would they bloat their node_modules and pay the performance penalty of tokenizing/parsing twice?


csslex strikes the right balance.

It tokenizes in the same minimal way as csstree but it also exposes a value function to gather the information I listed above.

Having such a function in csstree would be useful.

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

No branches or pull requests

3 participants