var UPPERCASE_URL_PREFIX = 'URL(';
var URL_SUFFIX = ')';
-function reduceUrls(data, context, callback) {
+var IMPORT_URL_PREFIX = '@import';
+var UPPERCASE_IMPORT_URL_PREFIX = '@IMPORT';
+
+function byUrl(data, context, callback) {
var nextStart = 0;
var nextStartUpperCase = 0;
var nextEnd = 0;
data;
}
-module.exports = reduceUrls;
+function byImport(data, context, callback) {
+ var nextStart = 0;
+ var nextStartUpperCase = 0;
+ var nextEnd = 0;
+ var cursor = 0;
+ var tempData = [];
+ var nextSingleQuote = 0;
+ var nextDoubleQuote = 0;
+ var withQuote;
+ var SINGLE_QUOTE = '\'';
+ var DOUBLE_QUOTE = '"';
+
+ for (; nextEnd < data.length;) {
+ nextStart = data.indexOf(IMPORT_URL_PREFIX, nextEnd);
+ nextStartUpperCase = data.indexOf(UPPERCASE_IMPORT_URL_PREFIX, nextEnd);
+ if (nextStart == -1 && nextStartUpperCase == -1)
+ break;
+
+ if (nextStart > -1 && nextStartUpperCase > -1 && nextStartUpperCase < nextStart)
+ nextStart = nextStartUpperCase;
+
+ nextSingleQuote = data.indexOf(SINGLE_QUOTE, nextStart);
+ nextDoubleQuote = data.indexOf(DOUBLE_QUOTE, nextStart);
+
+ if (nextSingleQuote > -1 && nextDoubleQuote > -1 && nextSingleQuote < nextDoubleQuote) {
+ nextStart = nextSingleQuote;
+ withQuote = SINGLE_QUOTE;
+ } else if (nextSingleQuote > -1 && nextDoubleQuote > -1 && nextSingleQuote > nextDoubleQuote) {
+ nextStart = nextDoubleQuote;
+ withQuote = DOUBLE_QUOTE;
+ } else if (nextSingleQuote > -1) {
+ nextStart = nextSingleQuote;
+ withQuote = SINGLE_QUOTE;
+ } else if (nextDoubleQuote > -1) {
+ nextStart = nextDoubleQuote;
+ withQuote = DOUBLE_QUOTE;
+ }
+
+ tempData.push(data.substring(cursor, nextStart));
+
+ nextEnd = data.indexOf(withQuote, nextStart + 1);
+ if (nextEnd == -1)
+ break;
+
+ var url = data.substring(nextStart, nextEnd + 1);
+ callback(url, tempData);
+
+ cursor = nextEnd + 1;
+ }
+
+ return tempData.length > 0 ?
+ tempData.join('') + data.substring(cursor, data.length) :
+ data;
+}
+
+function reduceAll(data, context, callback) {
+ data = byUrl(data, context, callback);
+ data = byImport(data, context, callback);
+ return data;
+}
+
+module.exports = reduceAll;
'should give right output': function (minified) {
assert.equal(minified.styles, '@import url(/partials/one.css);@import url(/partials/extra/three.css);@import url(/partials/extra/four.css);.two{color:#fff}');
}
+ },
+ 'with import URL as a string': {
+ 'topic': function () {
+ var source = 'test/fixtures/partials/two.css';
+ var asHash = sourcesAsHash([source]);
+ asHash[source].styles = asHash[source].styles.replace(/url\(|\)/g, '');
+ return new CleanCSS().minify(asHash);
+ },
+ 'should give right output': function (minified) {
+ assert.equal(minified.styles, '.one{color:red}.three{color:#0f0}.four{color:#00f}.two{color:#fff}');
+ }
+ },
+ 'with import URL as an uppercase string': {
+ 'topic': function () {
+ var source = 'test/fixtures/partials/two.css';
+ var asHash = sourcesAsHash([source]);
+ asHash[source].styles = asHash[source].styles.replace(/url\(|\)/g, '').replace('@import', '@IMPORT');
+ return new CleanCSS().minify(asHash);
+ },
+ 'should give right output': function (minified) {
+ assert.equal(minified.styles, '.one{color:red}.three{color:#0f0}.four{color:#00f}.two{color:#fff}');
+ }
}
},
'with remote paths': {