diff --git a/system/src/Grav/Common/Helpers/Truncator.php b/system/src/Grav/Common/Helpers/Truncator.php index c2e7349893..9cf27e1f6d 100644 --- a/system/src/Grav/Common/Helpers/Truncator.php +++ b/system/src/Grav/Common/Helpers/Truncator.php @@ -98,6 +98,7 @@ public static function truncateLetters($html, $limit = 0, $ellipsis = "") // Iterate over letters. $letters = new DOMLettersIterator($body); + $truncated = false; foreach ($letters as $letter) { // If we have exceeded the limit, we want to delete the remainder of this document. @@ -111,11 +112,18 @@ public static function truncateLetters($html, $limit = 0, $ellipsis = "") self::insertEllipsis($currentText[0], $ellipsis); } + $truncated = true; + break; } } - return self::innerHTML($body); + // Return original HTML if not truncated. + if ($truncated) { + return self::innerHTML($body); + } else { + return $html; + } } /** diff --git a/system/src/Grav/Common/Utils.php b/system/src/Grav/Common/Utils.php index 2736fd9047..02d5e158a3 100644 --- a/system/src/Grav/Common/Utils.php +++ b/system/src/Grav/Common/Utils.php @@ -296,10 +296,6 @@ public static function safeTruncate($string, $limit = 150) */ public static function truncateHtml($text, $length = 100, $ellipsis = '...') { - if (mb_strlen($text) <= $length) { - return $text; - } - return Truncator::truncateLetters($text, $length, $ellipsis); } diff --git a/tests/unit/Grav/Common/UtilsTest.php b/tests/unit/Grav/Common/UtilsTest.php index c4abc32ac8..bbda3be905 100644 --- a/tests/unit/Grav/Common/UtilsTest.php +++ b/tests/unit/Grav/Common/UtilsTest.php @@ -127,7 +127,7 @@ public function testTruncateHtml() $this->assertEquals('

This...

', Utils::truncateHtml('

This is a string to truncate

', 4)); $this->assertEquals('

This is a...

', Utils::truncateHtml('

This is a string to truncate

', 10)); $this->assertEquals('

This is a string to truncate

', Utils::truncateHtml('

This is a string to truncate

', 100)); - $this->assertEquals('', Utils::truncateHtml('', 6)); + $this->assertEquals('', Utils::truncateHtml('', 6)); $this->assertEquals('
  1. item 1 so...
', Utils::truncateHtml('
  1. item 1 something
  2. item 2 bold
', 10)); $this->assertEquals("

This is a string.

\n

It splits two lines.

", Utils::truncateHtml("

This is a string.

\n

It splits two lines.

", 100)); }