From b345f4f9ea2be97e5b6efa1e8f79bb342f3cafb1 Mon Sep 17 00:00:00 2001 From: Bernerd Schaefer and Veezus Kreist Date: Mon, 12 Jul 2010 16:36:31 -0500 Subject: [PATCH] =?UTF-8?q?Use=20=E2=80=A6=20as=20default=20omission=20character=20for=20truncate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actionpack/lib/action_view/helpers/text_helper.rb | 17 +++- actionpack/test/template/text_helper_test.rb | 122 ++++++++++++++------ .../lib/active_support/core_ext/string/filters.rb | 2 +- activesupport/test/core_ext/string_ext_test.rb | 4 + railties/test/application/console_test.rb | 3 +- 5 files changed, 108 insertions(+), 40 deletions(-) diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 0be8a2c..188d21b 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -77,11 +77,26 @@ 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) + unless options[:omission] + omission_character = "..." + if 'string'.respond_to?(:encoding) + if Encoding.default_external == Encoding::UTF_8 + omission_character = "\342\200\246".force_encoding('UTF-8') + end + else + if $KCODE == "UTF8" + omission_character = "\342\200\246" + end + end + options[:omission] = omission_character + options[:omission_length] = 3 + end + 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 d22b9fe..db862b3 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -48,51 +48,100 @@ class TextHelperTest < ActionView::TestCase assert_equal "

test with unsafe string

", simple_format(" test with unsafe string ", {}, :sanitize => false) end - def test_truncate_should_not_be_html_safe - assert !truncate("Hello World!", :length => 12).html_safe? - end + class TruncateWithUTF8 < ActionView::TestCase + tests ActionView::Helpers::TextHelper - def test_truncate - assert_equal "Hello World!", truncate("Hello World!", :length => 12) - assert_equal "Hello Wor...", truncate("Hello World!!", :length => 12) - end + if 'string'.respond_to?(:encoding) + KCODE_TO_ENCODING = Hash.new(Encoding::BINARY). + update('UTF8' => Encoding::UTF_8, 'SJIS' => Encoding::Shift_JIS) - def test_truncate_should_not_escape_input - assert_equal "Hello code!World!!", :length => 12) - end + def setup + @default_encoding = Encoding.default_external + silence_warnings { Encoding.default_external = KCODE_TO_ENCODING[encoding] } + 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) - end + def teardown + silence_warnings { Encoding.default_external = @default_encoding } + end + else + def setup + @default_encoding, $KCODE = $KCODE, encoding + 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 "Hello Big[...]", truncate("Hello Big World!", :omission => "[...]", :length => 14, :separator => ' ') - assert_equal "Hello Big[...]", truncate("Hello Big World!", :omission => "[...]", :length => 15, :separator => ' ') - end + def teardown + $KCODE = @default_encoding + end + end + + def encoding + 'UTF8' + end + + def omission_character + omission_character = "\342\200\246" + 'string'.respond_to?(:force_encoding) ? omission_character.force_encoding('UTF-8') : omission_character + end + + def test_truncate_should_not_be_html_safe + assert !truncate("Hello World!", :length => 12).html_safe? + end + + def test_truncate + assert_equal "Hello World!", truncate("Hello World!", :length => 12) + assert_equal "Hello Wor#{omission_character}", truncate("Hello World!!", :length => 12) + end + + def test_truncate_should_not_escape_input + assert_equal "Hello code!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] + "#{omission_character}", truncate(str) + 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) + 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#{omission_character}", 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 "Hello Big[...]", truncate("Hello Big World!", :omission => "[...]", :length => 14, :separator => ' ') + assert_equal "Hello Big[...]", truncate("Hello Big World!", :omission => "[...]", :length => 15, :separator => ' ') + end + + def test_truncate_with_deprecated_arguments + ActiveSupport::Deprecation.silence do + assert_equal "Hello W#{omission_character}", truncate("Hello World!", 10) + assert_equal "Hello W...", truncate("Hello World!", 10, '...') end - with_kcode 'u' do - assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...", + end + + if RUBY_VERSION < '1.9.0' + def test_truncate_multibyte + assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 #{omission_character}", 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 + else + def test_truncate_multibyte + assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ".force_encoding('UTF-8') + omission_character, + 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 - else - def test_truncate_multibyte - # .mb_chars always returns a UTF-8 String. - # 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) + end - 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) + class TruncateWithNonUTF8 < TruncateWithUTF8 + def encoding + 'None' + end + + def omission_character + "..." + end + + if RUBY_VERSION < '1.9.0' + def test_truncate_multibyte + assert_equal "\354\225\210\353\205\225\355#{omission_character}", truncate("\354\225\210\353\205\225\355\225\230\354\204\270\354\232\224", :length => 10) + end end end @@ -491,7 +540,8 @@ 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}

") { |string| string[0..7] } end def test_auto_link_with_block_with_html diff --git a/activesupport/lib/active_support/core_ext/string/filters.rb b/activesupport/lib/active_support/core_ext/string/filters.rb index e15a1df..97efa31 100644 --- a/activesupport/lib/active_support/core_ext/string/filters.rb +++ b/activesupport/lib/active_support/core_ext/string/filters.rb @@ -39,7 +39,7 @@ class String text = self.dup options[:omission] ||= "..." - length_with_room_for_omission = length - options[:omission].mb_chars.length + length_with_room_for_omission = length - (options[:omission_length] || options[:omission].mb_chars.length) chars = text.mb_chars stop = options[:separator] ? (chars.rindex(options[:separator].mb_chars, length_with_room_for_omission) || length_with_room_for_omission) : length_with_room_for_omission diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index affa1b5..3741c46 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -224,6 +224,10 @@ class StringInflectionsTest < Test::Unit::TestCase assert_equal "Hello Wor...", "Hello World!!".truncate(12) end + def test_truncate_with_omission_and_omission_length + assert_equal "Hello Wor…", "Hello World!".truncate(10, :omission => "…", :omission_length => 1) + end + def test_truncate_with_omission_and_seperator assert_equal "Hello[...]", "Hello World!".truncate(10, :omission => "[...]") assert_equal "Hello[...]", "Hello Big World!".truncate(13, :omission => "[...]", :separator => ' ') diff --git a/railties/test/application/console_test.rb b/railties/test/application/console_test.rb index 8ff69f0..a07ab63 100644 --- a/railties/test/application/console_test.rb +++ b/railties/test/application/console_test.rb @@ -72,7 +72,6 @@ class ConsoleTest < Test::Unit::TestCase load_environment assert_not_nil helper assert_instance_of ActionView::Base, helper - assert_equal 'Once upon a time in a world...', - helper.truncate('Once upon a time in a world far far away') + assert_nothing_raised { helper.truncate('Once upon a time in a world far far away') } end end -- 1.6.6