From b773c81ae93cfe0d46c1139482764216ab874351 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 14 Dec 2018 13:07:26 -0500 Subject: [PATCH] Fix issue processwire/processwire-issues#766 auto-remove UTF-8 value of `
` line-seperator entity in $sanitizer->text() function --- wire/core/Sanitizer.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/wire/core/Sanitizer.php b/wire/core/Sanitizer.php index c016e033..8f05e4c7 100644 --- a/wire/core/Sanitizer.php +++ b/wire/core/Sanitizer.php @@ -1022,7 +1022,8 @@ class Sanitizer extends Wire { 'truncateTail' => true, // if truncate necessary for maxLength, remove chars from tail? False to truncate from head. 'trim' => true, // trim whitespace from beginning/end, or specify character(s) to trim, or false to disable ); - + + static $alwaysReplace = null; $truncated = false; $options = array_merge($defaultOptions, $options); if(isset($options['multiline'])) $options['multiLine'] = $options['multiline']; // common case error @@ -1030,6 +1031,12 @@ class Sanitizer extends Wire { if($options['maxLength'] < 0) $options['maxLength'] = 0; if($options['maxBytes'] < 0) $options['maxBytes'] = 0; + if($alwaysReplace === null) { + $alwaysReplace = array( + mb_convert_encoding('
', 'UTF-8', 'HTML-ENTITIES') => '', // line-seperator that is sometimes copy/pasted + ); + } + if($options['reduceSpace'] !== false && $options['stripSpace'] === false) { // if reduceSpace option is used then provide necessary value for stripSpace option $options['stripSpace'] = is_string($options['reduceSpace']) ? $options['reduceSpace'] : ' '; @@ -1064,6 +1071,11 @@ class Sanitizer extends Wire { if($options['convertEntities']) { $value = $this->unentities($value, true, $options['outCharset']); } + + foreach($alwaysReplace as $find => $replace) { + if(strpos($value, $find) === false) continue; + $value = str_replace($find, $replace, $value); + } if($options['stripSpace'] !== false) { $c = is_string($options['stripSpace']) ? $options['stripSpace'] : ''; @@ -1968,7 +1980,6 @@ class Sanitizer extends Wire { * @param string $str String to purify * @param array $options See [config options](http://htmlpurifier.org/live/configdoc/plain.html). * @return string Purified markup string. - * @throws WireException if given something other than a string * */ public function purify($str, array $options = array()) {