XPath compiler support for the "attribute" axis

This commit is contained in:
Yorick Peterse 2015-07-19 00:11:58 +02:00
parent 4f03bf19c1
commit 616fd42600
1 changed files with 35 additions and 15 deletions

View File

@ -166,11 +166,20 @@ module Oga
# @return [Oga::Ruby::Node] # @return [Oga::Ruby::Node]
# #
def on_axis_attribute(ast, input) def on_axis_attribute(ast, input)
ns, name = *ast input.is_a?(XML::Element).if_true do
attribute = literal('attribute')
query = ns ? "#{ns}:#{name}" : name input.attributes.each.add_block(attribute) do
name_match = match_name_and_namespace(ast, attribute)
input.get(string(query)) if name_match
name_match.if_true { yield attribute }
else
yield attribute
end
end
end
#input.attribute(string(query))
end end
## ##
@ -294,19 +303,10 @@ module Oga
# @return [Oga::Ruby::Node] # @return [Oga::Ruby::Node]
# #
def on_test(ast, input) def on_test(ast, input)
ns, name = *ast condition = element_or_attribute(input)
name_match = match_name_and_namespace(ast, input)
condition = element_or_attribute(input) name_match ? condition.and(name_match) : condition
if name != STAR
condition = condition.and(input.name.eq(string(name)))
end
if ns and ns != STAR
condition = condition.and(input.namespace_name.eq(string(ns)))
end
condition
end end
## ##
@ -403,6 +403,26 @@ module Oga
node.is_a?(XML::Attribute).or(node.is_a?(XML::Element)) node.is_a?(XML::Attribute).or(node.is_a?(XML::Element))
end end
# @param [AST::Node] ast
# @param [Oga::Ruby::Node] input
# @return [Oga::Ruby::Node]
def match_name_and_namespace(ast, input)
ns, name = *ast
condition = nil
if name != STAR
condition = input.name.eq(string(name))
end
if ns and ns != STAR
ns_match = input.namespace_name.eq(string(ns))
condition = condition ? condition.and(ns_match) : ns_match
end
condition
end
# @return [Oga::Ruby::Node] # @return [Oga::Ruby::Node]
def matched_literal def matched_literal
literal('matched') literal('matched')