Created
May 14, 2011 02:02
-
-
Save AquaGeek/971600 to your computer and use it in GitHub Desktop.
Rails Lighthouse ticket #1641
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 7840c194b32b33f08dbee10b9b78dc0132b92dd4 Mon Sep 17 00:00:00 2001 | |
From: Bernerd Schaefer <bj.schaefer@gmail.com> and Veezus Kreist <mremsik@gmail.com> | |
Date: Tue, 23 Nov 2010 18:07:39 -0600 | |
Subject: [PATCH] truncate uses unicode ellipsis when possible | |
--- | |
actionpack/lib/action_view/helpers/text_helper.rb | 16 +++ | |
actionpack/test/template/text_helper_test.rb | 115 +++++++++++++------- | |
.../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, 99 insertions(+), 41 deletions(-) | |
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb | |
index 3d27600..48b9d12 100644 | |
--- a/actionpack/lib/action_view/helpers/text_helper.rb | |
+++ b/actionpack/lib/action_view/helpers/text_helper.rb | |
@@ -84,6 +84,22 @@ module ActionView | |
# # => "<p>Once upon a time in a wo..." | |
def truncate(text, options = {}) | |
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 9e9ed91..87a657a 100644 | |
--- a/actionpack/test/template/text_helper_test.rb | |
+++ b/actionpack/test/template/text_helper_test.rb | |
@@ -48,51 +48,90 @@ class TextHelperTest < ActionView::TestCase | |
assert_equal "<p><b> test with unsafe string </b><script>code!</script></p>", simple_format("<b> test with unsafe string </b><script>code!</script>", {}, :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 | |
+ if 'string'.respond_to?(:encoding) | |
+ KCODE_TO_ENCODING = Hash.new(Encoding::BINARY). | |
+ update('UTF8' => Encoding::UTF_8, 'SJIS' => Encoding::Shift_JIS) | |
+ def setup | |
+ @default_encoding = Encoding.default_external | |
+ silence_warnings { Encoding.default_external = KCODE_TO_ENCODING[encoding] } | |
+ end | |
+ def teardown | |
+ silence_warnings { Encoding.default_external = @default_encoding } | |
+ end | |
+ else | |
+ def setup | |
+ @default_encoding, $KCODE = $KCODE, encoding | |
+ end | |
+ def teardown | |
+ $KCODE = @default_encoding | |
+ end | |
+ end | |
- def test_truncate | |
- assert_equal "Hello World!", truncate("Hello World!", :length => 12) | |
- assert_equal "Hello Wor...", truncate("Hello World!!", :length => 12) | |
- end | |
+ def encoding | |
+ 'UTF8' | |
+ end | |
- def test_truncate_should_not_escape_input | |
- assert_equal "Hello <sc...", truncate("Hello <script>code!</script>World!!", :length => 12) | |
- 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_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 test_truncate_should_not_be_html_safe | |
+ assert !truncate("Hello World!", :length => 12).html_safe? | |
+ 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 test_truncate | |
+ assert_equal "Hello World!", truncate("Hello World!", :length => 12) | |
+ assert_equal "Hello Wor#{omission_character}", truncate("Hello World!!", :length => 12) | |
+ 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) | |
- end | |
- with_kcode 'u' do | |
- assert_equal "\354\225\204\353\246\254\353\236\221 \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) | |
+ def test_truncate_should_not_escape_input | |
+ assert_equal "Hello <sc#{omission_character}", truncate("Hello <script>code!</script>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 | |
+ | |
+ 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 | |
+ 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 | |
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 | |
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 bb865ca..ef34c0f 100644 | |
--- a/activesupport/test/core_ext/string_ext_test.rb | |
+++ b/activesupport/test/core_ext/string_ext_test.rb | |
@@ -256,6 +256,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 d4159dd..556db0f 100644 | |
--- a/railties/test/application/console_test.rb | |
+++ b/railties/test/application/console_test.rb | |
@@ -69,8 +69,7 @@ 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 | |
def test_active_record_does_not_panic_when_referencing_an_observed_constant | |
-- | |
1.7.3.1 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From b345f4f9ea2be97e5b6efa1e8f79bb342f3cafb1 Mon Sep 17 00:00:00 2001 | |
From: Bernerd Schaefer and Veezus Kreist <dev+bernerdschaefer+veezus@hashrocket.com> | |
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 "<p><b> test with unsafe string </b><script>code!</script></p>", simple_format("<b> test with unsafe string </b><script>code!</script>", {}, :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 <sc...", truncate("Hello <script>code!</script>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 <sc#{omission_character}", truncate("Hello <script>code!</script>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 %(<p><a href="#{url}">#{url[0...7]}...</a><br /><a href="mailto:#{email}">#{email[0...7]}...</a><br /></p>), auto_link("<p>#{url}<br />#{email}<br /></p>") { |url| truncate(url, :length => 10) } | |
+ assert_equal %(<p><a href="#{url}">#{url[0..7]}</a><br /><a href="mailto:#{email}">#{email[0..7]}</a><br /></p>), | |
+ auto_link("<p>#{url}<br />#{email}<br /></p>") { |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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From b5c344d848e68e95cead2490a8a1914cede90cb6 Mon Sep 17 00:00:00 2001 | |
From: Jeremy Olliver <jeremy.olliver@gmail.com> | |
Date: Sun, 8 Mar 2009 20:09:06 +1300 | |
Subject: [PATCH] Updating tests to fail, where behavior is not as expected | |
--- | |
actionpack/test/template/text_helper_test.rb | 10 +++++----- | |
1 files changed, 5 insertions(+), 5 deletions(-) | |
diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb | |
index 4e209f3..0b40cff 100644 | |
--- a/actionpack/test/template/text_helper_test.rb | |
+++ b/actionpack/test/template/text_helper_test.rb | |
@@ -36,27 +36,27 @@ class TextHelperTest < ActionView::TestCase | |
def test_truncate | |
assert_equal "Hello World!", truncate("Hello World!", :length => 12) | |
- assert_equal "Hell…", truncate("Hello World!!", :length => 12) | |
+ assert_equal "Hello Wor…", 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...22] + "…", truncate(str) | |
+ assert_equal str[0...27] + "…", 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 "He…", truncate("Hello World!", :length => 10) | |
+ assert_equal "Hello W…", 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…", truncate("\354\225\210\353\205\225\355\225\230\354\204\270\354\232\224", :length => 14) | |
+ assert_equal "\354\225\210\353\205\225\355\225\230\354\204…", 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…", | |
+ assert_equal "\354\225\204\353\246\254\353…", | |
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 | |
-- | |
1.5.6 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From eb61f56706298fbd230542beca73ab4a82fb7cca Mon Sep 17 00:00:00 2001 | |
From: Aupajo <pete@metanation.com> | |
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 <tt>:length</tt> if +text+ is longer than <tt>:length</tt> | |
- # (defaults to 30). The last characters will be replaced with the <tt>:omission</tt> (defaults to "..."). | |
+ # (defaults to 30). The last characters will be replaced with the <tt>:omission</tt>, which defaults to "<tt>…</tt>", 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 <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.m...</a>. | |
# Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>." | |
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 %(<p><a href="#{url}">#{url[0...7]}...</a><br /><a href="mailto:#{email}">#{email[0...7]}...</a><br /></p>), auto_link("<p>#{url}<br />#{email}<br /></p>") { |url| truncate(url, :length => 10) } | |
+ assert_equal %(<p><a href="#{url}">#{url[0...7]}...</a><br /><a href="mailto:#{email}">#{email[0...7]}...</a><br /></p>), auto_link("<p>#{url}<br />#{email}<br /></p>") { |url| truncate(url, :length => 10, :omission => '...') } | |
end | |
def test_auto_link_with_options_hash | |
-- | |
1.5.6.4 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment