aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2012-05-26 13:44:30 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-05-26 13:57:15 -0300
commit4ce3b5d6fe6451a7e6951f366d3e3f9324f75fdb (patch)
tree1e563d2b04346e0894b3c1043e95bfb98373b4ef /actionpack/lib/action_view/helpers
parent9c8132c7bb3224dd357df7246ac65b647a59806f (diff)
downloadrails-4ce3b5d6fe6451a7e6951f366d3e3f9324f75fdb.tar.gz
rails-4ce3b5d6fe6451a7e6951f366d3e3f9324f75fdb.tar.bz2
rails-4ce3b5d6fe6451a7e6951f366d3e3f9324f75fdb.zip
Test that the block used in truncate is escaped if it is not HTML safe
Refactoring the truncate method to not do a sort-circuit return
Diffstat (limited to 'actionpack/lib/action_view/helpers')
-rw-r--r--actionpack/lib/action_view/helpers/text_helper.rb25
1 files changed, 12 insertions, 13 deletions
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb
index 515bd78101..73c3c61a41 100644
--- a/actionpack/lib/action_view/helpers/text_helper.rb
+++ b/actionpack/lib/action_view/helpers/text_helper.rb
@@ -84,14 +84,13 @@ module ActionView
# truncate("Once upon a time in a world far far away") { link_to "Continue", "#" }
# # => "Once upon a time in a wo...<a href="#">Continue</a>"
def truncate(text, options = {}, &block)
- return unless text
+ if text
+ length = options.fetch(:length, 30)
- options = { :length => 30 }.merge!(options)
- length = options.delete(:length)
-
- content = ERB::Util.html_escape(text.truncate(length, options))
- content << capture(&block) if block_given? && text.length > length
- content
+ content = ERB::Util.html_escape(text.truncate(length, options))
+ content << capture(&block) if block_given? && text.length > length
+ content
+ end
end
# Highlights one or more +phrases+ everywhere in +text+ by inserting it into
@@ -112,7 +111,7 @@ module ActionView
# # => You searched for: <a href="search?q=rails">rails</a>
def highlight(text, phrases, options = {})
highlighter = options.fetch(:highlighter, '<mark>\1</mark>')
-
+
text = sanitize(text) if options.fetch(:sanitize, true)
if text.blank? || phrases.blank?
text
@@ -175,12 +174,12 @@ module ActionView
# pluralize(0, 'person')
# # => 0 people
def pluralize(count, singular, plural = nil)
- word = if (count == 1 || count =~ /^1(\.0+)?$/)
- singular
+ word = if (count == 1 || count =~ /^1(\.0+)?$/)
+ singular
else
plural || singular.pluralize
end
-
+
"#{count || 0} #{word}"
end
@@ -225,7 +224,7 @@ module ActionView
#
# simple_format(my_text)
# # => "<p>Here is some basic text...\n<br />...with a line break.</p>"
- #
+ #
# simple_format(my_text, {}, :wrapper_tag => "div")
# # => "<div>Here is some basic text...\n<br />...with a line break.</div>"
#
@@ -241,7 +240,7 @@ module ActionView
# # => "<p><span>I'm allowed!</span> It's true.</p>"
def simple_format(text, html_options = {}, options = {})
wrapper_tag = options.fetch(:wrapper_tag, :p)
-
+
text = sanitize(text) if options.fetch(:sanitize, true)
paragraphs = split_paragraphs(text)