SwiftSoup icon indicating copy to clipboard operation
SwiftSoup copied to clipboard

Unable to get cssSelector for node with escape character

Open siuying opened this issue 2 years ago • 2 comments

When there were non alphanumeric characters in css classes, cssSelector() will failed with error like "Could not parse query div.Fz(xs).Fw(b): unexpected token at (xs).Fw(b)"

Sample:

let html: String = #"<div class="Fz(xs) Fw(b)"><div>149.64</div></div>"#
let doc = try SwiftSoup.parse(html)
let element = try doc.getElementsContainingOwnText("149.64")
try element.first()?.cssSelector()

siuying avatar Jun 03 '22 09:06 siuying

What result do you expect?

As I know, parentheses are reserved in CSS. Safari also can't get an accurate CSS selector for this example.

nikolaykargin avatar Jun 03 '22 11:06 nikolaykargin

I'd expect they are escaped as described in CSS.escape() or escape a character

As an example, in this page, i can get the selector fin-streamer.Fw\(b\):nth-child(1) > span:nth-child(1) from Firefox. You can try it on the page with selector (note the escape) document.querySelector(".Fw\\(b\\)")

siuying avatar Jun 03 '22 14:06 siuying