Browse Source

Merge branch 'ahmedkamals-hotfix' into master

Refs #6259
Mark Story 11 years ago
parent
commit
f45d1edd40
2 changed files with 10 additions and 2 deletions
  1. 6 0
      src/Utility/Text.php
  2. 4 2
      tests/TestCase/Utility/TextTest.php

+ 6 - 0
src/Utility/Text.php

@@ -592,7 +592,13 @@ class Text
                 }
             }
             $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) {

+ 4 - 2
tests/TestCase/Utility/TextTest.php

@@ -468,18 +468,20 @@ TEXT;
         $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>");