Skip to content

Commit b774b2f

Browse files
author
evilebottnawi
committed
Fixed: don't handle empty import and url [#463].
1 parent de4356b commit b774b2f

File tree

3 files changed

+84
-12
lines changed

3 files changed

+84
-12
lines changed

lib/processCss.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
4949
} else if(url.type === "string") {
5050
url = url.value;
5151
} else throw rule.error("Unexpected format" + rule.params);
52+
if (!url.replace(/\s/g, '').length) {
53+
return;
54+
}
5255
values.nodes[0].nodes.shift();
5356
var mediaQuery = Tokenizer.stringifyValues(values);
5457
if(loaderUtils.isUrlRequest(url, options.root) && options.mode === "global") {
@@ -101,7 +104,7 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
101104
}
102105
break;
103106
case "url":
104-
if (options.url && !/^#/.test(item.url) && loaderUtils.isUrlRequest(item.url, options.root)) {
107+
if (options.url && item.url.replace(/\s/g, '').length && !/^#/.test(item.url) && loaderUtils.isUrlRequest(item.url, options.root)) {
105108
// Don't remove quotes around url when contain space
106109
if (item.url.indexOf(" ") === -1) {
107110
item.stringType = "";
@@ -158,18 +161,18 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
158161
var pipeline = postcss([
159162
localByDefault({
160163
mode: options.mode,
161-
rewriteUrl: function(global, url) {
164+
rewriteUrl: function(global, origUrl) {
162165
if(parserOptions.url){
163-
url = url.trim(" ");
166+
var url = origUrl.trim(" ");
164167

165-
if(!loaderUtils.isUrlRequest(url, root)) {
166-
return url;
168+
if(!origUrl.replace(/\s/g, '').length || !loaderUtils.isUrlRequest(url, root)) {
169+
return origUrl;
167170
}
168171
if(global) {
169172
return loaderUtils.urlToRequest(url, root);
170173
}
171174
}
172-
return url;
175+
return origUrl;
173176
}
174177
}),
175178
extractImports(),

test/importTest.js

+21
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,33 @@ describe("import", function() {
1515
], "", {
1616
"./test.css": [[2, ".test{a: b}", ""]]
1717
});
18+
test("import empty url", "@import url();\n.class { a: b c d; }", [
19+
[1, "@import url();\n.class { a: b c d; }", ""]
20+
], "");
21+
test("import empty url with quotes", "@import url('');\n.class { a: b c d; }", [
22+
[1, "@import url('');\n.class { a: b c d; }", ""]
23+
], "");
1824
test("import with string", "@import \"test.css\";\n.class { a: b c d; }", [
1925
[2, ".test{a: b}", ""],
2026
[1, ".class { a: b c d; }", ""]
2127
], "", {
2228
"./test.css": [[2, ".test{a: b}", ""]]
2329
});
30+
test("import with empty string", "@import \"\";\n.class { a: b c d; }", [
31+
[1, "@import \"\";\n.class { a: b c d; }", ""]
32+
], "");
33+
test("import with string contain spaces", "@import \" \";\n.class { a: b c d; }", [
34+
[1, "@import \" \";\n.class { a: b c d; }", ""]
35+
], "");
36+
test("import with string contain newline", "@import \"\n\";\n.class { a: b c d; }", [
37+
[1, "@import \"\n\";\n.class { a: b c d; }", ""]
38+
], "");
39+
test("import with string contain CRLF", "@import \"\r\n\";\r\n.class { a: b c d; }", [
40+
[1, "@import \"\r\n\";\r\n.class { a: b c d; }", ""]
41+
], "");
42+
test("import with string contain tab", "@import \"\t\";\n.class { a: b c d; }", [
43+
[1, "@import \"\t\";\n.class { a: b c d; }", ""]
44+
], "");
2445
test("import 2", "@import url('test.css');\n.class { a: b c d; }", [
2546
[2, ".test{a: b}", "screen"],
2647
[1, ".class { a: b c d; }", ""]

test/urlTest.js

+54-6
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ describe("url", function() {
1515
test("background img 4", ".class { background: green url( img.png ) xyz }", [
1616
[1, ".class { background: green url({./img.png}) xyz }", ""]
1717
]);
18-
test("background img contain space in name", ".class { background: green url( \"img img.png\" ) xyz }", [
19-
[1, ".class { background: green url(\"{./img img.png}\") xyz }", ""]
20-
]);
21-
test("background 2 img contain space in name", ".class { background: green url( 'img img.png' ) xyz }", [
22-
[1, ".class { background: green url('{./img img.png}') xyz }", ""]
23-
]);
18+
test("background img contain space in name", ".class { background: green url( \"img img.png\" ) xyz }", [
19+
[1, ".class { background: green url(\"{./img img.png}\") xyz }", ""]
20+
]);
21+
test("background 2 img contain space in name", ".class { background: green url( 'img img.png' ) xyz }", [
22+
[1, ".class { background: green url('{./img img.png}') xyz }", ""]
23+
]);
2424
test("background img absolute", ".class { background: green url(/img.png) xyz }", [
2525
[1, ".class { background: green url(/img.png) xyz }", ""]
2626
]);
@@ -70,6 +70,30 @@ describe("url", function() {
7070
test("-webkit-image-set", ".a { background-image: -webkit-image-set(url('url1x.png') 1x, url('url2x.png') 2x) }", [
7171
[1, ".a { background-image: -webkit-image-set(url({./url1x.png}) 1x, url({./url2x.png}) 2x) }", ""]
7272
]);
73+
test("empty url", ".class { background: green url() xyz }", [
74+
[1, ".class { background: green url() xyz }", ""]
75+
]);
76+
test("empty url with quotes", ".class { background: green url('') xyz }", [
77+
[1, ".class { background: green url('') xyz }", ""]
78+
]);
79+
test("empty url with spaces and quotes", ".class { background: green url(' ') xyz }", [
80+
[1, ".class { background: green url(' ') xyz }", ""]
81+
]);
82+
test("empty url with newline and quotes", ".class { background: green url('\n') xyz }", [
83+
[1, ".class { background: green url('\n') xyz }", ""]
84+
]);
85+
test("empty url with CRLF and quotes", ".class { background: green url('\r\n') xyz }", [
86+
[1, ".class { background: green url('\r\n') xyz }", ""]
87+
]);
88+
test("empty url with tab and quotes", ".class { background: green url('\t') xyz }", [
89+
[1, ".class { background: green url('\t') xyz }", ""]
90+
]);
91+
test("external absolute url", ".class { background: green url(https://raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", [
92+
[1, ".class { background: green url(https://raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", ""]
93+
]);
94+
test("external schema-less url", ".class { background: green url(//raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", [
95+
[1, ".class { background: green url(//raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", ""]
96+
]);
7397

7498
test("background img with url", ".class { background: green url( \"img.png\" ) xyz }", [
7599
[1, ".class { background: green url( \"img.png\" ) xyz }", ""]
@@ -132,4 +156,28 @@ describe("url", function() {
132156
test("keyframe background img with url", "@keyframes anim { background: green url('img.png') xyz }", [
133157
[1, "@keyframes anim { background: green url('img.png') xyz }", ""]
134158
], "?-url");
159+
test("empty url", ".class { background: green url() xyz }", [
160+
[1, ".class { background: green url() xyz }", ""]
161+
], "?-url");
162+
test("empty url with quotes", ".class { background: green url('') xyz }", [
163+
[1, ".class { background: green url('') xyz }", ""]
164+
], "?-url");
165+
test("empty url with spaces and quotes", ".class { background: green url(' ') xyz }", [
166+
[1, ".class { background: green url(' ') xyz }", ""]
167+
], "?-url");
168+
test("empty url with newline and quotes", ".class { background: green url('\n') xyz }", [
169+
[1, ".class { background: green url('\n') xyz }", ""]
170+
], "?-url");
171+
test("empty url with CRLF and quotes", ".class { background: green url('\r\n') xyz }", [
172+
[1, ".class { background: green url('\r\n') xyz }", ""]
173+
], "?-url");
174+
test("empty url with tab and quotes", ".class { background: green url('\t') xyz }", [
175+
[1, ".class { background: green url('\t') xyz }", ""]
176+
], "?-url");
177+
test("external absolute url", ".class { background: green url(https://raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", [
178+
[1, ".class { background: green url(https://raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", ""]
179+
], "?-url");
180+
test("external schema-less url", ".class { background: green url(//raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", [
181+
[1, ".class { background: green url(//raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", ""]
182+
], "?-url");
135183
});

0 commit comments

Comments
 (0)