From a256f5e5dbd25a0ea6e5cb13d7608e588642b996 Mon Sep 17 00:00:00 2001 From: Davide Liessi Date: Wed, 2 May 2018 11:31:43 +0200 Subject: [PATCH] Move fix for #1114 to Truncator::truncateLetters The original fix provided by #1125 in Utils::truncateHtml compared the summary size with the full HTML string length. Thus, the string was still being truncated (and the HTML rewritten) even when only the HTML string length, and not the text length, exceeded the summary size. --- system/src/Grav/Common/Helpers/Truncator.php | 10 +++++++++- system/src/Grav/Common/Utils.php | 4 ---- tests/unit/Grav/Common/UtilsTest.php | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) 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)); }