Basic support for evaluating XPath wildcards.

This commit is contained in:
Yorick Peterse 2014-07-09 20:06:31 +02:00
parent f660b11e47
commit ed45058983
3 changed files with 65 additions and 2 deletions

View File

@ -71,7 +71,19 @@ module Oga
@context.each do |xml_node| @context.each do |xml_node|
next unless xml_node.is_a?(XML::Element) 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 @stack << xml_node
end end
end end

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></a>') @document = parse('<a><b></b><b></b><ns1:c></ns1:c></a>')
@evaluator = described_class.new(@document) @evaluator = described_class.new(@document)
end end
@ -62,4 +62,24 @@ describe Oga::XPath::Evaluator do
@set[1].should == a.children[1] @set[1].should == a.children[1]
end end
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 end

View File

@ -0,0 +1,31 @@
require 'spec_helper'
describe Oga::XPath::Evaluator do
before do
@document = parse('<a><b></b><b></b><ns1:c></ns1:c></a>')
@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 <b> node' do
@set[0].name.should == 'b'
end
example 'include the second <b> node' do
@set[1].name.should == 'b'
end
example 'include the <ns1:c> node' do
@set[2].name.should == 'c'
@set[2].namespace.should == 'ns1'
end
end
end