4cb01aab42a6c345d3a0768522c3465b806fd0c3
10 * Surcharge du minifieur HTML
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
16 * C'était le cas du bouton statistiques du formulaire admin par exemple
18 * On permet de conserver également tout commentaire commençant par <!--keepme: -->
20 * @package SPIP\Compresseur\Minifieur
22 class Minify_HTML_SPIP
extends Minify_HTML
{
30 * @param array $options
31 * Tableau d'option avec les index possibles
32 * - 'cssMinifier' : (optional) callback function to process content of STYLE
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.
41 public static function minify($html, $options = array()) {
42 $min = new Minify_HTML_SPIP($html, $options);
43 return $min->process();
47 * Minification des commentaires
49 * Le cas <!--extra--> doit être conservé dans les commentaires,
50 * tout comme <!--keepme: xxx -->
52 * @param array $m Matches du preg_match d'un commentaire HTML
53 * @return string Contenu minifié
55 protected function _commentCB($m)
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);
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.
71 * A test suite is available.
72 * http://code.google.com/p/minify/source/browse/trunk/min/lib/Minify/HTML.php
75 * @author Stephen Clay <steve@mrclay.org>
80 * "Minify" an HTML page
84 * @param array $options
86 * 'cssMinifier' : (optional) callback function to process content of STYLE
89 * 'jsMinifier' : (optional) callback function to process content of SCRIPT
90 * elements. Note: the type attribute is ignored.
92 * 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
93 * unset, minify will sniff for an XHTML doctype.
97 public static function minify($html, $options = array()) {
98 $min = new Minify_HTML($html, $options);
99 return $min->process();
104 * Create a minifier object
106 * @param string $html
108 * @param array $options
110 * 'cssMinifier' : (optional) callback function to process content of STYLE
113 * 'jsMinifier' : (optional) callback function to process content of SCRIPT
114 * elements. Note: the type attribute is ignored.
116 * 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
117 * unset, minify will sniff for an XHTML doctype.
121 public function __construct($html, $options = array())
123 $this->_html
= str_replace("\r\n", "\n", trim($html));
124 if (isset($options['xhtml'])) {
125 $this->_isXhtml
= (bool)$options['xhtml'];
127 if (isset($options['cssMinifier'])) {
128 $this->_cssMinifier
= $options['cssMinifier'];
130 if (isset($options['jsMinifier'])) {
131 $this->_jsMinifier
= $options['jsMinifier'];
137 * Minify the markeup given in the constructor
141 public function process()
143 if ($this->_isXhtml
=== null) {
144 $this->_isXhtml
= (false !== strpos($this->_html
, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML'));
147 $this->_replacementHash
= 'MINIFYHTML' . md5($_SERVER['REQUEST_TIME']);
148 $this->_placeholders
= array();
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')
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')
162 // remove HTML comments (not containing IE conditional comments).
163 $this->_html
= preg_replace_callback(
164 '/<!--([\\s\\S]*?)-->/'
165 ,array($this, '_commentCB')
168 // replace PREs with placeholders
169 $this->_html
= preg_replace_callback('/\\s*(<pre\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i'
170 ,array($this, '_removePreCB')
173 // replace TEXTAREAs with placeholders
174 $this->_html
= preg_replace_callback(
175 '/\\s*(<textarea\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i'
176 ,array($this, '_removeTextareaCB')
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
);
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
);
192 // remove ws outside of all elements
193 $this->_html
= preg_replace_callback(
195 ,array($this, '_outsideTagCB')
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
);
202 $this->_html
= str_replace(
203 array_keys($this->_placeholders
)
204 ,array_values($this->_placeholders
)
210 protected function _commentCB($m)
212 return (0 === strpos($m[1], '[') ||
false !== strpos($m[1], '<!['))
217 protected function _reservePlace($content)
219 $placeholder = '%' . $this->_replacementHash
. count($this->_placeholders
) . '%';
220 $this->_placeholders
[$placeholder] = $content;
224 protected $_isXhtml = null;
225 protected $_replacementHash = null;
226 protected $_placeholders = array();
227 protected $_cssMinifier = null;
228 protected $_jsMinifier = null;
230 protected function _outsideTagCB($m)
232 return '>' . preg_replace('/^\\s+|\\s+$/', ' ', $m[1]) . '<';
235 protected function _removePreCB($m)
237 return $this->_reservePlace($m[1]);
240 protected function _removeTextareaCB($m)
242 return $this->_reservePlace($m[1]);
245 protected function _removeStyleCB($m)
249 // remove HTML comments
250 $css = preg_replace('/(?:^\\s*<!--|-->\\s*$)/', '', $css);
252 // remove CDATA section markers
253 $css = $this->_removeCdata($css);
256 $minifier = $this->_cssMinifier
257 ?
$this->_cssMinifier
259 $css = call_user_func($minifier, $css);
261 return $this->_reservePlace($this->_needsCdata($css)
262 ?
"{$openStyle}/*<![CDATA[*/{$css}/*]]>*/</style>"
263 : "{$openStyle}{$css}</style>"
267 protected function _removeScriptCB($m)
272 // whitespace surrounding? preserve at least one space
273 $ws1 = ($m[1] === '') ?
'' : ' ';
274 $ws2 = ($m[4] === '') ?
'' : ' ';
276 // remove HTML comments (and ending "//" if present)
277 $js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js);
279 // remove CDATA section markers
280 $js = $this->_removeCdata($js);
283 $minifier = $this->_jsMinifier
286 $js = call_user_func($minifier, $js);
288 return $this->_reservePlace($this->_needsCdata($js)
289 ?
"{$ws1}{$openScript}/*<![CDATA[*/{$js}/*]]>*/</script>{$ws2}"
290 : "{$ws1}{$openScript}{$js}</script>{$ws2}"
294 protected function _removeCdata($str)
296 return (false !== strpos($str, '<![CDATA['))
297 ?
str_replace(array('<![CDATA[', ']]>'), '', $str)
301 protected function _needsCdata($str)
303 return ($this->_isXhtml
&& preg_match('/(?:[<&]|\\-\\-|\\]\\]>)/', $str));