XPath support for processing instructions.

This commit is contained in:
Yorick Peterse 2014-08-16 22:37:57 +02:00
parent 56341b5585
commit bb503728af
3 changed files with 62 additions and 2 deletions

View File

@ -491,6 +491,24 @@ module Oga
return nodes
end
##
# Processes the `processing-instruction()` type test. This matches only
# processing-instruction nodes.
#
# @param [Oga::XPath::Node] ast_node
# @param [Oga::XML::NodeSet] context
# @return [Oga::XML::NodeSet]
#
def on_type_test_processing_instruction(ast_node, context)
nodes = XML::NodeSet.new
context.each do |node|
nodes << node if node.is_a?(XML::ProcessingInstruction)
end
return nodes
end
##
# Returns a node set containing all the child nodes of the given set of
# nodes.

View File

@ -7,7 +7,7 @@ describe Oga::XPath::Evaluator do
@evaluator = described_class.new(@document)
end
context 'matching text nodes' do
context 'matching comment nodes' do
before do
@set = @evaluator.evaluate('a/comment()')
end
@ -23,7 +23,7 @@ describe Oga::XPath::Evaluator do
end
end
context 'matching nested text nodes' do
context 'matching nested comment nodes' do
before do
@set = @evaluator.evaluate('a/b/comment()')
end

View File

@ -0,0 +1,42 @@
require 'spec_helper'
describe Oga::XPath::Evaluator do
context 'processing-instruction() tests' do
before do
@document = parse('<a><?a foo ?><b><?b bar ?></b></a>')
@evaluator = described_class.new(@document)
end
context 'matching processing instruction nodes' do
before do
@set = @evaluator.evaluate('a/processing-instruction()')
end
it_behaves_like :node_set, :length => 1
example 'return a ProcessingInstruction instance' do
@set[0].is_a?(Oga::XML::ProcessingInstruction).should == true
end
example 'return the "foo" node' do
@set[0].text.should == ' foo '
end
end
context 'matching nested processing instruction nodes' do
before do
@set = @evaluator.evaluate('a/b/processing-instruction()')
end
it_behaves_like :node_set, :length => 1
example 'return a ProcessingInstruction instance' do
@set[0].is_a?(Oga::XML::ProcessingInstruction).should == true
end
example 'return the "bar" node' do
@set[0].text.should == ' bar '
end
end
end
end