mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make the truncate() helper multi-byte safe (assuming $KCODE has been set to something other than "NONE") #2103
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2265 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
5da4c39782
commit
390280b842
4 changed files with 60 additions and 1 deletions
|
@ -1,5 +1,7 @@
|
|||
*SVN*
|
||||
|
||||
* Make the truncate() helper multi-byte safe (assuming $KCODE has been set to something other than "NONE") #2103
|
||||
|
||||
* Add routing tests from #1945 [ben@groovie.org]
|
||||
|
||||
* Add a routing test case covering #2101 [Nicholas Seckar]
|
||||
|
|
|
@ -20,7 +20,13 @@ module ActionView
|
|||
# if the +text+ is longer than +length+.
|
||||
def truncate(text, length = 30, truncate_string = "...")
|
||||
if text.nil? then return end
|
||||
if text.length > length then text[0..(length - 3)] + truncate_string else text end
|
||||
|
||||
if $KCODE == "NONE"
|
||||
text.length > length ? text[0..(length - 3)] + truncate_string : text
|
||||
else
|
||||
chars = text.split(//)
|
||||
chars.length > length ? chars[0..(length-3)].join + truncate_string : text
|
||||
end
|
||||
end
|
||||
|
||||
# Highlights the +phrase+ where it is found in the +text+ by surrounding it like
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
require 'test/unit'
|
||||
require "#{File.dirname(__FILE__)}/../testing_sandbox"
|
||||
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/text_helper'
|
||||
require File.dirname(__FILE__) + '/../../../activesupport/lib/active_support/core_ext/numeric' # for human_size
|
||||
require File.dirname(__FILE__) + '/../../../activesupport/lib/active_support/core_ext/hash' # for stringify_keys
|
||||
|
@ -7,6 +8,7 @@ require File.dirname(__FILE__) + '/../../../activesupport/lib/active_support/cor
|
|||
class TextHelperTest < Test::Unit::TestCase
|
||||
include ActionView::Helpers::TextHelper
|
||||
include ActionView::Helpers::TagHelper
|
||||
include TestingSandbox
|
||||
|
||||
def setup
|
||||
# This simulates the fact that instance variables are reset every time
|
||||
|
@ -25,6 +27,29 @@ class TextHelperTest < Test::Unit::TestCase
|
|||
assert_equal "Hello Worl...", truncate("Hello World!!", 12)
|
||||
end
|
||||
|
||||
def test_truncate_multibyte_without_kcode
|
||||
result = execute_in_sandbox(<<-'CODE')
|
||||
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\225...", result
|
||||
end
|
||||
|
||||
def test_truncate_multibyte_with_kcode
|
||||
result = execute_in_sandbox(<<-'CODE')
|
||||
$KCODE = "u"
|
||||
require 'jcode'
|
||||
|
||||
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 \354\225\204...", result
|
||||
end
|
||||
|
||||
def test_strip_links
|
||||
assert_equal "on my mind", strip_links("<a href='almost'>on my mind</a>")
|
||||
end
|
||||
|
|
26
actionpack/test/testing_sandbox.rb
Normal file
26
actionpack/test/testing_sandbox.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
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
|
||||
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
|
Loading…
Reference in a new issue