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.
This commit is contained in:
Yorick Peterse 2015-07-23 09:33:34 +02:00
parent e549b28ca4
commit 9000d5efdb
1 changed files with 2 additions and 9 deletions

View File

@ -20,10 +20,6 @@ module Oga
# Node types that require a NodeSet to push nodes into. # Node types that require a NodeSet to push nodes into.
USE_NODESET = [:path, :absolute_path, :axis, :predicate] 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. # Compiles and caches an AST.
# #
@ -43,7 +39,7 @@ module Oga
document = node_literal document = node_literal
matched = matched_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) } ruby_ast = process(ast, document) { |node| matched.push(node) }
else else
ruby_ast = process(ast, document) ruby_ast = process(ast, document)
@ -86,7 +82,6 @@ module Oga
# @return [Oga::Ruby::Node] # @return [Oga::Ruby::Node]
# #
def on_path(ast, input, &block) def on_path(ast, input, &block)
matched = matched_literal
ruby_ast = nil ruby_ast = nil
var_name = node_literal var_name = node_literal
last_index = ast.children.length - 1 last_index = ast.children.length - 1
@ -99,9 +94,7 @@ module Oga
# The last segment of the path should add the code that actually # The last segment of the path should add the code that actually
# pushes the matched node into the node set. # pushes the matched node into the node set.
if index == 0 if index == 0
ruby_ast = process(child, input_var) do |node| ruby_ast = process(child, input_var, &block)
matched.push(node)
end
else else
ruby_ast = process(child, input_var) { ruby_ast } ruby_ast = process(child, input_var) { ruby_ast }
end end