From 26d4bdc5b12bd03349d5cad33de7df045ee0f168 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Tue, 5 Aug 2014 21:10:12 +0200 Subject: [PATCH] Support for the XPath "self" axis. --- lib/oga/xpath/evaluator.rb | 17 ++++++++++++ spec/oga/xpath/evaluator/axes/self_spec.rb | 30 ++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 spec/oga/xpath/evaluator/axes/self_spec.rb diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index 058c797..ee34e32 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -381,6 +381,23 @@ module Oga return nodes end + ## + # Evaluates the `self` axis. + # + # @param [Oga::XPath::Node] ast_node + # @param [Oga::XML::NodeSet] context + # @return [Oga::XML::NodeSet] + # + def on_axis_self(ast_node, context) + nodes = XML::NodeSet.new + + context.each do |context_node| + nodes << context_node if node_matches?(context_node, ast_node) + 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/axes/self_spec.rb b/spec/oga/xpath/evaluator/axes/self_spec.rb new file mode 100644 index 0000000..164541c --- /dev/null +++ b/spec/oga/xpath/evaluator/axes/self_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +describe Oga::XPath::Evaluator do + context 'self axis' do + before do + @document = parse('') + @evaluator = described_class.new(@document) + end + + context 'matching the context node itself' do + before do + @set = @evaluator.evaluate('a/self::a') + end + + it_behaves_like :node_set, :length => 1 + + example 'return the node' do + @set[0].should == @document.children[0] + end + end + + context 'matching non existing nodes' do + before do + @set = @evaluator.evaluate('a/self::b') + end + + it_behaves_like :empty_node_set + end + end +end