From bb503728af1d5d7562e706e134a0c16a6dadf23a Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Sat, 16 Aug 2014 22:37:57 +0200 Subject: [PATCH] XPath support for processing instructions. --- lib/oga/xpath/evaluator.rb | 18 ++++++++ .../evaluator/type_tests/comment_spec.rb | 4 +- .../type_tests/processing_instruction_spec.rb | 42 +++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 spec/oga/xpath/evaluator/type_tests/processing_instruction_spec.rb diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index 43231f9..ef5c17d 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -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. diff --git a/spec/oga/xpath/evaluator/type_tests/comment_spec.rb b/spec/oga/xpath/evaluator/type_tests/comment_spec.rb index adef384..8cd72ed 100644 --- a/spec/oga/xpath/evaluator/type_tests/comment_spec.rb +++ b/spec/oga/xpath/evaluator/type_tests/comment_spec.rb @@ -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 diff --git a/spec/oga/xpath/evaluator/type_tests/processing_instruction_spec.rb b/spec/oga/xpath/evaluator/type_tests/processing_instruction_spec.rb new file mode 100644 index 0000000..f3e92b5 --- /dev/null +++ b/spec/oga/xpath/evaluator/type_tests/processing_instruction_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' + +describe Oga::XPath::Evaluator do + context 'processing-instruction() tests' do + before do + @document = parse('') + @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