# frozen_string_literal: truerequire'temple/static_analyzer'require'haml/ruby_expression'require'haml/string_splitter'moduleHamlclassCompilerclassScriptCompilerdefself.find_and_preserve(input,tags)tags=tags.map{|tag|Regexp.escape(tag)}.join('|')re=/<(#{tags})([^>]*)>(.*?)(<\/\1>)/iminput.to_s.gsub(re)do|s|s=~re# Can't rely on $1, etc. existing since Rails' SafeBuffer#gsub is incompatible"<#{$1}#{$2}>#{Haml::Helpers.preserve($3)}</#{$1}>"endenddefinitialize(identity,options)@identity=identity@disable_capture=options[:disable_capture]enddefcompile(node,&block)unlessRipper.respond_to?(:lex)# No Ripper.lex in trufflerubyreturndynamic_compile(node,&block)endno_children=node.children.empty?casewhenno_children&&node.value[:escape_interpolation]compile_interpolated_plain(node)whenno_children&&RubyExpression.string_literal?(node.value[:text])delegate_optimization(node)whenno_children&&Temple::StaticAnalyzer.static?(node.value[:text])static_compile(node)elsedynamic_compile(node,&block)endendprivate# String-interpolated plain text must be compiled with this method# because we have to escape only interpolated values.defcompile_interpolated_plain(node)temple=[:multi]StringSplitter.compile(node.value[:text]).eachdo|type,value|casetypewhen:statictemple<<[:static,value]when:dynamictemple<<[:escape,node.value[:escape_interpolation],[:dynamic,value]]endendtemple<<[:newline]end# :dynamic is optimized in other filter: StringSplitterdefdelegate_optimization(node)[:multi,[:escape,node.value[:escape_html],[:dynamic,node.value[:text]]],[:newline],]enddefstatic_compile(node)str=eval(node.value[:text]).to_sifnode.value[:escape_html]str=Haml::Util.escape_html(str)elsifnode.value[:preserve]str=ScriptCompiler.find_and_preserve(str,%w(textarea pre code))end[:multi,[:static,str],[:newline]]enddefdynamic_compile(node,&block)var=@identity.generatetemple=compile_script_assign(var,node,&block)temple<<compile_script_result(var,node)enddefcompile_script_assign(var,node,&block)ifnode.children.empty?[:multi,[:code,"#{var} = (#{node.value[:text]}"],[:newline],[:code,')'],]else[:multi,[:block,"#{var} = #{node.value[:text]}",[:multi,[:newline],@disable_capture?yield(node):[:capture,Temple::Utils.unique_name,yield(node)]]],]endenddefcompile_script_result(result,node)if!node.value[:escape_html]&&node.value[:preserve]result=find_and_preserve(result)end[:escapeany,node.value[:escape_html],[:dynamic,result]]enddeffind_and_preserve(code)%Q[::Haml::Compiler::ScriptCompiler.find_and_preserve(#{code}, %w(textarea pre code))]enddefescape_html(temple)[:escape,true,temple]endendendend