diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index f2c2c0324c..f81ca93bad 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -68,6 +68,7 @@ Yii Framework 2 Change Log - Enh #12382: Changed `yii\widgets\MaskedInput` to use `jQuery` instead of `$` to prevent conflicts (samdark) - Bug #12446: Disable slaves when execute migrations to resolve master-slave replication no-sync (lichunqiang) - Bug #12423: Fixed migration tool problem of creating fields with brackets in comment (pana1990) +- Bug #12537: Fixes issues with spaces in `StringHelper:truncateHtml` (Alex-Code) 2.0.9 July 11, 2016 ------------------- diff --git a/framework/helpers/BaseStringHelper.php b/framework/helpers/BaseStringHelper.php index d5aaf37334..77c46b71fb 100644 --- a/framework/helpers/BaseStringHelper.php +++ b/framework/helpers/BaseStringHelper.php @@ -109,7 +109,7 @@ class BaseStringHelper } if (mb_strlen($string, $encoding ?: Yii::$app->charset) > $length) { - return trim(mb_substr($string, 0, $length, $encoding ?: Yii::$app->charset)) . $suffix; + return rtrim(mb_substr($string, 0, $length, $encoding ?: Yii::$app->charset)) . $suffix; } else { return $string; } @@ -164,16 +164,14 @@ class BaseStringHelper $truncated[] = $token; } elseif ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) { //Text if (false === $encoding) { - $token->data = self::truncateWords($token->data, $count - $totalCount, ''); + preg_match('/^(\s*)/um', $token->data, $prefixSpace) ?: $prefixSpace = ['','']; + $token->data = $prefixSpace[1] . self::truncateWords(ltrim($token->data), $count - $totalCount, ''); $currentCount = self::countWords($token->data); } else { - $token->data = self::truncate($token->data, $count - $totalCount, '', $encoding) . ' '; + $token->data = self::truncate($token->data, $count - $totalCount, '', $encoding); $currentCount = mb_strlen($token->data, $encoding); } $totalCount += $currentCount; - if (1 === $currentCount) { - $token->data = ' ' . $token->data; - } $truncated[] = $token; } elseif ($token instanceof \HTMLPurifier_Token_End) { //Tag ends $openTokens--; diff --git a/tests/framework/helpers/StringHelperTest.php b/tests/framework/helpers/StringHelperTest.php index 318e8db7f9..cb210a538d 100644 --- a/tests/framework/helpers/StringHelperTest.php +++ b/tests/framework/helpers/StringHelperTest.php @@ -107,8 +107,14 @@ class StringHelperTest extends TestCase $this->assertEquals('исполь!!!', StringHelper::truncate('используем восклицательные знаки', 6, '!!!')); // With Html - $this->assertEquals('This is a test ...', StringHelper::truncate('This is a test sentance', 14, '...', null, true)); - $this->assertEquals('This is a test ...', StringHelper::truncate('This is a test sentance', 14, '...', null, true)); + $this->assertEquals('This is a test...', StringHelper::truncate('This is a test sentance', 14, '...', null, true)); + $this->assertEquals('This is a test...', StringHelper::truncate('This is a test sentance', 14, '...', null, true)); + $this->assertEquals('This is a test for...', StringHelper::truncate('This is a test for a sentance', 18, '...', null, true)); + $this->assertEquals('This is a test for...', StringHelper::truncate('This is a test for a sentance', 18, '...', null, true)); + + $this->assertEquals('This is a test...', StringHelper::truncate('This is a test sentance', 14, '...', null, true)); + $this->assertEquals('This is a test...', StringHelper::truncate('This is a test sentance', 14, '...', null, true)); + $this->assertEquals('This is a test for...', StringHelper::truncate('This is a test for a sentance', 18, '...', null, true)); } public function testTruncateWords() @@ -119,10 +125,16 @@ class StringHelperTest extends TestCase $this->assertEquals('это строка с неожиданными...', StringHelper::truncateWords('это строка с неожиданными пробелами', 4)); $this->assertEquals('lorem ipsum', StringHelper::truncateWords('lorem ipsum', 3, '...', true)); + $this->assertEquals(' lorem ipsum', StringHelper::truncateWords(' lorem ipsum', 3, '...', true)); // With Html $this->assertEquals('This is a test...', StringHelper::truncateWords('This is a test sentance', 4, '...', true)); - $this->assertEquals('This is a test...', StringHelper::truncateWords('This is a test sentance', 4, '...', true)); + $this->assertEquals('This is a test for...', StringHelper::truncateWords('This is a test for a sentance', 5, '...', true)); + $this->assertEquals('This is a test for...', StringHelper::truncateWords('This is a test for a sentance', 5, '...', true)); $this->assertEquals('

раз два три четыре пять

шесть

...', StringHelper::truncateWords('

раз два три четыре пять

шесть семь восемь девять десять

', 6, '...', true)); + + $this->assertEquals('This is a test...', StringHelper::truncateWords('This is a test sentance', 4, '...', true)); + $this->assertEquals('This is a test for...', StringHelper::truncateWords('This is a test for a sentance', 5, '...', true)); + $this->assertEquals('This is a test for...', StringHelper::truncateWords('This is a test for a sentance', 5, '...', true)); } /**