aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2012-05-26 14:11:28 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-05-26 14:27:07 -0300
commit2c2b0beaf46c997773b9adc8ef9ff57547a770a3 (patch)
tree70ebb473ba6a05b7bbe1fe9e2e193671f443facf /actionpack/lib/action_view/helpers
parenteedc513503785490a7caed23cd0a0aef3536759f (diff)
downloadrails-2c2b0beaf46c997773b9adc8ef9ff57547a770a3.tar.gz
rails-2c2b0beaf46c997773b9adc8ef9ff57547a770a3.tar.bz2
rails-2c2b0beaf46c997773b9adc8ef9ff57547a770a3.zip
Add `:escape` option for `truncate`
This options can be used to not escape the result by default.
Diffstat (limited to 'actionpack/lib/action_view/helpers')
-rw-r--r--actionpack/lib/action_view/helpers/text_helper.rb7
1 files changed, 5 insertions, 2 deletions
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb
index 72f9dd2cef..0cc0d069ea 100644
--- a/actionpack/lib/action_view/helpers/text_helper.rb
+++ b/actionpack/lib/action_view/helpers/text_helper.rb
@@ -64,7 +64,9 @@ module ActionView
#
# Pass a block if you want to show extra content when the text is truncated.
#
- # The result is marked as HTML-safe, but the it is escaped first.
+ # The result is marked as HTML-safe, but it is escaped by default, unless <tt>:escape</tt> is
+ # +false+. Care should be taken if +text+ contains HTML tags or entities, because truncation
+ # may produce invalid HTML (such as unbalanced or incomplete tags).
#
# truncate("Once upon a time in a world far far away")
# # => "Once upon a time in a world..."
@@ -87,7 +89,8 @@ module ActionView
if text
length = options.fetch(:length, 30)
- content = ERB::Util.html_escape(text.truncate(length, options))
+ content = text.truncate(length, options)
+ content = options[:escape] == false ? content.html_safe : ERB::Util.html_escape(content)
content << capture(&block) if block_given? && text.length > length
content
end