From 51609351f2c4b5082b7e6f0744cd3811c325303f Mon Sep 17 00:00:00 2001 From: emkael Date: Tue, 11 Oct 2016 14:01:29 +0200 Subject: * initial template --- lib/smarty/plugins/outputfilter.trimwhitespace.php | 92 ++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 lib/smarty/plugins/outputfilter.trimwhitespace.php (limited to 'lib/smarty/plugins/outputfilter.trimwhitespace.php') diff --git a/lib/smarty/plugins/outputfilter.trimwhitespace.php b/lib/smarty/plugins/outputfilter.trimwhitespace.php new file mode 100644 index 0000000..41828e1 --- /dev/null +++ b/lib/smarty/plugins/outputfilter.trimwhitespace.php @@ -0,0 +1,92 @@ +.*?#is', $source, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) { + foreach ($matches as $match) { + $store[] = $match[0][0]; + $_length = strlen($match[0][0]); + $replace = '@!@SMARTY:' . $_store . ':SMARTY@!@'; + $source = substr_replace($source, $replace, $match[0][1] - $_offset, $_length); + + $_offset += $_length - strlen($replace); + $_store++; + } + } + + // Strip all HTML-Comments + $source = preg_replace( '##ms', '', $source ); + + // capture html elements not to be messed with + $_offset = 0; + if (preg_match_all('#<(script|pre|textarea)[^>]*>.*?#is', $source, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) { + foreach ($matches as $match) { + $store[] = $match[0][0]; + $_length = strlen($match[0][0]); + $replace = '@!@SMARTY:' . $_store . ':SMARTY@!@'; + $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' => '>', + ); + + $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); + + $_offset += strlen($replace) - $_length; + $_store++; + } + } + + return $source; +} + +?> \ No newline at end of file -- cgit v1.2.3