var cursor = 0;
options.relativeTo = options.relativeTo || options.root;
+ options._baseRelativeTo = options._baseRelativeTo || options.relativeTo;
options.visited = options.visited || [];
var inlinedFile = function() {
options.visited.push(fullPath);
var importedData = fs.readFileSync(fullPath, 'utf8');
+ var importRelativeTo = path.dirname(fullPath);
+ importedData = CleanCSS._rebaseRelativeURLs(importedData, importRelativeTo, options._baseRelativeTo);
return CleanCSS._inlineImports(importedData, {
root: options.root,
- relativeTo: path.dirname(fullPath),
+ relativeTo: importRelativeTo,
+ _baseRelativeTo: options.baseRelativeTo,
visited: options.visited
});
} else {
data;
},
+ _rebaseRelativeURLs: function(data, fromBase, toBase) {
+ var tempData = [];
+ var nextStart = 0;
+ var nextEnd = 0;
+ var cursor = 0;
+
+ for (; nextEnd < data.length; ) {
+ nextStart = data.indexOf('url(', nextEnd);
+ if (nextStart == -1)
+ break;
+ nextEnd = data.indexOf(')', nextStart + 4);
+ if (nextEnd == -1)
+ break;
+
+ tempData.push(data.substring(cursor, nextStart));
+ var url = data.substring(nextStart + 4, nextEnd).replace(/['"]/g, '');
+ if (url[0] != '/' && url.substring(url.length - 4) != '.css') {
+ url = path.relative(toBase, path.join(fromBase, url)).replace(/\\/g, '/');
+ }
+ tempData.push('url(' + url + ')');
+ cursor = nextEnd + 1;
+ }
+
+ return tempData.length > 0 ?
+ tempData.join('') + data.substring(cursor, data.length) :
+ data;
+ },
+
// Strip special comments (/*! ... */) by replacing them by __CSSCOMMENT__ marker
// for further restoring. Plain comments are removed. It's done by scanning datq using
// String#indexOf scanning instead of regexps to speed up the process.
'of multi-level, circular dependency file': [
"@import url(test/data/partials/two.css);",
".one{color:red}.three{color:#0f0}.four{color:#00f}.two{color:#fff}"
+ ],
+ 'of a file with a relative resource path': [
+ "@import url(test/data/partials/three.css);",
+ ".three{background-image:url(test/data/partials/extra/down.gif)}",
+ ],
+ 'of a file with an absolute resource path': [
+ "@import url(test/data/partials/four.css);",
+ ".four{background-image:url(/partials/extra/down.gif)}",
]
}),
'@import with absolute paths': cssContext({