lib/hamlit/compiler/script_compiler.rb in hamlit-2.1.2 vs lib/hamlit/compiler/script_compiler.rb in hamlit-2.2.0

- old
+ new

@@ -1,51 +1,56 @@ require 'hamlit/ruby_expression' require 'hamlit/static_analyzer' -require 'hamlit/string_interpolation' +require 'hamlit/string_splitter' module Hamlit class Compiler class ScriptCompiler def initialize(identity) @identity = identity end def compile(node, &block) + no_children = node.children.empty? case - when node.children.empty? && RubyExpression.string_literal?(node.value[:text]) - string_compile(node) - when node.children.empty? && StaticAnalyzer.static?(node.value[:text]) + when no_children && node.value[:escape_interpolation] + compile_interpolated_plain(node) + when no_children && RubyExpression.string_literal?(node.value[:text]) + delegate_optimization(node) + when no_children && StaticAnalyzer.static?(node.value[:text]) static_compile(node) else dynamic_compile(node, &block) end end private # String-interpolated plain text must be compiled with this method - def string_compile(node) + # because we have to escape only interpolated values. + def compile_interpolated_plain(node) temple = [:multi] - StringInterpolation.compile(node.value[:text]).each do |type, value| + StringSplitter.compile(node.value[:text]).each do |type, value| case type when :static - value = Hamlit::Utils.escape_html(value) if node.value[:escape_html] temple << [:static, value] when :dynamic - if Hamlit::StaticAnalyzer.static?(value) - value = eval(value).to_s - value = Hamlit::Utils.escape_html(value) if node.value[:escape_html] || node.value[:escape_interpolation] - temple << [:static, value] - else - temple << [:escape, node.value[:escape_html] || node.value[:escape_interpolation], [:dynamic, value]] - end + temple << [:escape, node.value[:escape_interpolation], [:dynamic, value]] end end temple << [:newline] end + # :dynamic is optimized in other filter: StringSplitter + def delegate_optimization(node) + [:multi, + [:escape, node.value[:escape_html], [:dynamic, node.value[:text]]], + [:newline], + ] + end + def static_compile(node) - str = eval("(#{node.value[:text]}).to_s") + str = eval(node.value[:text]).to_s if node.value[:escape_html] str = Hamlit::Utils.escape_html(str) elsif node.value[:preserve] str = ::Hamlit::HamlHelpers.find_and_preserve(str, %w(textarea pre code)) end