Cheerio: $("tr:eq(2)") won't work but $("tr").eq(2) does

Created on 1 Dec 2012  路  7Comments  路  Source: cheeriojs/cheerio

The problem is :eq, :gt, :lt selectors are not working. They just return empty result.

All 7 comments

That's because :eq and friends aren't implemented. I'm wondering why you're getting results at all, as using them should throw a SyntaxError.

Could they be supported? Sizzle does. The same is true for jQuery, etc.

CSSselect applies selectors right-to-left, Sizzle left-to-right. RTL
comes with a nice performance benefit, as it's always O(n), while
Sizzle is O(n^m) (m being the level of child selectors).

There are some features that CSSselect therefore can't support, eg.
:eq (as that applies to matches on the left side) and child
selector-first queries.

if you can't support the jQuery api, then don't claim to support it at all.

Cheerio supports jQuery's _core API_. There are some parts that won't be supported due to practical reasons (animations), the rest is added as needed. Some weird parts won't be supported, unless someone makes a convincing case for them (& pull requests help, too).

Can't cheerio convert $('element:eq(1)') internally to work the same as $('element').eq(1)?

@awvalenti I've just build a module that does just that, though it doesn't hook into cheerio but you can use it along side cheerio: https://github.com/watson/cheerio-eq

Currently it only fixes :eq(), but it would be trivial to add support for other selectors (though in that case I would have to rename the module ;)

Let me know what you think and please open pull requests or issues if you have any suggestions/comments/bugs/feedback :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Canop picture Canop  路  3Comments

bxqgit picture bxqgit  路  3Comments

chenweiyj picture chenweiyj  路  5Comments

M3kH picture M3kH  路  4Comments

dandv picture dandv  路  5Comments