From 390280b842605d78ec1ab968605e5968677c9400 Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Mon, 19 Sep 2005 21:36:36 +0000 Subject: 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 --- actionpack/lib/action_view/helpers/text_helper.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'actionpack/lib/action_view/helpers/text_helper.rb') 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 -- cgit v1.2.3