andybalholm / cascadia

CSS selector library in Go
BSD 2-Clause "Simplified" License
697 stars 65 forks source link

Identifiers prefixed with multiple dashes don't work #47

Closed erkie closed 2 years ago

erkie commented 3 years ago

It looks like this is the offending line:

https://github.com/andybalholm/cascadia/blob/master/parser.go#L101

Simple test case:

func TestDoubleDash(t *testing.T) {
    // Works
    _, err := cascadia.Compile(".-foobar")
    if err != nil {
        t.Error("Should succeed with single dash")
    }

    // Doesn't work
    _, err = cascadia.Compile(".--foobar")
    if err != nil {
        t.Error("Should succeed with double dash")
    }
}
andybalholm commented 3 years ago

at https://www.w3.org/TR/CSS21/syndata.html#value-def-identifier:

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit.

So --foobar is not a valid CSS identifier because it starts with two hyphens.

erkie commented 3 years ago

Is this project intended to be 100% spec compliant or real world compliant? If the latter I would argue it's a reason to get this fixed. (If it's worth anyone's time however, is up for debate) The reason I found this bug after 3 hours of debugging was that the test case worked in both Chrome, Firefox, node and other command line tools like pup

andybalholm commented 3 years ago

It's intended to work in the real world. (But note that you can rewrite your selector in a spec-compliant way as .\2d\2d foobar)

If there's a real-world use case and somebody writes a high-quality pull request, I'll merge it.

erkie commented 2 years ago

Wow! Thanks for this everyone involved.