From e0b33b008d85f6d57da830c734e249cbd912a647 Mon Sep 17 00:00:00 2001 From: Steve St. Martin Date: Sat, 8 Aug 2009 14:34:05 -0400 Subject: [PATCH] truncate should not use omission in length calculation --- actionpack/lib/action_view/helpers/text_helper.rb | 17 ++++++++--------- actionpack/test/template/text_helper_test.rb | 21 ++++++++++----------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index c3ce4c6..96eb230 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -33,34 +33,34 @@ 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 "..."). # Pass a :separator to truncate +text+ at a natural break. # # ==== 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 fa... # # truncate("Once upon a time in a world far far away", :separator => ' ') # # => Once upon a time in a world... # # 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? @@ -73,9 +73,8 @@ module ActionView options.reverse_merge!(:length => 30, :omission => "...") if text - l = options[:length] - options[:omission].mb_chars.length chars = text.mb_chars - stop = options[:separator] ? (chars.rindex(options[:separator].mb_chars, l) || l) : l + stop = options[:separator] ? (chars.rindex(options[:separator].mb_chars, options[:length]) || options[:length]) : options[:length] (chars.length > options[:length] ? chars[0...stop] + options[:omission] : text).to_s end end @@ -581,4 +580,4 @@ module ActionView end end end -end +end \ No newline at end of file diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index 706b508..2def440 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -37,19 +37,18 @@ 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 "Hello[...]", truncate("Hello Big World!", :omission => "[...]", :length => 13, :separator => ' ') + 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) assert_equal "Hello Big[...]", truncate("Hello Big World!", :omission => "[...]", :length => 14, :separator => ' ') assert_equal "Hello Big[...]", truncate("Hello Big World!", :omission => "[...]", :length => 15, :separator => ' ') end @@ -57,19 +56,19 @@ class TextHelperTest < ActionView::TestCase 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 @@ -396,7 +395,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