From 157db6a2f7df1924f0c738fa4ac187029e547751 Mon Sep 17 00:00:00 2001 From: Wincent Colaiuta Date: Thu, 17 Jun 2010 08:39:13 +0200 Subject: [PATCH] truncate() should not try to produce HTML-safe output As discussed in Lighthouse ticket #4825 and ticket #4878, the truncate() method cannot guarantee safe output for all possible inputs/offsets, so it is best to leave the output unsafe so that it gets escaped when used in a view. Signed-off-by: Wincent Colaiuta --- actionpack/lib/action_view/helpers/text_helper.rb | 12 ++++-------- actionpack/test/template/text_helper_test.rb | 18 ++++-------------- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 3b37fd6..654f3c8 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -39,7 +39,10 @@ module ActionView # for a total length not exceeding :length. # # Pass a :separator to truncate +text+ at a natural break. - # Pass a :safe value as "true" to not to escape the content. + # + # The result is not marked as HTML-safe, so will be subject to the default escaping when + # used in views, unless wrapped by raw(). Care should be taken if +text+ contains HTML tags + # or entities, because truncation may produce invalid HTML (such as unbalanced or incomplete tags). # # ==== Examples # @@ -56,12 +59,6 @@ module ActionView # # => "And they f... (continued)" # # truncate("

Once upon a time in a world far far away

") - # # => "<p>Once upon a time i..." - # - # truncate("

Once upon a time in a world far far away

", :safe => true) - # # => "

Once upon a time in a wo..." - # - # truncate("

Once upon a time in a world far far away

".html_safe) # # => "

Once upon a time in a wo..." # # You can still use truncate with the old API that accepts the @@ -84,7 +81,6 @@ module ActionView options.reverse_merge!(:length => 30) - text = h(text) unless text.html_safe? || options[:safe] text.truncate(options.delete(:length), options) if text end diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index b0a4c2a..1cd6a59 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -52,8 +52,8 @@ class TextHelperTest < ActionView::TestCase assert_equal "

test with safe string

", simple_format(" test with safe string ".html_safe) end - def test_truncate_should_be_html_safe - assert truncate("Hello World!", :length => 12).html_safe? + def test_truncate_should_not_be_html_safe + assert !truncate("Hello World!", :length => 12).html_safe? end def test_truncate @@ -61,18 +61,8 @@ class TextHelperTest < ActionView::TestCase assert_equal "Hello Wor...", truncate("Hello World!!", :length => 12) end - def test_truncate_should_escape_unsafe_input - assert_equal "Hello <...", truncate("Hello World!!", :length => 12) - end - - def test_truncate_should_not_escape_input_if_safe_option - assert_equal "Hello code!World!", :length => 12, :safe => true) - assert_equal "Hello code!World!!", :length => 12, :safe => true) - end - - def test_truncate_should_not_escape_safe_input - assert_equal "Hello code!World!".html_safe, :length => 12) - assert_equal "Hello code!World!!".html_safe, :length => 12) + def test_truncate_should_not_escape_input + assert_equal "Hello code!World!!", :length => 12) end def test_truncate_should_use_default_length_of_30 -- 1.7.1