From 05f6fc2f8d278591b97793edb1bb1edc37ad5f4e Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Thu, 14 Aug 2014 22:30:14 +0200 Subject: [PATCH] Implement node() as a type test, not a function. --- lib/oga/xpath/evaluator.rb | 41 ++++++------------- .../{functions => node_types}/node_spec.rb | 2 +- 2 files changed, 13 insertions(+), 30 deletions(-) rename spec/oga/xpath/evaluator/{functions => node_types}/node_spec.rb (97%) diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index dbafa39..075a133 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -199,14 +199,15 @@ module Oga end ## - # Evaluates the `child` axis. This simply delegates work to {#on_test}. + # Evaluates the `child` axis. This simply delegates work to {#on_test} + # or {#on_node_type}. # # @param [Oga::XPath::Node] ast_node # @param [Oga::XML::NodeSet] context # @return [Oga::XML::NodeSet] # def on_axis_child(ast_node, context) - return on_test(ast_node, child_nodes(context)) + return process(ast_node, child_nodes(context)) end ## @@ -421,52 +422,34 @@ module Oga end ## - # Dispatches function calls to specific handlers. + # Dispatches node type matching to dedicated handlers. # # @param [Oga::XPath::Node] ast_node # @param [Oga::XML::NodeSet] context # @return [Oga::XML::NodeSet] # - def on_call(ast_node, context) + def on_node_type(ast_node, context) name, test = *ast_node handler = name.gsub('-', '_') - return send("on_call_#{handler}", test, context) + return send("on_node_type_#{handler}", test, context) end ## - # Processes the `node` function call. The W3 was apparently too lazy to - # properly document this function in their official specification. From - # Wikipedia: - # - # node() - # finds any node at all. - # - # Based on trial and error this function appears to match the following: - # - # * elements - # * text nodes - # * comments - # * CDATA nodes - # - # In Oga this translates to the following two classes: - # - # * {Oga::XML::Element} - # * {Oga::XML::Text} + # Processes the `node()` node type. This type matcher can be used to match + # all node types (text, comment, etc). # # @param [Oga::XPath::Node] ast_node # @param [Oga::XML::NodeSet] context # @return [Oga::XML::NodeSet] # - def on_call_node(ast_node, context) + def on_node_type_node(ast_node, context) nodes = XML::NodeSet.new - context.each do |context_node| - context_node.children.each do |child| - if child.is_a?(XML::Element) or child.is_a?(XML::Text) - nodes << child - end + context.each do |node| + if node.is_a?(XML::Element) or node.is_a?(XML::Text) + nodes << node end end diff --git a/spec/oga/xpath/evaluator/functions/node_spec.rb b/spec/oga/xpath/evaluator/node_types/node_spec.rb similarity index 97% rename from spec/oga/xpath/evaluator/functions/node_spec.rb rename to spec/oga/xpath/evaluator/node_types/node_spec.rb index 1e9f145..180caaf 100644 --- a/spec/oga/xpath/evaluator/functions/node_spec.rb +++ b/spec/oga/xpath/evaluator/node_types/node_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe Oga::XPath::Evaluator do - context 'node function' do + context 'node() tests' do before do @document = parse('foo') @evaluator = described_class.new(@document)