Skip to content

Commit

Permalink
Fixed: don't handle empty import and url [webpack-contrib#463].
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Apr 23, 2017
1 parent a5fdf84 commit fa2d941
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 12 deletions.
15 changes: 9 additions & 6 deletions lib/processCss.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
} else if(url.type === "string") {
url = url.value;
} else throw rule.error("Unexpected format" + rule.params);
if (!url.replace(/\s/g, '').length) {
return;
}
values.nodes[0].nodes.shift();
var mediaQuery = Tokenizer.stringifyValues(values);
if(loaderUtils.isUrlRequest(url, options.root) && options.mode === "global") {
Expand Down Expand Up @@ -101,7 +104,7 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
}
break;
case "url":
if (options.url && !/^#/.test(item.url) && loaderUtils.isUrlRequest(item.url, options.root)) {
if (options.url && item.url.replace(/\s/g, '').length && !/^#/.test(item.url) && loaderUtils.isUrlRequest(item.url, options.root)) {
if (item.url.indexOf(" ") === -1) {
item.stringType = "";
}
Expand Down Expand Up @@ -158,18 +161,18 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
var pipeline = postcss([
localByDefault({
mode: options.mode,
rewriteUrl: function(global, url) {
rewriteUrl: function(global, origUrl) {
if(parserOptions.url){
url = url.trim(" ");
var url = origUrl.trim(" ");

if(!loaderUtils.isUrlRequest(url, root)) {
return url;
if(!origUrl.replace(/\s/g, '').length || !loaderUtils.isUrlRequest(url, root)) {
return origUrl;
}
if(global) {
return loaderUtils.urlToRequest(url, root);
}
}
return url;
return origUrl;
}
}),
extractImports(),
Expand Down
21 changes: 21 additions & 0 deletions test/importTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,33 @@ describe("import", function() {
], "", {
"./test.css": [[2, ".test{a: b}", ""]]
});
test("import empty url", "@import url();\n.class { a: b c d; }", [
[1, "@import url();\n.class { a: b c d; }", ""]
], "");
test("import empty url with quotes", "@import url('');\n.class { a: b c d; }", [
[1, "@import url('');\n.class { a: b c d; }", ""]
], "");
test("import with string", "@import \"test.css\";\n.class { a: b c d; }", [
[2, ".test{a: b}", ""],
[1, ".class { a: b c d; }", ""]
], "", {
"./test.css": [[2, ".test{a: b}", ""]]
});
test("import with empty string", "@import \"\";\n.class { a: b c d; }", [
[1, "@import \"\";\n.class { a: b c d; }", ""]
], "");
test("import with string contain spaces", "@import \" \";\n.class { a: b c d; }", [
[1, "@import \" \";\n.class { a: b c d; }", ""]
], "");
test("import with string contain newline", "@import \"\n\";\n.class { a: b c d; }", [
[1, "@import \"\n\";\n.class { a: b c d; }", ""]
], "");
test("import with string contain CRLF", "@import \"\r\n\";\r\n.class { a: b c d; }", [
[1, "@import \"\r\n\";\r\n.class { a: b c d; }", ""]
], "");
test("import with string contain tab", "@import \"\t\";\n.class { a: b c d; }", [
[1, "@import \"\t\";\n.class { a: b c d; }", ""]
], "");
test("import 2", "@import url('test.css');\n.class { a: b c d; }", [
[2, ".test{a: b}", "screen"],
[1, ".class { a: b c d; }", ""]
Expand Down
60 changes: 54 additions & 6 deletions test/urlTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ describe("url", function() {
test("background img 4", ".class { background: green url( img.png ) xyz }", [
[1, ".class { background: green url({./img.png}) xyz }", ""]
]);
test("background img contain space in name", ".class { background: green url( \"img img.png\" ) xyz }", [
[1, ".class { background: green url(\"{./img img.png}\") xyz }", ""]
]);
test("background 2 img contain space in name", ".class { background: green url( 'img img.png' ) xyz }", [
[1, ".class { background: green url('{./img img.png}') xyz }", ""]
]);
test("background img contain space in name", ".class { background: green url( \"img img.png\" ) xyz }", [
[1, ".class { background: green url(\"{./img img.png}\") xyz }", ""]
]);
test("background 2 img contain space in name", ".class { background: green url( 'img img.png' ) xyz }", [
[1, ".class { background: green url('{./img img.png}') xyz }", ""]
]);
test("background img absolute", ".class { background: green url(/img.png) xyz }", [
[1, ".class { background: green url(/img.png) xyz }", ""]
]);
Expand Down Expand Up @@ -70,6 +70,30 @@ describe("url", function() {
test("-webkit-image-set", ".a { background-image: -webkit-image-set(url('url1x.png') 1x, url('url2x.png') 2x) }", [
[1, ".a { background-image: -webkit-image-set(url({./url1x.png}) 1x, url({./url2x.png}) 2x) }", ""]
]);
test("empty url", ".class { background: green url() xyz }", [
[1, ".class { background: green url() xyz }", ""]
]);
test("empty url with quotes", ".class { background: green url('') xyz }", [
[1, ".class { background: green url('') xyz }", ""]
]);
test("empty url with spaces and quotes", ".class { background: green url(' ') xyz }", [
[1, ".class { background: green url(' ') xyz }", ""]
]);
test("empty url with newline and quotes", ".class { background: green url('\n') xyz }", [
[1, ".class { background: green url('\n') xyz }", ""]
]);
test("empty url with CRLF and quotes", ".class { background: green url('\r\n') xyz }", [
[1, ".class { background: green url('\r\n') xyz }", ""]
]);
test("empty url with tab and quotes", ".class { background: green url('\t') xyz }", [
[1, ".class { background: green url('\t') xyz }", ""]
]);
test("external absolute url", ".class { background: green url(https://raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", [
[1, ".class { background: green url(https://raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", ""]
]);
test("external schema-less url", ".class { background: green url(//raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", [
[1, ".class { background: green url(//raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", ""]
]);

test("background img with url", ".class { background: green url( \"img.png\" ) xyz }", [
[1, ".class { background: green url( \"img.png\" ) xyz }", ""]
Expand Down Expand Up @@ -132,4 +156,28 @@ describe("url", function() {
test("keyframe background img with url", "@keyframes anim { background: green url('img.png') xyz }", [
[1, "@keyframes anim { background: green url('img.png') xyz }", ""]
], "?-url");
test("empty url", ".class { background: green url() xyz }", [
[1, ".class { background: green url() xyz }", ""]
], "?-url");
test("empty url with quotes", ".class { background: green url('') xyz }", [
[1, ".class { background: green url('') xyz }", ""]
], "?-url");
test("empty url with spaces and quotes", ".class { background: green url(' ') xyz }", [
[1, ".class { background: green url(' ') xyz }", ""]
], "?-url");
test("empty url with newline and quotes", ".class { background: green url('\n') xyz }", [
[1, ".class { background: green url('\n') xyz }", ""]
], "?-url");
test("empty url with CRLF and quotes", ".class { background: green url('\r\n') xyz }", [
[1, ".class { background: green url('\r\n') xyz }", ""]
], "?-url");
test("empty url with tab and quotes", ".class { background: green url('\t') xyz }", [
[1, ".class { background: green url('\t') xyz }", ""]
], "?-url");
test("external absolute url", ".class { background: green url(https://raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", [
[1, ".class { background: green url(https://raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", ""]
], "?-url");
test("external schema-less url", ".class { background: green url(//raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", [
[1, ".class { background: green url(//raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", ""]
], "?-url");
});

0 comments on commit fa2d941

Please sign in to comment.