1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Remove use of Haml::Util::def_static_method

Any performance benefit is outweighed by the current complexity
and unreadability of the code. Also, if statements are fast.
This commit is contained in:
Tee Parham 2014-01-14 22:47:25 -07:00 committed by Norman Clarke
parent d519f1b828
commit b73f65c84e
2 changed files with 71 additions and 66 deletions

View file

@ -130,75 +130,80 @@ module Haml
def adjust_tabs(tab_change) def adjust_tabs(tab_change)
@real_tabs += tab_change @real_tabs += tab_change
end end
Haml::Util.def_static_method(self, :format_script, [:result], def format_script(result, preserve_script, in_tag, preserve_tag, escape_html, nuke_inner_whitespace, interpolated, ugly)
:preserve_script, :in_tag, :preserve_tag, :escape_html, result_name = escape_html ? html_escape(result.to_s) : result.to_s
:nuke_inner_whitespace, :interpolated, :ugly, <<RUBY) unless ugly
<% # Escape HTML here so that the safety of the string is preserved in Rails
result_name = escape_html ? "html_escape(result.to_s)" : "result.to_s" %>
<% unless ugly %>
# If we're interpolated, # If we're interpolated,
# then the custom tabulation is handled in #push_text. # then the custom tabulation is handled in #push_text.
# The easiest way to avoid it here is to reset @tabulation. # The easiest way to avoid it here is to reset @tabulation.
<% if interpolated %> if interpolated
old_tabulation = @tabulation old_tabulation = @tabulation
@tabulation = 0 @tabulation = 0
<% end %>
<% if !(in_tag && preserve_tag && !nuke_inner_whitespace) %>
tabulation = @real_tabs
<% end %>
result = <%= result_name %>.<% if nuke_inner_whitespace %>strip<% else %>rstrip<% end %>
<% else %>
result = <%= result_name %><% if nuke_inner_whitespace %>.strip<% end %>
<% end %>
<% if preserve_tag %>
result = Haml::Helpers.preserve(result)
<% elsif preserve_script %>
result = Haml::Helpers.find_and_preserve(result, options[:preserve])
<% end %>
<% if ugly %>
fix_textareas!(result) if toplevel? && result.include?('<textarea')
return result
<% else %>
<% if !(in_tag && preserve_tag && !nuke_inner_whitespace) %>
has_newline = result.include?("\\n")
<% end %>
<% if in_tag && !nuke_inner_whitespace %>
<% unless preserve_tag %> if !has_newline <% end %>
@real_tabs -= 1
<% if interpolated %> @tabulation = old_tabulation <% end %>
return result
<% unless preserve_tag %> end <% end %>
<% end %>
<% if !(in_tag && preserve_tag && !nuke_inner_whitespace) %>
# Precompiled tabulation may be wrong
<% if !interpolated && !in_tag %>
result = tabs + result if @tabulation > 0
<% end %>
if has_newline
result.gsub! "\\n", "\\n" + tabs(tabulation)
# Add tabulation if it wasn't precompiled
<% if in_tag && !nuke_inner_whitespace %> result = tabs(tabulation) + result <% end %>
end end
fix_textareas!(result) if toplevel? && result.include?('<textarea') if !(in_tag && preserve_tag && !nuke_inner_whitespace)
tabulation = @real_tabs
end
if nuke_inner_whitespace
result = result_name.strip
else
result = result_name.rstrip
end
else
if nuke_inner_whitespace
result = result_name.strip
else
result = result_name
end
end
<% if in_tag && !nuke_inner_whitespace %> if preserve_tag
result = "\\n\#{result}\\n\#{tabs(tabulation-1)}" result = Haml::Helpers.preserve(result)
@real_tabs -= 1 elsif preserve_script
<% end %> result = Haml::Helpers.find_and_preserve(result, options[:preserve])
<% if interpolated %> @tabulation = old_tabulation <% end %> end
result
<% end %> if ugly
<% end %> fix_textareas!(result) if toplevel? && result.include?('<textarea')
RUBY return result
else
if !(in_tag && preserve_tag && !nuke_inner_whitespace)
has_newline = result.include?("\n")
end
if in_tag && !nuke_inner_whitespace
if preserve_tag || !has_newline
@real_tabs -= 1
@tabulation = old_tabulation if interpolated
return result
end
end
if !(in_tag && preserve_tag && !nuke_inner_whitespace)
# Precompiled tabulation may be wrong
if !interpolated && !in_tag
result = tabs + result if @tabulation > 0
end
if has_newline
result.gsub! "\n", "\n" + tabs(tabulation)
# Add tabulation if it wasn't precompiled
result = tabs(tabulation) + result if in_tag && !nuke_inner_whitespace
end
fix_textareas!(result) if toplevel? && result.include?('<textarea')
if in_tag && !nuke_inner_whitespace
result = "\n#{result}\n#{tabs(tabulation-1)}"
@real_tabs -= 1
end
@tabulation = old_tabulation if interpolated
result
end
end
end
def attributes(class_id, obj_ref, *attributes_hashes) def attributes(class_id, obj_ref, *attributes_hashes)
attributes = class_id attributes = class_id

View file

@ -371,19 +371,18 @@ END
return if @options.suppress_eval? return if @options.suppress_eval?
args = [:preserve_script, :in_tag, :preserve_tag, :escape_html, :nuke_inner_whitespace] args = [:preserve_script, :in_tag, :preserve_tag, :escape_html, :nuke_inner_whitespace]
args.map! {|name| opts[name]} args.map! {|name| !!opts[name]}
args << !block_given? << @options.ugly args << !block_given? << @options.ugly
no_format = @options.ugly && no_format = @options.ugly &&
!(opts[:preserve_script] || opts[:preserve_tag] || opts[:escape_html]) !(opts[:preserve_script] || opts[:preserve_tag] || opts[:escape_html])
output_expr = "(#{text}\n)"
static_method = "_hamlout.#{static_method_name(:format_script, *args)}"
# Prerender tabulation unless we're in a tag # Prerender tabulation unless we're in a tag
push_merged_text '' unless opts[:in_tag] push_merged_text '' unless opts[:in_tag]
unless block_given? unless block_given?
push_generated_script(no_format ? "#{text}\n" : "#{static_method}(#{output_expr});") format_script_method = "_hamlout.format_script((#{text}\n),#{args.join(',')});"
push_generated_script(no_format ? "#{text}\n" : format_script_method)
concat_merged_text("\n") unless opts[:in_tag] || opts[:nuke_inner_whitespace] concat_merged_text("\n") unless opts[:in_tag] || opts[:nuke_inner_whitespace]
return return
end end
@ -392,7 +391,8 @@ END
push_silent "haml_temp = #{text}" push_silent "haml_temp = #{text}"
yield yield
push_silent('end', :can_suppress) unless @node.value[:dont_push_end] push_silent('end', :can_suppress) unless @node.value[:dont_push_end]
@precompiled << "_hamlout.buffer << #{no_format ? "haml_temp.to_s;" : "#{static_method}(haml_temp);"}" format_script_method = "_hamlout.format_script(haml_temp,#{args.join(',')});"
@precompiled << "_hamlout.buffer << #{no_format ? "haml_temp.to_s;" : format_script_method}"
concat_merged_text("\n") unless opts[:in_tag] || opts[:nuke_inner_whitespace] || @options.ugly concat_merged_text("\n") unless opts[:in_tag] || opts[:nuke_inner_whitespace] || @options.ugly
end end