mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-03 22:12:37 +08:00
Merge pull request #12537 from Alex-Code/StringHelper
StringHelper fixes
This commit is contained in:
@ -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
|
||||
-------------------
|
||||
|
||||
@ -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--;
|
||||
|
||||
@ -107,8 +107,14 @@ class StringHelperTest extends TestCase
|
||||
$this->assertEquals('исполь!!!', StringHelper::truncate('используем восклицательные знаки', 6, '!!!'));
|
||||
|
||||
// With Html
|
||||
$this->assertEquals('<span>This is a test </span>...', StringHelper::truncate('<span>This is a test sentance</span>', 14, '...', null, true));
|
||||
$this->assertEquals('<span><img src="image.png" />This is a test </span>...', StringHelper::truncate('<span><img src="image.png" />This is a test sentance</span>', 14, '...', null, true));
|
||||
$this->assertEquals('<span>This is a test</span>...', StringHelper::truncate('<span>This is a test sentance</span>', 14, '...', null, true));
|
||||
$this->assertEquals('<span>This is a test</span>...', StringHelper::truncate('<span>This is a test </span>sentance', 14, '...', null, true));
|
||||
$this->assertEquals('<span>This is a test </span><strong>for</strong>...', StringHelper::truncate('<span>This is a test </span><strong>for a sentance</strong>', 18, '...', null, true));
|
||||
$this->assertEquals('<span>This is a test</span><strong> for</strong>...', StringHelper::truncate('<span>This is a test</span><strong> for a sentance</strong>', 18, '...', null, true));
|
||||
|
||||
$this->assertEquals('<span><img src="image.png" />This is a test</span>...', StringHelper::truncate('<span><img src="image.png" />This is a test sentance</span>', 14, '...', null, true));
|
||||
$this->assertEquals('<span><img src="image.png" />This is a test</span>...', StringHelper::truncate('<span><img src="image.png" />This is a test </span>sentance', 14, '...', null, true));
|
||||
$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));
|
||||
}
|
||||
|
||||
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('<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));
|
||||
$this->assertEquals('<span>This is a test </span><strong>for</strong>...', StringHelper::truncateWords('<span>This is a test </span><strong>for a sentance</strong>', 5, '...', true));
|
||||
$this->assertEquals('<span>This is a test</span><strong> for</strong>...', StringHelper::truncateWords('<span>This is a test</span><strong> for a sentance</strong>', 5, '...', true));
|
||||
$this->assertEquals('<p> раз два три четыре пять </p> <p> шесть</p>...', StringHelper::truncateWords('<p> раз два три четыре пять </p> <p> шесть семь восемь девять десять</p>', 6, '...', 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));
|
||||
$this->assertEquals('<span><img src="image.png" />This is a test </span><strong>for</strong>...', StringHelper::truncateWords('<span><img src="image.png" />This is a test </span><strong>for a sentance</strong>', 5, '...', true));
|
||||
$this->assertEquals('<span><img src="image.png" />This is a test</span><strong> for</strong>...', StringHelper::truncateWords('<span><img src="image.png" />This is a test</span><strong> for a sentance</strong>', 5, '...', true));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user