From fca9b55b6c6dadb00c134c079d7d24ed9991024d Mon Sep 17 00:00:00 2001 From: Steve St. Martin Date: Sat, 8 Aug 2009 14:42:14 -0400 Subject: [PATCH] truncate should not use omission in length calculation --- actionpack/lib/action_view/helpers/text_helper.rb | 14 +++++++------- actionpack/test/template/text_helper_test.rb | 20 ++++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 8463af9..9024652 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -33,30 +33,30 @@ module ActionView end # Truncates a given +text+ after a given :length if +text+ is longer than :length - # (defaults to 30). The last characters will be replaced with the :omission (defaults to "..."). + # (defaults to 30). The last characters will be appended with the :omission (defaults to "..."). # # ==== Examples # # truncate("Once upon a time in a world far far away") - # # => Once upon a time in a world f... + # # => Once upon a time in a world far... # # truncate("Once upon a time in a world far far away", :length => 14) - # # => Once upon a... + # # => Once upon a ti... # # truncate("And they found that many people were sleeping better.", :length => 25, "(clipped)") # # => And they found that many (clipped) # # truncate("And they found that many people were sleeping better.", :omission => "... (continued)", :length => 15) - # # => And they found... (continued) + # # => And they found ... (continued) # # You can still use truncate with the old API that accepts the # +length+ as its optional second and the +ellipsis+ as its # optional third parameter: # truncate("Once upon a time in a world far far away", 14) - # # => Once upon a time in a world f... + # # => Once upon a ti... # # truncate("And they found that many people were sleeping better.", 15, "... (continued)") - # # => And they found... (continued) + # # => And they found ... (continued) def truncate(text, *args) options = args.extract_options! unless args.empty? @@ -69,7 +69,7 @@ module ActionView options.reverse_merge!(:length => 30, :omission => "...") if text - l = options[:length] - options[:omission].mb_chars.length + l = options[:length] chars = text.mb_chars (chars.length > options[:length] ? chars[0...l] + options[:omission] : text).to_s end diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index a370f14..6b2e64e 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -36,36 +36,36 @@ class TextHelperTest < ActionView::TestCase def test_truncate assert_equal "Hello World!", truncate("Hello World!", :length => 12) - assert_equal "Hello Wor...", truncate("Hello World!!", :length => 12) + assert_equal "Hello World!...", truncate("Hello World!!", :length => 12) end def test_truncate_should_use_default_length_of_30 str = "This is a string that will go longer then the default truncate length of 30" - assert_equal str[0...27] + "...", truncate(str) + assert_equal str[0...30] + "...", truncate(str) end def test_truncate_with_options_hash - assert_equal "This is a string that wil[...]", truncate("This is a string that will go longer then the default truncate length of 30", :omission => "[...]") - assert_equal "Hello W...", truncate("Hello World!", :length => 10) - assert_equal "Hello[...]", truncate("Hello World!", :omission => "[...]", :length => 10) + assert_equal "This is a string that will go [...]", truncate("This is a string that will go longer then the default truncate length of 30", :omission => "[...]") + assert_equal "Hello Worl...", truncate("Hello World!", :length => 10) + assert_equal "Hello Worl[...]", truncate("Hello World!", :omission => "[...]", :length => 10) end if RUBY_VERSION < '1.9.0' def test_truncate_multibyte with_kcode 'none' do - assert_equal "\354\225\210\353\205\225\355...", truncate("\354\225\210\353\205\225\355\225\230\354\204\270\354\232\224", :length => 10) + assert_equal "\354\225\210\353\205\225\355\225\230\354...", truncate("\354\225\210\353\205\225\355\225\230\354\204\270\354\232\224", :length => 10) end with_kcode 'u' do - assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...", + assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254...", truncate("\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244", :length => 10) end end else def test_truncate_multibyte - assert_equal "\354\225\210\353\205\225\355...", + assert_equal "\354\225\210\353\205\225\355\225\230\354...", truncate("\354\225\210\353\205\225\355\225\230\354\204\270\354\232\224", :length => 10) - assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...".force_encoding('UTF-8'), + assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254...".force_encoding('UTF-8'), truncate("\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244".force_encoding('UTF-8'), :length => 10) end end @@ -392,7 +392,7 @@ class TextHelperTest < ActionView::TestCase url = "http://api.rubyonrails.com/Foo.html" email = "fantabulous@shiznadel.ic" - assert_equal %(

#{url[0...7]}...
#{email[0...7]}...

), auto_link("

#{url}
#{email}

") { |url| truncate(url, :length => 10) } + assert_equal %(

#{url[0...10]}...
#{email[0...10]}...

), auto_link("

#{url}
#{email}

") { |url| truncate(url, :length => 10) } end def test_auto_link_with_options_hash -- 1.6.4