From 1df04b526b5d2b311d92bb0bb876559f3866ef1f Mon Sep 17 00:00:00 2001 From: Alex-Code Date: Wed, 14 Sep 2016 12:38:31 +0100 Subject: [PATCH 1/4] Issue with spaces --- framework/helpers/BaseStringHelper.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) 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--; From 37caadfab805bac6e1e73895e4ad4cebf12f02e2 Mon Sep 17 00:00:00 2001 From: Alex-Code Date: Wed, 14 Sep 2016 12:40:24 +0100 Subject: [PATCH 2/4] Extra truncate HTML tests --- tests/framework/helpers/StringHelperTest.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) 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)); } /** From 84d015eb0213df2fb53afc493c42f4ba7b8a3246 Mon Sep 17 00:00:00 2001 From: Alex-Code Date: Sun, 18 Sep 2016 23:26:35 +0100 Subject: [PATCH 3/4] Update CHANGELOG.md --- framework/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 8cdff3cc34..a56de02d8a 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -65,6 +65,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:truncatHtml` (Alex-Code) 2.0.9 July 11, 2016 ------------------- From cb7ebd9cfb8e5940254ba061ec3671f49146b552 Mon Sep 17 00:00:00 2001 From: Alex-Code Date: Sun, 18 Sep 2016 23:29:02 +0100 Subject: [PATCH 4/4] Update CHANGELOG.md --- framework/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index a56de02d8a..802c7d24f1 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -65,7 +65,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:truncatHtml` (Alex-Code) +- Bug #12537: Fixes issues with spaces in `StringHelper:truncateHtml` (Alex-Code) 2.0.9 July 11, 2016 -------------------