diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-05-26 13:34:31 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-05-26 13:34:31 -0300 |
commit | 9c8132c7bb3224dd357df7246ac65b647a59806f (patch) | |
tree | 1d49f5dc7820e01a31da763fdcdbd6905de778e9 | |
parent | 2114e2187dd8cbdcc0ab304ec0adf1a1bf5cc09a (diff) | |
parent | 38813da6dc6611d6b68dd4d584012c19e86a218a (diff) | |
download | rails-9c8132c7bb3224dd357df7246ac65b647a59806f.tar.gz rails-9c8132c7bb3224dd357df7246ac65b647a59806f.tar.bz2 rails-9c8132c7bb3224dd357df7246ac65b647a59806f.zip |
Merge pull request #6031 from lellisga/truncate_readme
Truncate now has the ability to receive a html option that allows it to call rails helpers.
-rw-r--r-- | actionpack/lib/action_view/helpers/text_helper.rb | 14 | ||||
-rw-r--r-- | actionpack/test/template/text_helper_test.rb | 37 | ||||
-rw-r--r-- | activesupport/test/core_ext/string_ext_test.rb | 4 |
3 files changed, 45 insertions, 10 deletions
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 8cd7cf0052..515bd78101 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -80,8 +80,18 @@ module ActionView # # truncate("<p>Once upon a time in a world far far away</p>") # # => "<p>Once upon a time in a wo..." - def truncate(text, options = {}) - text.truncate(options.fetch(:length, 30), options) if text + # + # truncate("Once upon a time in a world far far away") { link_to "Continue", "#" } + # # => "Once upon a time in a wo...<a href="#">Continue</a>" + def truncate(text, options = {}, &block) + return unless text + + options = { :length => 30 }.merge!(options) + length = options.delete(:length) + + content = ERB::Util.html_escape(text.truncate(length, options)) + content << capture(&block) if block_given? && text.length > length + content end # Highlights one or more +phrases+ everywhere in +text+ by inserting it into diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index f58e474759..a7333a3af9 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -75,19 +75,11 @@ class TextHelperTest < ActionView::TestCase assert_equal options, passed_options 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...", truncate("Hello World!!", :length => 12) end - def test_truncate_should_not_escape_input - assert_equal "Hello <sc...", 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] + "...", truncate(str) @@ -114,6 +106,35 @@ class TextHelperTest < ActionView::TestCase assert_equal options, passed_options end + def test_truncate_with_link_options + assert_equal "Here's a long test and I...<a href=\"#\">Continue</a>", + truncate("Here's a long test and I need a continue to read link", :length => 27) { link_to 'Continue', '#' } + end + + def test_truncate_should_not_mutate_the_options_hash + options = { :length => 27 } + truncate("Here's a long test and I need a continue to read link", options) { link_to 'Continue', '#' } + assert_equal({ :length => 27 }, options) + end + + def test_truncate_should_be_html_safe + assert truncate("Hello World!", :length => 12).html_safe? + end + + def test_truncate_should_escape_the_input + assert_equal "Hello <sc...", truncate("Hello <script>code!</script>World!!", :length => 12) + end + + def test_truncate_with_block_should_be_html_safe + truncated = truncate("Here's a long test and I need a continue to read link", :length => 27) { link_to 'Continue', '#' } + assert truncated.html_safe? + end + + def test_truncate_with_block_should_escape_the_input + assert_equal "<script>code!</script>He...<a href=\"#\">Continue</a>", + truncate("<script>code!</script>Here's a long test and I need a continue to read link", :length => 27) { link_to 'Continue', '#' } + end + def test_highlight_should_be_html_safe assert highlight("This is a beautiful morning", "beautiful").html_safe? end diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index 8437ef1347..e5b774425e 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -290,6 +290,10 @@ class StringInflectionsTest < ActiveSupport::TestCase "\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').truncate(10) end + def test_truncate_should_not_be_html_safe + assert !"Hello World!".truncate(12).html_safe? + end + def test_constantize run_constantize_tests_on do |string| string.constantize |