mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 14:46:19 +08:00 
			
		
		
		
	Fixed truncateHtml leaving extra tags (#7727)
This commit is contained in:
		@ -23,6 +23,7 @@ Yii Framework 2 Change Log
 | 
				
			|||||||
- Bug #12939: Hard coded table names for MSSQL in RBAC migration (arogachev)
 | 
					- Bug #12939: Hard coded table names for MSSQL in RBAC migration (arogachev)
 | 
				
			||||||
- Bug #12974: Fixed incorrect order of migrations history in case `yii\console\controllers\MigrateController::$migrationNamespaces` is in use (evgen-d, klimov-paul)
 | 
					- Bug #12974: Fixed incorrect order of migrations history in case `yii\console\controllers\MigrateController::$migrationNamespaces` is in use (evgen-d, klimov-paul)
 | 
				
			||||||
- Bug #13071: Help option for commands was not working in modules (arogachev, haimanman)
 | 
					- Bug #13071: Help option for commands was not working in modules (arogachev, haimanman)
 | 
				
			||||||
 | 
					- Bug #7727: Fixed truncateHtml leaving extra tags (developeruz)
 | 
				
			||||||
- Enh #6809: Added `\yii\caching\Cache::$defaultDuration` property, allowing to set custom default cache duration (sdkiller)
 | 
					- Enh #6809: Added `\yii\caching\Cache::$defaultDuration` property, allowing to set custom default cache duration (sdkiller)
 | 
				
			||||||
- Enh #7333: Improved error message for `yii\di\Instance::ensure()` when a component does not exist (cebe)
 | 
					- Enh #7333: Improved error message for `yii\di\Instance::ensure()` when a component does not exist (cebe)
 | 
				
			||||||
- Enh #7420: Attributes for prompt generated with `renderSelectOptions` of `\yii\helpers\Html` helper (arogachev)
 | 
					- Enh #7420: Attributes for prompt generated with `renderSelectOptions` of `\yii\helpers\Html` helper (arogachev)
 | 
				
			||||||
 | 
				
			|||||||
@ -155,13 +155,15 @@ class BaseStringHelper
 | 
				
			|||||||
        $config->set('Cache.SerializerPath', \Yii::$app->getRuntimePath());
 | 
					        $config->set('Cache.SerializerPath', \Yii::$app->getRuntimePath());
 | 
				
			||||||
        $lexer = \HTMLPurifier_Lexer::create($config);
 | 
					        $lexer = \HTMLPurifier_Lexer::create($config);
 | 
				
			||||||
        $tokens = $lexer->tokenizeHTML($string, $config, null);
 | 
					        $tokens = $lexer->tokenizeHTML($string, $config, null);
 | 
				
			||||||
        $openTokens = 0;
 | 
					        $openTokens = [];
 | 
				
			||||||
        $totalCount = 0;
 | 
					        $totalCount = 0;
 | 
				
			||||||
        $truncated = [];
 | 
					        $truncated = [];
 | 
				
			||||||
        foreach ($tokens as $token) {
 | 
					        foreach ($tokens as $token) {
 | 
				
			||||||
            if ($token instanceof \HTMLPurifier_Token_Start) { //Tag begins
 | 
					            if ($token instanceof \HTMLPurifier_Token_Start) { //Tag begins
 | 
				
			||||||
                $openTokens++;
 | 
					                if ($totalCount < $count) {
 | 
				
			||||||
                $truncated[] = $token;
 | 
					                    $openTokens[$token->name] = isset($openTokens[$token->name]) ? $openTokens[$token->name] + 1 : 1;
 | 
				
			||||||
 | 
					                    $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) {
 | 
				
			||||||
                    preg_match('/^(\s*)/um', $token->data, $prefixSpace) ?: $prefixSpace = ['',''];
 | 
					                    preg_match('/^(\s*)/um', $token->data, $prefixSpace) ?: $prefixSpace = ['',''];
 | 
				
			||||||
@ -174,8 +176,10 @@ class BaseStringHelper
 | 
				
			|||||||
                $totalCount += $currentCount;
 | 
					                $totalCount += $currentCount;
 | 
				
			||||||
                $truncated[] = $token;
 | 
					                $truncated[] = $token;
 | 
				
			||||||
            } elseif ($token instanceof \HTMLPurifier_Token_End) { //Tag ends
 | 
					            } elseif ($token instanceof \HTMLPurifier_Token_End) { //Tag ends
 | 
				
			||||||
                $openTokens--;
 | 
					                if (!empty($openTokens[$token->name])) {
 | 
				
			||||||
                $truncated[] = $token;
 | 
					                    $openTokens[$token->name]--;
 | 
				
			||||||
 | 
					                    $truncated[] = $token;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            } elseif ($token instanceof \HTMLPurifier_Token_Empty) { //Self contained tags, i.e. <img/> etc.
 | 
					            } elseif ($token instanceof \HTMLPurifier_Token_Empty) { //Self contained tags, i.e. <img/> etc.
 | 
				
			||||||
                $truncated[] = $token;
 | 
					                $truncated[] = $token;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
				
			|||||||
@ -115,6 +115,8 @@ class StringHelperTest extends TestCase
 | 
				
			|||||||
        $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>...', 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));
 | 
					        $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));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $this->assertEquals('<p>This is a test</p><ul><li>bullet1</li><li>b</li></ul>...', StringHelper::truncate('<p>This is a test</p><ul><li>bullet1</li><li>bullet2</li><li>bullet3</li><li>bullet4</li></ul>', 22, '...', null, true));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function testTruncateWords()
 | 
					    public function testTruncateWords()
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user