Skip to content

Commit 868fc94

Browse files
evilebottnawimichael-ciniawsky
authored andcommitted
fix: don't handle empty @import and url() (#513)
1 parent 06d27a1 commit 868fc94

File tree

3 files changed

+81
-9
lines changed

3 files changed

+81
-9
lines changed

lib/processCss.js

+6-3
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 = "";
@@ -160,9 +163,9 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
160163
mode: options.mode,
161164
rewriteUrl: function(global, url) {
162165
if(parserOptions.url){
163-
url = url.trim(" ");
166+
url = url.trim();
164167

165-
if(!loaderUtils.isUrlRequest(url, root)) {
168+
if(!url.replace(/\s/g, '').length || !loaderUtils.isUrlRequest(url, root)) {
166169
return url;
167170
}
168171
if(global) {

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
]);
@@ -79,6 +79,30 @@ describe("url", function() {
7979
test("-webkit-image-set", ".a { background-image: -webkit-image-set(url('url1x.png') 1x, url('url2x.png') 2x) }", [
8080
[1, ".a { background-image: -webkit-image-set(url({./url1x.png}) 1x, url({./url2x.png}) 2x) }", ""]
8181
]);
82+
test("empty url", ".class { background: green url() xyz }", [
83+
[1, ".class { background: green url() xyz }", ""]
84+
]);
85+
test("empty url with quotes", ".class { background: green url('') xyz }", [
86+
[1, ".class { background: green url('') xyz }", ""]
87+
]);
88+
test("empty url with spaces and quotes", ".class { background: green url(' ') xyz }", [
89+
[1, ".class { background: green url('') xyz }", ""]
90+
]);
91+
test("empty url with newline and quotes", ".class { background: green url('\n') xyz }", [
92+
[1, ".class { background: green url('') xyz }", ""]
93+
]);
94+
test("empty url with CRLF and quotes", ".class { background: green url('\r\n') xyz }", [
95+
[1, ".class { background: green url('') xyz }", ""]
96+
]);
97+
test("empty url with tab and quotes", ".class { background: green url('\t') xyz }", [
98+
[1, ".class { background: green url('') xyz }", ""]
99+
]);
100+
test("external absolute url", ".class { background: green url(https://raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", [
101+
[1, ".class { background: green url(https://raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", ""]
102+
]);
103+
test("external schema-less url", ".class { background: green url(//raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", [
104+
[1, ".class { background: green url(//raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", ""]
105+
]);
82106

83107
test("background img with url", ".class { background: green url( \"img.png\" ) xyz }", [
84108
[1, ".class { background: green url( \"img.png\" ) xyz }", ""]
@@ -141,4 +165,28 @@ describe("url", function() {
141165
test("keyframe background img with url", "@keyframes anim { background: green url('img.png') xyz }", [
142166
[1, "@keyframes anim { background: green url('img.png') xyz }", ""]
143167
], "?-url");
168+
test("empty url", ".class { background: green url() xyz }", [
169+
[1, ".class { background: green url() xyz }", ""]
170+
], "?-url");
171+
test("empty url with quotes", ".class { background: green url('') xyz }", [
172+
[1, ".class { background: green url('') xyz }", ""]
173+
], "?-url");
174+
test("empty url with spaces and quotes", ".class { background: green url(' ') xyz }", [
175+
[1, ".class { background: green url(' ') xyz }", ""]
176+
], "?-url");
177+
test("empty url with newline and quotes", ".class { background: green url('\n') xyz }", [
178+
[1, ".class { background: green url('\n') xyz }", ""]
179+
], "?-url");
180+
test("empty url with CRLF and quotes", ".class { background: green url('\r\n') xyz }", [
181+
[1, ".class { background: green url('\r\n') xyz }", ""]
182+
], "?-url");
183+
test("empty url with tab and quotes", ".class { background: green url('\t') xyz }", [
184+
[1, ".class { background: green url('\t') xyz }", ""]
185+
], "?-url");
186+
test("external absolute url", ".class { background: green url(https://raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", [
187+
[1, ".class { background: green url(https://raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", ""]
188+
], "?-url");
189+
test("external schema-less url", ".class { background: green url(//raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", [
190+
[1, ".class { background: green url(//raw.githubusercontent.com/webpack/media/master/logo/icon.png) xyz }", ""]
191+
], "?-url");
144192
});

0 commit comments

Comments
 (0)