Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't handle empty @import and url() #513

Merged
merged 2 commits into from
Apr 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 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)) {
// Don't remove quotes around url when contain space
if (item.url.indexOf(" ") === -1) {
item.stringType = "";
Expand Down Expand Up @@ -160,9 +163,9 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
mode: options.mode,
rewriteUrl: function(global, url) {
if(parserOptions.url){
url = url.trim(" ");
url = url.trim();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent off? 🙃 😛


if(!loaderUtils.isUrlRequest(url, root)) {
if(!url.replace(/\s/g, '').length || !loaderUtils.isUrlRequest(url, root)) {
return url;
}
if(global) {
Expand Down
21 changes: 21 additions & 0 deletions test/importTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,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('') xyz }", ""]
]);
test("empty url with CRLF and quotes", ".class { background: green url('\r\n') xyz }", [
[1, ".class { background: green url('') xyz }", ""]
]);
test("empty url with tab and quotes", ".class { background: green url('\t') xyz }", [
[1, ".class { background: green url('') 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");
});