andybalholm / cascadia

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

Attribute value of zero #25

Closed suntong closed 7 years ago

suntong commented 7 years ago

Is this claim to be true:

using quotation marks around an attribute value is required only if this value is not a valid identifier

-- taken from here

I have a multiple attribute selector as "table[border=0][cellpadding=0][cellspacing=0]", and cascadia is failing with the following message:

panic: expected identifier, found 0 instead

goroutine 1 [running]:
panic(0x749a80, 0xc420075ec0)
        /usr/local/go/src/runtime/panic.go:500 +0x1a1
github.com/andybalholm/cascadia.MustCompile(0x7ffe2d3262ea, 0x1b, 0x0)
        /...gopath/src/github.com/andybalholm/cascadia/selector.go:59 +0x7e
...

At first I thought it was issues #24, but it turns out to be zero value of specified attribute. I.e., if I change the CSS selector to table[border="0"][cellpadding="0"][cellspacing="0"], it will work.

Do you think it is worthwhile for you to double-check please? I assume there's a problem because xidel handles table[border=0][cellpadding=0][cellspacing=0] without any problem. Thx.

suntong commented 7 years ago

PS, here is how I tested it, FYI,

$ echo '<table border="0" cellpadding="0" cellspacing="0" style="table-layout: fixed; width: 100%; border: 0 dashed; border-color: #FFFFFF"><tr style="height:64px">aaa</tr></table>' | cascadia -i -o -c 'table[border="0"][cellpadding="0"][cellspacing="0"]'
1 elements for 'table[border="0"][cellpadding="0"][cellspacing="0"]':
<table border="0" cellpadding="0" cellspacing="0" style="table-layout: fixed; width: 100%; border: 0 dashed; border-color: #FFFFFF"><tbody><tr style="height:64px"></tr></tbody></table>

That cascadia command above is what I build a thin wrapper around you cascadia package -- https://github.com/suntong/cascadia.

If you could mention that in your readme file, that'd be very appreciated, and I believe it might help a lot of people. Thx.

andybalholm commented 7 years ago

The problem is that 0 is not a valid identifier. An identifier must begin with a letter.

Safari doesn't accept the selector in your example either:

> document.querySelector("table[border=0][cellpadding=0][cellspacing=0]")
< SyntaxError (DOM Exception 12): The string did not match the expected pattern.