cezheng / Fuzi

A fast & lightweight XML & HTML parser in Swift with XPath & CSS support
MIT License
1.07k stars 154 forks source link

CSS selector expression generates XPath error #8

Open dafi opened 8 years ago

dafi commented 8 years ago

I heavly use css selectors and when they are a bit more complex the XPathFromCSS routine fails

The css selector .box-paging a:not(.active) generates the error

XPath error : Undefined namespace prefix
.//*[contains(concat(' ',normalize-space(@class),' '),' box-paging ')]/descendant::a:not([contains(concat(' ',normalize-space(@class),' '),' active) ')]

You can reproduce the error yourself using the following snippet

    func checkSelector() {
        let html = "<div class=\"box-paging\"><a class=\"active\" href=\"1.html\">1</a><a href=\"2.html\">2</a><a href=\"3.html\">3</a></div>"

        if let htmlDocument = try? HTMLDocument(string: html) {
            htmlDocument.css(".box-paging a:not(.active)")
        }

    }

Obviously the selector works fine using firebug or document.querySelectorAll()

cezheng commented 8 years ago

Hi, thanks for reporting this issue. I guess many CSS3 selectors are still not supported in Fuzi at the moment, but I'm planning to add them soon. Currently I'm on a vacation without a Mac at hand, so maybe when I have some free time after I get back.

iDevPro commented 5 years ago

Hello, thanks for fast library, but I have same issue:

root.xpath("//*[@id = 'booklist']//*[starts-with(@id, 'div-userbook-')]") // work
root.css("#booklist > div[id^='div-userbook-']") // crashed on xpath conversion rules
// XPath error : Invalid expression
// .//*[@id = 'booklist']/div[@id^='div-userbook-']