From 2e4ec095558828bbdeff29136c3fe72f4b159055 Mon Sep 17 00:00:00 2001 From: Sebastian Werner Date: Thu, 17 Aug 2017 10:11:29 +0200 Subject: [PATCH] fix: stricter `@import` tolerance (#593) * Added failed test for @import-normalize * Fixed import handling not matching import like constructs. * Made tolerant for null match on urls inside import * Added request test case --- lib/processCss.js | 6 +++--- test/importTest.js | 20 ++++++++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/processCss.js b/lib/processCss.js index 8c688e34..944bc89f 100644 --- a/lib/processCss.js +++ b/lib/processCss.js @@ -42,12 +42,12 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) { } if(options.import) { - css.walkAtRules(/import/i, function(rule) { + css.walkAtRules(/^import$/i, function(rule) { var values = Tokenizer.parseValues(rule.params); var url = values.nodes[0].nodes[0]; - if(url.type === "url") { + if(url && url.type === "url") { url = url.url; - } else if(url.type === "string") { + } else if(url && url.type === "string") { url = url.value; } else throw rule.error("Unexpected format " + rule.params); if (!url.replace(/\s/g, '').length) { diff --git a/test/importTest.js b/test/importTest.js index f52425fa..f3cdba58 100644 --- a/test/importTest.js +++ b/test/importTest.js @@ -1,6 +1,10 @@ /*globals describe */ -var test = require("./helpers").test; +var assert = require('assert'); + +var helpers = require("./helpers"); +var test = helpers.test; +var testError = helpers.testError; describe("import", function() { test("import", "@import url(test.css);\n.class { a: b c d; }", [ @@ -66,4 +70,16 @@ describe("import", function() { test("import disabled", "@import url(test.css);\n.class { a: b c d; }", [ [1, "@import url(test.css);\n.class { a: b c d; }", ""] ], "?-import"); -}); + test("@import-normalize left untouched", "@import-normalize;", [ + [1, "@import-normalize;", ""] + ]); + testError("@import without url", "@import;", function(err) { + assert.equal(err.message, [ + 'Unexpected format (1:1)', + '', + '> 1 | @import;', + ' | ^', + '', + ].join('\n')) + }) +})