diff options
author | Jamis Buck <jamis@37signals.com> | 2005-09-19 21:36:36 +0000 |
---|---|---|
committer | Jamis Buck <jamis@37signals.com> | 2005-09-19 21:36:36 +0000 |
commit | 390280b842605d78ec1ab968605e5968677c9400 (patch) | |
tree | eb9c062d3043c2a4e510a2e7afd89d89f1d45a52 /actionpack/lib | |
parent | 5da4c397826b26368efdd0eefc433f990bc90345 (diff) | |
download | rails-390280b842605d78ec1ab968605e5968677c9400.tar.gz rails-390280b842605d78ec1ab968605e5968677c9400.tar.bz2 rails-390280b842605d78ec1ab968605e5968677c9400.zip |
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
Diffstat (limited to 'actionpack/lib')
-rw-r--r-- | actionpack/lib/action_view/helpers/text_helper.rb | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index bc753262f2..8be3d3589c 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -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 |