/** * 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((string) $numLines); // e.g. 103 lines = 3 digits $inComment = false; $i = 0; $newLines = array(); while (null !== ($line = array_shift($lines))) { if ('' !== $id && 0 == $i % 50) { if ($inComment) { array_push($newLines, '', "/* {$id} *|", ''); } else { 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'])) { Minify_CSS_UriRewriter::$debugText = ''; $docRoot = isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT']; $symlinks = isset($options['symlinks']) ? $options['symlinks'] : array(); $content = Minify_CSS_UriRewriter::rewrite($content, $options['currentDir'], $docRoot, $symlinks); $content = "/* Minify_CSS_UriRewriter::\$debugText\n\n" . Minify_CSS_UriRewriter::$debugText . "*/\n" . $content; } return $content; }
/** * 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); // Hackily rewrite strings with XPath expressions that are // likely to throw off our dumb parser (for Prototype 1.6.1). $content = str_replace('"/*"', '"/"+"*"', $content); $content = preg_replace('@([\'"])(\\.?//?)\\*@', '$1$2$1+$1*', $content); $lines = explode("\n", $content); $numLines = count($lines); // determine left padding $padTo = strlen((string) $numLines); // e.g. 103 lines = 3 digits $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 MINIFY_MIN_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()); $content = "/* Minify_CSS_UriRewriter::\$debugText\n\n" . Minify_CSS_UriRewriter::$debugText . "*/\n" . $content; } return $content; }
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'); if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) { echo "\n---Input:\n\n{$in}\n"; echo "\n---Output: " . strlen($actual) . " bytes\n\n{$actual}\n\n"; if (!$passed) { echo "---Expected: " . strlen($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: " . strlen($actual) . " bytes\n\n{$actual}\n\n"; if (!$passed) { echo "---Expected: " . strlen($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; }
private static function _processImportUriCB($m) { $uri = trim($m[1], '()"\' '); // We want to grab the import. if (strpos($uri, '//') !== false) { $path = $uri; } elseif ($uri[0] == '/') { $path = self::_realpath(self::$_docRoot, $uri); } else { $path = realpath2(self::$_currentDir . '/' . trim($uri, '/\\')); if (substr_compare(self::$_docRoot, $path, 0, strlen($path)) != 0) { return "/* Error: {$uri} isn't in the webroot. */\n"; } elseif (substr_compare($path, '.css', -4, 4, true) != 0) { return "/* Error: {$uri} must end in .css. */\n"; } } $css = file_get_contents($path); // Not so fast, we've got to rewrite this file too. What's more, the current dir and path are different. $bak = array(self::$_currentDir, self::$_prependPath, self::$_docRoot, self::$debugText); self::$debugText = ''; if (IsUrl($path)) { $newCurrentDir = $path; $newDocRoot = $path; } else { $newDocRoot = self::$_docRoot; $newCurrentDir = realpath2($currentDirBak . realpath2(dirname($uri))); } $css = self::rewrite($css, $newCurrentDir, $newDocRoot); list(self::$_currentDir, self::$_prependPath, self::$_docRoot, self::$debugText) = $bak; return "/* @include url('{$uri}'); */\n" . $css; }