From 4ce3b5d6fe6451a7e6951f366d3e3f9324f75fdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Sat, 26 May 2012 13:44:30 -0300 Subject: 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 --- actionpack/lib/action_view/helpers/text_helper.rb | 25 +++++++++++------------ 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'actionpack/lib/action_view/helpers') 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...Continue" 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: rails def highlight(text, phrases, options = {}) highlighter = options.fetch(:highlighter, '\1') - + 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) # # => "

Here is some basic text...\n
...with a line break.

" - # + # # simple_format(my_text, {}, :wrapper_tag => "div") # # => "
Here is some basic text...\n
...with a line break.
" # @@ -241,7 +240,7 @@ module ActionView # # => "

I'm allowed! It's true.

" 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) -- cgit v1.2.3