diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index e110788..b9af481 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -71,7 +71,19 @@ module Oga @context.each do |xml_node| next unless xml_node.is_a?(XML::Element) - if xml_node.name == name and xml_node.namespace == ns + name_matches = xml_node.name == name || name == '*' + ns_matches = false + + if ns and (xml_node.namespace == ns or ns == '*') + ns_matches = true + + # If there's no namespace given but the name matches we'll also mark + # the namespace as matching. + elsif name_matches + ns_matches = true + end + + if name_matches and ns_matches @stack << xml_node end end diff --git a/spec/oga/xpath/evaluator/paths_spec.rb b/spec/oga/xpath/evaluator/paths_spec.rb index 6873371..6ca4ad7 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('') @evaluator = described_class.new(@document) end @@ -62,4 +62,24 @@ describe Oga::XPath::Evaluator do @set[1].should == a.children[1] end end + + context 'namespaced paths' do + before do + @set = @evaluator.evaluate('a/ns1:c') + end + + example 'return a NodeSet instance' do + @set.is_a?(Oga::XML::NodeSet).should == true + end + + example 'return the right amount of rows' do + @set.length.should == 1 + end + + example 'return the correct row' do + a = @document.children[0] + + @set[0].should == a.children[-1] + end + end end diff --git a/spec/oga/xpath/evaluator/wildcard_spec.rb b/spec/oga/xpath/evaluator/wildcard_spec.rb new file mode 100644 index 0000000..9ad60cb --- /dev/null +++ b/spec/oga/xpath/evaluator/wildcard_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe Oga::XPath::Evaluator do + before do + @document = parse('') + @evaluator = described_class.new(@document) + end + + context 'full wildcards' do + before do + @set = @evaluator.evaluate('a/*') + end + + example 'retunr the right amount of rows' do + @set.length.should == 3 + end + + example 'include the first node' do + @set[0].name.should == 'b' + end + + example 'include the second node' do + @set[1].name.should == 'b' + end + + example 'include the node' do + @set[2].name.should == 'c' + @set[2].namespace.should == 'ns1' + end + end +end