diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index 7c8f4e4..2efb983 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -107,7 +107,9 @@ module Oga nodes = XML::NodeSet.new context.each do |xml_node| - nodes << xml_node if node_matches?(xml_node, node) + if can_match_node?(xml_node) and node_matches?(xml_node, node) + nodes << xml_node + end end return nodes @@ -259,6 +261,16 @@ module Oga return name_matches && ns_matches end + ## + # Returns `true` if the given XML node can be compared using + # {#node_matches?}. + # + # @param [Oga::XML::Node] node + # + def can_match_node?(node) + return node.respond_to?(:name) && node.respond_to?(:namespace) + end + ## # @param [Oga::XML::Node] node # @return [TrueClass|FalseClass] diff --git a/spec/oga/xpath/evaluator/paths_spec.rb b/spec/oga/xpath/evaluator/paths_spec.rb index b5de5c0..e442647 100644 --- a/spec/oga/xpath/evaluator/paths_spec.rb +++ b/spec/oga/xpath/evaluator/paths_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' describe Oga::XPath::Evaluator do before do - @document = parse('') + @document = parse('Foo') @evaluator = described_class.new(@document) end @@ -56,8 +56,8 @@ describe Oga::XPath::Evaluator do example 'return the correct nodes' do a = @document.children[0] - @set[0].should == a.children[0] - @set[1].should == a.children[1] + @set[0].should == a.children[1] + @set[1].should == a.children[2] end end