Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[-] FO: Fix #PSCFV-7850 sometimes minify_html lib was returned nothin…

…g...
  • Loading branch information...
commit 2011cdb735c6d205316398f1e5b296f51696ca77 1 parent c8320fa
Rémi Gaillard rGaillard authored
Showing with 44 additions and 19 deletions.
  1. +22 −7 classes/Media.php
  2. +22 −12 tools/minify_html/minify_html.class.php
29 classes/Media.php
View
@@ -1,6 +1,6 @@
<?php
/*
-* 2007-2013 PrestaShop
+* 2007-2012 PrestaShop
*
* NOTICE OF LICENSE
*
@@ -68,11 +68,13 @@ public static function minifyHTML($html_content)
$html_content = preg_replace_callback(
'/(<[a-zA-Z0-9]+)((\s?[a-zA-Z0-9]+=[\"\\\'][^\"\\\']*[\"\\\']\s?)*)>/',
array('Media', 'minifyHTMLpregCallback'),
- $html_content);
+ $html_content,
+ Media::getBackTrackLimit());
require_once(_PS_TOOL_DIR_.'minify_html/minify_html.class.php');
$html_content = str_replace(chr(194).chr(160), '&nbsp;', $html_content);
- $html_content = Minify_HTML::minify($html_content, array('xhtml', 'cssMinifier', 'jsMinifier'));
+ if (trim($minified_content = Minify_HTML::minify($html_content, array('xhtml', 'cssMinifier', 'jsMinifier'))) != '')
+ $html_content = $minified_content;
return $html_content;
}
@@ -101,7 +103,8 @@ public static function packJSinHTML($html_content)
$html_content = preg_replace_callback(
'/\\s*(<script\\b[^>]*?>)([\\s\\S]*?)(<\\/script>)\\s*/i',
array('Media', 'packJSinHTMLpregCallback'),
- $html_content);
+ $html_content,
+ Media::getBackTrackLimit());
// If the string is too big preg_replace return an error
// In this case, we don't compress the content
@@ -149,10 +152,10 @@ public static function minifyCSS($css_content, $fileuri = false, &$import_url =
$current_css_file = $fileuri;
if (strlen($css_content) > 0)
{
- $css_content = preg_replace('#/\*.*?\*/#s', '', $css_content);
- $css_content = preg_replace_callback('#url\((?!data:)(?:\'|")?([^\)\'"]*)(?:\'|")?\)#s', array('Tools', 'replaceByAbsoluteURL'), $css_content);
+ $css_content = preg_replace('#/\*.*?\*/#s', '', $css_content, Media::getBackTrackLimit());
+ $css_content = preg_replace_callback('#url\((?!data:)(?:\'|")?([^\)\'"]*)(?:\'|")?\)#s', array('Tools', 'replaceByAbsoluteURL'), $css_content, Media::getBackTrackLimit());
- $css_content = preg_replace('#\s+#', ' ', $css_content);
+ $css_content = preg_replace('#\s+#', ' ', $css_content, Media::getBackTrackLimit());
$css_content = str_replace("\t", '', $css_content);
$css_content = str_replace("\n", '', $css_content);
//$css_content = str_replace('}', "}\n", $css_content);
@@ -471,6 +474,18 @@ public static function cccCss($css_files)
return $css_files;
}
+ public static function getBackTrackLimit()
+ {
+ static $limit = null;
+ if ($limit === null)
+ {
+ $limit = @ini_get('pcre.backtrack_limit');
+ if (!$limit)
+ $limit = -1;
+ }
+
+ return $limit;
+ }
/**
* Combine Compress and Cache (ccc) JS calls
34 tools/minify_html/minify_html.class.php
View
@@ -43,6 +43,10 @@ class Minify_HTML {
*
* @return string
*/
+
+ /* PrestaShop
+ added a limit for all preg_replace_callback
+ */
public static function minify($html, $options = array()) {
if (isset($options['cssMinifier'])) {
@@ -67,47 +71,53 @@ public static function minify($html, $options = array()) {
$html = preg_replace_callback(
'/\\s*(<script\\b[^>]*?>)([\\s\\S]*?)<\\/script>\\s*/i'
,array(self::$className, '_removeScriptCB')
- ,$html);
+ ,$html,
+ Media::getBackTrackLimit());
// replace STYLEs (and minify) with placeholders
$html = preg_replace_callback(
'/\\s*(<style\\b[^>]*?>)([\\s\\S]*?)<\\/style>\\s*/i'
,array(self::$className, '_removeStyleCB')
- ,$html);
-
+ ,$html,
+ Media::getBackTrackLimit());
+
// remove HTML comments (not containing IE conditional comments).
$html = preg_replace_callback(
'/<!--([\\s\\S]*?)-->/'
,array(self::$className, '_commentCB')
- ,$html);
+ ,$html,
+ Media::getBackTrackLimit());
// replace PREs with placeholders
$html = preg_replace_callback('/\\s*(<pre\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i'
,array(self::$className, '_removePreCB')
- , $html);
+ , $html,
+ Media::getBackTrackLimit());
// replace TEXTAREAs with placeholders
$html = preg_replace_callback(
'/\\s*(<textarea\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i'
,array(self::$className, '_removeTaCB')
- , $html);
+ , $html,
+ Media::getBackTrackLimit());
// trim each line.
// @todo take into account attribute values that span multiple lines.
- $html = preg_replace('/^\\s+|\\s+$/m', '', $html);
+ $html = preg_replace('/^\\s+|\\s+$/m', '', $html, Media::getBackTrackLimit());
// remove ws around block/undisplayed elements
$html = preg_replace('/\\s+(<\\/?(?:area|base(?:font)?|blockquote|body'
.'|caption|center|cite|col(?:group)?|dd|dir|div|dl|dt|fieldset|form'
.'|frame(?:set)?|h[1-6]|head|hr|html|legend|li|link|map|menu|meta'
.'|ol|opt(?:group|ion)|p|param|t(?:able|body|head|d|h||r|foot|itle)'
- .'|ul)\\b[^>]*>)/i', '$1', $html);
+ .'|ul)\\b[^>]*>)/i', '$1', $html, Media::getBackTrackLimit());
// remove ws outside of all elements
$html = preg_replace_callback(
'/>([^<]+)</'
,array(self::$className, '_outsideTagCB')
- ,$html);
+ ,$html,
+ Media::getBackTrackLimit());
// use newlines before 1st attribute in open tags (to limit line lengths)
//$html = preg_replace('/(<[a-z\\-]+)\\s+([^>]+>)/i', "$1\n$2", $html);
@@ -146,7 +156,7 @@ protected static function _reservePlace($content)
protected static function _outsideTagCB($m)
{
- return '>' . preg_replace('/^\\s+|\\s+$/', ' ', $m[1]) . '<';
+ return '>' . preg_replace('/^\\s+|\\s+$/', ' ', $m[1], Media::getBackTrackLimit()) . '<';
}
protected static function _removePreCB($m)
@@ -164,7 +174,7 @@ protected static function _removeStyleCB($m)
$openStyle = $m[1];
$css = $m[2];
// remove HTML comments
- $css = preg_replace('/(?:^\\s*<!--|-->\\s*$)/', '', $css);
+ $css = preg_replace('/(?:^\\s*<!--|-->\\s*$)/', '', $css, Media::getBackTrackLimit());
// remove CDATA section markers
$css = self::_removeCdata($css);
@@ -187,7 +197,7 @@ protected static function _removeScriptCB($m)
$js = $m[2];
// remove HTML comments (and ending "//" if present)
- $js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js);
+ $js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js, Media::getBackTrackLimit());
// remove CDATA section markers
$js = self::_removeCdata($js);
Please sign in to comment.
Something went wrong with that request. Please try again.