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:
parent
58aa885ad8
commit
c14f66f81a
|
@ -119,14 +119,14 @@ module TextHelper
|
|||
def truncate_text(text, options={})
|
||||
max_length = options[:max_length] || 30
|
||||
ellipsis = options[:ellipsis] || "..."
|
||||
words = options[:words] || false
|
||||
ellipsis_length = ellipsis.length
|
||||
content_length = text.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
|
||||
text
|
||||
truncated
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -96,4 +96,16 @@ describe TextHelper do
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue