Browse Source

Fixes #13657: Fixed `yii\helpers\StringHelper::truncateHtml()` skip extra tags at the end

tags/2.0.12
sam002 8 years ago committed by Alexander Makarov
parent
commit
c78b9e470d
  1. 1
      framework/CHANGELOG.md
  2. 19
      framework/helpers/BaseStringHelper.php
  3. 2
      tests/framework/helpers/StringHelperTest.php

1
framework/CHANGELOG.md

@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.12 under development
--------------------------
- Bug #13657: Fixed `yii\helpers\StringHelper::truncateHtml()` skip extra tags at the end (sam002)
- Bug #7946 Fixed a bug when the `form` attribute was not propagated to the hidden input of the checkbox (Kolyunya)
- Bug #13087: Fixed getting active validators for safe attribute (developeruz)
- Bug #13571: Fix `yii\db\mssql\QueryBuilder::checkIntegrity` for all tables (boboldehampsink)

19
framework/helpers/BaseStringHelper.php

@ -160,10 +160,8 @@ class BaseStringHelper
$truncated = [];
foreach ($tokens as $token) {
if ($token instanceof \HTMLPurifier_Token_Start) { //Tag begins
if ($totalCount < $count) {
$openTokens[$token->name] = isset($openTokens[$token->name]) ? $openTokens[$token->name] + 1 : 1;
$truncated[] = $token;
}
$openTokens[$token->name] = isset($openTokens[$token->name]) ? $openTokens[$token->name] + 1 : 1;
$truncated[] = $token;
} elseif ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) { //Text
if (false === $encoding) {
preg_match('/^(\s*)/um', $token->data, $prefixSpace) ?: $prefixSpace = ['',''];
@ -178,12 +176,23 @@ class BaseStringHelper
} elseif ($token instanceof \HTMLPurifier_Token_End) { //Tag ends
if (!empty($openTokens[$token->name])) {
$openTokens[$token->name]--;
if ($openTokens[$token->name] <= 0) {
unset($openTokens[$token->name]);
}
$truncated[] = $token;
}
} elseif ($token instanceof \HTMLPurifier_Token_Empty) { //Self contained tags, i.e. <img/> etc.
$truncated[] = $token;
}
if (0 === $openTokens && $totalCount >= $count) {
if ($totalCount >= $count) {
if (0 < count($openTokens)) {
foreach (array_reverse($openTokens) as $name => $countTag) {
while ($countTag > 0) {
$truncated[] = new \HTMLPurifier_Token_End($name);
$countTag--;
}
}
}
break;
}
}

2
tests/framework/helpers/StringHelperTest.php

@ -117,6 +117,8 @@ class StringHelperTest extends TestCase
$this->assertEquals('<span><img src="image.png" />This is a test </span><strong>for</strong>...', StringHelper::truncate('<span><img src="image.png" />This is a test </span><strong>for a sentance</strong>', 18, '...', null, true));
$this->assertEquals('<p>This is a test</p><ul><li>bullet1</li><li>b</li></ul>...', StringHelper::truncate('<p>This is a test</p><ul><li>bullet1</li><li>bullet2</li><li>bullet3</li><li>bullet4</li></ul>', 22, '...', null, true));
$this->assertEquals('<div><ul><li>bullet1</li><li>b</li></ul></div>...', StringHelper::truncate('<div><ul><li>bullet1</li><li>bullet2</li><li>bullet3</li></ul><br></div>', 8, '...', null, true));
}
public function testTruncateWords()

Loading…
Cancel
Save