From 7385d2f097047c87b18341a31388c62dddfd8e95 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Thu, 5 May 2016 20:18:14 +0900 Subject: [PATCH] Process imports before converting url() relative paths --- common/framework/formatter.php | 36 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/common/framework/formatter.php b/common/framework/formatter.php index 8d0d46215..4ce0d84f5 100644 --- a/common/framework/formatter.php +++ b/common/framework/formatter.php @@ -327,6 +327,24 @@ class Formatter // Clean the content. $content = utf8_clean(file_get_contents($filename)); + // Convert all paths in LESS and SCSS imports, too. + $import_type = ends_with('.scss', $filename) ? 'scss' : 'normal'; + $content = preg_replace_callback('/@import\s+(?:\\([^()]+\\))?([^;]+);/', function($matches) use($filename, $target_filename, $import_type) { + $import_content = ''; + $import_files = array_map(function($str) use($filename, $import_type) { + $str = trim(trim(trim(preg_replace('/^url\\(([^()]+)\\)$/', '$1', trim($str))), '"\'')); + return dirname($filename) . '/' . ($import_type === 'scss' ? "_$str.scss" : $str); + }, explode(',', $matches[1])); + foreach ($import_files as $import_filename) + { + if (file_exists($import_filename)) + { + $import_content .= self::concatCSS($import_filename, $target_filename); + } + } + return trim($import_content); + }, $content); + // Convert all paths to be relative to the new filename. $path_converter = new \MatthiasMullie\PathConverter\Converter($filename, $target_filename); $content = preg_replace_callback('/\burl\\(([^)]+)\\)/iU', function($matches) use ($path_converter) { @@ -342,24 +360,6 @@ class Formatter }, $content); unset($path_converter); - // Convert all paths in LESS and SCSS imports, too. - $import_type = ends_with('.scss', $filename) ? 'scss' : 'normal'; - $content = preg_replace_callback('/@import\s+(?:\\([^()]+\\))?([^;]+);/', function($matches) use($filename, $target_filename, $import_type) { - $import_content = ''; - $import_files = array_map(function($str) use($filename, $import_type) { - $str = trim(trim(trim($str), '"\'')); - return dirname($filename) . '/' . ($import_type === 'scss' ? "_$str.scss" : $str); - }, explode(',', $matches[1])); - foreach ($import_files as $import_filename) - { - if (file_exists($import_filename)) - { - $import_content .= self::concatCSS($import_filename, $target_filename); - } - } - return $import_content; - }, $content); - // Wrap the content in a media query if there is one. if ($media !== null) {