From 9cb145e644dd1dd4dc8c5c67cb6a9107d9762772 Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Thu, 19 Jun 2014 17:56:01 +0200 Subject: [PATCH] Fix for #490 --- .../Plugins/outputfilter.trimwhitespace.php | 75 +++++++++++++++++++ .../Core/Template/Smarty/SmartyParser.php | 7 +- 2 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 core/lib/Thelia/Core/Template/Smarty/Plugins/outputfilter.trimwhitespace.php diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/outputfilter.trimwhitespace.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/outputfilter.trimwhitespace.php new file mode 100644 index 000000000..000fea9ab --- /dev/null +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/outputfilter.trimwhitespace.php @@ -0,0 +1,75 @@ + + * Type: outputfilter
+ * Name: trimwhitespace
+ * Date: Jan 25, 2003
+ * Purpose: trim leading white space and blank lines from + * template source after it gets interpreted, cleaning + * up code and saving bandwidth. Does not affect + * <
>
and blocks.
+ * Install: Drop into the plugin directory, call + * $smarty->load_filter('output','trimwhitespace'); + * from application. + * @author Monte Ohrt + * @author Contributions from Lars Noschinski + * @version 1.3 + * @param string + * @param Smarty + */ +function smarty_outputfilter_trimwhitespace($source, &$smarty) +{ + // Pull out the script blocks + preg_match_all("!]*?>.*?!is", $source, $match); + $_script_blocks = $match[0]; + $source = preg_replace("!]*?>.*?!is", + '@@@SMARTY:TRIM:SCRIPT@@@', $source); + + // Pull out the pre blocks + preg_match_all("!]*?>.*?!is", $source, $match); + $_pre_blocks = $match[0]; + $source = preg_replace("!]*?>.*?!is", + '@@@SMARTY:TRIM:PRE@@@', $source); + + // Pull out the textarea blocks + preg_match_all("!]*?>.*?!is", $source, $match); + $_textarea_blocks = $match[0]; + $source = preg_replace("!]*?>.*?!is", + '@@@SMARTY:TRIM:TEXTAREA@@@', $source); + + // remove all leading spaces, tabs and carriage returns NOT + // preceeded by a php close tag. + $source = trim(preg_replace('/((?)\n)[\s]+/m', '\1', $source)); + + // replace textarea blocks + smarty_outputfilter_trimwhitespace_replace("@@@SMARTY:TRIM:TEXTAREA@@@",$_textarea_blocks, $source); + + // replace pre blocks + smarty_outputfilter_trimwhitespace_replace("@@@SMARTY:TRIM:PRE@@@",$_pre_blocks, $source); + + // replace script blocks + smarty_outputfilter_trimwhitespace_replace("@@@SMARTY:TRIM:SCRIPT@@@",$_script_blocks, $source); + + return $source; +} + +function smarty_outputfilter_trimwhitespace_replace($search_str, $replace, &$subject) { + $_len = strlen($search_str); + $_pos = 0; + for ($_i=0, $_count=count($replace); $_i<$_count; $_i++) + if (($_pos=strpos($subject, $search_str, $_pos))!==false) + $subject = substr_replace($subject, $replace[$_i], $_pos, $_len); + else + break; + +} + +?> \ No newline at end of file diff --git a/core/lib/Thelia/Core/Template/Smarty/SmartyParser.php b/core/lib/Thelia/Core/Template/Smarty/SmartyParser.php index bcc915018..807ef2438 100644 --- a/core/lib/Thelia/Core/Template/Smarty/SmartyParser.php +++ b/core/lib/Thelia/Core/Template/Smarty/SmartyParser.php @@ -95,7 +95,7 @@ class SmartyParser extends Smarty implements ParserInterface // The default HTTP status $this->status = 200; - $this->registerFilter('output', array($this, "removeBlankLines")); + $this->loadFilter('output', "trimwhitespace"); $this->registerFilter('variable', array(__CLASS__, "theliaEscape")); } @@ -139,11 +139,6 @@ class SmartyParser extends Smarty implements ParserInterface return $this->templateDirectories[$templateType]; } - public function removeBlankLines($tpl_source, \Smarty_Internal_Template $template) - { - return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $tpl_source); - } - public static function theliaEscape($content, $smarty) { if (is_scalar($content)) {