Skip to content

Commit cd15113

Browse files
Also simplify external links paths
For normal files, @import & url() syntax is unified already (e.g. removing quotes around the path, where not needed), but this was not done for external paths. This also simplifies the huge regex a little
1 parent bc46e8b commit cd15113

2 files changed

Lines changed: 33 additions & 39 deletions

File tree

src/CSS.php

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ protected function combineImports($source, $content, $parents)
122122
# fetch path
123123
(?P<path>
124124
125-
# do not fetch data uris or external sources
125+
# do not fetch data uris, external sources or absolute paths
126126
(?!(
127127
["\']?
128-
(data|https?):
128+
(data:|https?:\\/\\/|\\/)
129129
))
130130
131131
.+?
@@ -166,10 +166,10 @@ protected function combineImports($source, $content, $parents)
166166
# fetch path
167167
(?P<path>
168168
169-
# do not fetch data uris or external sources
169+
# do not fetch data uris, external sources or absolute paths
170170
(?!(
171171
["\']?
172-
(data|https?):
172+
(data:|https?:\\/\\/|\\/)
173173
))
174174
175175
.+?
@@ -380,17 +380,7 @@ protected function move(Converter $converter, $content)
380380
(?P<quotes>["\'])?
381381
382382
# fetch path
383-
(?P<path>
384-
385-
# do not fetch data uris or external sources
386-
(?!(
387-
\s?
388-
["\']?
389-
(data|https?):
390-
))
391-
392-
.+?
393-
)
383+
(?P<path>.+?)
394384
395385
# close path enclosure
396386
(?(quotes)(?P=quotes))
@@ -417,16 +407,7 @@ protected function move(Converter $converter, $content)
417407
(?P<quotes>["\'])
418408
419409
# fetch path
420-
(?P<path>
421-
422-
# do not fetch data uris or external sources
423-
(?!(
424-
["\']?
425-
(data|https?):
426-
))
427-
428-
.+?
429-
)
410+
(?P<path>.+?)
430411
431412
# close path enclosure
432413
(?P=quotes)
@@ -450,29 +431,30 @@ protected function move(Converter $converter, $content)
450431
// determine if it's a url() or an @import match
451432
$type = (strpos($match[0], '@import') === 0 ? 'import' : 'url');
452433

453-
// attempting to interpret GET-params makes no sense, so let's discard them for awhile
454-
$params = strrchr($match['path'], '?');
455-
$url = $params ? substr($match['path'], 0, -strlen($params)) : $match['path'];
434+
$url = $match['path'];
435+
if ($this->canImportByPath($url)) {
436+
// attempting to interpret GET-params makes no sense, so let's discard them for awhile
437+
$params = strrchr($url, '?');
438+
$url = $params ? substr($url, 0, -strlen($params)) : $url;
456439

457-
// fix relative url
458-
$url = $converter->convert($url);
440+
// fix relative url
441+
$url = $converter->convert($url);
459442

460-
// now that the path has been converted, re-apply GET-params
461-
$url .= $params;
443+
// now that the path has been converted, re-apply GET-params
444+
$url .= $params;
445+
}
462446

463447
// build replacement
464448
$search[] = $match[0];
465-
if ($type == 'url') {
449+
if ($type === 'url') {
466450
$replace[] = 'url('.$url.')';
467-
} elseif ($type == 'import') {
451+
} elseif ($type === 'import') {
468452
$replace[] = '@import "'.$url.'"';
469453
}
470454
}
471455

472456
// replace urls
473-
$content = str_replace($search, $replace, $content);
474-
475-
return $content;
457+
return str_replace($search, $replace, $content);
476458
}
477459

478460
/**
@@ -671,4 +653,16 @@ protected function canImportBySize($path)
671653
{
672654
return ($size = @filesize($path)) && $size <= $this->maxImportSize * 1024;
673655
}
656+
657+
/**
658+
* Check if file a file can be imported, going by the path.
659+
*
660+
* @param string $path
661+
*
662+
* @return bool
663+
*/
664+
protected function canImportByPath($path)
665+
{
666+
return preg_match('/^(data:|https?:|\\/)/', $path) === 0;
667+
}
674668
}

tests/css/CSSTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ public function dataProvider()
504504
body{
505505
background: white;
506506
}',
507-
'@import "./css1.css";@import url(\'https://www.google.com/main.css\');body{background:white}',
507+
'@import "./css1.css";@import url(https://www.google.com/main.css);body{background:white}',
508508
);
509509

510510
// https://github.com/matthiasmullie/minify/commit/3253a81d07cd01afcb651e309900d8ad58a052da#commitcomment-19223603
@@ -584,7 +584,7 @@ public function dataProviderPaths()
584584
$tests[] = array(
585585
$source.'/issue29.css',
586586
$target.'/issue29.css',
587-
"@import url('http://myurl.de');",
587+
"@import url(http://myurl.de);",
588588
);
589589

590590
// https://github.com/matthiasmullie/minify/issues/38

0 commit comments

Comments
 (0)