diff --git a/core/lib/Drupal/Component/Utility/SafeMarkup.php b/core/lib/Drupal/Component/Utility/SafeMarkup.php index 69d12fe..e880100 100644 --- a/core/lib/Drupal/Component/Utility/SafeMarkup.php +++ b/core/lib/Drupal/Component/Utility/SafeMarkup.php @@ -329,4 +329,43 @@ public static function replace($search, $replace, $subject) { } } + /** + * Truncates an HTML string safely to a number of text characters. + * + * Functions similarly to \Drupal\Component\Utility\Unicode::truncate(), + * but strips HTML tags prior to truncating and ensures the truncated string + * has well-formed HTML entities. + * + * @param string $string + * The string to truncate. HTML tags are stripped prior to truncation, but + * HTML entities that fit in the output length are preserved. + * @param int $max_length + * An upper limit on the returned string length, including trailing ellipsis + * if $add_ellipsis is TRUE. + * @param bool $wordsafe + * If TRUE, attempt to truncate on a word boundary. See Unicode::truncate() + * for details. + * @param bool $add_ellipsis + * If TRUE, add '...' to the end of the truncated string (defaults to + * FALSE). The string length will still fall within $max_length. + * @param int $min_wordsafe_length + * If $wordsafe is TRUE, the minimum acceptable length for truncation (before + * adding an ellipsis, if $add_ellipsis is TRUE). See Unicode::truncate() + * for details. + * + * @return string + * The truncated string. + */ + public static function truncate($string, $max_length, $wordsafe = FALSE, $add_ellipsis = FALSE, $min_wordsafe_length = 1) { + // Strip tags prior to truncation, so that the output has useful text. + $string = strip_tags($string); + + // Truncate. + $string = Unicode::truncate($string, $max_length, $wordsafe, $add_ellipsis, $min_wordsafe_length); + + // The truncation might have split an HTML entity, so invoke Xss::filter() + // to correct that. + return Xss::filter($string, []); + } + } diff --git a/core/lib/Drupal/Component/Utility/Unicode.php b/core/lib/Drupal/Component/Utility/Unicode.php index 7691e00..4938b0d 100644 --- a/core/lib/Drupal/Component/Utility/Unicode.php +++ b/core/lib/Drupal/Component/Utility/Unicode.php @@ -508,7 +508,7 @@ public static function substr($text, $start, $length = NULL) { * @param bool $add_ellipsis * If TRUE, add '...' to the end of the truncated string (defaults to * FALSE). The string length will still fall within $max_length. - * @param bool $min_wordsafe_length + * @param int $min_wordsafe_length * If $wordsafe is TRUE, the minimum acceptable length for truncation (before * adding an ellipsis, if $add_ellipsis is TRUE). Has no effect if $wordsafe * is FALSE. This can be used to prevent having a very short resulting string diff --git a/core/modules/dblog/src/Controller/DbLogController.php b/core/modules/dblog/src/Controller/DbLogController.php index 2e2eccf..fd22e46 100644 --- a/core/modules/dblog/src/Controller/DbLogController.php +++ b/core/modules/dblog/src/Controller/DbLogController.php @@ -185,9 +185,7 @@ public function overview() { $message = $this->formatMessage($dblog); if ($message && isset($dblog->wid)) { // Truncate link_text to 56 chars of message. - // @todo Reevaluate the SafeMarkup::set() in - // https://www.drupal.org/node/2399261. - $log_text = SafeMarkup::set(Unicode::truncate(Xss::filter($message, array()), 56, TRUE, TRUE)); + $log_text = SafeMarkup::truncate($message, 56, TRUE, TRUE); $message = $this->l($log_text, new Url('dblog.event', array('event_id' => $dblog->wid), array( 'attributes' => array( // Provide a title for the link for useful hover hints.