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

Customizable tokenizer in the fork API #253

Open
scripthunter7 opened this issue Apr 15, 2023 · 0 comments · May be fixed by #264
Open

Customizable tokenizer in the fork API #253

scripthunter7 opened this issue Apr 15, 2023 · 0 comments · May be fixed by #264

Comments

@scripthunter7
Copy link
Contributor

scripthunter7 commented Apr 15, 2023

It would be nice to be able to specify a custom tokenizer function in the fork API, so that CSSTree could be extended to syntaxes that don't follow the spec at all points. For example, Adblock Extended CSS syntax, where some psudeo classes don't follow the CSS spec, and bracket balancing or other things need to be checked, similar to how the CSS spec declares bad-url-token. Two typical problems:

  • :contains(a'b) tokenized as unclosed string
  • :xpath(//*[contains(text(),"a")]) tokenized as unclosed comment

These problems can be solved by the customizability of the parser via the fork API, but it would be a more efficient solution if the tokenizer could be directly customizable at some level.

This way, the default tokenizer in CSSTree can still strictly follow the CSS spec, but the forks could be more flexible.

I thought of a very simple thing:

import * as cssTree from 'css-tree';

// this function should be compatible with the default tokenizer
// and should follow the type of basic tokens:
const customTokenize = function(source, onToken) {
    // ...
}

const fork = cssTree.fork({
    // Use the customized tokenizer
    tokenize: customTokenize,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants