Don't touch relative paths if no target path is given

Fixes issue #38
This commit is contained in:
Matthias Mullie 2015-02-20 16:50:56 +01:00
parent 3528c67597
commit 227efa7bf8
2 changed files with 50 additions and 32 deletions

View File

@ -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,14 +351,11 @@ 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);
}
// 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
@ -365,17 +364,23 @@ class CSS extends Minify
*/
$this->extractStrings();
$this->stripComments();
$content = $this->replace($content);
$css = $this->replace($css);
$content = $this->stripWhitespace($content);
$content = $this->shortenHex($content);
$content = $this->shortenZeroes($content);
$css = $this->stripWhitespace($css);
$css = $this->shortenHex($css);
$css = $this->shortenZeroes($css);
// restore the string we've extracted earlier
$content = $this->restoreExtractedData($content);
$css = $this->restoreExtractedData($css);
$content = $this->importFiles($path, $content);
$content = $this->combineImports($path, $content);
// 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;
}
// save to path
if ($path !== null) {

View File

@ -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;
}
}