Support for the XPath pipe operator.

This commit is contained in:
Yorick Peterse 2014-08-17 22:04:08 +02:00
parent d1735750c1
commit 2817784e6b
2 changed files with 76 additions and 0 deletions

View File

@ -509,6 +509,20 @@ module Oga
return nodes
end
##
# Processes the pipe (`|`) operator. This operator creates a union of two
# sets.
#
# @param [Oga::XPath::Node] ast_node
# @param [Oga::XML::NodeSet] context
# @return [Oga::XML::NodeSet]
#
def on_pipe(ast_node, context)
left, right = *ast_node
return process(left, context) + process(right, context)
end
##
# Returns a node set containing all the child nodes of the given set of
# nodes.

View File

@ -0,0 +1,62 @@
require 'spec_helper'
describe Oga::XPath::Evaluator do
context 'pipe operator' do
before do
@document = parse('<root><a></a><b></b></root>')
@evaluator = described_class.new(@document)
end
context 'merge two sets' do
before do
@set = @evaluator.evaluate('/root/a | /root/b')
end
it_behaves_like :node_set, :length => 2
example 'include the <a> node' do
@set[0].name.should == 'a'
end
example 'include the <b> node' do
@set[1].name.should == 'b'
end
end
context 'merge two sets when the left hand side is empty' do
before do
@set = @evaluator.evaluate('foo | /root/b')
end
it_behaves_like :node_set, :length => 1
example 'include the <b> node' do
@set[0].name.should == 'b'
end
end
context 'merge two sets when the right hand side is empty' do
before do
@set = @evaluator.evaluate('/root/a | foo')
end
it_behaves_like :node_set, :length => 1
example 'include the <a> node' do
@set[0].name.should == 'a'
end
end
context 'merge two identical sets' do
before do
@set = @evaluator.evaluate('/root/a | /root/a')
end
it_behaves_like :node_set, :length => 1
example 'include only a single <a> node' do
@set[0].name.should == 'a'
end
end
end
end