diff --git a/core/lib/Thelia/Core/Template/Smarty/SmartyParser.php b/core/lib/Thelia/Core/Template/Smarty/SmartyParser.php index 53adfc88b..580da808e 100644 --- a/core/lib/Thelia/Core/Template/Smarty/SmartyParser.php +++ b/core/lib/Thelia/Core/Template/Smarty/SmartyParser.php @@ -24,6 +24,7 @@ use Thelia\Core\Template\ParserContext; use Thelia\Core\Template\TemplateDefinition; use Imagine\Exception\InvalidArgumentException; use Thelia\Core\Translation\Translator; +use Thelia\Model\ConfigQuery; /** * @@ -95,14 +96,115 @@ 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('output', array($this, "trimWhitespaces")); $this->registerFilter('variable', array(__CLASS__, "theliaEscape")); } - public function removeBlankLines($tpl_source, \Smarty_Internal_Template $template) + /** + * Trim whitespaces from the HTML output, preserving required ones in pre, textarea, javascript. + * This methois uses 3 levels of trimming : + * + * - 0 : whitespaces are not trimmed, code remains as is. + * - 1 : only blank lines are trimmed, code remains indented and human-readable (the default) + * - 2 or more : all unnecessary whitespace are removed. Code is very hard to read. + * + * The trim level is defined by the configuration variable html_output_trim_level + * + * @param string $source the HTML source + * @param \Smarty_Internal_Template $template + * @return string + */ + public function trimWhitespaces($source, \Smarty_Internal_Template $template) { - return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $tpl_source); + $compressionMode = ConfigQuery::read('html_output_trim_level', 1); + + if ($compressionMode == 0) { + return $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 ($compressionMode == 1) { + $expressions = array( + // 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', + '/(^[\n]*|[\n]+)[\s\t]*[\n]+/' => "\n" + ); + } + else if ($compressionMode >= 2) { + if (preg_match_all('##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 + // yes, even the ones in