From 87c002d5b3e20d1f662b28671846048c17659525 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Tue, 25 Nov 2014 07:29:27 -0500 Subject: [PATCH] Fixes #6207: Added support for truncating HTML strings using `StringHelper::truncate()` and `StringHelper::truncateWords()` --- framework/CHANGELOG.md | 1 + framework/helpers/BaseStringHelper.php | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 150fe7a..a73a1be 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -58,6 +58,7 @@ Yii Framework 2 Change Log - Enh #5954: `yii message` command now shows user friendly error if it's not able to parse source file (samdark) - Enh #5983: Added `Inflector::sentence()` (pana1990, qiangxue) - Enh #6113: Improved debugger configuration and request UI (schmunk42) +- Enh #6207: Added support for truncating HTML strings using `StringHelper::truncate()` and `StringHelper::truncateWords()` (Alex-Code) - Enh: `Console::confirm()` now uses `Console::stdout()` instead of `echo` to be consistent with all other functions (cebe) - Enh: `yii\rbac\DbManager` migration now uses database component specified in component settings instead of always using default `db` (samdark) - Enh: Added `yii\base\Controller::renderContent()` (qiangxue) diff --git a/framework/helpers/BaseStringHelper.php b/framework/helpers/BaseStringHelper.php index b064e66..1928e48 100644 --- a/framework/helpers/BaseStringHelper.php +++ b/framework/helpers/BaseStringHelper.php @@ -98,7 +98,7 @@ class BaseStringHelper * @param integer $length How many characters from original string to include into truncated string. * @param string $suffix String to append to the end of truncated string. * @param string $encoding The charset to use, defaults to charset currently used by application. - * @param boolean $asHtml If the string contains HTML set this to true to preserve it. + * @param boolean $asHtml Whether to treat the string being truncated as HTML and preserve proper HTML tags * @return string the truncated string. */ public static function truncate($string, $length, $suffix = '...', $encoding = null, $asHtml = false) @@ -120,7 +120,7 @@ class BaseStringHelper * @param string $string The string to truncate. * @param integer $count How many words from original string to include into truncated string. * @param string $suffix String to append to the end of truncated string. - * @param boolean $asHtml If the string contains HTML set this to true to preserve it. + * @param boolean $asHtml Whether to treat the string being truncated as HTML and preserve proper HTML tags * @return string the truncated string. */ public static function truncateWords($string, $count, $suffix = '...', $asHtml = false) @@ -143,10 +143,10 @@ class BaseStringHelper * @param string $string The string to truncate * @param integer $count * @param string $suffix String to append to the end of the truncated string. - * @param string $encoding + * @param string|boolean $encoding * @return string */ - private static function truncateHtml($string, $count, $suffix, $encoding = false) + protected static function truncateHtml($string, $count, $suffix, $encoding = false) { $config = \HTMLPurifier_Config::create(null); $lexer = \HTMLPurifier_Lexer::create($config);