Ben
Ben

Reputation: 10146

Why is Nokogiri failing on multiple pseudo element selectors?

I'm using Nokogiri, and I ran into a problem with pseudo-selector chaining.

These work:

document.at_css(".link:not(.button)")
document.at_css(".link:after")

But this doesn't:

document.at_css(".link:not(.button):after")

I get a rather long error message along these lines:

/Users/me/.rvm/gems/ruby-2.0.0-p195/gems/nokogiri-1.6.0/lib/nokogiri/css/parser_extras.rb:87:in `on_error':
unexpected ':' after '[#<Nokogiri::CSS::Node:0x007fc9540932b8 @type=:CONDITIONAL_SELECTOR, @value=[#<Nokogiri::CSS::Node:0x007fc954093da8 @type=:ELEMENT_NAME, @value=["*"]>, #<Nokogiri::CSS::Node:0x007fc954093f38 @type=:COMBINATOR, @value=[#<Nokogiri::CSS::Node:0x007fc95409b350 @type=:CLASS_CONDITION, @value=["link"]>, #<Nokogiri::CSS::Node:0x007fc95409ab80 @type=:NOT, @value=[#<Nokogiri::CSS::Node:0x007fc95409acc0 @type=:CLASS_CONDITION, @value=["button"]>]>]>]>]' 
(Nokogiri::CSS::SyntaxError)

How do I resolve this issue?

Upvotes: 0

Views: 765

Answers (2)

pguardiario
pguardiario

Reputation: 54984

After isn't really a selector. You might need to explain what you're trying to do otherwise I suggest:

document.at(".link:not(.button)").after "content"

Upvotes: 3

the Tin Man
the Tin Man

Reputation: 160551

Without sample HTML it's hard to tell what you're after, but I'll take a wild-shot starting with a replacement of:

document.at_css(".link:not(.button):after")

with:

document.css(".link").reject{ |n| n['class'][/\b button \b/ix] }.first.next_sibling

I've read comments that some of the CSS isn't an exact match to jQuery's so workarounds are needed. Take a glance at:

https://github.com/sparklemotion/nokogiri/issues/907

The doc.css("input:not([disabled])").count work around might be useful.

https://github.com/sparklemotion/nokogiri/blob/master/ROADMAP.md

Upvotes: 2

Related Questions