csstree / csstree

A tool set for CSS including fast detailed parser, walker, generator and lexer based on W3C specs and browser implementations
https://csstree.github.io/docs/
MIT License
1.89k stars 83 forks source link

Syntax validation - class starting with a number returns incorrect error message #275

Open coxrichuk opened 2 months ago

coxrichuk commented 2 months ago

If I attempt to validate a class selector that starts with a number I returned with a message of Parse error: Percent sign is expected

My code looks like:

const selector = '.123';

try {
    const ast = cssTree.parse(selector.trim(), {
        context: 'selector',
        positions: true
    });

} catch (err) {
    throw err; // returns Percent sign is expected
}

Is this expected behavior?

lahmatiy commented 2 months ago

Class names in a selector can't start with a number. If you need a class name starting with a number, you need to escape it (for instance, using CSS.escape('123')). The correct selector would be .\\31 23.

You see the error Percent sign is expected because currently, csstree uses the same parser for selectors and keyframe preludes, where a percentage is acceptable. According to CSS parsing rules, the tokenizer produces a number token from .123, which instructs csstree to expect a percentage.

In short, yes, that's expected behavior.

lahmatiy commented 3 weeks ago

Relevant to #178