mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 06:37:55 +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)
 | 
					- 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 #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 #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
 | 
					2.0.9 July 11, 2016
 | 
				
			||||||
-------------------
 | 
					-------------------
 | 
				
			||||||
 | 
				
			|||||||
@ -109,7 +109,7 @@ class BaseStringHelper
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        if (mb_strlen($string, $encoding ?: Yii::$app->charset) > $length) {
 | 
					        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 {
 | 
					        } else {
 | 
				
			||||||
            return $string;
 | 
					            return $string;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@ -164,16 +164,14 @@ class BaseStringHelper
 | 
				
			|||||||
                $truncated[] = $token;
 | 
					                $truncated[] = $token;
 | 
				
			||||||
            } elseif ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) { //Text
 | 
					            } elseif ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) { //Text
 | 
				
			||||||
                if (false === $encoding) {
 | 
					                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);
 | 
					                    $currentCount = self::countWords($token->data);
 | 
				
			||||||
                } else {
 | 
					                } 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);
 | 
					                    $currentCount = mb_strlen($token->data, $encoding);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                $totalCount += $currentCount;
 | 
					                $totalCount += $currentCount;
 | 
				
			||||||
                if (1 === $currentCount) {
 | 
					 | 
				
			||||||
                    $token->data = ' ' . $token->data;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                $truncated[] = $token;
 | 
					                $truncated[] = $token;
 | 
				
			||||||
            } elseif ($token instanceof \HTMLPurifier_Token_End) { //Tag ends
 | 
					            } elseif ($token instanceof \HTMLPurifier_Token_End) { //Tag ends
 | 
				
			||||||
                $openTokens--;
 | 
					                $openTokens--;
 | 
				
			||||||
 | 
				
			|||||||
@ -108,7 +108,13 @@ class StringHelperTest extends TestCase
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        // With Html
 | 
					        // 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>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 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()
 | 
					    public function testTruncateWords()
 | 
				
			||||||
@ -118,11 +124,17 @@ class StringHelperTest extends TestCase
 | 
				
			|||||||
        $this->assertEquals('это тестовая multibyte!!!', StringHelper::truncateWords('это тестовая multibyte строка', 3, '!!!'));
 | 
					        $this->assertEquals('это тестовая multibyte!!!', StringHelper::truncateWords('это тестовая multibyte строка', 3, '!!!'));
 | 
				
			||||||
        $this->assertEquals('это строка с          неожиданными...', StringHelper::truncateWords('это строка с          неожиданными пробелами', 4));
 | 
					        $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));
 | 
					        $this->assertEquals(' lorem ipsum', StringHelper::truncateWords(' lorem ipsum', 3, '...', true));
 | 
				
			||||||
        // With Html
 | 
					        // With Html
 | 
				
			||||||
        $this->assertEquals('<span>This is a test</span>...', StringHelper::truncateWords('<span>This is a test sentance</span>', 4, '...', true));
 | 
					        $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('<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