PluginProbe
Autoptimize / 1.8.2
Autoptimize v1.8.2
2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 All 107 releases
← All changes | classes/external/php/minify-html.php +65 -67 2.4.41.8.2 View file →
@@ -1,7 +1,7 @@
1 1 <?php
2 2 /**
3 - * Class Minify_HTML
3 + * Class Minify_HTML
4 4 * @package Minify
5 5 */
6 6
7 7 /**
@@ -6,14 +6,14 @@
6 6
7 7 /**
8 8 * Compress HTML
9 9 *
10 - * This is a heavy regex-based removal of whitespace, unnecessary comments and
10 + * This is a heavy regex-based removal of whitespace, unnecessary comments and
11 11 * tokens. IE conditional comments are preserved. There are also options to have
12 - * STYLE and SCRIPT blocks compressed by callback functions.
13 - *
12 + * STYLE and SCRIPT blocks compressed by callback functions.
13 + *
14 14 * A test suite is available.
15 - *
15 + *
16 16 * @package Minify
17 17 * @author Stephen Clay <steve@mrclay.org>
18 18 */
19 19 class Minify_HTML {
@@ -26,18 +26,18 @@
26 26 * @param array $options
27 27 *
28 28 * 'cssMinifier' : (optional) callback function to process content of STYLE
29 29 * elements.
30 - *
30 + *
31 31 * 'jsMinifier' : (optional) callback function to process content of SCRIPT
32 32 * elements. Note: the type attribute is ignored.
33 - *
33 + *
34 34 * 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
35 35 * unset, minify will sniff for an XHTML doctype.
36 - *
36 + *
37 37 * 'keepComments' : (optional boolean) should the HTML comments be kept
38 38 * in the HTML Code?
39 - *
39 + *
40 40 * @return string
41 41 */
42 42 public static function minify($html, $options = array()) {
43 43 $min = new Minify_HTML($html, $options);
@@ -42,10 +42,10 @@
42 42 public static function minify($html, $options = array()) {
43 43 $min = new Minify_HTML($html, $options);
44 44 return $min->process();
45 45 }
46 -
47 -
46 +
47 +
48 48 /**
49 49 * Create a minifier object
50 50 *
51 51 * @param string $html
@@ -53,18 +53,18 @@
53 53 * @param array $options
54 54 *
55 55 * 'cssMinifier' : (optional) callback function to process content of STYLE
56 56 * elements.
57 - *
57 + *
58 58 * 'jsMinifier' : (optional) callback function to process content of SCRIPT
59 59 * elements. Note: the type attribute is ignored.
60 - *
60 + *
61 61 * 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
62 62 * unset, minify will sniff for an XHTML doctype.
63 - *
63 + *
64 64 * 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
65 65 * unset, minify will sniff for an XHTML doctype.
66 - *
66 + *
67 67 * @return null
68 68 */
69 69 public function __construct($html, $options = array())
70 70 {
@@ -81,13 +81,13 @@
81 81 if (isset($options['keepComments'])) {
82 82 $this->_keepComments = $options['keepComments'];
83 83 }
84 84 }
85 -
86 -
85 +
86 +
87 87 /**
88 88 * Minify the markeup given in the constructor
89 - *
89 + *
90 90 * @return string
91 91 */
92 92 public function process()
93 93 {
@@ -93,69 +93,69 @@
93 93 {
94 94 if ($this->_isXhtml === null) {
95 95 $this->_isXhtml = (false !== strpos($this->_html, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML'));
96 96 }
97 -
97 +
98 98 $this->_replacementHash = 'MINIFYHTML' . md5($_SERVER['REQUEST_TIME']);
99 99 $this->_placeholders = array();
100 -
100 +
101 101 // replace SCRIPTs (and minify) with placeholders
102 102 $this->_html = preg_replace_callback(
103 103 '/(\\s*)(<script\\b[^>]*?>)([\\s\\S]*?)<\\/script>(\\s*)/i'
104 104 ,array($this, '_removeScriptCB')
105 105 ,$this->_html);
106 -
106 +
107 107 // replace STYLEs (and minify) with placeholders
108 108 $this->_html = preg_replace_callback(
109 109 '/\\s*(<style\\b[^>]*?>)([\\s\\S]*?)<\\/style>\\s*/i'
110 110 ,array($this, '_removeStyleCB')
111 111 ,$this->_html);
112 -
112 +
113 113 // remove HTML comments (not containing IE conditional comments).
114 - if ($this->_keepComments == false) {
115 - $this->_html = preg_replace_callback(
116 - '/<!--([\\s\\S]*?)-->/'
117 - ,array($this, '_commentCB')
118 - ,$this->_html);
114 + if ($this->_keepComments == false) {
115 + $this->_html = preg_replace_callback(
116 + '/<!--([\\s\\S]*?)-->/'
117 + ,array($this, '_commentCB')
118 + ,$this->_html);
119 119 }
120 -
120 +
121 121 // replace PREs with placeholders
122 122 $this->_html = preg_replace_callback('/\\s*(<pre\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i'
123 123 ,array($this, '_removePreCB')
124 124 ,$this->_html);
125 -
125 +
126 126 // replace TEXTAREAs with placeholders
127 127 $this->_html = preg_replace_callback(
128 128 '/\\s*(<textarea\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i'
129 129 ,array($this, '_removeTextareaCB')
130 130 ,$this->_html);
131 -
132 - // replace data: URIs with placeholders
131 +
132 + // replace data: URIs with placeholders
133 133 $this->_html = preg_replace_callback(
134 134 '/(=("|\')data:.*\\2)/Ui'
135 135 ,array($this, '_removeDataURICB')
136 136 ,$this->_html);
137 -
137 +
138 138 // trim each line.
139 - // replace by space instead of '' to avoid newline after opening tag getting zapped
140 - $this->_html = preg_replace('/^\s+|\s+$/m', ' ', $this->_html);
141 -
139 + // @todo take into account attribute values that span multiple lines.
140 + $this->_html = preg_replace('/^\\s+|\\s+$/m', '', $this->_html);
141 +
142 142 // remove ws around block/undisplayed elements
143 - $this->_html = preg_replace('/\\s+(<\\/?(?:area|article|aside|base(?:font)?|blockquote|body'
144 - .'|canvas|caption|center|col(?:group)?|dd|dir|div|dl|dt|fieldset|figcaption|figure|footer|form'
145 - .'|frame(?:set)?|h[1-6]|head|header|hgroup|hr|html|legend|li|link|main|map|menu|meta|nav'
146 - .'|ol|opt(?:group|ion)|output|p|param|section|t(?:able|body|head|d|h||r|foot|itle)'
147 - .'|ul|video)\\b[^>]*>)/i', '$1', $this->_html);
148 -
143 + $this->_html = preg_replace('/\\s+(<\\/?(?:area|base(?:font)?|blockquote|body'
144 + .'|caption|center|cite|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 149 // remove ws outside of all elements
150 150 $this->_html = preg_replace_callback(
151 151 '/>([^<]+)</'
152 152 ,array($this, '_outsideTagCB')
153 153 ,$this->_html);
154 -
154 +
155 155 // use newlines before 1st attribute in open tags (to limit line lengths)
156 156 //$this->_html = preg_replace('/(<[a-z\\-]+)\\s+([^>]+>)/i', "$1\n$2", $this->_html);
157 -
157 +
158 158 // fill placeholders
159 159 $this->_html = str_replace(
160 160 array_keys($this->_placeholders)
161 161 ,array_values($this->_placeholders)
@@ -162,9 +162,9 @@
162 162 ,$this->_html
163 163 );
164 164 return $this->_html;
165 165 }
166 -
166 +
167 167 protected function _commentCB($m)
168 168 {
169 169 return (0 === strpos($m[1], '[') || false !== strpos($m[1], '<!['))
170 170 ? $m[0]
@@ -169,9 +169,9 @@
169 169 return (0 === strpos($m[1], '[') || false !== strpos($m[1], '<!['))
170 170 ? $m[0]
171 171 : '';
172 172 }
173 -
173 +
174 174 protected function _reservePlace($content)
175 175 {
176 176 $placeholder = '%' . $this->_replacementHash . count($this->_placeholders) . '%';
177 177 $this->_placeholders[$placeholder] = $content;
@@ -182,26 +182,26 @@
182 182 protected $_replacementHash = null;
183 183 protected $_placeholders = array();
184 184 protected $_cssMinifier = null;
185 185 protected $_jsMinifier = null;
186 - protected $_keepComments = false;
186 + protected $_keepComments = false;
187 187
188 188 protected function _outsideTagCB($m)
189 189 {
190 190 return '>' . preg_replace('/^\\s+|\\s+$/', ' ', $m[1]) . '<';
191 191 }
192 -
192 +
193 193 protected function _removePreCB($m)
194 194 {
195 195 return $this->_reservePlace($m[1]);
196 196 }
197 -
197 +
198 198 protected function _removeTextareaCB($m)
199 199 {
200 200 return $this->_reservePlace($m[1]);
201 201 }
202 -
203 - protected function _removeDataURICB($m)
202 +
203 + protected function _removeDataURICB($m)
204 204 {
205 205 return $this->_reservePlace($m[1]);
206 206 }
207 207
@@ -210,18 +210,18 @@
210 210 $openStyle = $m[1];
211 211 $css = $m[2];
212 212 // remove HTML comments
213 213 $css = preg_replace('/(?:^\\s*<!--|-->\\s*$)/', '', $css);
214 -
214 +
215 215 // remove CDATA section markers
216 216 $css = $this->_removeCdata($css);
217 -
217 +
218 218 // minify
219 219 $minifier = $this->_cssMinifier
220 220 ? $this->_cssMinifier
221 221 : 'trim';
222 222 $css = call_user_func($minifier, $css);
223 -
223 +
224 224 return $this->_reservePlace($this->_needsCdata($css)
225 225 ? "{$openStyle}/*<![CDATA[*/{$css}/*]]>*/</style>"
226 226 : "{$openStyle}{$css}</style>"
227 227 );
@@ -230,27 +230,25 @@
230 230 protected function _removeScriptCB($m)
231 231 {
232 232 $openScript = $m[2];
233 233 $js = $m[3];
234 -
234 +
235 235 // whitespace surrounding? preserve at least one space
236 236 $ws1 = ($m[1] === '') ? '' : ' ';
237 237 $ws2 = ($m[4] === '') ? '' : ' ';
238 -
239 - if ($this->_keepComments == false) {
240 - // remove HTML comments (and ending "//" if present)
241 - $js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js);
242 -
243 - // remove CDATA section markers
244 - $js = $this->_removeCdata($js);
245 - }
246 -
238 +
239 + // remove HTML comments (and ending "//" if present)
240 + $js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js);
241 +
242 + // remove CDATA section markers
243 + $js = $this->_removeCdata($js);
244 +
247 245 // minify
248 246 $minifier = $this->_jsMinifier
249 247 ? $this->_jsMinifier
250 - : 'trim';
248 + : 'trim';
251 249 $js = call_user_func($minifier, $js);
252 -
250 +
253 251 return $this->_reservePlace($this->_needsCdata($js)
254 252 ? "{$ws1}{$openScript}/*<![CDATA[*/{$js}/*]]>*/</script>{$ws2}"
255 253 : "{$ws1}{$openScript}{$js}</script>{$ws2}"
256 254 );
@@ -258,12 +256,12 @@
258 256
259 257 protected function _removeCdata($str)
260 258 {
261 259 return (false !== strpos($str, '<![CDATA['))
262 - ? str_replace(array('/* <![CDATA[ */','/* ]]> */','/*<![CDATA[*/','/*]]>*/','<![CDATA[', ']]>'), '', $str)
260 + ? str_replace(array('<![CDATA[', ']]>'), '', $str)
263 261 : $str;
264 262 }
265 -
263 +
266 264 protected function _needsCdata($str)
267 265 {
268 266 return ($this->_isXhtml && preg_match('/(?:[<&]|\\-\\-|\\]\\]>)/', $str));
269 267 }