/** * Minify a CSS string * * @param string $css * * @param array $options available options: * * 'preserveComments': (default true) multi-line comments that begin * with "/*!" will be preserved with newlines before and after to * enhance readability. * * 'prependRelativePath': (default null) if given, this string will be * prepended to all relative URIs in import/url declarations * * 'currentDir': (default null) if given, this is assumed to be the * directory of the current CSS file. Using this, minify will rewrite * all relative URIs in import/url declarations to correctly point to * the desired files. For this to work, the files *must* exist and be * visible by the PHP process. * * 'symlinks': (default = array()) If the CSS file is stored in * a symlink-ed directory, provide an array of link paths to * target paths, where the link paths are within the document root. Because * paths need to be normalized for this to work, use "//" to substitute * the doc root in the link paths (the array keys). E.g.: * <code> * array('//symlink' => '/real/target/path') // unix * array('//static' => 'D:\\staticStorage') // Windows * </code> * * @return string */ static public function minify($css, $options = array()) { require_once 'Minify/CSS/Compressor.php'; if (isset($options['preserveComments']) && !$options['preserveComments']) { $css = Minify_CSS_Compressor::process($css, $options); } else { require_once 'Minify/CommentPreserver.php'; $css = Minify_CommentPreserver::process( $css ,array('Minify_CSS_Compressor', 'process') ,array($options) ); } if (! isset($options['currentDir']) && ! isset($options['prependRelativePath'])) { return $css; } require_once 'Minify/CSS/UriRewriter.php'; if (isset($options['currentDir'])) { return Minify_CSS_UriRewriter::rewrite( $css ,$options['currentDir'] ,isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT'] ,isset($options['symlinks']) ? $options['symlinks'] : array() ); } else { return Minify_CSS_UriRewriter::prepend( $css ,$options['prependRelativePath'] ); } }
/** * Minify a CSS string * * @param string $css * * @param array $options available options: * * 'preserveComments': (default true) multi-line comments that begin * with "/*!" will be preserved with newlines before and after to * enhance readability. * * 'removeCharsets': (default true) remove all @charset at-rules * * 'prependRelativePath': (default null) if given, this string will be * prepended to all relative URIs in import/url declarations * * 'currentDir': (default null) if given, this is assumed to be the * directory of the current CSS file. Using this, minify will rewrite * all relative URIs in import/url declarations to correctly point to * the desired files. For this to work, the files *must* exist and be * visible by the PHP process. * * 'symlinks': (default = array()) If the CSS file is stored in * a symlink-ed directory, provide an array of link paths to * target paths, where the link paths are within the document root. Because * paths need to be normalized for this to work, use "//" to substitute * the doc root in the link paths (the array keys). E.g.: * <code> * array('//symlink' => '/real/target/path') // unix * array('//static' => 'D:\\staticStorage') // Windows * </code> * * 'docRoot': (default = $_SERVER['DOCUMENT_ROOT']) * see Minify_CSS_UriRewriter::rewrite * * @return string */ public static function minify($css, $options = array()) { $options = array_merge(array('compress' => true, 'removeCharsets' => true, 'preserveComments' => true, 'currentDir' => null, 'docRoot' => $_SERVER['DOCUMENT_ROOT'], 'prependRelativePath' => null, 'symlinks' => array()), $options); if ($options['removeCharsets']) { $css = preg_replace('/@charset[^;]+;\\s*/', '', $css); } if ($options['compress']) { if (!$options['preserveComments']) { require_once 'Compressor.php'; $css = Minify_CSS_Compressor::process($css, $options); } else { require_once 'CommentPreserver.php'; require_once 'Compressor.php'; $css = Minify_CommentPreserver::process($css, array('Minify_CSS_Compressor', 'process'), array($options)); } } if (!$options['currentDir'] && !$options['prependRelativePath']) { return $css; } require_once 'UriRewriter.php'; if ($options['currentDir']) { return Minify_CSS_UriRewriter::rewrite($css, $options['currentDir'], $options['docRoot'], $options['symlinks']); } else { return Minify_CSS_UriRewriter::prepend($css, $options['prependRelativePath']); } }
function test_Minify_CSS_UriRewriter() { global $thisDir; Minify_CSS_UriRewriter::$debugText = ''; $in = file_get_contents($thisDir . '/_test_files/css_uriRewriter/in.css'); $expected = file_get_contents($thisDir . '/_test_files/css_uriRewriter/exp.css'); $actual = Minify_CSS_UriRewriter::rewrite($in, $thisDir . '/_test_files/css_uriRewriter', $thisDir); $passed = assertTrue($expected === $actual, 'Minify_CSS_UriRewriter : rewrite'); if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) { echo "\n---Input:\n\n{$in}\n"; echo "\n---Output: " . countBytes($actual) . " bytes\n\n{$actual}\n\n"; if (!$passed) { echo "---Expected: " . countBytes($expected) . " bytes\n\n{$expected}\n\n\n"; } // show debugging only when test run directly echo "--- Minify_CSS_UriRewriter::\$debugText\n\n", Minify_CSS_UriRewriter::$debugText; } Minify_CSS_UriRewriter::$debugText = ''; $in = file_get_contents($thisDir . '/_test_files/css_uriRewriter/in.css'); $expected = file_get_contents($thisDir . '/_test_files/css_uriRewriter/exp_prepend.css'); $actual = Minify_CSS_UriRewriter::prepend($in, 'http://cnd.com/A/B/'); $passed = assertTrue($expected === $actual, 'Minify_CSS_UriRewriter : prepend1'); if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) { echo "\n---Input:\n\n{$in}\n"; echo "\n---Output: " . countBytes($actual) . " bytes\n\n{$actual}\n\n"; if (!$passed) { echo "---Expected: " . countBytes($expected) . " bytes\n\n{$expected}\n\n\n"; } // show debugging only when test run directly echo "--- Minify_CSS_UriRewriter::\$debugText\n\n", Minify_CSS_UriRewriter::$debugText; } Minify_CSS_UriRewriter::$debugText = ''; $in = file_get_contents($thisDir . '/_test_files/css_uriRewriter/in.css'); $expected = file_get_contents($thisDir . '/_test_files/css_uriRewriter/exp_prepend2.css'); $actual = Minify_CSS_UriRewriter::prepend($in, '//cnd.com/A/B/'); $passed = assertTrue($expected === $actual, 'Minify_CSS_UriRewriter : prepend2'); if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) { echo "\n---Input:\n\n{$in}\n"; echo "\n---Output: " . countBytes($actual) . " bytes\n\n{$actual}\n\n"; if (!$passed) { echo "---Expected: " . countBytes($expected) . " bytes\n\n{$expected}\n\n\n"; } // show debugging only when test run directly echo "--- Minify_CSS_UriRewriter::\$debugText\n\n", Minify_CSS_UriRewriter::$debugText; } Minify_CSS_UriRewriter::$debugText = ''; $in = '../../../../assets/skins/sam/sprite.png'; $exp = '/yui/assets/skins/sam/sprite.png'; $actual = Minify_CSS_UriRewriter::rewriteRelative($in, 'sf_root_dir\\web\\yui\\menu\\assets\\skins\\sam', 'sf_root_dir\\web'); $passed = assertTrue($exp === $actual, 'Minify_CSS_UriRewriter : Issue 99'); if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) { echo "\n---Input:\n\n{$in}\n"; echo "\n---Output: " . countBytes($actual) . " bytes\n\n{$actual}\n\n"; if (!$passed) { echo "---Expected: " . countBytes($exp) . " bytes\n\n{$exp}\n\n\n"; } // show debugging only when test run directly echo "--- Minify_CSS_UriRewriter::\$debugText\n\n", Minify_CSS_UriRewriter::$debugText; } }
/** * Add line numbers in C-style comments * * This uses a very basic parser easily fooled by comment tokens inside * strings or regexes, but, otherwise, generally clean code will not be * mangled. URI rewriting can also be performed. * * @param string $content * * @param array $options available options: * * 'id': (optional) string to identify file. E.g. file name/path * * 'currentDir': (default null) if given, this is assumed to be the * directory of the current CSS file. Using this, minify will rewrite * all relative URIs in import/url declarations to correctly point to * the desired files, and prepend a comment with debugging information about * this process. * * @return string */ public static function minify($content, $options = array()) { $id = isset($options['id']) && $options['id'] ? $options['id'] : ''; $content = str_replace("\r\n", "\n", $content); $lines = explode("\n", $content); $numLines = count($lines); // determine left padding $padTo = strlen($numLines); $inComment = false; $i = 0; $newLines = array(); while (null !== ($line = array_shift($lines))) { if ('' !== $id && 0 == $i % 50) { array_push($newLines, '', "/* {$id} */", ''); } ++$i; $newLines[] = self::_addNote($line, $i, $inComment, $padTo); $inComment = self::_eolInComment($line, $inComment); } $content = implode("\n", $newLines) . "\n"; // check for desired URI rewriting if (isset($options['currentDir'])) { require_once W3TC_LIB_MINIFY_DIR . '/Minify/CSS/UriRewriter.php'; Minify_CSS_UriRewriter::$debugText = ''; $content = Minify_CSS_UriRewriter::rewrite($content, $options['currentDir'], isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT'], isset($options['symlinks']) ? $options['symlinks'] : array(), isset($options['browserCacheId']) ? $options['browserCacheId'] : 0, isset($options['browserCacheExtensions']) ? $options['browserCacheExtensions'] : array()); $content = "/* Minify_CSS_UriRewriter::\$debugText\n\n" . Minify_CSS_UriRewriter::$debugText . "*/\n" . $content; } elseif (isset($options['prependRelativePath'])) { require_once W3TC_LIB_MINIFY_DIR . '/Minify/CSS/UriRewriter.php'; Minify_CSS_UriRewriter::$debugText = ''; $content = Minify_CSS_UriRewriter::prepend($content, $options['prependRelativePath'], isset($options['browserCacheId']) ? $options['browserCacheId'] : 0, isset($options['browserCacheExtensions']) ? $options['browserCacheExtensions'] : array()); } return $content; }
public static function minify($css, $options = array()) { $options = array_merge(array('remove_bslash' => true, 'compress_colors' => true, 'compress_font-weight' => true, 'lowercase_s' => false, 'optimise_shorthands' => 1, 'remove_last_;' => false, 'case_properties' => 1, 'sort_properties' => false, 'sort_selectors' => false, 'merge_selectors' => 2, 'discard_invalid_properties' => false, 'css_level' => 'CSS2.1', 'preserve_css' => false, 'timestamp' => false, 'template' => 'default'), $options); set_include_path(get_include_path() . PATH_SEPARATOR . W3TC_LIB_CSSTIDY_DIR); require_once 'class.csstidy.php'; $csstidy = new csstidy(); foreach ($options as $option => $value) { $csstidy->set_cfg($option, $value); } $csstidy->load_template($options['template']); $csstidy->parse($css); $css = $csstidy->print->plain(); if (isset($options['currentDir']) || isset($options['prependRelativePath'])) { require_once W3TC_LIB_MINIFY_DIR . '/Minify/CSS/UriRewriter.php'; $browsercache_id = isset($options['browserCacheId']) ? $options['browserCacheId'] : 0; $browsercache_extensions = isset($options['browserCacheExtensions']) ? $options['browserCacheExtensions'] : array(); if (isset($options['currentDir'])) { $document_root = isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT']; $symlinks = isset($options['symlinks']) ? $options['symlinks'] : array(); return Minify_CSS_UriRewriter::rewrite($css, $options['currentDir'], $document_root, $symlinks, $browsercache_id, $browsercache_extensions); } else { return Minify_CSS_UriRewriter::prepend($css, $options['prependRelativePath'], $browsercache_id, $browsercache_extensions); } } return $css; }
/** * Minifies content * @param string $content * @param array $options * @return string */ public static function minify($content, $options = array()) { if (isset($options['currentDir'])) { require_once W3TC_LIB_MINIFY_DIR . '/Minify/CSS/UriRewriter.php'; $content = Minify_CSS_UriRewriter::rewrite($content, $options['currentDir'], isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT'], isset($options['symlinks']) ? $options['symlinks'] : array()); } elseif (isset($options['prependRelativePath'])) { require_once W3TC_LIB_MINIFY_DIR . '/Minify/CSS/UriRewriter.php'; $content = Minify_CSS_UriRewriter::prepend($content, $options['prependRelativePath']); } return $content; }
/** * Minifies content * @param string $content * @param array $options * @return string */ public static function minify($content, $options = array()) { $browsercache_id = isset($options['browserCacheId']) ? $options['browserCacheId'] : 0; $browsercache_extensions = isset($options['browserCacheExtensions']) ? $options['browserCacheExtensions'] : array(); if (isset($options['currentDir'])) { require_once W3TC_LIB_MINIFY_DIR . '/Minify/CSS/UriRewriter.php'; $document_root = isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT']; $symlinks = isset($options['symlinks']) ? $options['symlinks'] : array(); $content = Minify_CSS_UriRewriter::rewrite($content, $options['currentDir'], $document_root, $symlinks, $browsercache_id, $browsercache_extensions); } elseif (isset($options['prependRelativePath'])) { require_once W3TC_LIB_MINIFY_DIR . '/Minify/CSS/UriRewriter.php'; $content = Minify_CSS_UriRewriter::prepend($content, $options['prependRelativePath'], $browsercache_id, $browsercache_extensions); } return $content; }
/** * Minify a CSS string * * @param string $css * * @param array $options available options: * * 'removeCharsets': (default true) remove all @charset at-rules * * 'prependRelativePath': (default null) if given, this string will be * prepended to all relative URIs in import/url declarations * * 'currentDir': (default null) if given, this is assumed to be the * directory of the current CSS file. Using this, minify will rewrite * all relative URIs in import/url declarations to correctly point to * the desired files. For this to work, the files *must* exist and be * visible by the PHP process. * * 'symlinks': (default = array()) If the CSS file is stored in * a symlink-ed directory, provide an array of link paths to * target paths, where the link paths are within the document root. Because * paths need to be normalized for this to work, use "//" to substitute * the doc root in the link paths (the array keys). E.g.: * <code> * array('//symlink' => '/real/target/path') // unix * array('//static' => 'D:\\staticStorage') // Windows * </code> * * 'docRoot': (default = $_SERVER['DOCUMENT_ROOT']) * see Minify_CSS_UriRewriter::rewrite * * @return string */ public static function minify($css, $options = array()) { $options = array_merge(array('compress' => true, 'removeCharsets' => true, 'currentDir' => null, 'docRoot' => $_SERVER['DOCUMENT_ROOT'], 'prependRelativePath' => null, 'symlinks' => array()), $options); if ($options['removeCharsets']) { $css = preg_replace('/@charset[^;]+;\\s*/', '', $css); } if ($options['compress']) { $obj = new CSSmin(); $css = $obj->run($css); } if (!$options['currentDir'] && !$options['prependRelativePath']) { return $css; } if ($options['currentDir']) { return Minify_CSS_UriRewriter::rewrite($css, $options['currentDir'], $options['docRoot'], $options['symlinks']); } else { return Minify_CSS_UriRewriter::prepend($css, $options['prependRelativePath']); } }
/** * Internal function for (maybe) minifying assets * * @param Type of asset to minify (css/js) * @param Contents to be minified * @param The name of the file being minified (used for logging) * @param mixed A relative path to use, if provided (for css minification) * @return String (maybe) minified contents of file */ protected function _minify($type, $contents, $filename, $rel = FALSE) { // used in case we need to return orig $contents_orig = $contents; switch ($type) { case 'js': /** * JS pre-minify hook */ if ($this->EE->extensions->active_hook('minimee_pre_minify_js')) { Minimee_helper::log('Hook `minimee_pre_minify_js` has been activated.', 3); // pass contents to be minified, and instance of self $contents = $this->EE->extensions->call('minimee_pre_minify_js', $contents, $filename, $this); if ($this->EE->extensions->end_script === TRUE) { return $contents; } // re-set $contents_orig in case we need to return $contents_orig = $contents; } // HOOK END // be sure we want to minify if ($this->config->is_yes('minify_js')) { // See if JSMinPlus was explicitly requested if ($this->config->js_library == 'jsminplus') { Minimee_helper::log('Running minification with JSMinPlus.', 3); Minimee_helper::library('jsminplus'); $contents = JSMinPlus::minify($contents); } else { if ($this->config->js_library == 'jsmin') { Minimee_helper::log('Running minification with JSMin.', 3); Minimee_helper::library('jsmin'); $contents = JSMin::minify($contents); } } } break; case 'css': /** * CSS pre-minify hook */ if ($this->EE->extensions->active_hook('minimee_pre_minify_css')) { Minimee_helper::log('Hook `minimee_pre_minify_css` has been activated.', 3); // pass contents to be minified, relative path, and instance of self $contents = $this->EE->extensions->call('minimee_pre_minify_css', $contents, $filename, $rel, $this); if ($this->EE->extensions->end_script === TRUE) { return $contents; } // copy to $contents_orig in case we need to return $contents_orig = $contents; } // HOOK END // prepend URL if relative path exists & configured to do so if ($rel !== FALSE && $this->config->is_yes('css_prepend_mode')) { Minimee_helper::library('css_urirewriter'); $contents = Minify_CSS_UriRewriter::prepend($contents, $rel . '/'); // copy to $contents_orig in case we need to return $contents_orig = $contents; } // minify if configured to do so if ($this->config->is_yes('minify_css')) { // See if CSSMin was explicitly requested if ($this->config->css_library == 'cssmin') { Minimee_helper::log('Running minification with CSSMin.', 3); Minimee_helper::library('cssmin'); $cssmin = new CSSmin(FALSE); $contents = $cssmin->run($contents); unset($cssmin); } else { if ($this->config->css_library == 'minify') { Minimee_helper::log('Running minification with Minify_CSS.', 3); Minimee_helper::library('minify'); $contents = Minify_CSS::minify($contents); } } } break; } // calculate weight loss $before = strlen($contents_orig); $after = strlen($contents); $diff = $before - $after; // quick check that contents are not empty if ($after == 0) { Minimee_helper::log('Minification has returned an empty string for `' . $filename . '`.', 2); } // did we actually reduce our file size? It's possible an already minified asset // uses a more aggressive algorithm than Minify; in that case, keep original contents if ($diff > 0) { $diff_formatted = $diff < 100 ? $diff . 'b' : round($diff / 1000, 2) . 'kb'; $change = round($diff / $before * 100, 2); Minimee_helper::log('Minification has reduced ' . $filename . ' by ' . $diff_formatted . ' (' . $change . '%).', 3); // add to our running total $this->diff_total($diff); } else { Minimee_helper::log('Minification unable to reduce ' . $filename . ', so using original content.', 3); $contents = $contents_orig; } // cleanup (leave some smaller variables because they may or may not have ever been set) unset($contents_orig); // return our (maybe) minified contents return $contents; }
function CssEncoder($css, $options = array()) { require_once 'Minify/CSS/UriRewriter.php'; if (isset($options['currentDir'])) { return Minify_CSS_UriRewriter::rewrite($css, $options['currentDir'], isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT'], isset($options['symlinks']) ? $options['symlinks'] : array()); } else { return Minify_CSS_UriRewriter::prepend($css, $options['prependRelativePath']); } }
/** * Given an asset, fetches and returns minified contents. * * @param Minimee_BaseAssetModel $asset * @return String */ protected function minifyAsset($asset) { craft()->config->maxPowerCaptain(); switch ($this->type) { case MinimeeType::Js: if ($this->settings->minifyJsEnabled) { $contents = \JSMin::minify($asset->contents); } else { $contents = $asset->contents; } // Play nice with others by ensuring a semicolon at eof if (substr($contents, -1) != ';') { $contents .= ';'; } break; case MinimeeType::Css: $cssPrependUrl = dirname($asset->filenameUrl) . '/'; $contents = \Minify_CSS_UriRewriter::prepend($asset->contents, $cssPrependUrl); if ($this->settings->minifyCssEnabled) { $contents = \Minify_CSS::minify($contents); } break; } return $contents; }
/** * Rewrite the urls in a CSS file * * @param string $file The path to the file * @param string $content The file contents * @return string */ public static function rewriteUris($file, $content) { $path = dirname($file) . '/'; return Minify_CSS_UriRewriter::prepend($content, $path); }
/** * Method to (maybe) minify CSS asset * * @param Minimee_BaseAssetModel $asset * @return String */ protected function minifyCssAsset($asset) { $contents = $asset->contents; if ($this->settings->minifyCssEnabled) { $compressor = new \CSSmin(); $contents = $compressor->run($contents); } if ($this->settings->cssPrependUrlEnabled) { $cssPrependUrl = $this->settings->cssPrependUrl; if (!$cssPrependUrl) { $cssPrependUrl = dirname($asset->filenameUrl) . '/'; } $contents = \Minify_CSS_UriRewriter::prepend($contents, $cssPrependUrl); } return $contents; }
/** * In CSS content, prepend a path to relative URLs. * * This is useful to process a CSS view being used as an extension. * * @param string $css CSS * @param string $path Path to prepend. E.g. "foo/bar/" or "../" * * @return string * @since 2.2 */ function elgg_prepend_css_urls($css, $path) { return Minify_CSS_UriRewriter::prepend($css, $path); }