Use each_ancestor for ancestor-or-self in XPath

The XPath compiler now uses XML::Node#each_ancestor for compiling the
"ancestor-or-self" axis.
This commit is contained in:
Yorick Peterse 2015-07-18 01:05:32 +02:00
parent 52741a3b78
commit f7484f1c8d
1 changed files with 5 additions and 22 deletions

View File

@ -179,20 +179,15 @@ module Oga
# @return [Oga::Ruby::Node] # @return [Oga::Ruby::Node]
# #
def on_axis_ancestor_or_self(ast, input, &block) def on_axis_ancestor_or_self(ast, input, &block)
parent_var = literal('parent') parent = literal('parent')
assign = parent_var.assign(input)
has_parent = parent_var.respond_to?(symbol(:parent))
.and(parent_var.parent)
body = has_parent.while_true do self_test = process(ast, input, &block).if_true { yield input }
if_statement = process(ast, parent_var, &block).if_true do
yield parent_var ancestors_test = input.each_ancestor.add_block(parent) do
process(ast, parent, &block).if_true { yield parent }
end end
if_statement.followed_by(parent_var.assign(parent_var.parent)) self_test.followed_by(ancestors_test)
end
assign.followed_by(body)
end end
## ##
@ -360,43 +355,33 @@ module Oga
private private
##
# @param [#to_s] value # @param [#to_s] value
# @return [Oga::Ruby::Node] # @return [Oga::Ruby::Node]
#
def literal(value) def literal(value)
Ruby::Node.new(:lit, [value.to_s]) Ruby::Node.new(:lit, [value.to_s])
end end
##
# @param [#to_s] value # @param [#to_s] value
# @return [Oga::Ruby::Node] # @return [Oga::Ruby::Node]
#
def string(value) def string(value)
Ruby::Node.new(:string, [value.to_s]) Ruby::Node.new(:string, [value.to_s])
end end
##
# @param [String] value # @param [String] value
# @return [Oga::Ruby::Node] # @return [Oga::Ruby::Node]
#
def symbol(value) def symbol(value)
Ruby::Node.new(:symbol, [value.to_sym]) Ruby::Node.new(:symbol, [value.to_sym])
end end
##
# @param [String] name # @param [String] name
# @param [Array] args # @param [Array] args
# @return [Oga::Ruby::Node] # @return [Oga::Ruby::Node]
#
def send_message(name, *args) def send_message(name, *args)
Ruby::Node.new(:send, [nil, name, *args]) Ruby::Node.new(:send, [nil, name, *args])
end end
##
# @param [Oga::Ruby::Node] node # @param [Oga::Ruby::Node] node
# @return [Oga::Ruby::Node] # @return [Oga::Ruby::Node]
#
def element_or_attribute(node) def element_or_attribute(node)
node.is_a?(XML::Attribute).or(node.is_a?(XML::Element)) node.is_a?(XML::Attribute).or(node.is_a?(XML::Element))
end end
@ -416,10 +401,8 @@ module Oga
literal('variables') literal('variables')
end end
##
# @param [AST::Node] ast # @param [AST::Node] ast
# @return [TrueClass|FalseClass] # @return [TrueClass|FalseClass]
#
def xpath_number?(ast) def xpath_number?(ast)
ast.type == :int || ast.type == :float ast.type == :int || ast.type == :float
end end