From d09ab26680112142cf46b94198be984f87edcafb Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Tue, 15 Jul 2014 09:41:12 +0200 Subject: [PATCH] Support for the ancestor-or-self axis. --- lib/oga/xpath/evaluator.rb | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index 4d6e68d..94c9543 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -72,14 +72,16 @@ module Oga def on_axis(node, context) name, test = *node - return send("on_axis_#{name}", test, context) + handler = name.gsub('-', '_') + + return send("on_axis_#{handler}", test, context) end def on_axis_ancestor(node, context) nodes = XML::NodeSet.new context.each do |xml_node| - while xml_node.respond_to?(:parent) and xml_node.parent + while has_parent?(xml_node) xml_node = xml_node.parent if node_matches?(xml_node, node) @@ -92,6 +94,23 @@ module Oga return nodes end + def on_axis_ancestor_or_self(node, context) + nodes = XML::NodeSet.new + + context.each do |xml_node| + while has_parent?(xml_node) + if node_matches?(xml_node, node) + nodes << xml_node + break + end + + xml_node = xml_node.parent + end + end + + return nodes + end + def child_nodes(nodes) children = XML::NodeSet.new @@ -121,6 +140,10 @@ module Oga return name_matches && ns_matches end + + def has_parent?(node) + return node.respond_to?(:parent) && !!node.parent + end end # Evaluator end # XPath end # Oga