From 65e951e412e92447390ed44817490ab7193b5f6a Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Sun, 6 Feb 2022 20:10:14 +0300 Subject: [PATCH] fix: data url handling in css-loader (#34034) --- .../webpack/loaders/css-loader/src/utils.js | 26 +++++++++++++++++-- .../css-fixtures/data-url/pages/index.js | 9 +++++++ .../data-url/pages/index.module.css | 4 +++ test/integration/css/test/index.test.js | 22 ++++++++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 test/integration/css-fixtures/data-url/pages/index.js create mode 100644 test/integration/css-fixtures/data-url/pages/index.module.css diff --git a/packages/next/build/webpack/loaders/css-loader/src/utils.js b/packages/next/build/webpack/loaders/css-loader/src/utils.js index d3e1198e5fcf..0ab83b2fd9ec 100644 --- a/packages/next/build/webpack/loaders/css-loader/src/utils.js +++ b/packages/next/build/webpack/loaders/css-loader/src/utils.js @@ -43,6 +43,10 @@ function normalizePath(file) { return path.sep === '\\' ? file.replace(/\\/g, '/') : file } +function fixedEncodeURIComponent(str) { + return str.replace(/[!'()*]/g, (c) => `%${c.charCodeAt(0).toString(16)}`) +} + function normalizeUrl(url, isStringValue) { let normalizedUrl = url @@ -51,10 +55,28 @@ function normalizeUrl(url, isStringValue) { } if (matchNativeWin32Path.test(url)) { - return decodeURIComponent(normalizedUrl) + try { + normalizedUrl = decodeURIComponent(normalizedUrl) + } catch (error) { + // Ignores invalid and broken URLs and try to resolve them as is + } + + return normalizedUrl + } + + normalizedUrl = unescape(normalizedUrl) + + if (isDataUrl(url)) { + return fixedEncodeURIComponent(normalizedUrl) + } + + try { + normalizedUrl = decodeURI(normalizedUrl) + } catch (error) { + // Ignores invalid and broken URLs and try to resolve them as is } - return decodeURIComponent(unescape(normalizedUrl)) + return normalizedUrl } function requestify(url, rootContext) { diff --git a/test/integration/css-fixtures/data-url/pages/index.js b/test/integration/css-fixtures/data-url/pages/index.js new file mode 100644 index 000000000000..2a5dc77aec1b --- /dev/null +++ b/test/integration/css-fixtures/data-url/pages/index.js @@ -0,0 +1,9 @@ +import { className } from './index.module.css' + +export default function Home() { + return ( +