Skip to content

Commit d57c21c

Browse files
fix(css,html): keep a renderer's non-text answer from breaking serialization
A renderer returning `undefined` reached `escapeAttribute` on the srcdoc path and threw on `.includes` — the one offer written back escaped rather than spliced verbatim. Both serializers now read anything but text as a renderer that did not answer, and keep the body as written.
1 parent 88c7714 commit d57c21c

4 files changed

Lines changed: 29 additions & 5 deletions

File tree

‎lib/css/syntax.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12020,7 +12020,9 @@ const _renderDataUrl = (url) => {
1202012020
} catch (_err) {
1202112021
return null;
1202212022
}
12023-
return rendered === payload ? null : buildDataURI(parsed, rendered);
12023+
// Anything but text is a renderer that did not answer, not a payload to write.
12024+
if (typeof rendered !== "string" || rendered === payload) return null;
12025+
return buildDataURI(parsed, rendered);
1202412026
};
1202512027
// What the target can read, from `output.environment` — resolved once per run
1202612028
// rather than per color. Unset means it can: only browserslist reports otherwise.

‎lib/html/syntax.js‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9202,10 +9202,11 @@ let _renderEmbeddedSource;
92029202
*/
92039203
const _renderEmbedded = (source, type) => {
92049204
try {
9205-
return /** @type {EmbeddedSourceRenderer} */ (_renderEmbeddedSource)(
9206-
source,
9207-
{ type, hostType: HTML_TYPE }
9208-
);
9205+
const rendered = /** @type {EmbeddedSourceRenderer} */ (
9206+
_renderEmbeddedSource
9207+
)(source, { type, hostType: HTML_TYPE });
9208+
// Anything but text is a renderer that did not answer, not a body to write.
9209+
return typeof rendered === "string" ? rendered : source;
92099210
} catch (_err) {
92109211
return source;
92119212
}

‎test/CssSyntax.unittest.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6534,6 +6534,15 @@ describe("SourceProcessor — renderEmbeddedSource over a data: url", () => {
65346534
expect(shrunk.map.mappings).not.toBe(plain.map.mappings);
65356535
});
65366536

6537+
it("emits the url as written when the renderer answers with a non-string", () => {
6538+
const untouched = minify(svgUrl);
6539+
for (const answer of [undefined, null, 42]) {
6540+
expect(minify(svgUrl, () => /** @type {EXPECTED_ANY} */ (answer))).toBe(
6541+
untouched
6542+
);
6543+
}
6544+
});
6545+
65376546
it("emits the url as written when the renderer declines or throws", () => {
65386547
for (const sheet of [svgUrl, base64Url]) {
65396548
const untouched = minify(sheet);

‎test/HtmlSyntax.unittest.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5137,6 +5137,18 @@ describe("SourceProcessor — renderEmbeddedSource", () => {
51375137
);
51385138
});
51395139

5140+
it("keeps the body when the renderer answers with something other than text", () => {
5141+
// `srcdoc` is written back escaped, so a non-string would reach the escaper.
5142+
const doc =
5143+
'<iframe srcdoc="&lt;p&gt;x&lt;/p&gt;"></iframe><style>.a{color:red}</style>';
5144+
const untouched = minify(doc);
5145+
for (const answer of [undefined, null, 42, {}]) {
5146+
expect(minify(doc, () => /** @type {EXPECTED_ANY} */ (answer))).toBe(
5147+
untouched
5148+
);
5149+
}
5150+
});
5151+
51405152
it("does not offer a non-CSS `<style>` or a data-block `<script>`", () => {
51415153
expect(
51425154
offered(

0 commit comments

Comments
 (0)