From 2b96d651031ba68fa4159f71314f8c9af164333a Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Wed, 3 Sep 2014 09:40:17 +0200 Subject: [PATCH] XPath "self" axis inside predicates. The "self" axis should use the current context node when inside a predicate. --- lib/oga/xpath/evaluator.rb | 8 ++++++-- spec/oga/xpath/evaluator/axes/self_spec.rb | 14 +++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index 11e6ec2..5d4be27 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -490,8 +490,12 @@ module Oga 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) + if current_node + nodes << current_node if node_matches?(current_node, ast_node) + else + context.each do |context_node| + nodes << context_node if node_matches?(context_node, ast_node) + end end return nodes diff --git a/spec/oga/xpath/evaluator/axes/self_spec.rb b/spec/oga/xpath/evaluator/axes/self_spec.rb index 346bb4e..c6a7444 100644 --- a/spec/oga/xpath/evaluator/axes/self_spec.rb +++ b/spec/oga/xpath/evaluator/axes/self_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe Oga::XPath::Evaluator do context 'self axis' do before do - @document = parse('') + @document = parse('foobar') @evaluator = described_class.new(@document) end @@ -38,5 +38,17 @@ describe Oga::XPath::Evaluator do @set[0].should == @document.children[0] end end + + context 'matching nodes inside predicates' do + before do + @set = @evaluator.evaluate('a/b[.="foo"]') + end + + it_behaves_like :node_set, :length => 1 + + example 'return the first node' do + @set[0].should == @document.children[0].children[0] + end + end end end