From eb61f56706298fbd230542beca73ab4a82fb7cca Mon Sep 17 00:00:00 2001 From: Aupajo Date: Sat, 27 Dec 2008 15:06:50 +1300 Subject: [PATCH] truncate uses HTML character entity for ellipsis. --- actionpack/lib/action_view/helpers/text_helper.rb | 16 ++++++++-------- actionpack/test/template/text_helper_test.rb | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 1d9e4fe..c337b50 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -33,15 +33,15 @@ 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 replaced with the :omission, which defaults to "", the HTML character entity for an ellipsis (three dots). # # ==== 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 wo… # - # truncate("Once upon a time in a world far far away", :length => 14) - # # => Once upon a... + # truncate("Once upon a time in a world far far away", :length => 19) + # # => Once upon a… # # truncate("And they found that many people were sleeping better.", :length => 25, "(clipped)") # # => And they found that many (clipped) @@ -53,7 +53,7 @@ module ActionView # +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 time in a wo… # # truncate("And they found that many people were sleeping better.", 15, "... (continued)") # # => And they found... (continued) @@ -64,9 +64,9 @@ module ActionView 'length and omission arguments', caller) options[:length] = args[0] || 30 - options[:omission] = args[1] || "..." + options[:omission] = args[1] || "…" end - options.reverse_merge!(:length => 30, :omission => "...") + options.reverse_merge!(:length => 30, :omission => "…") if text l = options[:length] - options[:omission].mb_chars.length @@ -342,7 +342,7 @@ module ActionView # # post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com." # auto_link(post_body, :href_options => { :target => '_blank' }) do |text| - # truncate(text, 15) + # truncate(text, :omission => '...', :length => 15) # end # # => "Welcome to my new blog at http://www.m.... # Please e-mail me at me@email.com." diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index a6200fb..17b6f9e 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 "Hell…", 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...22] + "…", 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 "He…", truncate("Hello World!", :length => 10) assert_equal "Hello[...]", 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…", truncate("\354\225\210\353\205\225\355\225\230\354\204\270\354\232\224", :length => 14) 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…", 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…", 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 …".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 @@ -363,7 +363,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...7]}...
#{email[0...7]}...

), auto_link("

#{url}
#{email}

") { |url| truncate(url, :length => 10, :omission => '...') } end def test_auto_link_with_options_hash -- 1.5.6.4