Permalink
Browse files

Fixing an issue with relative paths not converting to absolute paths …

…correctly. This should fix issue #32.  I also implemented the feature requests in issue #24, issue #27, and issue #21.
  • Loading branch information...
meenie committed Oct 17, 2013
1 parent 8b5be4b commit 52c78f6ddfd816a449f78f821a3b33f582606859
Showing with 14 additions and 10 deletions.
  1. +14 −10 src/Munee/Asset/Type/Css.php
@@ -169,21 +169,25 @@ protected function isScss($file)
*/
protected function fixRelativeImagePaths($content, $originalFile)
{
$regEx = '%((?:background(?:-image)?|list-style-image):.*?url[\\s]*\()[\\s\'"]*(\.\.[^\\)\'"]*)[\\s\'"]*(\\)[\\s]*)%';
$regEx = '%(url[\\s]*\\()[\\s\'"]*([^\\)\'"]*)[\\s\'"]*(\\))%';
$webroot = $this->request->webroot;
$changedContent = preg_replace_callback($regEx, function ($match) use ($originalFile, $webroot) {
$basePath = trim($match[2]);
// Skip conversion if the first character is a '/' since it's already an absolute path;
if ($basePath[0] !== '/') {
$basePathPrefix = str_replace($webroot, '', dirname($originalFile));
if (! empty($basePathPrefix)) {
$basePathPrefix .= '/';
}
$basePathPrefix = str_replace($webroot, '', dirname($originalFile));
if (! empty($basePathPrefix)) {
$basePathPrefix .= '/';
}
$basePath = $basePathPrefix . $basePath;
$basePath = $basePathPrefix . trim($match[2]);
$count = 1;
while ($count > 0) {
$basePath = preg_replace('%\\w+/\\.\\./%', '', $basePath, -1, $count);
// Lets remove the relative path markers (../../) and the directory above them.
$count = 1;
while ($count > 0) {
$basePath = preg_replace('%([^/]+/\\.\\./|\\./)%', '', $basePath, -1, $count);
}
}
return $match[1] . $basePath . $match[3];

0 comments on commit 52c78f6

Please sign in to comment.