Skip to content

Commit

Permalink
-bugfix {strip} should exclude some html tags from stripping, related…
Browse files Browse the repository at this point in the history
… to fix for #111
  • Loading branch information
uwetews committed Dec 9, 2015
1 parent ee743d2 commit 260d3e3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
3 changes: 3 additions & 0 deletions change_log.txt
Original file line number Diff line number Diff line change
@@ -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

Expand Down
52 changes: 49 additions & 3 deletions libs/sysplugins/smarty_internal_templatecompilerbase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('#(<script[^>]*>.*?</script[^>]*>)|(<textarea[^>]*>.*?</textarea[^>]*>)|(<pre[^>]*>.*?</pre[^>]*>)#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
Expand Down

0 comments on commit 260d3e3

Please sign in to comment.