Version Notes
+ Compatibility with Varnish cache extensions
Download this release
Release Info
Developer | Apptrian |
Extension | Apptrian_Minify_HTML_CSS_JS |
Version | 1.1.1 |
Comparing to | |
See all releases |
Code changes from version 1.1.0 to 1.1.1
- app/code/community/Apptrian/Minify/Model/Observer.php +2 -2
- app/code/community/Apptrian/Minify/etc/config.xml +1 -1
- app/code/community/Apptrian/Minify/etc/system.xml +2 -3
- app/locale/en_US/Apptrian_Minify.csv +1 -2
- lib/Minify/HTMLComp.php +265 -0
- lib/Minify/HTMLMax.php +10 -0
- lib/Minify/HTMLMaxComp.php +268 -0
- package.xml +6 -7
app/code/community/Apptrian/Minify/Model/Observer.php
CHANGED
@@ -93,9 +93,9 @@ class Apptrian_Minify_Model_Observer
|
|
93 |
$html = $transport->getHtml();
|
94 |
|
95 |
if ($this->getMaxMinifyStatus()) {
|
96 |
-
$transport->setHtml(
|
97 |
} else {
|
98 |
-
$transport->setHtml(
|
99 |
}
|
100 |
|
101 |
}
|
93 |
$html = $transport->getHtml();
|
94 |
|
95 |
if ($this->getMaxMinifyStatus()) {
|
96 |
+
$transport->setHtml(Minify_HTMLMaxComp::minify($html, $this->minifyOptions));
|
97 |
} else {
|
98 |
+
$transport->setHtml(Minify_HTMLComp::minify($html, $this->minifyOptions));
|
99 |
}
|
100 |
|
101 |
}
|
app/code/community/Apptrian/Minify/etc/config.xml
CHANGED
@@ -11,7 +11,7 @@
|
|
11 |
<config>
|
12 |
<modules>
|
13 |
<Apptrian_Minify>
|
14 |
-
<version>1.1.
|
15 |
</Apptrian_Minify>
|
16 |
</modules>
|
17 |
<global>
|
11 |
<config>
|
12 |
<modules>
|
13 |
<Apptrian_Minify>
|
14 |
+
<version>1.1.1</version>
|
15 |
</Apptrian_Minify>
|
16 |
</modules>
|
17 |
<global>
|
app/code/community/Apptrian/Minify/etc/system.xml
CHANGED
@@ -100,7 +100,7 @@
|
|
100 |
<minify_html>1</minify_html>
|
101 |
</depends>
|
102 |
</max_minification>
|
103 |
-
<compatibility translate="label comment
|
104 |
<label>Enable Cache Compatibility Mode</label>
|
105 |
<frontend_type>select</frontend_type>
|
106 |
<source_model>adminhtml/system_config_source_yesno</source_model>
|
@@ -108,8 +108,7 @@
|
|
108 |
<show_in_default>1</show_in_default>
|
109 |
<show_in_website>1</show_in_website>
|
110 |
<show_in_store>1</show_in_store>
|
111 |
-
<comment><![CDATA[Enables or disables Cache Compatibility Mode.<br />WARNING!
|
112 |
-
<tooltip>You should enable this if you have problems with third party cache extensions.</tooltip>
|
113 |
<depends>
|
114 |
<minify_html>1</minify_html>
|
115 |
</depends>
|
100 |
<minify_html>1</minify_html>
|
101 |
</depends>
|
102 |
</max_minification>
|
103 |
+
<compatibility translate="label comment">
|
104 |
<label>Enable Cache Compatibility Mode</label>
|
105 |
<frontend_type>select</frontend_type>
|
106 |
<source_model>adminhtml/system_config_source_yesno</source_model>
|
108 |
<show_in_default>1</show_in_default>
|
109 |
<show_in_website>1</show_in_website>
|
110 |
<show_in_store>1</show_in_store>
|
111 |
+
<comment><![CDATA[Enables or disables Cache Compatibility Mode.<br />WARNING! You should enable this if you use third party Varnish cache extensions.]]></comment>
|
|
|
112 |
<depends>
|
113 |
<minify_html>1</minify_html>
|
114 |
</depends>
|
app/locale/en_US/Apptrian_Minify.csv
CHANGED
@@ -10,8 +10,7 @@
|
|
10 |
"Enables or disables Maximum HTML Minification.<br />WARNING! Slower and unsafe. See tooltip for more information.","Enables or disables Maximum HTML Minification.<br />WARNING! Slower and unsafe. See tooltip for more information."
|
11 |
"If you enable this option all multiple spaces will be eliminated and replaced with one space. All new line characters will be substituted with one space character. Your entire code will be on one line. This is not recommended, you should keep this option disabled.","If you enable this option all multiple spaces will be eliminated and replaced with one space. All new line characters will be substituted with one space character. Your entire code will be on one line. This is not recommended, you should keep this option disabled."
|
12 |
"Enable Cache Compatibility Mode","Enable Cache Compatibility Mode"
|
13 |
-
"Enables or disables Cache Compatibility Mode.<br />WARNING!
|
14 |
-
"You should enable this if you have problems with third party cache extensions.","You should enable this if you have problems with third party cache extensions."
|
15 |
"CSS Settings","CSS Settings"
|
16 |
"Minify CSS","Minify CSS"
|
17 |
"Enables or disables CSS minification.<br />WARNING! Before you enable this option you must have valid CSS code on all of the pages on your site. In order for minification to work you must enable Admin>System>Configuration>Developer>CSS Settings>Merge CSS Files.","Enables or disables CSS minification.<br />WARNING! Before you enable this option you must have valid CSS code on all of the pages on your site. In order for minification to work you must enable Admin>System>Configuration>Developer>CSS Settings>Merge CSS Files."
|
10 |
"Enables or disables Maximum HTML Minification.<br />WARNING! Slower and unsafe. See tooltip for more information.","Enables or disables Maximum HTML Minification.<br />WARNING! Slower and unsafe. See tooltip for more information."
|
11 |
"If you enable this option all multiple spaces will be eliminated and replaced with one space. All new line characters will be substituted with one space character. Your entire code will be on one line. This is not recommended, you should keep this option disabled.","If you enable this option all multiple spaces will be eliminated and replaced with one space. All new line characters will be substituted with one space character. Your entire code will be on one line. This is not recommended, you should keep this option disabled."
|
12 |
"Enable Cache Compatibility Mode","Enable Cache Compatibility Mode"
|
13 |
+
"Enables or disables Cache Compatibility Mode.<br />WARNING! You should enable this if you use third party Varnish cache extensions.","Enables or disables Cache Compatibility Mode.<br />WARNING! You should enable this if you use third party Varnish cache extensions."
|
|
|
14 |
"CSS Settings","CSS Settings"
|
15 |
"Minify CSS","Minify CSS"
|
16 |
"Enables or disables CSS minification.<br />WARNING! Before you enable this option you must have valid CSS code on all of the pages on your site. In order for minification to work you must enable Admin>System>Configuration>Developer>CSS Settings>Merge CSS Files.","Enables or disables CSS minification.<br />WARNING! Before you enable this option you must have valid CSS code on all of the pages on your site. In order for minification to work you must enable Admin>System>Configuration>Developer>CSS Settings>Merge CSS Files."
|
lib/Minify/HTMLComp.php
ADDED
@@ -0,0 +1,265 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/**
|
3 |
+
* Class Minify_HTML
|
4 |
+
* @package Minify
|
5 |
+
*/
|
6 |
+
|
7 |
+
/**
|
8 |
+
* Compress HTML
|
9 |
+
*
|
10 |
+
* This is a heavy regex-based removal of whitespace, unnecessary comments and
|
11 |
+
* tokens. IE conditional comments are preserved. There are also options to have
|
12 |
+
* STYLE and SCRIPT blocks compressed by callback functions.
|
13 |
+
*
|
14 |
+
* A test suite is available.
|
15 |
+
*
|
16 |
+
* @package Minify
|
17 |
+
* @author Stephen Clay <steve@mrclay.org>
|
18 |
+
*/
|
19 |
+
|
20 |
+
/**
|
21 |
+
* Modified version of a class for compatibility with Varnish cache extensions for Magento.
|
22 |
+
*
|
23 |
+
* @category Apptrian
|
24 |
+
* @package Apptrian_Minify
|
25 |
+
* @author Apptrian
|
26 |
+
* @copyright Copyright (c) 2014 Apptrian (http://www.apptrian.com)
|
27 |
+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
28 |
+
*/
|
29 |
+
class Minify_HTMLComp {
|
30 |
+
/**
|
31 |
+
* @var boolean
|
32 |
+
*/
|
33 |
+
protected $_jsCleanComments = true;
|
34 |
+
|
35 |
+
/**
|
36 |
+
* "Minify" an HTML page
|
37 |
+
*
|
38 |
+
* @param string $html
|
39 |
+
*
|
40 |
+
* @param array $options
|
41 |
+
*
|
42 |
+
* 'cssMinifier' : (optional) callback function to process content of STYLE
|
43 |
+
* elements.
|
44 |
+
*
|
45 |
+
* 'jsMinifier' : (optional) callback function to process content of SCRIPT
|
46 |
+
* elements. Note: the type attribute is ignored.
|
47 |
+
*
|
48 |
+
* 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
|
49 |
+
* unset, minify will sniff for an XHTML doctype.
|
50 |
+
*
|
51 |
+
* @return string
|
52 |
+
*/
|
53 |
+
public static function minify($html, $options = array()) {
|
54 |
+
$min = new self($html, $options);
|
55 |
+
return $min->process();
|
56 |
+
}
|
57 |
+
|
58 |
+
|
59 |
+
/**
|
60 |
+
* Create a minifier object
|
61 |
+
*
|
62 |
+
* @param string $html
|
63 |
+
*
|
64 |
+
* @param array $options
|
65 |
+
*
|
66 |
+
* 'cssMinifier' : (optional) callback function to process content of STYLE
|
67 |
+
* elements.
|
68 |
+
*
|
69 |
+
* 'jsMinifier' : (optional) callback function to process content of SCRIPT
|
70 |
+
* elements. Note: the type attribute is ignored.
|
71 |
+
*
|
72 |
+
* 'jsCleanComments' : (optional) whether to remove HTML comments beginning and end of script block
|
73 |
+
*
|
74 |
+
* 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
|
75 |
+
* unset, minify will sniff for an XHTML doctype.
|
76 |
+
*/
|
77 |
+
public function __construct($html, $options = array())
|
78 |
+
{
|
79 |
+
$this->_html = str_replace("\r\n", "\n", trim($html));
|
80 |
+
if (isset($options['xhtml'])) {
|
81 |
+
$this->_isXhtml = (bool)$options['xhtml'];
|
82 |
+
}
|
83 |
+
if (isset($options['cssMinifier'])) {
|
84 |
+
$this->_cssMinifier = $options['cssMinifier'];
|
85 |
+
}
|
86 |
+
if (isset($options['jsMinifier'])) {
|
87 |
+
$this->_jsMinifier = $options['jsMinifier'];
|
88 |
+
}
|
89 |
+
if (isset($options['jsCleanComments'])) {
|
90 |
+
$this->_jsCleanComments = (bool)$options['jsCleanComments'];
|
91 |
+
}
|
92 |
+
}
|
93 |
+
|
94 |
+
|
95 |
+
/**
|
96 |
+
* Minify the markeup given in the constructor
|
97 |
+
*
|
98 |
+
* @return string
|
99 |
+
*/
|
100 |
+
public function process()
|
101 |
+
{
|
102 |
+
if ($this->_isXhtml === null) {
|
103 |
+
$this->_isXhtml = (false !== strpos($this->_html, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML'));
|
104 |
+
}
|
105 |
+
|
106 |
+
$this->_replacementHash = 'MINIFYHTML' . md5($_SERVER['REQUEST_TIME']);
|
107 |
+
$this->_placeholders = array();
|
108 |
+
|
109 |
+
// replace SCRIPTs (and minify) with placeholders
|
110 |
+
$this->_html = preg_replace_callback(
|
111 |
+
'/(\\s*)<script(\\b[^>]*?>)([\\s\\S]*?)<\\/script>(\\s*)/i'
|
112 |
+
,array($this, '_removeScriptCB')
|
113 |
+
,$this->_html);
|
114 |
+
|
115 |
+
// replace STYLEs (and minify) with placeholders
|
116 |
+
$this->_html = preg_replace_callback(
|
117 |
+
'/\\s*<style(\\b[^>]*>)([\\s\\S]*?)<\\/style>\\s*/i'
|
118 |
+
,array($this, '_removeStyleCB')
|
119 |
+
,$this->_html);
|
120 |
+
|
121 |
+
// remove HTML comments (not containing IE conditional comments).
|
122 |
+
$this->_html = preg_replace_callback(
|
123 |
+
'/<!--([\\s\\S]*?)-->/'
|
124 |
+
,array($this, '_commentCB')
|
125 |
+
,$this->_html);
|
126 |
+
|
127 |
+
// replace PREs with placeholders
|
128 |
+
$this->_html = preg_replace_callback('/\\s*<pre(\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i'
|
129 |
+
,array($this, '_removePreCB')
|
130 |
+
,$this->_html);
|
131 |
+
|
132 |
+
// replace TEXTAREAs with placeholders
|
133 |
+
$this->_html = preg_replace_callback(
|
134 |
+
'/\\s*<textarea(\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i'
|
135 |
+
,array($this, '_removeTextareaCB')
|
136 |
+
,$this->_html);
|
137 |
+
|
138 |
+
// trim each line.
|
139 |
+
// @todo take into account attribute values that span multiple lines.
|
140 |
+
$this->_html = preg_replace('/^\\s+|\\s+$/m', '', $this->_html);
|
141 |
+
|
142 |
+
// remove ws around block/undisplayed elements
|
143 |
+
$this->_html = preg_replace('/\\s+(<\\/?(?:area|base(?:font)?|blockquote|body'
|
144 |
+
.'|caption|center|col(?:group)?|dd|dir|div|dl|dt|fieldset|form'
|
145 |
+
.'|frame(?:set)?|h[1-6]|head|hr|html|legend|li|link|map|menu|meta'
|
146 |
+
.'|ol|opt(?:group|ion)|p|param|t(?:able|body|head|d|h||r|foot|itle)'
|
147 |
+
.'|ul)\\b[^>]*>)/i', '$1', $this->_html);
|
148 |
+
|
149 |
+
// remove ws outside of all elements
|
150 |
+
$this->_html = preg_replace(
|
151 |
+
'/>(\\s(?:\\s*))?([^<]+)(\\s(?:\s*))?</'
|
152 |
+
,'>$1$2$3<'
|
153 |
+
,$this->_html);
|
154 |
+
|
155 |
+
// use newlines before 1st attribute in open tags (to limit line lengths)
|
156 |
+
$this->_html = preg_replace('/(<[a-z\\-]+)\\s+([^>]+>)/i', "$1\n$2", $this->_html);
|
157 |
+
|
158 |
+
// fill placeholders
|
159 |
+
$this->_html = str_replace(
|
160 |
+
array_keys($this->_placeholders)
|
161 |
+
,array_values($this->_placeholders)
|
162 |
+
,$this->_html
|
163 |
+
);
|
164 |
+
// issue 229: multi-pass to catch scripts that didn't get replaced in textareas
|
165 |
+
$this->_html = str_replace(
|
166 |
+
array_keys($this->_placeholders)
|
167 |
+
,array_values($this->_placeholders)
|
168 |
+
,$this->_html
|
169 |
+
);
|
170 |
+
return $this->_html;
|
171 |
+
}
|
172 |
+
|
173 |
+
protected function _commentCB($m)
|
174 |
+
{
|
175 |
+
return (0 === strpos($m[1], '[') || false !== strpos($m[1], '<![') || false !== stripos($m[1], 'esi <'))
|
176 |
+
? $m[0]
|
177 |
+
: '';
|
178 |
+
}
|
179 |
+
|
180 |
+
protected function _reservePlace($content)
|
181 |
+
{
|
182 |
+
$placeholder = '%' . $this->_replacementHash . count($this->_placeholders) . '%';
|
183 |
+
$this->_placeholders[$placeholder] = $content;
|
184 |
+
return $placeholder;
|
185 |
+
}
|
186 |
+
|
187 |
+
protected $_isXhtml = null;
|
188 |
+
protected $_replacementHash = null;
|
189 |
+
protected $_placeholders = array();
|
190 |
+
protected $_cssMinifier = null;
|
191 |
+
protected $_jsMinifier = null;
|
192 |
+
|
193 |
+
protected function _removePreCB($m)
|
194 |
+
{
|
195 |
+
return $this->_reservePlace("<pre{$m[1]}");
|
196 |
+
}
|
197 |
+
|
198 |
+
protected function _removeTextareaCB($m)
|
199 |
+
{
|
200 |
+
return $this->_reservePlace("<textarea{$m[1]}");
|
201 |
+
}
|
202 |
+
|
203 |
+
protected function _removeStyleCB($m)
|
204 |
+
{
|
205 |
+
$openStyle = "<style{$m[1]}";
|
206 |
+
$css = $m[2];
|
207 |
+
// remove HTML comments
|
208 |
+
$css = preg_replace('/(?:^\\s*<!--|-->\\s*$)/', '', $css);
|
209 |
+
|
210 |
+
// remove CDATA section markers
|
211 |
+
$css = $this->_removeCdata($css);
|
212 |
+
|
213 |
+
// minify
|
214 |
+
$minifier = $this->_cssMinifier
|
215 |
+
? $this->_cssMinifier
|
216 |
+
: 'trim';
|
217 |
+
$css = call_user_func($minifier, $css);
|
218 |
+
|
219 |
+
return $this->_reservePlace($this->_needsCdata($css)
|
220 |
+
? "{$openStyle}/*<![CDATA[*/{$css}/*]]>*/</style>"
|
221 |
+
: "{$openStyle}{$css}</style>"
|
222 |
+
);
|
223 |
+
}
|
224 |
+
|
225 |
+
protected function _removeScriptCB($m)
|
226 |
+
{
|
227 |
+
$openScript = "<script{$m[2]}";
|
228 |
+
$js = $m[3];
|
229 |
+
|
230 |
+
// whitespace surrounding? preserve at least one space
|
231 |
+
$ws1 = ($m[1] === '') ? '' : ' ';
|
232 |
+
$ws2 = ($m[4] === '') ? '' : ' ';
|
233 |
+
|
234 |
+
// remove HTML comments (and ending "//" if present)
|
235 |
+
if ($this->_jsCleanComments) {
|
236 |
+
$js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js);
|
237 |
+
}
|
238 |
+
|
239 |
+
// remove CDATA section markers
|
240 |
+
$js = $this->_removeCdata($js);
|
241 |
+
|
242 |
+
// minify
|
243 |
+
$minifier = $this->_jsMinifier
|
244 |
+
? $this->_jsMinifier
|
245 |
+
: 'trim';
|
246 |
+
$js = call_user_func($minifier, $js);
|
247 |
+
|
248 |
+
return $this->_reservePlace($this->_needsCdata($js)
|
249 |
+
? "{$ws1}{$openScript}/*<![CDATA[*/{$js}/*]]>*/</script>{$ws2}"
|
250 |
+
: "{$ws1}{$openScript}{$js}</script>{$ws2}"
|
251 |
+
);
|
252 |
+
}
|
253 |
+
|
254 |
+
protected function _removeCdata($str)
|
255 |
+
{
|
256 |
+
return (false !== strpos($str, '<![CDATA['))
|
257 |
+
? str_replace(array('<![CDATA[', ']]>'), '', $str)
|
258 |
+
: $str;
|
259 |
+
}
|
260 |
+
|
261 |
+
protected function _needsCdata($str)
|
262 |
+
{
|
263 |
+
return ($this->_isXhtml && preg_match('/(?:[<&]|\\-\\-|\\]\\]>)/', $str));
|
264 |
+
}
|
265 |
+
}
|
lib/Minify/HTMLMax.php
CHANGED
@@ -16,6 +16,16 @@
|
|
16 |
* @package Minify
|
17 |
* @author Stephen Clay <steve@mrclay.org>
|
18 |
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
class Minify_HTMLMax {
|
20 |
/**
|
21 |
* @var boolean
|
16 |
* @package Minify
|
17 |
* @author Stephen Clay <steve@mrclay.org>
|
18 |
*/
|
19 |
+
|
20 |
+
/**
|
21 |
+
* Modified version of a class for max minification.
|
22 |
+
*
|
23 |
+
* @category Apptrian
|
24 |
+
* @package Apptrian_Minify
|
25 |
+
* @author Apptrian
|
26 |
+
* @copyright Copyright (c) 2014 Apptrian (http://www.apptrian.com)
|
27 |
+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
28 |
+
*/
|
29 |
class Minify_HTMLMax {
|
30 |
/**
|
31 |
* @var boolean
|
lib/Minify/HTMLMaxComp.php
ADDED
@@ -0,0 +1,268 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/**
|
3 |
+
* Class Minify_HTML
|
4 |
+
* @package Minify
|
5 |
+
*/
|
6 |
+
|
7 |
+
/**
|
8 |
+
* Compress HTML
|
9 |
+
*
|
10 |
+
* This is a heavy regex-based removal of whitespace, unnecessary comments and
|
11 |
+
* tokens. IE conditional comments are preserved. There are also options to have
|
12 |
+
* STYLE and SCRIPT blocks compressed by callback functions.
|
13 |
+
*
|
14 |
+
* A test suite is available.
|
15 |
+
*
|
16 |
+
* @package Minify
|
17 |
+
* @author Stephen Clay <steve@mrclay.org>
|
18 |
+
*/
|
19 |
+
|
20 |
+
/**
|
21 |
+
* Modified version of a class for max minification and compatibility with Varnish cache extensions for Magento.
|
22 |
+
*
|
23 |
+
* @category Apptrian
|
24 |
+
* @package Apptrian_Minify
|
25 |
+
* @author Apptrian
|
26 |
+
* @copyright Copyright (c) 2014 Apptrian (http://www.apptrian.com)
|
27 |
+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
28 |
+
*/
|
29 |
+
class Minify_HTMLMaxComp {
|
30 |
+
/**
|
31 |
+
* @var boolean
|
32 |
+
*/
|
33 |
+
protected $_jsCleanComments = true;
|
34 |
+
|
35 |
+
/**
|
36 |
+
* "Minify" an HTML page
|
37 |
+
*
|
38 |
+
* @param string $html
|
39 |
+
*
|
40 |
+
* @param array $options
|
41 |
+
*
|
42 |
+
* 'cssMinifier' : (optional) callback function to process content of STYLE
|
43 |
+
* elements.
|
44 |
+
*
|
45 |
+
* 'jsMinifier' : (optional) callback function to process content of SCRIPT
|
46 |
+
* elements. Note: the type attribute is ignored.
|
47 |
+
*
|
48 |
+
* 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
|
49 |
+
* unset, minify will sniff for an XHTML doctype.
|
50 |
+
*
|
51 |
+
* @return string
|
52 |
+
*/
|
53 |
+
public static function minify($html, $options = array()) {
|
54 |
+
$min = new self($html, $options);
|
55 |
+
return $min->process();
|
56 |
+
}
|
57 |
+
|
58 |
+
|
59 |
+
/**
|
60 |
+
* Create a minifier object
|
61 |
+
*
|
62 |
+
* @param string $html
|
63 |
+
*
|
64 |
+
* @param array $options
|
65 |
+
*
|
66 |
+
* 'cssMinifier' : (optional) callback function to process content of STYLE
|
67 |
+
* elements.
|
68 |
+
*
|
69 |
+
* 'jsMinifier' : (optional) callback function to process content of SCRIPT
|
70 |
+
* elements. Note: the type attribute is ignored.
|
71 |
+
*
|
72 |
+
* 'jsCleanComments' : (optional) whether to remove HTML comments beginning and end of script block
|
73 |
+
*
|
74 |
+
* 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
|
75 |
+
* unset, minify will sniff for an XHTML doctype.
|
76 |
+
*/
|
77 |
+
public function __construct($html, $options = array())
|
78 |
+
{
|
79 |
+
$this->_html = str_replace("\r\n", "\n", trim($html));
|
80 |
+
if (isset($options['xhtml'])) {
|
81 |
+
$this->_isXhtml = (bool)$options['xhtml'];
|
82 |
+
}
|
83 |
+
if (isset($options['cssMinifier'])) {
|
84 |
+
$this->_cssMinifier = $options['cssMinifier'];
|
85 |
+
}
|
86 |
+
if (isset($options['jsMinifier'])) {
|
87 |
+
$this->_jsMinifier = $options['jsMinifier'];
|
88 |
+
}
|
89 |
+
if (isset($options['jsCleanComments'])) {
|
90 |
+
$this->_jsCleanComments = (bool)$options['jsCleanComments'];
|
91 |
+
}
|
92 |
+
}
|
93 |
+
|
94 |
+
|
95 |
+
/**
|
96 |
+
* Minify the markeup given in the constructor
|
97 |
+
*
|
98 |
+
* @return string
|
99 |
+
*/
|
100 |
+
public function process()
|
101 |
+
{
|
102 |
+
if ($this->_isXhtml === null) {
|
103 |
+
$this->_isXhtml = (false !== strpos($this->_html, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML'));
|
104 |
+
}
|
105 |
+
|
106 |
+
$this->_replacementHash = 'MINIFYHTML' . md5($_SERVER['REQUEST_TIME']);
|
107 |
+
$this->_placeholders = array();
|
108 |
+
|
109 |
+
// replace SCRIPTs (and minify) with placeholders
|
110 |
+
$this->_html = preg_replace_callback(
|
111 |
+
'/(\\s*)<script(\\b[^>]*?>)([\\s\\S]*?)<\\/script>(\\s*)/i'
|
112 |
+
,array($this, '_removeScriptCB')
|
113 |
+
,$this->_html);
|
114 |
+
|
115 |
+
// replace STYLEs (and minify) with placeholders
|
116 |
+
$this->_html = preg_replace_callback(
|
117 |
+
'/\\s*<style(\\b[^>]*>)([\\s\\S]*?)<\\/style>\\s*/i'
|
118 |
+
,array($this, '_removeStyleCB')
|
119 |
+
,$this->_html);
|
120 |
+
|
121 |
+
// remove HTML comments (not containing IE conditional comments).
|
122 |
+
$this->_html = preg_replace_callback(
|
123 |
+
'/<!--([\\s\\S]*?)-->/'
|
124 |
+
,array($this, '_commentCB')
|
125 |
+
,$this->_html);
|
126 |
+
|
127 |
+
// replace PREs with placeholders
|
128 |
+
$this->_html = preg_replace_callback('/\\s*<pre(\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i'
|
129 |
+
,array($this, '_removePreCB')
|
130 |
+
,$this->_html);
|
131 |
+
|
132 |
+
// replace TEXTAREAs with placeholders
|
133 |
+
$this->_html = preg_replace_callback(
|
134 |
+
'/\\s*<textarea(\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i'
|
135 |
+
,array($this, '_removeTextareaCB')
|
136 |
+
,$this->_html);
|
137 |
+
|
138 |
+
// trim each line.
|
139 |
+
// @todo take into account attribute values that span multiple lines.
|
140 |
+
$this->_html = preg_replace('/^\\s+|\\s+$/m', '', $this->_html);
|
141 |
+
|
142 |
+
// remove ws around block/undisplayed elements
|
143 |
+
$this->_html = preg_replace('/\\s+(<\\/?(?:area|base(?:font)?|blockquote|body'
|
144 |
+
.'|caption|center|col(?:group)?|dd|dir|div|dl|dt|fieldset|form'
|
145 |
+
.'|frame(?:set)?|h[1-6]|head|hr|html|legend|li|link|map|menu|meta'
|
146 |
+
.'|ol|opt(?:group|ion)|p|param|t(?:able|body|head|d|h||r|foot|itle)'
|
147 |
+
.'|ul)\\b[^>]*>)/i', '$1', $this->_html);
|
148 |
+
|
149 |
+
// remove ws outside of all elements
|
150 |
+
$this->_html = preg_replace(
|
151 |
+
'/>(\\s(?:\\s*))?([^<]+)(\\s(?:\s*))?</'
|
152 |
+
,'>$1$2$3<'
|
153 |
+
,$this->_html);
|
154 |
+
|
155 |
+
// use newlines before 1st attribute in open tags (to limit line lengths)
|
156 |
+
//$this->_html = preg_replace('/(<[a-z\\-]+)\\s+([^>]+>)/i', "$1\n$2", $this->_html);
|
157 |
+
|
158 |
+
// Strip all multiple spaces to one space
|
159 |
+
$this->_html = preg_replace('/\s+/ui', ' ', $this->_html);
|
160 |
+
|
161 |
+
// fill placeholders
|
162 |
+
$this->_html = str_replace(
|
163 |
+
array_keys($this->_placeholders)
|
164 |
+
,array_values($this->_placeholders)
|
165 |
+
,$this->_html
|
166 |
+
);
|
167 |
+
// issue 229: multi-pass to catch scripts that didn't get replaced in textareas
|
168 |
+
$this->_html = str_replace(
|
169 |
+
array_keys($this->_placeholders)
|
170 |
+
,array_values($this->_placeholders)
|
171 |
+
,$this->_html
|
172 |
+
);
|
173 |
+
return $this->_html;
|
174 |
+
}
|
175 |
+
|
176 |
+
protected function _commentCB($m)
|
177 |
+
{
|
178 |
+
return (0 === strpos($m[1], '[') || false !== strpos($m[1], '<![') || false !== stripos($m[1], 'esi <'))
|
179 |
+
? $m[0]
|
180 |
+
: '';
|
181 |
+
}
|
182 |
+
|
183 |
+
protected function _reservePlace($content)
|
184 |
+
{
|
185 |
+
$placeholder = '%' . $this->_replacementHash . count($this->_placeholders) . '%';
|
186 |
+
$this->_placeholders[$placeholder] = $content;
|
187 |
+
return $placeholder;
|
188 |
+
}
|
189 |
+
|
190 |
+
protected $_isXhtml = null;
|
191 |
+
protected $_replacementHash = null;
|
192 |
+
protected $_placeholders = array();
|
193 |
+
protected $_cssMinifier = null;
|
194 |
+
protected $_jsMinifier = null;
|
195 |
+
|
196 |
+
protected function _removePreCB($m)
|
197 |
+
{
|
198 |
+
return $this->_reservePlace("<pre{$m[1]}");
|
199 |
+
}
|
200 |
+
|
201 |
+
protected function _removeTextareaCB($m)
|
202 |
+
{
|
203 |
+
return $this->_reservePlace("<textarea{$m[1]}");
|
204 |
+
}
|
205 |
+
|
206 |
+
protected function _removeStyleCB($m)
|
207 |
+
{
|
208 |
+
$openStyle = "<style{$m[1]}";
|
209 |
+
$css = $m[2];
|
210 |
+
// remove HTML comments
|
211 |
+
$css = preg_replace('/(?:^\\s*<!--|-->\\s*$)/', '', $css);
|
212 |
+
|
213 |
+
// remove CDATA section markers
|
214 |
+
$css = $this->_removeCdata($css);
|
215 |
+
|
216 |
+
// minify
|
217 |
+
$minifier = $this->_cssMinifier
|
218 |
+
? $this->_cssMinifier
|
219 |
+
: 'trim';
|
220 |
+
$css = call_user_func($minifier, $css);
|
221 |
+
|
222 |
+
return $this->_reservePlace($this->_needsCdata($css)
|
223 |
+
? "{$openStyle}/*<![CDATA[*/{$css}/*]]>*/</style>"
|
224 |
+
: "{$openStyle}{$css}</style>"
|
225 |
+
);
|
226 |
+
}
|
227 |
+
|
228 |
+
protected function _removeScriptCB($m)
|
229 |
+
{
|
230 |
+
$openScript = "<script{$m[2]}";
|
231 |
+
$js = $m[3];
|
232 |
+
|
233 |
+
// whitespace surrounding? preserve at least one space
|
234 |
+
$ws1 = ($m[1] === '') ? '' : ' ';
|
235 |
+
$ws2 = ($m[4] === '') ? '' : ' ';
|
236 |
+
|
237 |
+
// remove HTML comments (and ending "//" if present)
|
238 |
+
if ($this->_jsCleanComments) {
|
239 |
+
$js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js);
|
240 |
+
}
|
241 |
+
|
242 |
+
// remove CDATA section markers
|
243 |
+
$js = $this->_removeCdata($js);
|
244 |
+
|
245 |
+
// minify
|
246 |
+
$minifier = $this->_jsMinifier
|
247 |
+
? $this->_jsMinifier
|
248 |
+
: 'trim';
|
249 |
+
$js = call_user_func($minifier, $js);
|
250 |
+
|
251 |
+
return $this->_reservePlace($this->_needsCdata($js)
|
252 |
+
? "{$ws1}{$openScript}/*<![CDATA[*/{$js}/*]]>*/</script>{$ws2}"
|
253 |
+
: "{$ws1}{$openScript}{$js}</script>{$ws2}"
|
254 |
+
);
|
255 |
+
}
|
256 |
+
|
257 |
+
protected function _removeCdata($str)
|
258 |
+
{
|
259 |
+
return (false !== strpos($str, '<![CDATA['))
|
260 |
+
? str_replace(array('<![CDATA[', ']]>'), '', $str)
|
261 |
+
: $str;
|
262 |
+
}
|
263 |
+
|
264 |
+
protected function _needsCdata($str)
|
265 |
+
{
|
266 |
+
return ($this->_isXhtml && preg_match('/(?:[<&]|\\-\\-|\\]\\]>)/', $str));
|
267 |
+
}
|
268 |
+
}
|
package.xml
CHANGED
@@ -1,19 +1,18 @@
|
|
1 |
<?xml version="1.0"?>
|
2 |
<package>
|
3 |
<name>Apptrian_Minify_HTML_CSS_JS</name>
|
4 |
-
<version>1.1.
|
5 |
<stability>stable</stability>
|
6 |
<license uri="http://opensource.org/licenses/osl-3.0.php">Open Software License (OSL 3.0)</license>
|
7 |
<channel>community</channel>
|
8 |
<extends/>
|
9 |
<summary>Minify HTML CSS JS including inline CSS/JS and speed up you site. Works with default Magento CSS/JS merger.</summary>
|
10 |
-
<description>Apptrian Minify HTML CSS JS is a very small and efficient extension. It will minify HTML including inline CSS and JS code. Minification of CSS and JS files is compatible with default Magento CSS and JS file merger. There are no complex setups nor query strings on minified CSS and JS files. Extension is very easy to install and use.</description>
|
11 |
-
<notes>+
|
12 |
-
- Removed "min-" prefix from minified file names</notes>
|
13 |
<authors><author><name>Apptrian</name><user>apptrian</user><email>apptrian@yahoo.com</email></author></authors>
|
14 |
-
<date>2014-
|
15 |
-
<time>
|
16 |
-
<contents><target name="magecommunity"><dir name="Apptrian"><dir name="Minify"><dir name="Block"><file name="About.php" hash="8cc6cc29904965c491d6015da85c625e"/><file name="Info.php" hash="3b40e6044fe201a83ffeac9aecc178dd"/><dir name="Page"><dir name="Html"><file name="Head.php" hash="1cec12e35718f70a6f1324441d9cc706"/></dir></dir></dir><dir name="Helper"><file name="Data.php" hash="42afca96bb847cb1df7e5d083b50fadd"/></dir><dir name="Model"><file name="Cron.php" hash="ba54b94e4ac5a5cbaba96a6e1e9ceee0"/><file name="Observer.php" hash="
|
17 |
<compatible/>
|
18 |
<dependencies><required><php><min>5.1.0</min><max>6.0.0</max></php></required></dependencies>
|
19 |
</package>
|
1 |
<?xml version="1.0"?>
|
2 |
<package>
|
3 |
<name>Apptrian_Minify_HTML_CSS_JS</name>
|
4 |
+
<version>1.1.1</version>
|
5 |
<stability>stable</stability>
|
6 |
<license uri="http://opensource.org/licenses/osl-3.0.php">Open Software License (OSL 3.0)</license>
|
7 |
<channel>community</channel>
|
8 |
<extends/>
|
9 |
<summary>Minify HTML CSS JS including inline CSS/JS and speed up you site. Works with default Magento CSS/JS merger.</summary>
|
10 |
+
<description>Apptrian Minify HTML CSS JS is a very small and efficient extension. It will minify HTML including inline CSS and JS code. Minification of CSS and JS files is compatible with default Magento CSS and JS file merger. There are no complex setups nor query strings on minified CSS and JS files. Extension is very easy to install and use. Compatible with Varnish cache extensions.</description>
|
11 |
+
<notes>+ Compatibility with Varnish cache extensions</notes>
|
|
|
12 |
<authors><author><name>Apptrian</name><user>apptrian</user><email>apptrian@yahoo.com</email></author></authors>
|
13 |
+
<date>2014-12-02</date>
|
14 |
+
<time>16:07:34</time>
|
15 |
+
<contents><target name="magecommunity"><dir name="Apptrian"><dir name="Minify"><dir name="Block"><file name="About.php" hash="8cc6cc29904965c491d6015da85c625e"/><file name="Info.php" hash="3b40e6044fe201a83ffeac9aecc178dd"/><dir name="Page"><dir name="Html"><file name="Head.php" hash="1cec12e35718f70a6f1324441d9cc706"/></dir></dir></dir><dir name="Helper"><file name="Data.php" hash="42afca96bb847cb1df7e5d083b50fadd"/></dir><dir name="Model"><file name="Cron.php" hash="ba54b94e4ac5a5cbaba96a6e1e9ceee0"/><file name="Observer.php" hash="2a04f64a803451807e67f6be038d5303"/></dir><dir name="etc"><file name="config.xml" hash="1829ce0b8241739a07e4fffce30ca74f"/><file name="system.xml" hash="635519951c5f200688922c82c91e2563"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Apptrian_Minify.xml" hash="808e3f53c0c944fffaf66d1f5fa1506f"/></dir></target><target name="magelocale"><dir name="en_US"><file name="Apptrian_Minify.csv" hash="b22fa3b5e1d10c541f273b9638352020"/></dir></target><target name="magelib"><dir name="HTTP"><file name="ConditionalGet.php" hash="84d5f4e0b97e37228310a31a4bdc1240"/><file name="Encoder.php" hash="d697f04651d6363d1dd5dd8c2ce94cbd"/></dir><dir name="Minify"><file name="Build.php" hash="99800ab664e1fb0ff46a7133ab13bb1b"/><dir name="CSS"><file name="Compressor.php" hash="6f1d5d8c3f7ac47cabaca9d6ee923206"/><file name="UriRewriter.php" hash="d6c800c1b9b0603c3386f84074bc41a0"/></dir><file name="CSS.php" hash="c585f82e0a6f8af12da2d3337aa7e901"/><file name="CSSmin.php" hash="7e0549e63f49b99aac0f6688bfcf99ef"/><dir name="Cache"><file name="APC.php" hash="f83c096f1ea3eb712bf020371b7d3755"/><file name="File.php" hash="5550b7ad5d3cc20710a0835fdf20e3ec"/><file name="Memcache.php" hash="a572e300270f5cdb535de67eb1d9ce3b"/><file name="WinCache.php" hash="037003b972c8f648880b43b2c912afb7"/><file name="XCache.php" hash="a3239040e672955a8a520eef6e251d68"/><file name="ZendPlatform.php" hash="a20755bc554cfef6733ac64574c4f90e"/></dir><file name="ClosureCompiler.php" hash="0d04a74dae339bd8d2d0465503590819"/><file name="CommentPreserver.php" hash="f9fbae74aea3125d25f375942846d9c0"/><dir name="Controller"><file name="Base.php" hash="756cd27141da15f6bc0a9d34960e0338"/><file name="Files.php" hash="a0bc419aa48d256e19a64596dee53991"/><file name="Groups.php" hash="bf35ccd3d384c40033b978a402eaf917"/><file name="MinApp.php" hash="0910bc4af5280098b8411a8a23aa3e91"/><file name="Page.php" hash="c54a4b19474f9d0e9beb8ccb4e3678b3"/><file name="Version1.php" hash="0e4bce53a0b66c79aa6d15e52cf8ec50"/></dir><file name="DebugDetector.php" hash="d1bee7f6ab4dcf7be5d36dbcd1b81354"/><dir name="HTML"><file name="Helper.php" hash="af89e2e30f70dd935dd86d31649159e6"/></dir><file name="HTML.php" hash="d48eabaae177099a264b5a97379b0abf"/><file name="HTMLComp.php" hash="c8f6ccd147f1bfdc9aee4c03c3034101"/><file name="HTMLMax.php" hash="02b023eb766a72ed0a7a991a16347ed6"/><file name="HTMLMaxComp.php" hash="038b636ad88868b49d983390ca59c73f"/><file name="ImportProcessor.php" hash="075c561afa4825021fa44c3cac68ab94"/><dir name="JS"><file name="ClosureCompiler.php" hash="0d6d0017c3b9decdf0e1f647c0b53f17"/></dir><file name="Lines.php" hash="526499d43d682432dac9483da4509179"/><file name="Loader.php" hash="5e84b0e739587d8df742f47953f135d5"/><file name="Logger.php" hash="ee493543ebb47aa06f976f23a0f3d86a"/><file name="Packer.php" hash="e91f63a82c4fbd660c2341163e87dd4b"/><file name="Source.php" hash="f7055f963f00e5ae9f5b0005d5e4a5ec"/><dir name="YUI"><file name="CssCompressor.java" hash="cb15a586f2dcc4fead535bc982e3f91e"/><file name="CssCompressor.php" hash="12d5b4e38488c68bc84cc0efba6eb338"/></dir><file name="YUICompressor.php" hash="3ada081677d0cf118bba1f4b533b97cf"/></dir><dir name="."><file name="CSSmin.php" hash="b88ddd36d0ff681aa8a221467c0c71c1"/><file name="DooDigestAuth.php" hash="9d66abc8cfa37b5f593fc09c734f0269"/><file name="FirePHP.php" hash="f619b5a77fee4b21e4397e98d858fbf4"/><file name="JavaScriptPacker.php" hash="84900da372a375d3b2f117c5abe740a9"/><file name="JSMin.php" hash="63ada69cc753fe4136c67e1d3daabca3"/><file name="JSMinPlus.php" hash="13d47b54dd73ef825e5d86e6cc633e32"/><file name="Minify.php" hash="df50518e69c132b1354eb8ba73a8ed7c"/></dir></target></contents>
|
16 |
<compatible/>
|
17 |
<dependencies><required><php><min>5.1.0</min><max>6.0.0</max></php></required></dependencies>
|
18 |
</package>
|