public static function minifyCSS($css_content, $fileuri = false, &$import_url = array()) { global $current_css_file; $current_css_file = $fileuri; if (strlen($css_content) > 0) { $limit = Media::getBackTrackLimit(); $css_content = preg_replace('#/\\*.*?\\*/#s', '', $css_content, $limit); $css_content = preg_replace_callback('#(url\\((?![\\\'"]?data:)(?!http://)(?!https://)(?:\'|")?)([^\\)\'"]*(?:\'|")?\\))#s', array('Tools', 'replaceByAbsoluteURL'), $css_content, $limit); $css_content = preg_replace('#\\s+#', ' ', $css_content, $limit); $css_content = str_replace(array("\t", "\n", "\r"), '', $css_content); $css_content = str_replace(array('; ', ': '), array(';', ':'), $css_content); $css_content = str_replace(array(' {', '{ '), '{', $css_content); $css_content = str_replace(', ', ',', $css_content); $css_content = str_replace(array('} ', ' }', ';}'), '}', $css_content); $css_content = str_replace(array(':0px', ':0em', ':0pt', ':0%'), ':0', $css_content); $css_content = str_replace(array(' 0px', ' 0em', ' 0pt', ' 0%'), ' 0', $css_content); $css_content = str_replace('\'images_ie/', '\'images/', $css_content); $css_content = preg_replace_callback('#(AlphaImageLoader\\(src=\')([^\']*\',)#s', array('Tools', 'replaceByAbsoluteURL'), $css_content); // Store all import url preg_match_all('#@(import|charset) .*?;#i', $css_content, $m); for ($i = 0, $total = count($m[0]); $i < $total; $i++) { if (isset($m[1][$i]) && $m[1][$i] == 'import') { $import_url[] = $m[0][$i]; } $css_content = str_replace($m[0][$i], '', $css_content); } return trim($css_content); } return false; }
protected static function _removeScriptCB($m) { $openScript = $m[1]; $js = $m[2]; // remove HTML comments (and ending "//" if present) $js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js, Media::getBackTrackLimit()); // remove CDATA section markers $js = self::_removeCdata($js); // minify $minifier = self::$_jsMinifier ? self::$_jsMinifier : 'trim'; $js = call_user_func($minifier, $js); return self::_reservePlace(self::_needsCdata($js) ? "{$openScript}/*<![CDATA[*/{$js}/*]]>*/</script>" : "{$openScript}{$js}</script>"); }
/** * Minify the markeup given in the constructor * * @return string */ public function process() { if ($this->_isXhtml === null) { $this->_isXhtml = false !== strpos($this->_html, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML'); } $this->_replacementHash = 'MINIFYHTML' . md5($_SERVER['REQUEST_TIME']); $this->_placeholders = array(); // replace SCRIPTs (and minify) with placeholders $this->_html = preg_replace_callback('/(\\s*)<script(\\b[^>]*?>)([\\s\\S]*?)<\\/script>(\\s*)/i', array($this, '_removeScriptCB'), $this->_html, Media::getBackTrackLimit()); // replace STYLEs (and minify) with placeholders $this->_html = preg_replace_callback('/\\s*<style(\\b[^>]*>)([\\s\\S]*?)<\\/style>\\s*/i', array($this, '_removeStyleCB'), $this->_html, Media::getBackTrackLimit()); // remove HTML comments (not containing IE conditional comments). $this->_html = preg_replace_callback('/<!--([\\s\\S]*?)-->/', array($this, '_commentCB'), $this->_html, Media::getBackTrackLimit()); // replace PREs with placeholders $this->_html = preg_replace_callback('/\\s*<pre(\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i', array($this, '_removePreCB'), $this->_html, Media::getBackTrackLimit()); // replace TEXTAREAs with placeholders $this->_html = preg_replace_callback('/\\s*<textarea(\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i', array($this, '_removeTextareaCB'), $this->_html, Media::getBackTrackLimit()); // trim each line. // @todo take into account attribute values that span multiple lines. $this->_html = preg_replace('/^\\s+|\\s+$/m', '', $this->_html); // remove ws around block/undisplayed elements $this->_html = preg_replace('/\\s+(<\\/?(?:area|base(?:font)?|blockquote|body' . '|caption|center|cite|col(?:group)?|dd|dir|div|dl|dt|fieldset|form' . '|frame(?:set)?|h[1-6]|head|hr|html|legend|li|link|map|menu|meta' . '|ol|opt(?:group|ion)|p|param|t(?:able|body|head|d|h||r|foot|itle)' . '|ul)\\b[^>]*>)/i', '$1', $this->_html); // remove ws outside of all elements $this->_html = preg_replace('/>(\\s(?:\\s*))?([^<]+)(\\s(?:\\s*))?</', '>$1$2$3<', $this->_html); // use newlines before 1st attribute in open tags (to limit line lengths) // $this->_html = preg_replace('/(<[a-z\\-]+)\\s+([^>]+>)/i', "$1\n$2", $this->_html); $this->_html = preg_replace('/\\s+/m', ' ', $this->_html); // fill placeholders $this->_html = str_replace(array_keys($this->_placeholders), array_values($this->_placeholders), $this->_html); // issue 229: multi-pass to catch scripts that didn't get replaced in textareas $this->_html = str_replace(array_keys($this->_placeholders), array_values($this->_placeholders), $this->_html); return $this->_html; }
/** * Minify CSS * * @param string $cssContent * @param bool $fileUri * @param array $importUrl * * @return bool|string */ public static function minifyCSS($cssContent, $fileUri = false, &$importUrl = array()) { Media::$current_css_file = $fileUri; if (strlen($cssContent) > 0) { $cssContent = \Minify_CSSmin::minify($cssContent); $limit = Media::getBackTrackLimit(); $cssContent = preg_replace_callback(Media::$pattern_callback, array('Media', 'replaceByAbsoluteURL'), $cssContent, $limit); $cssContent = str_replace('\'images_ie/', '\'images/', $cssContent); $cssContent = preg_replace_callback('#(AlphaImageLoader\\(src=\')([^\']*\',)#s', array('Tools', 'replaceByAbsoluteURL'), $cssContent); // Store all import url preg_match_all('#@(import|charset) .*?;#i', $cssContent, $m); for ($i = 0, $total = count($m[0]); $i < $total; $i++) { if (isset($m[1][$i]) && $m[1][$i] == 'import') { $importUrl[] = $m[0][$i]; } $cssContent = str_replace($m[0][$i], '', $cssContent); } return trim($cssContent); } return false; }