From a62bb30990d2d771a517579c88fb7f4a0dc10979 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Sun, 23 Apr 2017 21:40:10 +0300 Subject: [PATCH] Fixed: don't handle empty import and url [#463]. --- lib/processCss.js | 15 +++++++----- test/importTest.js | 21 ++++++++++++++++ test/urlTest.js | 60 +++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 84 insertions(+), 12 deletions(-) diff --git a/lib/processCss.js b/lib/processCss.js index 2c2fea6d..9ce76c3a 100644 --- a/lib/processCss.js +++ b/lib/processCss.js @@ -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") { @@ -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 = ""; @@ -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(), diff --git a/test/importTest.js b/test/importTest.js index 52c94510..f52425fa 100644 --- a/test/importTest.js +++ b/test/importTest.js @@ -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; }", ""] diff --git a/test/urlTest.js b/test/urlTest.js index e25a4b77..aa690271 100644 --- a/test/urlTest.js +++ b/test/urlTest.js @@ -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 }", ""] ]); @@ -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 }", ""] @@ -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"); });