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.
14 * A test suite is available.
17 * @author Stephen Clay <steve@mrclay.org>
23 protected $_jsCleanComments = true;
26 * "Minify" an HTML page
30 * @param array $options
32 * 'cssMinifier' : (optional) callback function to process content of STYLE
35 * 'jsMinifier' : (optional) callback function to process content of SCRIPT
36 * elements. Note: the type attribute is ignored.
38 * 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
39 * unset, minify will sniff for an XHTML doctype.
43 public static function minify($html, $options = array()) {
44 $min = new self($html, $options);
45 return $min->process();
50 * Create a minifier object
54 * @param array $options
56 * 'cssMinifier' : (optional) callback function to process content of STYLE
59 * 'jsMinifier' : (optional) callback function to process content of SCRIPT
60 * elements. Note: the type attribute is ignored.
62 * 'jsCleanComments' : (optional) whether to remove HTML comments beginning and end of script block
64 * 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
65 * unset, minify will sniff for an XHTML doctype.
69 public function __construct($html, $options = array())
71 $this->_html = str_replace("\r\n", "\n", trim($html));
72 if (isset($options['xhtml'])) {
73 $this->_isXhtml = (bool)$options['xhtml'];
75 if (isset($options['cssMinifier'])) {
76 $this->_cssMinifier = $options['cssMinifier'];
78 if (isset($options['jsMinifier'])) {
79 $this->_jsMinifier = $options['jsMinifier'];
81 if (isset($options['jsCleanComments'])) {
82 $this->_jsCleanComments = (bool)$options['jsCleanComments'];
88 * Minify the markeup given in the constructor
92 public function process()
94 if ($this->_isXhtml === null) {
95 $this->_isXhtml = (false !== strpos($this->_html, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML'));
98 $this->_replacementHash = 'MINIFYHTML' . md5($_SERVER['REQUEST_TIME']);
99 $this->_placeholders = array();
101 // replace SCRIPTs (and minify) with placeholders
102 $this->_html = preg_replace_callback(
103 '/(\\s*)<script(\\b[^>]*?>)([\\s\\S]*?)<\\/script>(\\s*)/i'
104 ,array($this, '_removeScriptCB')
107 // replace STYLEs (and minify) with placeholders
108 $this->_html = preg_replace_callback(
109 '/\\s*<style(\\b[^>]*>)([\\s\\S]*?)<\\/style>\\s*/i'
110 ,array($this, '_removeStyleCB')
113 // remove HTML comments (not containing IE conditional comments).
114 $this->_html = preg_replace_callback(
115 '/<!--([\\s\\S]*?)-->/'
116 ,array($this, '_commentCB')
119 // replace PREs with placeholders
120 $this->_html = preg_replace_callback('/\\s*<pre(\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i'
121 ,array($this, '_removePreCB')
124 // replace TEXTAREAs with placeholders
125 $this->_html = preg_replace_callback(
126 '/\\s*<textarea(\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i'
127 ,array($this, '_removeTextareaCB')
131 // @todo take into account attribute values that span multiple lines.
132 $this->_html = preg_replace('/^\\s+|\\s+$/m', '', $this->_html);
134 // remove ws around block/undisplayed elements
135 $this->_html = preg_replace('/\\s+(<\\/?(?:area|base(?:font)?|blockquote|body'
136 .'|caption|center|cite|col(?:group)?|dd|dir|div|dl|dt|fieldset|form'
137 .'|frame(?:set)?|h[1-6]|head|hr|html|legend|li|link|map|menu|meta'
138 .'|ol|opt(?:group|ion)|p|param|t(?:able|body|head|d|h||r|foot|itle)'
139 .'|ul)\\b[^>]*>)/i', '$1', $this->_html);
141 // remove ws outside of all elements
142 $this->_html = preg_replace(
143 '/>(\\s(?:\\s*))?([^<]+)(\\s(?:\s*))?</'
147 // use newlines before 1st attribute in open tags (to limit line lengths)
148 $this->_html = preg_replace('/(<[a-z\\-]+)\\s+([^>]+>)/i', "$1\n$2", $this->_html);
151 $this->_html = str_replace(
152 array_keys($this->_placeholders)
153 ,array_values($this->_placeholders)
156 // issue 229: multi-pass to catch scripts that didn't get replaced in textareas
157 $this->_html = str_replace(
158 array_keys($this->_placeholders)
159 ,array_values($this->_placeholders)
165 protected function _commentCB($m)
167 return (0 === strpos($m[1], '[') || false !== strpos($m[1], '<!['))
172 protected function _reservePlace($content)
174 $placeholder = '%' . $this->_replacementHash . count($this->_placeholders) . '%';
175 $this->_placeholders[$placeholder] = $content;
179 protected $_isXhtml = null;
180 protected $_replacementHash = null;
181 protected $_placeholders = array();
182 protected $_cssMinifier = null;
183 protected $_jsMinifier = null;
185 protected function _removePreCB($m)
187 return $this->_reservePlace("<pre{$m[1]}");
190 protected function _removeTextareaCB($m)
192 return $this->_reservePlace("<textarea{$m[1]}");
195 protected function _removeStyleCB($m)
197 $openStyle = "<style{$m[1]}";
199 // remove HTML comments
200 $css = preg_replace('/(?:^\\s*<!--|-->\\s*$)/', '', $css);
202 // remove CDATA section markers
203 $css = $this->_removeCdata($css);
206 $minifier = $this->_cssMinifier
207 ? $this->_cssMinifier
209 $css = call_user_func($minifier, $css);
211 return $this->_reservePlace($this->_needsCdata($css)
212 ? "{$openStyle}/*<![CDATA[*/{$css}/*]]>*/</style>"
213 : "{$openStyle}{$css}</style>"
217 protected function _removeScriptCB($m)
219 $openScript = "<script{$m[2]}";
222 // whitespace surrounding? preserve at least one space
223 $ws1 = ($m[1] === '') ? '' : ' ';
224 $ws2 = ($m[4] === '') ? '' : ' ';
226 // remove HTML comments (and ending "//" if present)
227 if ($this->_jsCleanComments) {
228 $js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js);
231 // remove CDATA section markers
232 $js = $this->_removeCdata($js);
235 $minifier = $this->_jsMinifier
238 $js = call_user_func($minifier, $js);
240 return $this->_reservePlace($this->_needsCdata($js)
241 ? "{$ws1}{$openScript}/*<![CDATA[*/{$js}/*]]>*/</script>{$ws2}"
242 : "{$ws1}{$openScript}{$js}</script>{$ws2}"
246 protected function _removeCdata($str)
248 return (false !== strpos($str, '<![CDATA['))
249 ? str_replace(array('<![CDATA[', ']]>'), '', $str)
253 protected function _needsCdata($str)
255 return ($this->_isXhtml && preg_match('/(?:[<&]|\\-\\-|\\]\\]>)/', $str));