Skip to content

Commit

Permalink
10629 - BaseStringHelper::truncateHtml fix - should not add suffix if…
Browse files Browse the repository at this point in the history
… words count less than needed yiisoft#10629
  • Loading branch information
andrewnester committed Jan 21, 2016
1 parent 50deb07 commit 6acab77
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 1 deletion.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Yii Framework 2 Change Log
- Bug #10541: Fixed division by zero issue in `Console` helper progress bar (youmad)
- Bug #10580: Fixed `yii\grid\GridView::guessColumns()` to work with numeric column names (silverfire)
- Bug #10625: Fixed `activeForm.js` - when submit doesn't reload page, submit button value simulation with hidden input did not work (andrewnester)
- Bug #10629: Fixed `yii\helpers\BaseStringHelper` - BaseStringHelper::truncateHtml adds suffix regardless of the string length (andrewnester)
- Bug: Fixed generation of canonical URLs for `ViewAction` pages (samdark)
- Bug: Fixed `mb_*` functions calls to use `UTF-8` or `Yii::$app->charset` (silverfire)
- Enh #3506: Added `yii\validators\IpValidator` to perform validation of IP addresses and subnets (SilverFire, samdark)
Expand Down
2 changes: 1 addition & 1 deletion framework/helpers/BaseStringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ protected static function truncateHtml($string, $count, $suffix, $encoding = fal
}
$context = new \HTMLPurifier_Context();
$generator = new \HTMLPurifier_Generator($config, $context);
return $generator->generateFromTokens($truncated) . $suffix;
return $generator->generateFromTokens($truncated) . ($totalCount >= $count ? $suffix : '');
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/framework/helpers/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public function testTruncateWords()
$this->assertEquals('это тестовая multibyte!!!', StringHelper::truncateWords('это тестовая multibyte строка', 3, '!!!'));
$this->assertEquals('это строка с неожиданными...', StringHelper::truncateWords('это строка с неожиданными пробелами', 4));

$this->assertEquals('lorem ipsum', StringHelper::truncateWords('lorem ipsum', 3, '...', true));
// With Html
$this->assertEquals('<span>This is a test</span>...', StringHelper::truncateWords('<span>This is a test sentance</span>', 4, '...', true));
$this->assertEquals('<span><img src="image.png" />This is a test</span>...', StringHelper::truncateWords('<span><img src="image.png" />This is a test sentance</span>', 4, '...', true));
Expand Down

0 comments on commit 6acab77

Please sign in to comment.