From 265a6f1c39653ed8ceb743296b5023cf496fb68e Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Sun, 26 May 2013 20:47:34 +0300 Subject: [PATCH] Relative path composing at "AssetController::adjustCssUrl()" has been complete. --- .../yii/console/controllers/AssetController.php | 37 ++++++++++++++++------ .../console/controllers/AssetControllerTest.php | 20 +++++++++--- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/framework/yii/console/controllers/AssetController.php b/framework/yii/console/controllers/AssetController.php index 71709e8..249938f 100644 --- a/framework/yii/console/controllers/AssetController.php +++ b/framework/yii/console/controllers/AssetController.php @@ -536,15 +536,20 @@ EOD */ protected function adjustCssUrl($cssContent, $inputFilePath, $outputFilePath) { - $sharedPath = ''; - for ($i = 0; $i < strlen($inputFilePath); $i++) { - if (!isset($outputFilePath[$i])) { + $sharedPathParts = array(); + $inputFilePathParts = explode('/', $inputFilePath); + $inputFilePathPartsCount = count($inputFilePathParts); + $outputFilePathParts = explode('/', $outputFilePath); + $outputFilePathPartsCount = count($outputFilePathParts); + for ($i =0; $i < $inputFilePathPartsCount && $i < $outputFilePathPartsCount; $i++) { + if ($inputFilePathParts[$i] == $outputFilePathParts[$i]) { + $sharedPathParts[] = $inputFilePathParts[$i]; + } else { break; } - if ($inputFilePath[$i] == $outputFilePath[$i]) { - $sharedPath .= $inputFilePath[$i]; - } } + $sharedPath = implode('/', $sharedPathParts); + $inputFileRelativePath = trim(str_replace($sharedPath, '', $inputFilePath), '/'); $outputFileRelativePath = trim(str_replace($sharedPath, '', $outputFilePath), '/'); $inputFileRelativePathParts = explode('/', $inputFileRelativePath); @@ -554,12 +559,26 @@ EOD $fullMatch = $matches[0]; $inputUrl = $matches[1]; - $urlPrefix = str_repeat('../', count($outputFileRelativePathParts)); - $urlPrefix .= implode('/', $inputFileRelativePathParts); + $outputUrlParts = array_fill(0, count($outputFileRelativePathParts), '..'); + $outputUrlParts = array_merge($outputUrlParts, $inputFileRelativePathParts); + + if (strpos($inputUrl, '/') !== false) { + $inputUrlParts = explode('/', $inputUrl); + foreach ($inputUrlParts as $key => $inputUrlPart) { + if ($inputUrlPart == '..') { + array_pop($outputUrlParts); + unset($inputUrlParts[$key]); + } + } + $outputUrlParts[] = implode('/', $inputUrlParts); + } else { + $outputUrlParts[] = $inputUrl; + } + $outputUrl = implode('/', $outputUrlParts); - $outputUrl = $urlPrefix . '/' . $inputUrl; return str_replace($inputUrl, $outputUrl, $fullMatch); }; + $cssContent = preg_replace_callback('/[\w\-]:\s*url\("([^"]*)"\)+/is', $callback, $cssContent); return $cssContent; diff --git a/tests/unit/framework/console/controllers/AssetControllerTest.php b/tests/unit/framework/console/controllers/AssetControllerTest.php index 4983568..4f25fe6 100644 --- a/tests/unit/framework/console/controllers/AssetControllerTest.php +++ b/tests/unit/framework/console/controllers/AssetControllerTest.php @@ -263,16 +263,28 @@ class AssetControllerTest extends TestCase { return array( array( - '.test-class {background-image: url("test.png");}', + '.published-same-dir-class {background-image: url("published_same_dir.png");}', '/test/base/path/assets/input', '/test/base/path/assets/output', - '.test-class {background-image: url("../input/test.png");}', + '.published-same-dir-class {background-image: url("../input/published_same_dir.png");}', ), array( - '.test-class {background-image: url("../img/test.png");}', + '.published-relative-dir-class {background-image: url("../img/published_relative_dir.png");}', '/test/base/path/assets/input', '/test/base/path/assets/output', - '.test-class {background-image: url("../input/img/test.png");}', + '.published-relative-dir-class {background-image: url("../img/published_relative_dir.png");}', + ), + array( + '.static-same-dir-class {background-image: url("static_same_dir.png");}', + '/test/base/path/css', + '/test/base/path/assets/output', + '.static-same-dir-class {background-image: url("../../css/static_same_dir.png");}', + ), + array( + '.static-relative-dir-class {background-image: url("../img/static_relative_dir.png");}', + '/test/base/path/css', + '/test/base/path/assets/output', + '.static-relative-dir-class {background-image: url("../../img/static_relative_dir.png");}', ), ); }