correctly truncate text that contains MB chars, fixes #4135

TextHelper#truncate_html doesn't appear to have this issue, because it
always splits and truncates on word boundaries (using String#split()).

Change-Id: I8bf301055818d7080d8e12859707458ba41e89f4
Reviewed-on: https://gerrit.instructure.com/2851
Reviewed-by: Zach Wily <zach@instructure.com>
Tested-by: Hudson <hudson@instructure.com>
This commit is contained in:
Brian Palmer 2011-03-29 09:59:10 -06:00
parent 58aa885ad8
commit c14f66f81a
2 changed files with 17 additions and 5 deletions

View File

@ -119,14 +119,14 @@ module TextHelper
def truncate_text(text, options={}) def truncate_text(text, options={})
max_length = options[:max_length] || 30 max_length = options[:max_length] || 30
ellipsis = options[:ellipsis] || "..." ellipsis = options[:ellipsis] || "..."
words = options[:words] || false
ellipsis_length = ellipsis.length ellipsis_length = ellipsis.length
content_length = text.length
actual_length = max_length - ellipsis_length actual_length = max_length - ellipsis_length
if content_length > max_length
truncated = text[0, actual_length] + ellipsis truncated = (text || "")[/.{0,#{actual_length}}/mu]
if truncated.length < text.length
truncated + ellipsis
else else
text truncated
end end
end end

View File

@ -96,4 +96,16 @@ describe TextHelper do
end end
end end
context "truncate_text" do
it "should split on multi-byte character boundaries" do
str = "This\ntext\nhere\n\nis\nutf-8"
th.truncate_text(str, :max_length => 9).should == "This\nt..."
th.truncate_text(str, :max_length => 21).should == "This\ntext\nhere\n\ni..."
th.truncate_text(str, :max_length => 18).should == "This\ntext\nhere\n..."
th.truncate_text(str, :max_length => 19).should == "This\ntext\nhere\n获..."
th.truncate_text(str, :max_length => 20).should == "This\ntext\nhere\n\n..."
th.truncate_text(str, :max_length => 80).should == str
end
end
end end