From 8a9b26fa735fff23fdd37008f63edd806209809d Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Tue, 5 Aug 2014 19:28:26 +0200 Subject: [PATCH] Basic support for the preceding-sibling xpath axis --- lib/oga/xpath/evaluator.rb | 27 +++++++++++++++ .../evaluator/axes/preceding_sibling_spec.rb | 33 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 spec/oga/xpath/evaluator/axes/preceding_sibling_spec.rb diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index 9d4df4b..058c797 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -354,6 +354,33 @@ module Oga return nodes end + ## + # Evaluates the `preceding-sibling` axis. + # + # @param [Oga::XPath::Node] ast_node + # @param [Oga::XML::NodeSet] context + # @return [Oga::XML::NodeSet] + # + def on_axis_preceding_sibling(ast_node, context) + nodes = XML::NodeSet.new + + context.each do |context_node| + check = true + parent = has_parent?(context_node) ? context_node.parent : nil + + @document.each_node do |doc_node| + # Test everything *until* we hit the current context node. + if doc_node == context_node + break + elsif doc_node.parent == parent and node_matches?(doc_node, ast_node) + nodes << doc_node + end + end + 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/preceding_sibling_spec.rb b/spec/oga/xpath/evaluator/axes/preceding_sibling_spec.rb new file mode 100644 index 0000000..27b015e --- /dev/null +++ b/spec/oga/xpath/evaluator/axes/preceding_sibling_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +describe Oga::XPath::Evaluator do + context 'preceding-sibling axis' do + before do + @document = parse(<<-EOF.strip.gsub(/\s+/m, '')) + + + + + + + + + EOF + + @second_foo = @document.children[0].children[1].children[0] + @evaluator = described_class.new(@document) + end + + context 'matching nodes in the current context' do + before do + @set = @evaluator.evaluate('root/bar/baz/preceding-sibling::foo') + end + + it_behaves_like :node_set, :length => 1 + + example 'return the second node' do + @set[0].should == @second_foo + end + end + end +end