Renamed `node` to `ast_node`.
This should make it a bit easier to understand what kind of data the variable is holding.
This commit is contained in:
parent
211caf00c6
commit
57c0f4b35e
|
@ -35,31 +35,31 @@ module Oga
|
||||||
# dedicated handler method. Handler methods are called "on_X" where "X" is
|
# dedicated handler method. Handler methods are called "on_X" where "X" is
|
||||||
# the node type.
|
# the node type.
|
||||||
#
|
#
|
||||||
# @param [Oga::XPath::Node] node The XPath AST node to process.
|
# @param [Oga::XPath::Node] ast_node The XPath AST node to process.
|
||||||
# @param [Oga::XML::NodeSet] context The context (a set of nodes) to
|
# @param [Oga::XML::NodeSet] context The context (a set of nodes) to
|
||||||
# evaluate an expression in.
|
# evaluate an expression in.
|
||||||
#
|
#
|
||||||
# @return [Oga::XML::NodeSet]
|
# @return [Oga::XML::NodeSet]
|
||||||
#
|
#
|
||||||
def process(node, context)
|
def process(ast_node, context)
|
||||||
handler = "on_#{node.type}"
|
handler = "on_#{ast_node.type}"
|
||||||
|
|
||||||
return send(handler, node, context)
|
return send(handler, ast_node, context)
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Processes an absolute XPath expression such as `/foo`.
|
# Processes an absolute XPath expression such as `/foo`.
|
||||||
#
|
#
|
||||||
# @param [Oga::XPath::Node] node
|
# @param [Oga::XPath::Node] ast_node
|
||||||
# @param [Oga::XML::NodeSet] context
|
# @param [Oga::XML::NodeSet] context
|
||||||
# @return [Oga::XML::NodeSet]
|
# @return [Oga::XML::NodeSet]
|
||||||
#
|
#
|
||||||
def on_absolute_path(node, context)
|
def on_absolute_path(ast_node, context)
|
||||||
if @document.respond_to?(:root_node)
|
if @document.respond_to?(:root_node)
|
||||||
context = XML::NodeSet.new([@document.root_node])
|
context = XML::NodeSet.new([@document.root_node])
|
||||||
end
|
end
|
||||||
|
|
||||||
return on_path(node, context)
|
return on_path(ast_node, context)
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
|
@ -69,14 +69,14 @@ module Oga
|
||||||
# `&&` / `and` operator. Whenever a path results in an empty node set the
|
# `&&` / `and` operator. Whenever a path results in an empty node set the
|
||||||
# evaluation is aborted immediately.
|
# evaluation is aborted immediately.
|
||||||
#
|
#
|
||||||
# @param [Oga::XPath::Node] node
|
# @param [Oga::XPath::Node] ast_node
|
||||||
# @param [Oga::XML::NodeSet] context
|
# @param [Oga::XML::NodeSet] context
|
||||||
# @return [Oga::XML::NodeSet]
|
# @return [Oga::XML::NodeSet]
|
||||||
#
|
#
|
||||||
def on_path(node, context)
|
def on_path(ast_node, context)
|
||||||
nodes = XML::NodeSet.new
|
nodes = XML::NodeSet.new
|
||||||
|
|
||||||
node.children.each do |test|
|
ast_node.children.each do |test|
|
||||||
nodes = process(test, context)
|
nodes = process(test, context)
|
||||||
|
|
||||||
if nodes.empty?
|
if nodes.empty?
|
||||||
|
@ -93,15 +93,15 @@ module Oga
|
||||||
# Processes a node test. Nodes are compared using {#node_matches?} so see
|
# Processes a node test. Nodes are compared using {#node_matches?} so see
|
||||||
# that method for more information on that matching logic.
|
# that method for more information on that matching logic.
|
||||||
#
|
#
|
||||||
# @param [Oga::XPath::Node] node
|
# @param [Oga::XPath::Node] ast_node
|
||||||
# @param [Oga::XML::NodeSet] context
|
# @param [Oga::XML::NodeSet] context
|
||||||
# @return [Oga::XML::NodeSet]
|
# @return [Oga::XML::NodeSet]
|
||||||
#
|
#
|
||||||
def on_test(node, context)
|
def on_test(ast_node, context)
|
||||||
nodes = XML::NodeSet.new
|
nodes = XML::NodeSet.new
|
||||||
|
|
||||||
context.each do |xml_node|
|
context.each do |xml_node|
|
||||||
if can_match_node?(xml_node) and node_matches?(xml_node, node)
|
if can_match_node?(xml_node) and node_matches?(xml_node, ast_node)
|
||||||
nodes << xml_node
|
nodes << xml_node
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -114,12 +114,12 @@ module Oga
|
||||||
# similar to {#process} except the handler names are "on_axis_X" with "X"
|
# similar to {#process} except the handler names are "on_axis_X" with "X"
|
||||||
# being the axis name.
|
# being the axis name.
|
||||||
#
|
#
|
||||||
# @param [Oga::XPath::Node] node
|
# @param [Oga::XPath::Node] ast_node
|
||||||
# @param [Oga::XML::NodeSet] context
|
# @param [Oga::XML::NodeSet] context
|
||||||
# @return [Oga::XML::NodeSet]
|
# @return [Oga::XML::NodeSet]
|
||||||
#
|
#
|
||||||
def on_axis(node, context)
|
def on_axis(ast_node, context)
|
||||||
name, test = *node
|
name, test = *ast_node
|
||||||
|
|
||||||
handler = name.gsub('-', '_')
|
handler = name.gsub('-', '_')
|
||||||
|
|
||||||
|
@ -133,18 +133,18 @@ module Oga
|
||||||
# Evaluation happens using a "short-circuit" mechanism. The moment a
|
# Evaluation happens using a "short-circuit" mechanism. The moment a
|
||||||
# matching node is found it is returned immediately.
|
# matching node is found it is returned immediately.
|
||||||
#
|
#
|
||||||
# @param [Oga::XPath::Node] node
|
# @param [Oga::XPath::Node] ast_node
|
||||||
# @param [Oga::XML::NodeSet] context
|
# @param [Oga::XML::NodeSet] context
|
||||||
# @return [Oga::XML::NodeSet]
|
# @return [Oga::XML::NodeSet]
|
||||||
#
|
#
|
||||||
def on_axis_ancestor(node, context)
|
def on_axis_ancestor(ast_node, context)
|
||||||
nodes = XML::NodeSet.new
|
nodes = XML::NodeSet.new
|
||||||
|
|
||||||
context.each do |xml_node|
|
context.each do |xml_node|
|
||||||
while has_parent?(xml_node)
|
while has_parent?(xml_node)
|
||||||
xml_node = xml_node.parent
|
xml_node = xml_node.parent
|
||||||
|
|
||||||
if can_match_node?(xml_node) and node_matches?(xml_node, node)
|
if can_match_node?(xml_node) and node_matches?(xml_node, ast_node)
|
||||||
nodes << xml_node
|
nodes << xml_node
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
@ -159,12 +159,12 @@ module Oga
|
||||||
#
|
#
|
||||||
# @see [#on_axis_ancestor]
|
# @see [#on_axis_ancestor]
|
||||||
#
|
#
|
||||||
def on_axis_ancestor_or_self(node, context)
|
def on_axis_ancestor_or_self(ast_node, context)
|
||||||
nodes = XML::NodeSet.new
|
nodes = XML::NodeSet.new
|
||||||
|
|
||||||
context.each do |xml_node|
|
context.each do |xml_node|
|
||||||
while has_parent?(xml_node)
|
while has_parent?(xml_node)
|
||||||
if node_matches?(xml_node, node)
|
if node_matches?(xml_node, ast_node)
|
||||||
nodes << xml_node
|
nodes << xml_node
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
@ -184,17 +184,17 @@ module Oga
|
||||||
# (unlike some other methods which return the moment they find a matching
|
# (unlike some other methods which return the moment they find a matching
|
||||||
# node).
|
# node).
|
||||||
#
|
#
|
||||||
# @param [Oga::XPath::Node] node
|
# @param [Oga::XPath::Node] ast_node
|
||||||
# @param [Oga::XML::NodeSet] context
|
# @param [Oga::XML::NodeSet] context
|
||||||
# @return [Oga::XML::NodeSet]
|
# @return [Oga::XML::NodeSet]
|
||||||
#
|
#
|
||||||
def on_axis_attribute(node, context)
|
def on_axis_attribute(ast_node, context)
|
||||||
nodes = XML::NodeSet.new
|
nodes = XML::NodeSet.new
|
||||||
|
|
||||||
context.each do |xml_node|
|
context.each do |xml_node|
|
||||||
next unless xml_node.is_a?(XML::Element)
|
next unless xml_node.is_a?(XML::Element)
|
||||||
|
|
||||||
nodes += on_test(node, xml_node.attributes)
|
nodes += on_test(ast_node, xml_node.attributes)
|
||||||
end
|
end
|
||||||
|
|
||||||
return nodes
|
return nodes
|
||||||
|
@ -203,28 +203,28 @@ module Oga
|
||||||
##
|
##
|
||||||
# Evaluates the `child` axis. This simply delegates work to {#on_test}.
|
# Evaluates the `child` axis. This simply delegates work to {#on_test}.
|
||||||
#
|
#
|
||||||
# @param [Oga::XPath::Node] node
|
# @param [Oga::XPath::Node] ast_node
|
||||||
# @param [Oga::XML::NodeSet] context
|
# @param [Oga::XML::NodeSet] context
|
||||||
# @return [Oga::XML::NodeSet]
|
# @return [Oga::XML::NodeSet]
|
||||||
#
|
#
|
||||||
def on_axis_child(node, context)
|
def on_axis_child(ast_node, context)
|
||||||
return on_test(node, child_nodes(context))
|
return on_test(ast_node, child_nodes(context))
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Evaluates the `descendant` axis. This method processes child nodes until
|
# Evaluates the `descendant` axis. This method processes child nodes until
|
||||||
# the very end of the tree, no "short-circuiting" mechanism is used.
|
# the very end of the tree, no "short-circuiting" mechanism is used.
|
||||||
#
|
#
|
||||||
# @param [Oga::XPath::Node] node
|
# @param [Oga::XPath::Node] ast_node
|
||||||
# @param [Oga::XML::NodeSet] context
|
# @param [Oga::XML::NodeSet] context
|
||||||
# @return [Oga::XML::NodeSet]
|
# @return [Oga::XML::NodeSet]
|
||||||
#
|
#
|
||||||
def on_axis_descendant(node, context)
|
def on_axis_descendant(ast_node, context)
|
||||||
nodes = XML::NodeSet.new
|
nodes = XML::NodeSet.new
|
||||||
|
|
||||||
context.each do |context_node|
|
context.each do |context_node|
|
||||||
nodes += on_test(node, context_node.children)
|
nodes += on_test(ast_node, context_node.children)
|
||||||
nodes += on_axis_descendant(node, context_node.children)
|
nodes += on_axis_descendant(ast_node, context_node.children)
|
||||||
end
|
end
|
||||||
|
|
||||||
return nodes
|
return nodes
|
||||||
|
@ -233,22 +233,23 @@ module Oga
|
||||||
##
|
##
|
||||||
# Evaluates the `descendant-or-self` axis.
|
# Evaluates the `descendant-or-self` axis.
|
||||||
#
|
#
|
||||||
# @param [Oga::XPath::Node] node
|
# @param [Oga::XPath::Node] ast_node
|
||||||
# @param [Oga::XML::NodeSet] context
|
# @param [Oga::XML::NodeSet] context
|
||||||
# @return [Oga::XML::NodeSet]
|
# @return [Oga::XML::NodeSet]
|
||||||
#
|
#
|
||||||
def on_axis_descendant_or_self(node, context)
|
def on_axis_descendant_or_self(ast_node, context)
|
||||||
return on_test(node, context) + on_axis_descendant(node, context)
|
return on_test(ast_node, context) +
|
||||||
|
on_axis_descendant(ast_node, context)
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Evaluates the `following` axis.
|
# Evaluates the `following` axis.
|
||||||
#
|
#
|
||||||
# @param [Oga::XPath::Node] node
|
# @param [Oga::XPath::Node] ast_node
|
||||||
# @param [Oga::XML::NodeSet] context
|
# @param [Oga::XML::NodeSet] context
|
||||||
# @return [Oga::XML::NodeSet]
|
# @return [Oga::XML::NodeSet]
|
||||||
#
|
#
|
||||||
def on_axis_following(node, context)
|
def on_axis_following(ast_node, context)
|
||||||
nodes = XML::NodeSet.new
|
nodes = XML::NodeSet.new
|
||||||
|
|
||||||
context.each do |context_node|
|
context.each do |context_node|
|
||||||
|
@ -264,7 +265,7 @@ module Oga
|
||||||
|
|
||||||
next unless check
|
next unless check
|
||||||
|
|
||||||
if can_match_node?(doc_node) and node_matches?(doc_node, node)
|
if can_match_node?(doc_node) and node_matches?(doc_node, ast_node)
|
||||||
nodes << doc_node
|
nodes << doc_node
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -330,18 +331,18 @@ module Oga
|
||||||
# Returns `true` if the given XML node can be compared using
|
# Returns `true` if the given XML node can be compared using
|
||||||
# {#node_matches?}.
|
# {#node_matches?}.
|
||||||
#
|
#
|
||||||
# @param [Oga::XML::Node] node
|
# @param [Oga::XML::Node] ast_node
|
||||||
#
|
#
|
||||||
def can_match_node?(node)
|
def can_match_node?(ast_node)
|
||||||
return node.respond_to?(:name) && node.respond_to?(:namespace)
|
return ast_node.respond_to?(:name) && ast_node.respond_to?(:namespace)
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# @param [Oga::XML::Node] node
|
# @param [Oga::XML::Node] ast_node
|
||||||
# @return [TrueClass|FalseClass]
|
# @return [TrueClass|FalseClass]
|
||||||
#
|
#
|
||||||
def has_parent?(node)
|
def has_parent?(ast_node)
|
||||||
return node.respond_to?(:parent) && !!node.parent
|
return ast_node.respond_to?(:parent) && !!ast_node.parent
|
||||||
end
|
end
|
||||||
end # Evaluator
|
end # Evaluator
|
||||||
end # XPath
|
end # XPath
|
||||||
|
|
Loading…
Reference in New Issue