Stricted matching of namespaced elements.

This commit is contained in:
Yorick Peterse 2014-08-11 00:47:07 +02:00
parent 78da274452
commit 873bd82273
2 changed files with 15 additions and 6 deletions

View File

@ -451,18 +451,19 @@ module Oga
def node_matches?(xml_node, ast_node) def node_matches?(xml_node, ast_node)
ns, name = *ast_node ns, name = *ast_node
# If only the name is given and is a wildcard then we'll also want to
# match the namespace as a wildcard.
if !ns and name == '*'
ns = '*'
end
name_matches = name_matches?(xml_node, name) name_matches = name_matches?(xml_node, name)
ns_matches = false ns_matches = false
if ns if ns
ns_matches = namespace_matches?(xml_node, ns) ns_matches = namespace_matches?(xml_node, ns)
# If there's no namespace given but the name matches we'll also mark elsif name_matches and !xml_node.namespace
# the namespace as matching.
#
# THINK: stop automatically matching namespaces if left out?
#
elsif name_matches
ns_matches = true ns_matches = true
end end

View File

@ -69,6 +69,14 @@ describe Oga::XPath::Evaluator do
@evaluator.node_matches?(text, s(:test, nil, 'a')).should == false @evaluator.node_matches?(text, s(:test, nil, 'a')).should == false
end end
example 'return false when the node has a namespace that is not given' do
@evaluator.node_matches?(@name_ns_node, s(:test, nil, 'b')).should == false
end
example 'return true if a node with a namespace is matched using a wildcard' do
@evaluator.node_matches?(@name_ns_node, s(:test, nil, '*')).should == true
end
end end
context '#has_parent?' do context '#has_parent?' do