Skip to content

Commit 63e2d8e

Browse files
authored
fix(css): keep the space before an SVG data: URL the minifier rewrites (#22880)
* fix(css): keep the space before an SVG `data:` URL the minifier rewrites * docs(css): describe what the deferred data URL helper now returns
1 parent 8050699 commit 63e2d8e

3 files changed

Lines changed: 36 additions & 7 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Keep the space before an SVG `data:` URL the CSS minifier rewrites.

‎lib/css/syntax-printer.js‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,7 @@ const _minifyUrlFunction = (fn, inner) => {
18141814
if (_S._deferEmbeddedSource !== undefined) {
18151815
const deferred = _deferDataUrl(
18161816
decoded,
1817+
`${fn}(`,
18171818
(url) => `${fn}(${_serializeUrl(url, mark)})`,
18181819
_plainUrlFunction(fn, written, decoded, mark) ||
18191820
`${fn}(${mark}${written}${mark})`
@@ -5278,6 +5279,7 @@ const _printUrlNode = (path, minify) => {
52785279
const opener = head === null ? raw.slice(0, open + 1) : head;
52795280
const deferred = _deferDataUrl(
52805281
_urlValue(path, unterminated),
5282+
opener,
52815283
(url) => `${opener}${_serializeUrl(url, '"')})`,
52825284
plainUrl()
52835285
);
@@ -6080,15 +6082,17 @@ const _serializeUrl = (url, mark) => {
60806082

60816083
/**
60826084
* Record a url's `data:` payload for an asynchronous caller and print the
6083-
* marker standing in for it, or `null` when there is nothing to offer. `build`
6084-
* spells the whole `url()` from the answer, so its quoting is decided by what
6085-
* the payload turns out to be rather than by what it was.
6085+
* `url()` holding the marker that stands in for it, or `null` when there is
6086+
* nothing to offer. `build` spells the whole `url()` from the answer, so its
6087+
* quoting is decided by what the payload turns out to be rather than by what
6088+
* it was.
60866089
* @param {string} body the url body, unquoted and unescaped
6090+
* @param {string} opener the `url(` that `build` and `fallback` both open with
60876091
* @param {(url: string) => string} build the `url()` text around a rebuilt URL
60886092
* @param {string} fallback the text to print when the caller declines
6089-
* @returns {string | null} the marker to print, or null when nothing is offered
6093+
* @returns {string | null} the `url()` to print, or null when nothing is offered
60906094
*/
6091-
const _deferDataUrl = (body, build, fallback) => {
6095+
const _deferDataUrl = (body, opener, build, fallback) => {
60926096
const read = _readDataUrl(body);
60936097
if (read === null) return null;
60946098
const holes = /** @type {DeferredEmbeddedSource[]} */ (
@@ -6100,11 +6104,12 @@ const _deferDataUrl = (body, build, fallback) => {
61006104
hostType: CSS_TYPE,
61016105
source: read.payload,
61026106
build: (rendered) =>
6103-
typeof rendered !== "string" || rendered === read.payload
6107+
(typeof rendered !== "string" || rendered === read.payload
61046108
? fallback
61056109
: build(buildDataURI(read.parsed, rendered))
6110+
).slice(opener.length, -1)
61066111
});
6107-
return deferredWrite(id);
6112+
return `${opener}${deferredWrite(id)})`;
61086113
};
61096114

61106115
/**

‎test/unitCases/css/printer.unittest.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12376,6 +12376,25 @@ describe("cssMinify export", () => {
1237612376
expect(code).toBe('@charset "UTF-8";a:before{content:"é"}');
1237712377
});
1237812378

12379+
it("keeps the space before an SVG `data:` URL it minifies", async () => {
12380+
const cssMinify = require("../../../lib/css/cssMinify");
12381+
12382+
const { code } = await cssMinify(
12383+
{
12384+
"a.css":
12385+
'.a{background:red url("data:image/svg+xml,<svg></svg>")}' +
12386+
".b{background:no-repeat url(data:image/svg+xml,<svg></svg>)}"
12387+
},
12388+
undefined,
12389+
{ svg: true }
12390+
);
12391+
12392+
expect(code).toBe(
12393+
".a{background:red url(data:image/svg+xml,<svg/>)}" +
12394+
".b{background:no-repeat url(data:image/svg+xml,<svg/>)}"
12395+
);
12396+
});
12397+
1237912398
describe("extractComments", () => {
1238012399
const sheet =
1238112400
"/*! one */a{color:red}/* plain */b{color:blue}/*! one */" +

0 commit comments

Comments
 (0)