diff --git a/change_log.txt b/change_log.txt index 605133bf5..850f38098 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,4 +1,7 @@ ===== 3.1.28-dev===== (xx.xx.2015) + 09.12.2015 + -bugfix {strip} should exclude some html tags from stripping, related to fix for https://github.com/smarty-php/smarty/issues/111 + 08.12.2015 - bugfix internal template function data got stored in wrong compiled file https://github.com/smarty-php/smarty/issues/114 diff --git a/libs/sysplugins/smarty_internal_templatecompilerbase.php b/libs/sysplugins/smarty_internal_templatecompilerbase.php index 0418e7b48..04584a40f 100644 --- a/libs/sysplugins/smarty_internal_templatecompilerbase.php +++ b/libs/sysplugins/smarty_internal_templatecompilerbase.php @@ -783,12 +783,58 @@ public function compileConfigVariable($variable) */ public function processText($text) { + $store = array(); + $_store = 0; + $_offset = 0; if ($this->parser->strip) { - return new Smarty_Internal_ParseTree_Text(preg_replace($this->stripRegEx, ' ', $text)); - } else { + if (strpos($text, '<') !== false) { + // capture html elements not to be messed with + $_offset = 0; + if (preg_match_all('#(]*>)|(]*>)|(
]*>.*?]*>)#is', + $text, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) { + foreach ($matches as $match) { + $store[] = $match[ 0 ][ 0 ]; + $_length = strlen($match[ 0 ][ 0 ]); + $replace = '@!@SMARTY:' . $_store . ':SMARTY@!@'; + $text = substr_replace($text, $replace, $match[ 0 ][ 1 ] - $_offset, $_length); + + $_offset += $_length - strlen($replace); + $_store ++; + } + } + + $expressions = array(// replace multiple spaces between tags by a single space + // can't remove them entirely, becaue that might break poorly implemented CSS display:inline-block elements + '#(:SMARTY@!@|>)\s+(?=@!@SMARTY:|<)#s' => '\1 \2', + // remove spaces between attributes (but not in attribute values!) + '#(([a-z0-9]\s*=\s*("[^"]*?")|(\'[^\']*?\'))|<[a-z0-9_]+)\s+([a-z/>])#is' => '\1 \5', + '#^\s+<#Ss' => '<', + '#>\s+$#Ss' => '>', + $this->stripRegEx => '' + ); + + $text = preg_replace(array_keys($expressions), array_values($expressions), $text); + $_offset = 0; + if (preg_match_all('#@!@SMARTY:([0-9]+):SMARTY@!@#is', $text, $matches, + PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) { + foreach ($matches as $match) { + $_length = strlen($match[ 0 ][ 0 ]); + $replace = $store[ $match[ 1 ][ 0 ] ]; + $text = substr_replace($text, $replace, $match[ 0 ][ 1 ] + $_offset, $_length); + + $_offset += strlen($replace) - $_length; + $_store ++; + } + } + } else { + $text = preg_replace($this->stripRegEx, '', $text); + } + } + if ($text) { return new Smarty_Internal_ParseTree_Text($text); } - } + return null; + } /** * lazy loads internal compile plugin for tag and calls the compile method