From 602f2fe8bba8f5e36b06ced863dc5d840c0dc900 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Tue, 4 Nov 2014 23:41:45 +0100 Subject: [PATCH] Apply node() type tests to the document too. When running XPath queries such as "self::node()" the result should be a set containing the document itself. This in turn fixes expressions such as descendant-or-self::node()/a. --- lib/oga/xpath/evaluator.rb | 4 +++- spec/oga/xpath/evaluator/axes/self_spec.rb | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index 6065c54..b0402ce 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -550,7 +550,9 @@ module Oga nodes = XML::NodeSet.new context.each do |node| - nodes << node if node.is_a?(XML::Node) + if node.is_a?(XML::Node) or node.is_a?(XML::Document) + nodes << 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 b499ce9..85eb0e8 100644 --- a/spec/oga/xpath/evaluator/axes/self_spec.rb +++ b/spec/oga/xpath/evaluator/axes/self_spec.rb @@ -74,5 +74,17 @@ describe Oga::XPath::Evaluator do @set[0].should == @document.children[0].children[1] end end + + context 'matching the document itself' do + before do + @set = @evaluator.evaluate('self::node()') + end + + it_behaves_like :node_set, :length => 1 + + example 'return the document itself' do + @set[0].is_a?(Oga::XML::Document).should == true + end + end end end