From 55209442436c8f75acd1db2dd0558aa2c82da94a Mon Sep 17 00:00:00 2001 From: Steve Clay Date: Tue, 5 May 2009 13:05:07 +0000 Subject: [PATCH] CSS/UriRewriter.php : fix Issue 99 ImportProcessor.php : fix Issue 99 --- min/lib/Minify/CSS/UriRewriter.php | 19 ++++++------ min/lib/Minify/ImportProcessor.php | 4 +-- .../test_Minify_CSS_UriRewriter.php | 30 +++++++++++++++---- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/min/lib/Minify/CSS/UriRewriter.php b/min/lib/Minify/CSS/UriRewriter.php index ca3ee38..c5bf58a 100644 --- a/min/lib/Minify/CSS/UriRewriter.php +++ b/min/lib/Minify/CSS/UriRewriter.php @@ -236,12 +236,14 @@ class Minify_CSS_UriRewriter { self::$debugText .= "docroot stripped : {$path}\n"; // fix to root-relative URI - $uri = strtr($path, DIRECTORY_SEPARATOR, '/'); + + $uri = strtr($path, '/\\', '//'); + // remove /./ and /../ where possible $uri = str_replace('/./', '/', $uri); // inspired by patch from Oleg Cherniy do { - $uri = preg_replace('@/[^/]+/\\.\\./@', '/', $uri, -1, $changed); + $uri = preg_replace('@/[^/]+/\\.\\./@', '/', $uri, 1, $changed); } while ($changed); self::$debugText .= "traversals removed : {$uri}\n\n"; @@ -249,20 +251,19 @@ class Minify_CSS_UriRewriter { return $uri; } - - /** - * Get realpath with any trailing slash removed + * Get realpath with any trailing slash removed. If realpath() fails, + * just remove the trailing slash. * * @param string $path * - * @return mixed real path or false on realpath() failure + * @return mixed path with no trailing slash */ protected static function _realpath($path) { - $path = realpath($path); - if (! $path) { - return false; + $realPath = realpath($path); + if ($realPath !== false) { + $path = $realPath; } $last = $path[strlen($path) - 1]; return ($last === '/' || $last === '\\') diff --git a/min/lib/Minify/ImportProcessor.php b/min/lib/Minify/ImportProcessor.php index be7efb7..0d6d90a 100644 --- a/min/lib/Minify/ImportProcessor.php +++ b/min/lib/Minify/ImportProcessor.php @@ -143,12 +143,12 @@ class Minify_ImportProcessor { // strip doc root $path = substr($path, strlen(realpath($_SERVER['DOCUMENT_ROOT']))); // fix to absolute URL - $url = strtr($path, DIRECTORY_SEPARATOR, '/'); + $url = strtr($path, '/\\', '//'); // remove /./ and /../ where possible $url = str_replace('/./', '/', $url); // inspired by patch from Oleg Cherniy do { - $url = preg_replace('@/[^/]+/\\.\\./@', '/', $url, -1, $changed); + $url = preg_replace('@/[^/]+/\\.\\./@', '/', $url, 1, $changed); } while ($changed); } } diff --git a/min_unit_tests/test_Minify_CSS_UriRewriter.php b/min_unit_tests/test_Minify_CSS_UriRewriter.php index 6970d4f..55f09b0 100644 --- a/min_unit_tests/test_Minify_CSS_UriRewriter.php +++ b/min_unit_tests/test_Minify_CSS_UriRewriter.php @@ -7,12 +7,10 @@ require_once 'Minify/CSS/UriRewriter.php'; 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'); - - Minify_CSS_UriRewriter::$debugText = ''; - $actual = Minify_CSS_UriRewriter::rewrite( $in ,$thisDir . '/_test_files/css_uriRewriter' // currentDir @@ -30,7 +28,29 @@ function test_Minify_CSS_UriRewriter() // 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; + } } test_Minify_CSS_UriRewriter(); \ No newline at end of file