PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/system/jch_optimize/cache/HTML.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 245 lines | 134 code | 35 blank | 76 comment | 6 complexity | 68548d178e00bc344847cf88faac2d37 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * Class Minify_HTML
  4. * @package Minify
  5. */
  6. /**
  7. * Compress HTML
  8. *
  9. * This is a heavy regex-based removal of whitespace, unnecessary comments and
  10. * tokens. IE conditional comments are preserved. There are also options to have
  11. * STYLE and SCRIPT blocks compressed by callback functions.
  12. *
  13. * A test suite is available.
  14. *
  15. * @package Minify
  16. * @author Stephen Clay <steve@mrclay.org>
  17. */
  18. class Minify_HTML {
  19. /**
  20. * "Minify" an HTML page
  21. *
  22. * @param string $html
  23. *
  24. * @param array $options
  25. *
  26. * 'cssMinifier' : (optional) callback function to process content of STYLE
  27. * elements.
  28. *
  29. * 'jsMinifier' : (optional) callback function to process content of SCRIPT
  30. * elements. Note: the type attribute is ignored.
  31. *
  32. * 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
  33. * unset, minify will sniff for an XHTML doctype.
  34. *
  35. * @return string
  36. */
  37. public static function minify($html, $options = array()) {
  38. $min = new Minify_HTML($html, $options);
  39. return $min->process();
  40. }
  41. /**
  42. * Create a minifier object
  43. *
  44. * @param string $html
  45. *
  46. * @param array $options
  47. *
  48. * 'cssMinifier' : (optional) callback function to process content of STYLE
  49. * elements.
  50. *
  51. * 'jsMinifier' : (optional) callback function to process content of SCRIPT
  52. * elements. Note: the type attribute is ignored.
  53. *
  54. * 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
  55. * unset, minify will sniff for an XHTML doctype.
  56. *
  57. * @return null
  58. */
  59. public function __construct($html, $options = array())
  60. {
  61. $this->_html = str_replace("\r\n", "\n", trim($html));
  62. if (isset($options['xhtml'])) {
  63. $this->_isXhtml = (bool)$options['xhtml'];
  64. }
  65. if (isset($options['cssMinifier'])) {
  66. $this->_cssMinifier = $options['cssMinifier'];
  67. }
  68. if (isset($options['jsMinifier'])) {
  69. $this->_jsMinifier = $options['jsMinifier'];
  70. }
  71. }
  72. /**
  73. * Minify the markeup given in the constructor
  74. *
  75. * @return string
  76. */
  77. public function process()
  78. {
  79. if ($this->_isXhtml === null) {
  80. $this->_isXhtml = (false !== strpos($this->_html, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML'));
  81. }
  82. $this->_replacementHash = 'MINIFYHTML' . md5($_SERVER['REQUEST_TIME']);
  83. $this->_placeholders = array();
  84. // replace SCRIPTs (and minify) with placeholders
  85. $this->_html = preg_replace_callback(
  86. '/(\\s*)(<script\\b[^>]*?>)([\\s\\S]*?)<\\/script>(\\s*)/i'
  87. ,array($this, '_removeScriptCB')
  88. ,$this->_html);
  89. // replace STYLEs (and minify) with placeholders
  90. $this->_html = preg_replace_callback(
  91. '/\\s*(<style\\b[^>]*?>)([\\s\\S]*?)<\\/style>\\s*/i'
  92. ,array($this, '_removeStyleCB')
  93. ,$this->_html);
  94. // remove HTML comments (not containing IE conditional comments).
  95. $this->_html = preg_replace_callback(
  96. '/<!--([\\s\\S]*?)-->/'
  97. ,array($this, '_commentCB')
  98. ,$this->_html);
  99. // replace PREs with placeholders
  100. $this->_html = preg_replace_callback('/\\s*(<pre\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i'
  101. ,array($this, '_removePreCB')
  102. ,$this->_html);
  103. // replace TEXTAREAs with placeholders
  104. $this->_html = preg_replace_callback(
  105. '/\\s*(<textarea\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i'
  106. ,array($this, '_removeTextareaCB')
  107. ,$this->_html);
  108. // trim each line.
  109. // @todo take into account attribute values that span multiple lines.
  110. $this->_html = preg_replace('/^\\s+|\\s+$/m', '', $this->_html);
  111. // remove ws around block/undisplayed elements
  112. $this->_html = preg_replace('/\\s+(<\\/?(?:area|base(?:font)?|blockquote|body'
  113. .'|caption|center|cite|col(?:group)?|dd|dir|div|dl|dt|fieldset|form'
  114. .'|frame(?:set)?|h[1-6]|head|hr|html|legend|li|link|map|menu|meta'
  115. .'|ol|opt(?:group|ion)|p|param|t(?:able|body|head|d|h||r|foot|itle)'
  116. .'|ul)\\b[^>]*>)/i', '$1', $this->_html);
  117. // remove ws outside of all elements
  118. $this->_html = preg_replace_callback(
  119. '/>([^<]+)</'
  120. ,array($this, '_outsideTagCB')
  121. ,$this->_html);
  122. // use newlines before 1st attribute in open tags (to limit line lengths)
  123. //$this->_html = preg_replace('/(<[a-z\\-]+)\\s+([^>]+>)/i', "$1\n$2", $this->_html);
  124. // fill placeholders
  125. $this->_html = str_replace(
  126. array_keys($this->_placeholders)
  127. ,array_values($this->_placeholders)
  128. ,$this->_html
  129. );
  130. return $this->_html;
  131. }
  132. protected function _commentCB($m)
  133. {
  134. return (0 === strpos($m[1], '[') || false !== strpos($m[1], '<!['))
  135. ? $m[0]
  136. : '';
  137. }
  138. protected function _reservePlace($content)
  139. {
  140. $placeholder = '%' . $this->_replacementHash . count($this->_placeholders) . '%';
  141. $this->_placeholders[$placeholder] = $content;
  142. return $placeholder;
  143. }
  144. protected $_isXhtml = null;
  145. protected $_replacementHash = null;
  146. protected $_placeholders = array();
  147. protected $_cssMinifier = null;
  148. protected $_jsMinifier = null;
  149. protected function _outsideTagCB($m)
  150. {
  151. return '>' . preg_replace('/^\\s+|\\s+$/', ' ', $m[1]) . '<';
  152. }
  153. protected function _removePreCB($m)
  154. {
  155. return $this->_reservePlace($m[1]);
  156. }
  157. protected function _removeTextareaCB($m)
  158. {
  159. return $this->_reservePlace($m[1]);
  160. }
  161. protected function _removeStyleCB($m)
  162. {
  163. $openStyle = $m[1];
  164. $css = $m[2];
  165. // remove HTML comments
  166. $css = preg_replace('/(?:^\\s*<!--|-->\\s*$)/', '', $css);
  167. // remove CDATA section markers
  168. $css = $this->_removeCdata($css);
  169. // minify
  170. $minifier = $this->_cssMinifier
  171. ? $this->_cssMinifier
  172. : 'trim';
  173. $css = call_user_func($minifier, $css);
  174. return $this->_reservePlace($this->_needsCdata($css)
  175. ? "{$openStyle}/*<![CDATA[*/{$css}/*]]>*/</style>"
  176. : "{$openStyle}{$css}</style>"
  177. );
  178. }
  179. protected function _removeScriptCB($m)
  180. {
  181. $openScript = $m[2];
  182. $js = $m[3];
  183. // whitespace surrounding? preserve at least one space
  184. $ws1 = ($m[1] === '') ? '' : ' ';
  185. $ws2 = ($m[4] === '') ? '' : ' ';
  186. // remove HTML comments (and ending "//" if present)
  187. $js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js);
  188. // remove CDATA section markers
  189. $js = $this->_removeCdata($js);
  190. // minify
  191. $minifier = $this->_jsMinifier
  192. ? $this->_jsMinifier
  193. : 'trim';
  194. $js = call_user_func($minifier, $js);
  195. return $this->_reservePlace($this->_needsCdata($js)
  196. ? "{$ws1}{$openScript}/*<![CDATA[*/{$js}/*]]>*/</script>{$ws2}"
  197. : "{$ws1}{$openScript}{$js}</script>{$ws2}"
  198. );
  199. }
  200. protected function _removeCdata($str)
  201. {
  202. return (false !== strpos($str, '<![CDATA['))
  203. ? str_replace(array('<![CDATA[', ']]>'), '', $str)
  204. : $str;
  205. }
  206. protected function _needsCdata($str)
  207. {
  208. return ($this->_isXhtml && preg_match('/(?:[<&]|\\-\\-|\\]\\]>)/', $str));
  209. }
  210. }