4cb01aab42a6c345d3a0768522c3465b806fd0c3
[ptitvelo/web/www.git] / www / plugins-dist / compresseur / lib / minify_html / class.minify_html.php
1 <?php
2
3 /**
4 * Class Minify_HTML
5 * @package Minify
6 */
7
8
9 /**
10 * Surcharge du minifieur HTML
11 *
12 * Surcharge pour ne pas manger les commentaires <!--extra-->
13 * qui servent parfois aux plugins, et parfois même après
14 * le passage du compacteur HTML
15 *
16 * C'était le cas du bouton statistiques du formulaire admin par exemple
17 *
18 * On permet de conserver également tout commentaire commençant par <!--keepme: -->
19 *
20 * @package SPIP\Compresseur\Minifieur
21 **/
22 class Minify_HTML_SPIP extends Minify_HTML {
23
24 /**
25 * {@inheritdoc}
26 *
27 *
28 * @param string $html
29 * HTML à minifier
30 * @param array $options
31 * Tableau d'option avec les index possibles
32 * - 'cssMinifier' : (optional) callback function to process content of STYLE
33 * elements.
34 * - 'jsMinifier' : (optional) callback function to process content of SCRIPT
35 * elements. Note: the type attribute is ignored.
36 * - 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
37 * unset, minify will sniff for an XHTML doctype.
38 * @return string
39 * HTML minifié
40 **/
41 public static function minify($html, $options = array()) {
42 $min = new Minify_HTML_SPIP($html, $options);
43 return $min->process();
44 }
45
46 /**
47 * Minification des commentaires
48 *
49 * Le cas <!--extra--> doit être conservé dans les commentaires,
50 * tout comme <!--keepme: xxx -->
51 *
52 * @param array $m Matches du preg_match d'un commentaire HTML
53 * @return string Contenu minifié
54 */
55 protected function _commentCB($m)
56 {
57 if ($m[1] === 'extra') return $m[0];
58 if ($m[1] AND $m[1][0] === 'k' AND substr($m[1],0,7) === 'keepme:') return $m[0];
59 return parent::_commentCB($m);
60 }
61 }
62
63
64 /**
65 * Compress HTML
66 *
67 * This is a heavy regex-based removal of whitespace, unnecessary comments and
68 * tokens. IE conditional comments are preserved. There are also options to have
69 * STYLE and SCRIPT blocks compressed by callback functions.
70 *
71 * A test suite is available.
72 * http://code.google.com/p/minify/source/browse/trunk/min/lib/Minify/HTML.php
73 *
74 * @package Minify
75 * @author Stephen Clay <steve@mrclay.org>
76 */
77 class Minify_HTML {
78
79 /**
80 * "Minify" an HTML page
81 *
82 * @param string $html
83 *
84 * @param array $options
85 *
86 * 'cssMinifier' : (optional) callback function to process content of STYLE
87 * elements.
88 *
89 * 'jsMinifier' : (optional) callback function to process content of SCRIPT
90 * elements. Note: the type attribute is ignored.
91 *
92 * 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
93 * unset, minify will sniff for an XHTML doctype.
94 *
95 * @return string
96 */
97 public static function minify($html, $options = array()) {
98 $min = new Minify_HTML($html, $options);
99 return $min->process();
100 }
101
102
103 /**
104 * Create a minifier object
105 *
106 * @param string $html
107 *
108 * @param array $options
109 *
110 * 'cssMinifier' : (optional) callback function to process content of STYLE
111 * elements.
112 *
113 * 'jsMinifier' : (optional) callback function to process content of SCRIPT
114 * elements. Note: the type attribute is ignored.
115 *
116 * 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
117 * unset, minify will sniff for an XHTML doctype.
118 *
119 * @return null
120 */
121 public function __construct($html, $options = array())
122 {
123 $this->_html = str_replace("\r\n", "\n", trim($html));
124 if (isset($options['xhtml'])) {
125 $this->_isXhtml = (bool)$options['xhtml'];
126 }
127 if (isset($options['cssMinifier'])) {
128 $this->_cssMinifier = $options['cssMinifier'];
129 }
130 if (isset($options['jsMinifier'])) {
131 $this->_jsMinifier = $options['jsMinifier'];
132 }
133 }
134
135
136 /**
137 * Minify the markeup given in the constructor
138 *
139 * @return string
140 */
141 public function process()
142 {
143 if ($this->_isXhtml === null) {
144 $this->_isXhtml = (false !== strpos($this->_html, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML'));
145 }
146
147 $this->_replacementHash = 'MINIFYHTML' . md5($_SERVER['REQUEST_TIME']);
148 $this->_placeholders = array();
149
150 // replace SCRIPTs (and minify) with placeholders
151 $this->_html = preg_replace_callback(
152 '/(\\s*)(<script\\b[^>]*?>)([\\s\\S]*?)<\\/script>(\\s*)/i'
153 ,array($this, '_removeScriptCB')
154 ,$this->_html);
155
156 // replace STYLEs (and minify) with placeholders
157 $this->_html = preg_replace_callback(
158 '/\\s*(<style\\b[^>]*?>)([\\s\\S]*?)<\\/style>\\s*/i'
159 ,array($this, '_removeStyleCB')
160 ,$this->_html);
161
162 // remove HTML comments (not containing IE conditional comments).
163 $this->_html = preg_replace_callback(
164 '/<!--([\\s\\S]*?)-->/'
165 ,array($this, '_commentCB')
166 ,$this->_html);
167
168 // replace PREs with placeholders
169 $this->_html = preg_replace_callback('/\\s*(<pre\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i'
170 ,array($this, '_removePreCB')
171 ,$this->_html);
172
173 // replace TEXTAREAs with placeholders
174 $this->_html = preg_replace_callback(
175 '/\\s*(<textarea\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i'
176 ,array($this, '_removeTextareaCB')
177 ,$this->_html);
178
179 // trim each line.
180 // @todo take into account attribute values that span multiple lines.
181 // 2 regexp because merging un /^\\s+|\\s+$/m also del a lot of newline chars ???
182 $this->_html = preg_replace('/\\s+$/m', '', $this->_html);
183 $this->_html = preg_replace('/^\\s+/m', '', $this->_html);
184
185 // remove ws around block/undisplayed elements
186 $this->_html = preg_replace('/\\s+(<\\/?(?:area|base(?:font)?|blockquote|body'
187 .'|caption|center|cite|col(?:group)?|dd|dir|div|dl|dt|fieldset|form'
188 .'|frame(?:set)?|h[1-6]|head|hr|html|legend|li|link|map|menu|meta'
189 .'|ol|opt(?:group|ion)|p|param|t(?:able|body|head|d|h||r|foot|itle)'
190 .'|ul)\\b[^>]*>)/i', '$1', $this->_html);
191
192 // remove ws outside of all elements
193 $this->_html = preg_replace_callback(
194 '/>([^<]+)</'
195 ,array($this, '_outsideTagCB')
196 ,$this->_html);
197
198 // use newlines before 1st attribute in open tags (to limit line lengths)
199 $this->_html = preg_replace('/(<[a-z\\-]+)\\s+([^>]+>)/i', "$1\n$2", $this->_html);
200
201 // fill placeholders
202 $this->_html = str_replace(
203 array_keys($this->_placeholders)
204 ,array_values($this->_placeholders)
205 ,$this->_html
206 );
207 return $this->_html;
208 }
209
210 protected function _commentCB($m)
211 {
212 return (0 === strpos($m[1], '[') || false !== strpos($m[1], '<!['))
213 ? $m[0]
214 : '';
215 }
216
217 protected function _reservePlace($content)
218 {
219 $placeholder = '%' . $this->_replacementHash . count($this->_placeholders) . '%';
220 $this->_placeholders[$placeholder] = $content;
221 return $placeholder;
222 }
223
224 protected $_isXhtml = null;
225 protected $_replacementHash = null;
226 protected $_placeholders = array();
227 protected $_cssMinifier = null;
228 protected $_jsMinifier = null;
229
230 protected function _outsideTagCB($m)
231 {
232 return '>' . preg_replace('/^\\s+|\\s+$/', ' ', $m[1]) . '<';
233 }
234
235 protected function _removePreCB($m)
236 {
237 return $this->_reservePlace($m[1]);
238 }
239
240 protected function _removeTextareaCB($m)
241 {
242 return $this->_reservePlace($m[1]);
243 }
244
245 protected function _removeStyleCB($m)
246 {
247 $openStyle = $m[1];
248 $css = $m[2];
249 // remove HTML comments
250 $css = preg_replace('/(?:^\\s*<!--|-->\\s*$)/', '', $css);
251
252 // remove CDATA section markers
253 $css = $this->_removeCdata($css);
254
255 // minify
256 $minifier = $this->_cssMinifier
257 ? $this->_cssMinifier
258 : 'trim';
259 $css = call_user_func($minifier, $css);
260
261 return $this->_reservePlace($this->_needsCdata($css)
262 ? "{$openStyle}/*<![CDATA[*/{$css}/*]]>*/</style>"
263 : "{$openStyle}{$css}</style>"
264 );
265 }
266
267 protected function _removeScriptCB($m)
268 {
269 $openScript = $m[2];
270 $js = $m[3];
271
272 // whitespace surrounding? preserve at least one space
273 $ws1 = ($m[1] === '') ? '' : ' ';
274 $ws2 = ($m[4] === '') ? '' : ' ';
275
276 // remove HTML comments (and ending "//" if present)
277 $js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js);
278
279 // remove CDATA section markers
280 $js = $this->_removeCdata($js);
281
282 // minify
283 $minifier = $this->_jsMinifier
284 ? $this->_jsMinifier
285 : 'trim';
286 $js = call_user_func($minifier, $js);
287
288 return $this->_reservePlace($this->_needsCdata($js)
289 ? "{$ws1}{$openScript}/*<![CDATA[*/{$js}/*]]>*/</script>{$ws2}"
290 : "{$ws1}{$openScript}{$js}</script>{$ws2}"
291 );
292 }
293
294 protected function _removeCdata($str)
295 {
296 return (false !== strpos($str, '<![CDATA['))
297 ? str_replace(array('<![CDATA[', ']]>'), '', $str)
298 : $str;
299 }
300
301 protected function _needsCdata($str)
302 {
303 return ($this->_isXhtml && preg_match('/(?:[<&]|\\-\\-|\\]\\]>)/', $str));
304 }
305 }