diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-04-05 13:56:29 -0700 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-04-05 13:56:29 -0700 |
commit | 08926bd06c35ed92b4e49f54ca53ac6a8fbf6b53 (patch) | |
tree | 556eac58f2f1095bd4fb751448b3969e89775cae /actionpack | |
parent | 096ee1594d329f53ef47d0aff9bb6eef25a18b96 (diff) | |
parent | bd308a0d02c4a6459c05770a564405986a663f47 (diff) | |
download | rails-08926bd06c35ed92b4e49f54ca53ac6a8fbf6b53.tar.gz rails-08926bd06c35ed92b4e49f54ca53ac6a8fbf6b53.tar.bz2 rails-08926bd06c35ed92b4e49f54ca53ac6a8fbf6b53.zip |
Merge pull request #9555 from tmm1/optimize-erb
Optimize generated ERB to reduce method calls
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_view/buffers.rb | 6 | ||||
-rw-r--r-- | actionpack/lib/action_view/template/handlers/erb.rb | 32 |
2 files changed, 35 insertions, 3 deletions
diff --git a/actionpack/lib/action_view/buffers.rb b/actionpack/lib/action_view/buffers.rb index 2372d3c433..361a0dccbe 100644 --- a/actionpack/lib/action_view/buffers.rb +++ b/actionpack/lib/action_view/buffers.rb @@ -8,9 +8,15 @@ module ActionView end def <<(value) + return self if value.nil? super(value.to_s) end alias :append= :<< + + def safe_concat(value) + return self if value.nil? + super(value.to_s) + end alias :safe_append= :safe_concat end diff --git a/actionpack/lib/action_view/template/handlers/erb.rb b/actionpack/lib/action_view/template/handlers/erb.rb index 5aaafc15c1..7d7a7af51d 100644 --- a/actionpack/lib/action_view/template/handlers/erb.rb +++ b/actionpack/lib/action_view/template/handlers/erb.rb @@ -6,12 +6,23 @@ module ActionView module Handlers class Erubis < ::Erubis::Eruby def add_preamble(src) + @newline_pending = 0 src << "@output_buffer = output_buffer || ActionView::OutputBuffer.new;" end def add_text(src, text) return if text.empty? - src << "@output_buffer.safe_concat('" << escape_text(text) << "');" + + if text == "\n" + @newline_pending += 1 + else + src << "@output_buffer.safe_append='" + src << "\n" * @newline_pending if @newline_pending > 0 + src << escape_text(text) + src << "';" + + @newline_pending = 0 + end end # Erubis toggles <%= and <%== behavior when escaping is enabled. @@ -28,24 +39,39 @@ module ActionView BLOCK_EXPR = /\s+(do|\{)(\s*\|[^|]*\|)?\s*\Z/ def add_expr_literal(src, code) + flush_newline_if_pending(src) if code =~ BLOCK_EXPR src << '@output_buffer.append= ' << code else - src << '@output_buffer.append= (' << code << ');' + src << '@output_buffer.append=(' << code << ');' end end def add_expr_escaped(src, code) + flush_newline_if_pending(src) if code =~ BLOCK_EXPR src << "@output_buffer.safe_append= " << code else - src << "@output_buffer.safe_concat((" << code << ").to_s);" + src << "@output_buffer.safe_append=(" << code << ");" end end + def add_stmt(src, code) + flush_newline_if_pending(src) + super + end + def add_postamble(src) + flush_newline_if_pending(src) src << '@output_buffer.to_s' end + + def flush_newline_if_pending(src) + if @newline_pending > 0 + src << "@output_buffer.safe_append='#{"\n" * @newline_pending}';" + @newline_pending = 0 + end + end end class ERB |