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>
22 * "Minify" an HTML page
26 * @param array $options
28 * 'cssMinifier' : (optional) callback function to process content of STYLE
31 * 'jsMinifier' : (optional) callback function to process content of SCRIPT
32 * elements. Note: the type attribute is ignored.
34 * 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
35 * unset, minify will sniff for an XHTML doctype.
39 public static function minify($html, $options = array()) {
40 $min = new Minify_HTML($html, $options);
41 return $min->process();
46 * Create a minifier object
50 * @param array $options
52 * 'cssMinifier' : (optional) callback function to process content of STYLE
55 * 'jsMinifier' : (optional) callback function to process content of SCRIPT
56 * elements. Note: the type attribute is ignored.
58 * 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
59 * unset, minify will sniff for an XHTML doctype.
63 public function __construct($html, $options = array())
65 $this->_html = str_replace("\r\n", "\n", trim($html));
66 if (isset($options['xhtml'])) {
67 $this->_isXhtml = (bool)$options['xhtml'];
69 if (isset($options['cssMinifier'])) {
70 $this->_cssMinifier = $options['cssMinifier'];
72 if (isset($options['jsMinifier'])) {
73 $this->_jsMinifier = $options['jsMinifier'];
79 * Minify the markeup given in the constructor
83 public function process()
85 if ($this->_isXhtml === null) {
86 $this->_isXhtml = (false !== strpos($this->_html, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML'));
89 $this->_replacementHash = 'MINIFYHTML' . md5($_SERVER['REQUEST_TIME']);
90 $this->_placeholders = array();
92 // replace SCRIPTs (and minify) with placeholders
93 $this->_html = preg_replace_callback(
94 '/(\\s*)(<script\\b[^>]*?>)([\\s\\S]*?)<\\/script>(\\s*)/i'
95 ,array($this, '_removeScriptCB')
98 // replace STYLEs (and minify) with placeholders
99 $this->_html = preg_replace_callback(
100 '/\\s*(<style\\b[^>]*?>)([\\s\\S]*?)<\\/style>\\s*/i'
101 ,array($this, '_removeStyleCB')
104 // remove HTML comments (not containing IE conditional comments).
105 $this->_html = preg_replace_callback(
106 '/<!--([\\s\\S]*?)-->/'
107 ,array($this, '_commentCB')
110 // replace PREs with placeholders
111 $this->_html = preg_replace_callback('/\\s*(<pre\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i'
112 ,array($this, '_removePreCB')
115 // replace TEXTAREAs with placeholders
116 $this->_html = preg_replace_callback(
117 '/\\s*(<textarea\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i'
118 ,array($this, '_removeTextareaCB')
122 // @todo take into account attribute values that span multiple lines.
123 $this->_html = preg_replace('/^\\s+|\\s+$/m', '', $this->_html);
125 // remove ws around block/undisplayed elements
126 $this->_html = preg_replace('/\\s+(<\\/?(?:area|base(?:font)?|blockquote|body'
127 .'|caption|center|cite|col(?:group)?|dd|dir|div|dl|dt|fieldset|form'
128 .'|frame(?:set)?|h[1-6]|head|hr|html|legend|li|link|map|menu|meta'
129 .'|ol|opt(?:group|ion)|p|param|t(?:able|body|head|d|h||r|foot|itle)'
130 .'|ul)\\b[^>]*>)/i', '$1', $this->_html);
132 // remove ws outside of all elements
133 $this->_html = preg_replace_callback(
135 ,array($this, '_outsideTagCB')
138 // use newlines before 1st attribute in open tags (to limit line lengths)
139 $this->_html = preg_replace('/(<[a-z\\-]+)\\s+([^>]+>)/i', "$1\n$2", $this->_html);
142 $this->_html = str_replace(
143 array_keys($this->_placeholders)
144 ,array_values($this->_placeholders)
150 protected function _commentCB($m)
152 return (0 === strpos($m[1], '[') || false !== strpos($m[1], '<!['))
157 protected function _reservePlace($content)
159 $placeholder = '%' . $this->_replacementHash . count($this->_placeholders) . '%';
160 $this->_placeholders[$placeholder] = $content;
164 protected $_isXhtml = null;
165 protected $_replacementHash = null;
166 protected $_placeholders = array();
167 protected $_cssMinifier = null;
168 protected $_jsMinifier = null;
170 protected function _outsideTagCB($m)
172 return '>' . preg_replace('/^\\s+|\\s+$/', ' ', $m[1]) . '<';
175 protected function _removePreCB($m)
177 return $this->_reservePlace($m[1]);
180 protected function _removeTextareaCB($m)
182 return $this->_reservePlace($m[1]);
185 protected function _removeStyleCB($m)
189 // remove HTML comments
190 $css = preg_replace('/(?:^\\s*<!--|-->\\s*$)/', '', $css);
192 // remove CDATA section markers
193 $css = $this->_removeCdata($css);
196 $minifier = $this->_cssMinifier
197 ? $this->_cssMinifier
199 $css = call_user_func($minifier, $css);
201 return $this->_reservePlace($this->_needsCdata($css)
202 ? "{$openStyle}/*<![CDATA[*/{$css}/*]]>*/</style>"
203 : "{$openStyle}{$css}</style>"
207 protected function _removeScriptCB($m)
212 // whitespace surrounding? preserve at least one space
213 $ws1 = ($m[1] === '') ? '' : ' ';
214 $ws2 = ($m[4] === '') ? '' : ' ';
216 // remove HTML comments (and ending "//" if present)
217 $js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js);
219 // remove CDATA section markers
220 $js = $this->_removeCdata($js);
223 $minifier = $this->_jsMinifier
226 $js = call_user_func($minifier, $js);
228 return $this->_reservePlace($this->_needsCdata($js)
229 ? "{$ws1}{$openScript}/*<![CDATA[*/{$js}/*]]>*/</script>{$ws2}"
230 : "{$ws1}{$openScript}{$js}</script>{$ws2}"
234 protected function _removeCdata($str)
236 return (false !== strpos($str, '<![CDATA['))
237 ? str_replace(array('<![CDATA[', ']]>'), '', $str)
241 protected function _needsCdata($str)
243 return ($this->_isXhtml && preg_match('/(?:[<&]|\\-\\-|\\]\\]>)/', $str));