Basic support for evaluating XPath wildcards.
This commit is contained in:
parent
f660b11e47
commit
ed45058983
|
@ -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
|
||||
|
|
|
@ -2,7 +2,7 @@ require 'spec_helper'
|
|||
|
||||
describe Oga::XPath::Evaluator 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)
|
||||
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
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue