From 9000d5efdb663944de56c34177e03336b48f531d Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Thu, 23 Jul 2015 09:33:34 +0200 Subject: [PATCH] XPath::Compiler#compile specifies blocks for paths Previously the on_path method itself would specify a block to use when a node was matched. This makes it impossible to customize this behaviour which is needed when a path is used in an operator or predicate. By letting the #compile() method specify the block other handlers can overwrite it. --- lib/oga/xpath/compiler.rb | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/oga/xpath/compiler.rb b/lib/oga/xpath/compiler.rb index b979c49..a6565e0 100644 --- a/lib/oga/xpath/compiler.rb +++ b/lib/oga/xpath/compiler.rb @@ -20,10 +20,6 @@ module Oga # Node types that require a NodeSet to push nodes into. USE_NODESET = [:path, :absolute_path, :axis, :predicate] - # Node types that require "compile" to define a block to call upon - # matching a node. - ADD_PUSH_BLOCK = [:axis, :predicate] - ## # Compiles and caches an AST. # @@ -43,7 +39,7 @@ module Oga document = node_literal matched = matched_literal - if ADD_PUSH_BLOCK.include?(ast.type) + if USE_NODESET.include?(ast.type) ruby_ast = process(ast, document) { |node| matched.push(node) } else ruby_ast = process(ast, document) @@ -86,7 +82,6 @@ module Oga # @return [Oga::Ruby::Node] # def on_path(ast, input, &block) - matched = matched_literal ruby_ast = nil var_name = node_literal last_index = ast.children.length - 1 @@ -99,9 +94,7 @@ module Oga # The last segment of the path should add the code that actually # pushes the matched node into the node set. if index == 0 - ruby_ast = process(child, input_var) do |node| - matched.push(node) - end + ruby_ast = process(child, input_var, &block) else ruby_ast = process(child, input_var) { ruby_ast } end