From 61bc72f93afbfcb24058d59fcd06dbc891efce13 Mon Sep 17 00:00:00 2001 From: Jack Westbrook Date: Wed, 16 Aug 2023 07:18:06 +0200 Subject: [PATCH] fix(module-types): ignore undefined when checking for quotes in css url strings (#2467) --- src/extras/module-types.js | 2 +- test/browser/core.js | 11 +++++++++++ test/fixtures/css-modules/url-without-quotes.css | 9 +++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/css-modules/url-without-quotes.css diff --git a/src/extras/module-types.js b/src/extras/module-types.js index 3f429aa6a..d7dcd2620 100644 --- a/src/extras/module-types.js +++ b/src/extras/module-types.js @@ -40,7 +40,7 @@ import { resolveUrl } from '../common.js'; return res.text() .then(function (source) { source = source.replace(/url\(\s*(?:(["'])((?:\\.|[^\n\\"'])+)\1|((?:\\.|[^\s,"'()\\])+))\s*\)/g, function (match, quotes, relUrl1, relUrl2) { - return 'url(' + quotes + resolveUrl(relUrl1 || relUrl2, url) + quotes + ')'; + return ['url(', quotes, resolveUrl(relUrl1 || relUrl2, url), quotes, ')'].join(''); }); return new Response(new Blob([ 'System.register([],function(e){return{execute:function(){var s=new CSSStyleSheet();s.replaceSync(' + JSON.stringify(source) + ');e("default",s)}}})' diff --git a/test/browser/core.js b/test/browser/core.js index a89b6eca1..fdfa06c5b 100644 --- a/test/browser/core.js +++ b/test/browser/core.js @@ -227,6 +227,17 @@ suite('SystemJS Standard Tests', function() { }); }); + test('should handle css modules with urls without quotes', function () { + return System.import('fixturesbase/css-modules/url-without-quotes.css').then(function (m) { + assert.ok(m); + assert.ok(isCSSStyleSheet(m.default)); + assert.equal(m.default.cssRules[0].cssText,'.hello { background-image: url("http://localhost:8080/test/fixtures/css-modules/path/to/image.png"); }') + assert.equal(m.default.cssRules[1].cssText,'.world { background-image: url("http://localhost:8080/test/fixtures/css-modules/path/to/image.png"); }') + assert.equal(m.default.cssRules[2].cssText,'body { background-image: url("http://localhost:8080/test/fixtures/css-modules/path/to/image.png"); }') + document.adoptedStyleSheets = document.adoptedStyleSheets.concat(m.default); + }); + }); + test('should support application/javascript css module override', function () { return System.import('fixturesbase/css-modules/javascript.css').then(function (m) { assert.ok(m); diff --git a/test/fixtures/css-modules/url-without-quotes.css b/test/fixtures/css-modules/url-without-quotes.css new file mode 100644 index 000000000..518447af7 --- /dev/null +++ b/test/fixtures/css-modules/url-without-quotes.css @@ -0,0 +1,9 @@ +.hello { + background-image: url(./path/to/image.png); +} +.world { + background-image: url('./path/to/image.png'); +} +body { + background-image: url("./path/to/image.png"); +}