My JavaScript book is out! Don't miss the opportunity to upgrade your beginner or average dev skills.

Thursday, April 30, 2009

vice-versa sub project: css2xpath

Update

I completely re-wrote the test suite. You can check a live example here where results are compared with Sizzle selector engine against the common W3C page.



After this Ajaxian post and some opinion exchange in its comments, I decided to extract the css2xpath function I used inside experiments in vice-versa project, creating a separated project specific for this purpose that some how could be interesting or useful.
Bugs, problems, questions, and helps are more than welcome, so far I focused my attention to SlickSpeed test selectors and nothing else but at least for that it seems to be stable and reliable.

9 comments:

V1 said...

What to believe,

css2xpath or sizzle :)

Query: div[class!=""]

Function : css2xpath
Result : //div[not(@class) or @class!=""]
Valid : true
Found : 46
Via Sizzle : 45

Andrea Giammarchi said...

well, depends ... it is not simple to say.

If we cosider attrib != value as every element where attrib is different and where every element without attrib obviously will considered different, XPath is the right one.

But if we consider attrib != value as attrib must be present and have to be different from value, Sizzle is the right one.

So, the question is, which way to interpreter != is the correct one?

Andrea Giammarchi said...

uhm ... considering that div[class!="none"] is 46 in Sizzle, I guess it is a Sizzle problem with empty clause in class ... at least, logically speaking, I would expect 45 as well ...

Anonymous said...

wow.. you really rocks at regex and XPath! ; )

kangax said...

Andrea,

How do you deal with buggy xpath's position() function in webkit (up to and including Safari 4 beta, afaik)?

See IS_XPATH_POSITION_FUNCTION_BUGGY test in my CFT (http://yura.thinkweb2.com/cft/)

Andrea Giammarchi said...
This comment has been removed by the author.
Andrea Giammarchi said...

Juriy, I do not like that much to deal with bugs presents in browsers that people updates without being stuck forever a la Internet Explorer ... but I will consider to create a work around for that. Thanks to tell me about that problem, I did not know about it :)

kentaromiura said...

dumb question:

//*[1]/self::div
equals to
//*[1]/.[name(.) ='div']

right?

instead I always think it is equals to
//*[1]/./div
I don't know why ...
:(

MostThingsWeb said...

Hello,

Very well done! As a heads up, I have ported this project to C#: https://code.google.com/p/css2xpath-csharp/