Nil error when css querying for three paths
Created by: benben
Hi Partypeople,
First of all, thank you very much for making this gem.
So here is my story. One day I tried to be smart and thought "Why not selecting all the links, images and script tags at once?"
Oga.parse_html(html_body).css('a, img, script')
Sadly, this tiny cute simple thing blews up Oga. Tested it with latest version and current master (40501f95). A stack trace is following of a test I wrote for it:
NoMethodError:
undefined method `type' for nil:NilClass
# ./lib/oga/ruby/generator.rb:12:in `process'
# ./lib/oga/ruby/generator.rb:157:in `block in on_send'
# ./lib/oga/ruby/generator.rb:157:in `map'
# ./lib/oga/ruby/generator.rb:157:in `on_send'
# ./lib/oga/ruby/generator.rb:12:in `process'
# ./lib/oga/ruby/generator.rb:108:in `on_if'
# ./lib/oga/ruby/generator.rb:12:in `process'
# ./lib/oga/ruby/generator.rb:18:in `block in on_followed_by'
# ./lib/oga/ruby/generator.rb:18:in `map'
# ./lib/oga/ruby/generator.rb:18:in `on_followed_by'
# ./lib/oga/ruby/generator.rb:12:in `process'
# ./lib/oga/ruby/generator.rb:18:in `block in on_followed_by'
# ./lib/oga/ruby/generator.rb:18:in `map'
# ./lib/oga/ruby/generator.rb:18:in `on_followed_by'
# ./lib/oga/ruby/generator.rb:12:in `process'
# ./lib/oga/ruby/generator.rb:18:in `block in on_followed_by'
# ./lib/oga/ruby/generator.rb:18:in `map'
# ./lib/oga/ruby/generator.rb:18:in `on_followed_by'
# ./lib/oga/ruby/generator.rb:12:in `process'
# ./lib/oga/ruby/generator.rb:18:in `block in on_followed_by'
# ./lib/oga/ruby/generator.rb:18:in `map'
# ./lib/oga/ruby/generator.rb:18:in `on_followed_by'
# ./lib/oga/ruby/generator.rb:12:in `process'
# ./lib/oga/ruby/generator.rb:18:in `block in on_followed_by'
# ./lib/oga/ruby/generator.rb:18:in `map'
# ./lib/oga/ruby/generator.rb:18:in `on_followed_by'
# ./lib/oga/ruby/generator.rb:12:in `process'
# ./lib/oga/ruby/generator.rb:177:in `on_block'
# ./lib/oga/ruby/generator.rb:12:in `process'
# ./lib/oga/xpath/compiler.rb:93:in `compile'
# ./lib/oga/xml/querying.rb:79:in `css'
# ./spec/oga/xml/querying_spec.rb:42:in `block (3 levels) in <top (required)>'
I thought: Wow a genuine bug, lets clone it fast, fix it and get some nice street credibility since oga was on HN not long ago.
it 'parses three paths separated by a comma' do
parse_css('foo, bar, baz').should ==
parse_xpath('descendant::foo | descendant::bar | descendant::baz')
end
It is green. So I added my own test in this describe block, which looks like this:
it 'queries for three paths separated by a comma' do
@document.css('a, a, a').should == @document.children
end
This produces the error shown above. I tried to understand where the problem originates from and pry'ed my way through the oga code but got stuck. I think at a point where you did too complex stuff and my brain started to melt. Oga::XPath::Compiler#compile
method here. I'v checked the ast input and this looks good, but after this, the proc_ast
variable contains a nil value which blows up the following process
by Ruby::Generator
. But it can also be that the previous created AST is not correct already and then just gets through like that.
Sorry that I cannot provide you with a ready PR for this problem, but I would love to fix it by myself. Maybe you could give me some directions and insights how this part of Oga is working so I can continue fixing it. If this takes too much time, you can also fix it by yourself.
In any way: Thank your for reading I felt a bit like writing today.