From 873bd82273a685deb89164efe590bce4c9dfcaca Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Mon, 11 Aug 2014 00:47:07 +0200 Subject: [PATCH] Stricted matching of namespaced elements. --- lib/oga/xpath/evaluator.rb | 13 +++++++------ spec/oga/xpath/evaluator/general_spec.rb | 8 ++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index 4f6f90f..c2b6bfe 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -451,18 +451,19 @@ module Oga def node_matches?(xml_node, 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) ns_matches = false if ns ns_matches = namespace_matches?(xml_node, ns) - # If there's no namespace given but the name matches we'll also mark - # the namespace as matching. - # - # THINK: stop automatically matching namespaces if left out? - # - elsif name_matches + elsif name_matches and !xml_node.namespace ns_matches = true end diff --git a/spec/oga/xpath/evaluator/general_spec.rb b/spec/oga/xpath/evaluator/general_spec.rb index 801432a..382caf0 100644 --- a/spec/oga/xpath/evaluator/general_spec.rb +++ b/spec/oga/xpath/evaluator/general_spec.rb @@ -69,6 +69,14 @@ describe Oga::XPath::Evaluator do @evaluator.node_matches?(text, s(:test, nil, 'a')).should == false 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 context '#has_parent?' do