summaryrefslogtreecommitdiff
path: root/lib/smarty/plugins/outputfilter.trimwhitespace.php
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2018-10-18 02:39:34 +0200
committeremkael <emkael@tlen.pl>2018-10-18 02:39:42 +0200
commitab5d8d4e07bb3c8230d0285ef8902ef1979fce51 (patch)
tree0b955e585cb2fdbc7207392a5f2c97d610b6a5bc /lib/smarty/plugins/outputfilter.trimwhitespace.php
parentc055ce2ab60c6582bad3e5babcb1d00384fde78a (diff)
Updating Smarty
Diffstat (limited to 'lib/smarty/plugins/outputfilter.trimwhitespace.php')
-rw-r--r--lib/smarty/plugins/outputfilter.trimwhitespace.php91
1 files changed, 44 insertions, 47 deletions
diff --git a/lib/smarty/plugins/outputfilter.trimwhitespace.php b/lib/smarty/plugins/outputfilter.trimwhitespace.php
index 41828e1..7e4503a 100644
--- a/lib/smarty/plugins/outputfilter.trimwhitespace.php
+++ b/lib/smarty/plugins/outputfilter.trimwhitespace.php
@@ -2,91 +2,88 @@
/**
* Smarty plugin
*
- * @package Smarty
+ * @package Smarty
* @subpackage PluginsFilter
*/
-
/**
* Smarty trimwhitespace outputfilter plugin
- *
* Trim unnecessary whitespace from HTML markup.
*
- * @author Rodney Rehm
- * @param string $source input string
- * @param Smarty_Internal_Template $smarty Smarty object
+ * @author Rodney Rehm
+ *
+ * @param string $source input string
+ *
* @return string filtered output
+ * @todo substr_replace() is not overloaded by mbstring.func_overload - so this function might fail!
*/
-function smarty_outputfilter_trimwhitespace($source, Smarty_Internal_Template $smarty)
+function smarty_outputfilter_trimwhitespace($source)
{
$store = array();
$_store = 0;
$_offset = 0;
-
// Unify Line-Breaks to \n
- $source = preg_replace("/\015\012|\015|\012/", "\n", $source);
-
- // capture Internet Explorer Conditional Comments
- if (preg_match_all('#<!--\[[^\]]+\]>.*?<!\[[^\]]+\]-->#is', $source, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {
+ $source = preg_replace('/\015\012|\015|\012/', "\n", $source);
+ // capture Internet Explorer and KnockoutJS Conditional Comments
+ if (preg_match_all(
+ '#<!--((\[[^\]]+\]>.*?<!\[[^\]]+\])|(\s*/?ko\s+.+))-->#is',
+ $source,
+ $matches,
+ PREG_OFFSET_CAPTURE | PREG_SET_ORDER
+ )
+ ) {
foreach ($matches as $match) {
- $store[] = $match[0][0];
- $_length = strlen($match[0][0]);
+ $store[] = $match[ 0 ][ 0 ];
+ $_length = strlen($match[ 0 ][ 0 ]);
$replace = '@!@SMARTY:' . $_store . ':SMARTY@!@';
- $source = substr_replace($source, $replace, $match[0][1] - $_offset, $_length);
-
+ $source = substr_replace($source, $replace, $match[ 0 ][ 1 ] - $_offset, $_length);
$_offset += $_length - strlen($replace);
$_store++;
}
}
-
// Strip all HTML-Comments
- $source = preg_replace( '#<!--.*?-->#ms', '', $source );
-
+ // yes, even the ones in <script> - see http://stackoverflow.com/a/808850/515124
+ $source = preg_replace('#<!--.*?-->#ms', '', $source);
// capture html elements not to be messed with
$_offset = 0;
- if (preg_match_all('#<(script|pre|textarea)[^>]*>.*?</\\1>#is', $source, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {
+ if (preg_match_all(
+ '#(<script[^>]*>.*?</script[^>]*>)|(<textarea[^>]*>.*?</textarea[^>]*>)|(<pre[^>]*>.*?</pre[^>]*>)#is',
+ $source,
+ $matches,
+ PREG_OFFSET_CAPTURE | PREG_SET_ORDER
+ )
+ ) {
foreach ($matches as $match) {
- $store[] = $match[0][0];
- $_length = strlen($match[0][0]);
+ $store[] = $match[ 0 ][ 0 ];
+ $_length = strlen($match[ 0 ][ 0 ]);
$replace = '@!@SMARTY:' . $_store . ':SMARTY@!@';
- $source = substr_replace($source, $replace, $match[0][1] - $_offset, $_length);
-
+ $source = substr_replace($source, $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*(["\'])[^\3]*?\3)|<[a-z0-9_]+)\s+([a-z/>])#is' => '\1 \4',
- // note: for some very weird reason trim() seems to remove spaces inside attributes.
- // maybe a \0 byte or something is interfering?
- '#^\s+<#Ss' => '<',
- '#>\s+$#Ss' => '>',
+ $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',
+ // note: for some very weird reason trim() seems to remove spaces inside attributes.
+ // maybe a \0 byte or something is interfering?
+ '#^\s+<#Ss' => '<',
+ '#>\s+$#Ss' => '>',
);
-
- $source = preg_replace( array_keys($expressions), array_values($expressions), $source );
+ $source = preg_replace(array_keys($expressions), array_values($expressions), $source);
// note: for some very weird reason trim() seems to remove spaces inside attributes.
// maybe a \0 byte or something is interfering?
// $source = trim( $source );
-
- // capture html elements not to be messed with
$_offset = 0;
if (preg_match_all('#@!@SMARTY:([0-9]+):SMARTY@!@#is', $source, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {
foreach ($matches as $match) {
- $store[] = $match[0][0];
- $_length = strlen($match[0][0]);
- $replace = array_shift($store);
- $source = substr_replace($source, $replace, $match[0][1] + $_offset, $_length);
-
+ $_length = strlen($match[ 0 ][ 0 ]);
+ $replace = $store[ $match[ 1 ][ 0 ] ];
+ $source = substr_replace($source, $replace, $match[ 0 ][ 1 ] + $_offset, $_length);
$_offset += strlen($replace) - $_length;
$_store++;
}
}
-
return $source;
}
-
-?> \ No newline at end of file