From fb3fc46cddfe5d1a5f8a4cfb10aac94a11b4c77d Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 18 Nov 2005 01:25:39 +0000 Subject: Correct length for the truncate text helper. Closes #2913. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3080 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_view/helpers/text_helper.rb | 6 +++--- actionpack/test/template/text_helper_test.rb | 8 ++++---- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 6c40c6a522..c1f30b2b74 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Correct length for the truncate text helper. #2913 [Stefan Kaes] + * Update to Prototype 1.4.0_rc3. Closes #1893, #2505, #2550, #2748, #2783. [Sam Stephenson] * Add support for new rjs templates which wrap an update_page block. [Marcel Molina Jr.] diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index b31668203e..2c38ffb345 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -20,12 +20,12 @@ module ActionView # if the +text+ is longer than +length+. def truncate(text, length = 30, truncate_string = "...") if text.nil? then return end - + l = length - truncate_string.length if $KCODE == "NONE" - text.length > length ? text[0..(length - 3)] + truncate_string : text + text.length > length ? text[0...l] + truncate_string : text else chars = text.split(//) - chars.length > length ? chars[0..(length-3)].join + truncate_string : text + chars.length > length ? chars[0...l].join + truncate_string : text end end diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index d43bc43b9c..5bfe0709d9 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -24,7 +24,7 @@ class TextHelperTest < Test::Unit::TestCase def test_truncate assert_equal "Hello World!", truncate("Hello World!", 12) - assert_equal "Hello Worl...", truncate("Hello World!!", 12) + assert_equal "Hello Wor...", truncate("Hello World!!", 12) end def test_truncate_multibyte_without_kcode @@ -34,7 +34,7 @@ class TextHelperTest < Test::Unit::TestCase truncate("\354\225\210\353\205\225\355\225\230\354\204\270\354\232\224", 10) CODE - assert_equal "\354\225\210\353\205\225\355\225...", result + assert_equal "\354\225\210\353\205\225\355...", result end def test_truncate_multibyte_with_kcode @@ -47,7 +47,7 @@ class TextHelperTest < Test::Unit::TestCase truncate("\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254\353\236 \354\225\204\353\235\274\353\246\254\354\230\244", 10) CODE - assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254\353\236 \354\225\204...", result + assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254\353\236 ...", result end def test_strip_links @@ -165,7 +165,7 @@ class TextHelperTest < Test::Unit::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, 10) } + assert_equal %(

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

), auto_link("

#{url}
#{email}

") { |url| truncate(url, 10) } end def test_sanitize_form -- cgit v1.2.3