Lindsey B
Lindsey B

Reputation: 556

Two nots in CSS selector with Nokogiri

Right now I have a selector working with jQuery as follows:

.original-tweet:not([data-is-reply-to="true"],.retweeted)

However this doesn't seem to work using the Nokogiri gem in ruby:

doc.css('.original-tweet:not([data-is-reply-to="true"],.retweeted)')

The above causes a cash, but each of the parts of the not independently work:

 doc.css('.original-tweet:not([data-is-reply-to="true"])')

and

 doc.css('.original-tweet:not(.retweeted)')

What's the best way to actually get the selector I want. Is this something that just isn't supported in nokogiri?

Upvotes: 2

Views: 1136

Answers (3)

Lindsey B
Lindsey B

Reputation: 556

Okay, I solved it with XPATH

The following worked (note: the xpath I created was entirely computer generated)

doc.xpath("//*[contains(concat(' ', @class, ' '), ' original-tweet ') and not(@data-is-reply-to = \"true\") and not(@data-retweet-id)]")

Edit: further inspection shows that this is still selecting items with the retweeted class (turns out this was a false assumption on my part, I should have been looking for the data-retweet-id attribute instead of the retweet class)

github.com/sparklemotion/nokogiri/issues/451 - this issue relates to why I needed to use xpath here.

Upvotes: 3

pguardiario
pguardiario

Reputation: 54984

For now a possible workaround might be:

doc.css('.original-tweet:not([data-is-reply-to="true"])') - doc.css('.retweeted')

Upvotes: 1

Blender
Blender

Reputation: 298256

While the selector may work with jQuery, it's not a valid CSS selector:

> $$('.original-tweet:not([data-is-reply-to="true"], .retweeted)')
Error: SyntaxError: DOM Exception 12

.original-tweet:not([data-is-reply-to="true"]):not(.retweeted) should work.

Upvotes: 1

Related Questions