Skip to content

Commit

Permalink
Don't truncate HTML if content length is less than summary size (#1125)…
Browse files Browse the repository at this point in the history
…, fixes #1114
  • Loading branch information
xadammr authored and flaviocopes committed Oct 25, 2016
1 parent afc1823 commit cd15b91
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion system/src/Grav/Common/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ public function summary($size = null)
}

$summary = Utils::truncateHTML($content, $size);

return html_entity_decode($summary);
}

Expand Down
6 changes: 5 additions & 1 deletion system/src/Grav/Common/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,11 @@ public static function safeTruncate($string, $limit = 150)
*/
public static function truncateHtml($text, $length = 100, $ellipsis = '...')
{
return Truncator::truncateLetters($text, $length, $ellipsis);
if (mb_strlen($text) <= $length) {
return $text;
} else {
return Truncator::truncateLetters($text, $length, $ellipsis);
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Grav/Common/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public function testTruncateHtml()
$this->assertEquals('<p>This is a string to truncate</p>', Utils::truncateHtml('<p>This is a string to truncate</p>', 100));
$this->assertEquals('<input type="file" id="file" multiple>', Utils::truncateHtml('<input type="file" id="file" multiple />', 6));
$this->assertEquals('<ol><li>item 1 <i>so...</i></li></ol>', Utils::truncateHtml('<ol><li>item 1 <i>something</i></li><li>item 2 <strong>bold</strong></li></ol>', 10));
$this->assertEquals("<p>This is a string.</p>\n<p>It splits two lines.</p>", Utils::truncateHtml("<p>This is a string.</p>\n<p>It splits two lines.</p>", 100));
}

public function testSafeTruncateHtml()
Expand Down

0 comments on commit cd15b91

Please sign in to comment.