diff --git a/src/CSS.php b/src/CSS.php index 3868341..f7d02cc 100644 --- a/src/CSS.php +++ b/src/CSS.php @@ -181,7 +181,7 @@ class CSS extends Minify // grab referenced file & minify it (which may include importing // yet other @import statements recursively) $minifier = new static($importPath); - $importContent = $minifier->minify($source); + $importContent = $minifier->minify(); // check if this is only valid for certain media if ($match['media']) { @@ -254,7 +254,9 @@ class CSS extends Minify */ $path = explode('/', $path); - $to = explode('/', $to); + // $to can be empty (e.g. no path is given), in which case it shouldn't + // expand to array(''), which would later strip $path's root / + $to = $to ? explode('/', $to) : array(); // compare paths & strip identical ancestors foreach ($path as $i => $chunk) { @@ -349,34 +351,37 @@ class CSS extends Minify // loop files foreach ($this->data as $source => $css) { // if we'll save to a new path, we'll have to fix the relative paths - if ($source !== 0) { + // to be relative no longer to the source file, but to the new path + if ($source !== 0 && $path) { $css = $this->move($source, $path, $css); } + /* + * Let's first take out strings & comments, since we can't just remove + * whitespace anywhere. If whitespace occurs inside a string, we should + * leave it alone. E.g.: + * p { content: "a test" } + */ + $this->extractStrings(); + $this->stripComments(); + $css = $this->replace($css); + + $css = $this->stripWhitespace($css); + $css = $this->shortenHex($css); + $css = $this->shortenZeroes($css); + + // restore the string we've extracted earlier + $css = $this->restoreExtractedData($css); + + // if no target path is given, relative paths were not converted, so + // they'll still be relative to the source file then + $css = $this->importFiles($path ?: $source, $css); + $css = $this->combineImports($path ?: $source, $css); + // combine css $content .= $css; } - /* - * Let's first take out strings & comments, since we can't just remove - * whitespace anywhere. If whitespace occurs inside a string, we should - * leave it alone. E.g.: - * p { content: "a test" } - */ - $this->extractStrings(); - $this->stripComments(); - $content = $this->replace($content); - - $content = $this->stripWhitespace($content); - $content = $this->shortenHex($content); - $content = $this->shortenZeroes($content); - - // restore the string we've extracted earlier - $content = $this->restoreExtractedData($content); - - $content = $this->importFiles($path, $content); - $content = $this->combineImports($path, $content); - // save to path if ($path !== null) { $this->save($content, $path); diff --git a/tests/css/CSSTest.php b/tests/css/CSSTest.php index e14dade..32705d4 100644 --- a/tests/css/CSSTest.php +++ b/tests/css/CSSTest.php @@ -38,7 +38,10 @@ class CSSTest extends PHPUnit_Framework_TestCase */ public function minify($input, $expected) { - $this->minifier->add($input); + $input = (array) $input; + foreach ($input as $css) { + $this->minifier->add($css); + } $result = $this->minifier->minify(); $this->assertEquals($expected, $result); @@ -52,7 +55,10 @@ class CSSTest extends PHPUnit_Framework_TestCase */ public function convertRelativePath($source, $target, $expected) { - $this->minifier->add($source); + $source = (array) $source; + foreach ($source as $css) { + $this->minifier->add($css); + } $result = $this->minifier->minify($target); $this->assertEquals($expected, $result); @@ -318,6 +324,20 @@ margin-left: -0.3125rem; '@import url(../image.jpg);', ); + // https://github.com/matthiasmullie/minify/issues/29 + $tests[] = array( + $source . '/issue29.css', + $target . '/issue29.css', + "@import url('http://myurl.de');", + ); + + // https://github.com/matthiasmullie/minify/issues/38 + $tests[] = array( + $source . '/relative.css', + null, // no output file + file_get_contents($source . '/relative.css'), + ); + $sourceRelative = 'tests/css/sample/convert_relative_path/source'; $targetRelative = 'tests/css/sample/convert_relative_path/target'; @@ -338,13 +358,6 @@ margin-left: -0.3125rem; '@import url(image.jpg);', ); - // https://github.com/matthiasmullie/minify/issues/29 - $tests[] = array( - $sourceRelative . '/issue29.css', - $targetRelative . '/issue29.css', - "@import url('http://myurl.de');", - ); - return $tests; } }