diff options
author | Michael Koziarski <michael@koziarski.com> | 2006-10-12 21:13:05 +0000 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2006-10-12 21:13:05 +0000 |
commit | 41c362352433f265a95c00d8e7b5c1f1299e9011 (patch) | |
tree | ef84b72d406c22c8764d19a5ee7db52481bc18b1 /actionpack | |
parent | 48cef84150d2c5f7be351787d0584626f5b8155a (diff) | |
download | rails-41c362352433f265a95c00d8e7b5c1f1299e9011.tar.gz rails-41c362352433f265a95c00d8e7b5c1f1299e9011.tar.bz2 rails-41c362352433f265a95c00d8e7b5c1f1299e9011.zip |
Use String#chars in TextHelper::excerpt. Closes #6386 [Manfred Stienstra]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5288 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/CHANGELOG | 3 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/text_helper.rb | 8 | ||||
-rw-r--r-- | actionpack/test/template/text_helper_test.rb | 43 | ||||
-rw-r--r-- | actionpack/test/testing_sandbox.rb | 29 |
4 files changed, 31 insertions, 52 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 5f28208836..190a5578c8 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Use String#chars in TextHelper::excerpt. Closes #6386 [Manfred Stienstra] + * Install named routes into ActionView::Base instead of proxying them to the view via helper_method. Closes #5932. [Nicholas Seckar] * Update to latest Prototype and script.aculo.us trunk versions [Thomas Fuchs] @@ -10,7 +12,6 @@ * render_text may optionally append to the response body. render_javascript appends by default. This allows you to chain multiple render :update calls by setting @performed_render = false between them (awaiting a better public API). [Jeremy Kemper] ->>>>>>> .r5282 * Rename test assertion to prevent shadowing. Closes #6306. [psross] * Fixed that NumberHelper#number_to_delimiter should respect precision of higher than two digits #6231 [phallstrom] diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 27e093a1c3..0e142cbaff 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -45,14 +45,14 @@ module ActionView if text.nil? || phrase.nil? then return end phrase = Regexp.escape(phrase) - if found_pos = text =~ /(#{phrase})/i + if found_pos = text.chars =~ /(#{phrase})/i start_pos = [ found_pos - radius, 0 ].max - end_pos = [ found_pos + phrase.length + radius, text.length ].min + end_pos = [ found_pos + phrase.chars.length + radius, text.chars.length ].min prefix = start_pos > 0 ? excerpt_string : "" - postfix = end_pos < text.length ? excerpt_string : "" + postfix = end_pos < text.chars.length ? excerpt_string : "" - prefix + text[start_pos..end_pos].strip + postfix + prefix + text.chars[start_pos..end_pos].strip + postfix else nil end diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index a3962095ae..dd127cde66 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -24,31 +24,16 @@ class TextHelperTest < Test::Unit::TestCase assert_equal "Hello Wor...", truncate("Hello World!!", 12) end - def test_truncate_multibyte_without_kcode - result = execute_in_sandbox(<<-'CODE') - require File.dirname(__FILE__) + '/../../activesupport/lib/active_support/core_ext/kernel' - require "#{File.dirname(__FILE__)}/../lib/action_view/helpers/text_helper" - include ActionView::Helpers::TextHelper - 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...", result - end - - def test_truncate_multibyte_with_kcode - result = execute_in_sandbox(<<-'CODE') - $KCODE = "u" - require 'jcode' - - require File.dirname(__FILE__) + '/../../activesupport/lib/active_support/core_ext/kernel' - require "#{File.dirname(__FILE__)}/../lib/action_view/helpers/text_helper" - include ActionView::Helpers::TextHelper - 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 ...", result + 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", 10) + end + with_kcode 'u' do + assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254\353\236 ...", + 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) + end end - + def test_strip_links assert_equal "on my mind", strip_links("<a href='almost'>on my mind</a>") assert_equal "on my mind", strip_links("<A href='almost'>on my mind</A>") @@ -105,7 +90,15 @@ class TextHelperTest < Test::Unit::TestCase assert_equal('...is a beautiful! morn...', excerpt('This is a beautiful! morning', 'beautiful', 5)) assert_equal('...is a beautiful? morn...', excerpt('This is a beautiful? morning', 'beautiful', 5)) end - + + def test_excerpt_with_utf8 + with_kcode('u') do + assert_equal("...fficiency could not be h...", excerpt("That's why efficiency could not be helped", 'could', 8)) + end + with_kcode('none') do + assert_equal("...\203ciency could not be h...", excerpt("That's why efficiency could not be helped", 'could', 8)) + end + end def test_word_wrap assert_equal("my very very\nvery long\nstring", word_wrap("my very very very long string", 15)) diff --git a/actionpack/test/testing_sandbox.rb b/actionpack/test/testing_sandbox.rb index b21f411730..b3b8b0f4d4 100644 --- a/actionpack/test/testing_sandbox.rb +++ b/actionpack/test/testing_sandbox.rb @@ -1,26 +1,11 @@ module TestingSandbox - - # This whole thing *could* be much simpler, but I don't think Tempfile, - # popen and others exist on all platforms (like Windows). - def execute_in_sandbox(code) - test_name = "#{File.dirname(__FILE__)}/test.#{$$}.rb" - res_name = "#{File.dirname(__FILE__)}/test.#{$$}.out" - - File.open(test_name, "w+") do |file| - file.write(<<-CODE) - $:.unshift "../lib" - block = Proc.new do - #{code} - end - print block.call - CODE + # Temporarily replaces KCODE for the block + def with_kcode(kcode) + old_kcode, $KCODE = $KCODE, kcode + begin + yield + ensure + $KCODE = old_kcode end - - system("ruby #{test_name} > #{res_name}") or raise "could not run test in sandbox" - File.read(res_name) - ensure - File.delete(test_name) rescue nil - File.delete(res_name) rescue nil end - end |