Skip node matching for incompatible nodes.

This commit is contained in:
Yorick Peterse 2014-07-22 20:45:15 +02:00
parent 713d8a092b
commit 1f9d2ede95
2 changed files with 16 additions and 4 deletions

View File

@ -107,7 +107,9 @@ module Oga
nodes = XML::NodeSet.new nodes = XML::NodeSet.new
context.each do |xml_node| 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 end
return nodes return nodes
@ -259,6 +261,16 @@ module Oga
return name_matches && ns_matches return name_matches && ns_matches
end 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 # @param [Oga::XML::Node] node
# @return [TrueClass|FalseClass] # @return [TrueClass|FalseClass]

View File

@ -2,7 +2,7 @@ require 'spec_helper'
describe Oga::XPath::Evaluator do describe Oga::XPath::Evaluator do
before do before do
@document = parse('<a><b></b><b></b><ns1:c></ns1:c></a>') @document = parse('<a>Foo<b></b><b></b><ns1:c></ns1:c></a>')
@evaluator = described_class.new(@document) @evaluator = described_class.new(@document)
end end
@ -56,8 +56,8 @@ describe Oga::XPath::Evaluator do
example 'return the correct nodes' do example 'return the correct nodes' do
a = @document.children[0] a = @document.children[0]
@set[0].should == a.children[0] @set[0].should == a.children[1]
@set[1].should == a.children[1] @set[1].should == a.children[2]
end end
end end