Skip to content

Commit

Permalink
Make TextHelper truncate more reasonable.
Browse files Browse the repository at this point in the history
When exact => false this method should always return at least some part
of the original text.

Refs #6259
  • Loading branch information
markstory committed Apr 21, 2015
1 parent 709c76a commit d0683c5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Utility/Text.php
Expand Up @@ -589,13 +589,13 @@ public static function truncate($text, $length = 100, array $options = [])
}
}
$truncate = mb_substr($truncate, 0, $spacepos);

// If truncate still empty, then we don't need to count ellipsis in the cut.
if (mb_strlen($truncate) === 0) {
$truncate = mb_substr($text, 0, $length);
}
}

$truncate .= $ellipsis;

if ($html) {
Expand Down
6 changes: 4 additions & 2 deletions tests/TestCase/Utility/TextTest.php
Expand Up @@ -468,18 +468,20 @@ public function testTruncate()
$text9 = 'НОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыь';
$text10 = 'http://example.com/something/foo:bar';

$this->assertSame($this->Text->truncate('Hello', 3), '...');
$this->assertSame($this->Text->truncate('Hello', 3, ['exact' => false]), 'Hel...');
$this->assertSame($this->Text->truncate($text1, 15), 'The quick br...');
$this->assertSame($this->Text->truncate($text1, 15, ['exact' => false]), 'The quick...');
$this->assertSame($this->Text->truncate($text1, 100), 'The quick brown fox jumps over the lazy dog');
$this->assertSame($this->Text->truncate($text2, 10), 'Heiz&ou...');
$this->assertSame($this->Text->truncate($text2, 10, ['exact' => false]), '...');
$this->assertSame($this->Text->truncate($text2, 10, ['exact' => false]), 'Heizö...');
$this->assertSame($this->Text->truncate($text3, 20), '<b>&copy; 2005-20...');
$this->assertSame($this->Text->truncate($text4, 15), '<img src="my...');
$this->assertSame($this->Text->truncate($text5, 6, ['ellipsis' => '']), '0<b>1<');
$this->assertSame($this->Text->truncate($text1, 15, ['html' => true]), "The quick brow\xe2\x80\xa6");
$this->assertSame($this->Text->truncate($text1, 15, ['exact' => false, 'html' => true]), "The quick\xe2\x80\xa6");
$this->assertSame($this->Text->truncate($text2, 10, ['html' => true]), "Heiz&ouml;lr&uuml;c\xe2\x80\xa6");
$this->assertSame($this->Text->truncate($text2, 10, ['exact' => false, 'html' => true]), "\xe2\x80\xa6");
$this->assertSame($this->Text->truncate($text2, 10, ['exact' => false, 'html' => true]), "Heiz&ouml;\xe2\x80\xa6");
$this->assertSame($this->Text->truncate($text3, 20, ['html' => true]), "<b>&copy; 2005-2007, Cake S\xe2\x80\xa6</b>");
$this->assertSame($this->Text->truncate($text4, 15, ['html' => true]), "<img src=\"mypic.jpg\"> This image ta\xe2\x80\xa6");
$this->assertSame($this->Text->truncate($text4, 45, ['html' => true]), "<img src=\"mypic.jpg\"> This image tag is not XHTML conform!<br><hr/><b>But the\xe2\x80\xa6</b>");
Expand Down

0 comments on commit d0683c5

Please sign in to comment.