From 2585fbd0b7f65d19cb8d95213d721d8c340f53ea Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Tue, 18 Aug 2015 14:25:57 +0200 Subject: [PATCH] XPath compiler support for normalize-space() --- lib/oga/xpath/compiler.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/oga/xpath/compiler.rb b/lib/oga/xpath/compiler.rb index bcb316a..20cdcc6 100644 --- a/lib/oga/xpath/compiler.rb +++ b/lib/oga/xpath/compiler.rb @@ -893,6 +893,27 @@ module Oga end end + # @param [Oga::Ruby::Node] input + # @param [AST::Node] arg + # @return [Oga::Ruby::Node] + def on_call_normalize_space(input, arg = nil) + conversion = literal(Conversion) + norm_var = unique_literal(:normalized) + + find = literal('/\s+/') + replace = string(' ') + + first_node_or_argument(input, arg) do |arg_var| + norm_var + .assign(conversion.to_string(arg_var).strip.gsub(find, replace)) + .followed_by do + norm_var.empty? + .if_true { string('') } + .else { block_given? ? yield : norm_var } + end + end + end + ## # Delegates type tests to specific handlers. #