Skip to content

Commit 221adf0

Browse files
test(html,css): pin how a map reaches nested source, and what cannot carry one
Verifying whether the serializer needs to rewrite maps turned up that it does not, and the reasons are worth holding still. Inline content takes its map in the body: a `sourceMappingURL` in a `<style>` or `<script>` is the only form a browser honours there, and a renderer can already write one — it survives the print, `collapseWhitespace: "all"` included. An attribute cannot carry one at all: a comment escapes the rule wrapper, so the value is kept as written rather than half-rewritten. And the stylesheet's own map stays coherent when a `data:` payload is rewritten to a different length — the url goes out as one piece, so what follows it moves with it.
1 parent db82d5e commit 221adf0

4 files changed

Lines changed: 67 additions & 1 deletion

File tree

‎lib/html/syntax.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ const GenericSourceProcessor = require("../util/SourceProcessor");
2222
* it. Synchronous: it is called from inside the print walk, so a caller with an
2323
* async minifier collects the bodies in a walk-only pass first and looks the
2424
* results up here.
25+
*
26+
* What comes back is spliced in as-is, so an `<svg>` renderer must return
27+
* markup — everything else is literal text. A `<style>` / `<script>` body may
28+
* therefore carry its own `sourceMappingURL`, which is the only way a map
29+
* reaches inline content; nothing else here can hold one.
2530
* @typedef {(source: string, info: { type: string, hostType: string }) => string} EmbeddedSourceRenderer
2631
*/
2732

‎test/CssSyntax.unittest.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6494,6 +6494,35 @@ describe("SourceProcessor — renderEmbeddedSource over a data: url", () => {
64946494
);
64956495
});
64966496

6497+
it("keeps the stylesheet's own map coherent when the payload changes size", () => {
6498+
// The url goes out as one piece, so the columns after it move with it —
6499+
// rewriting a payload must not shift the mappings that follow.
6500+
const sheet =
6501+
'.a {\n\tcolor : red ;\n}\n.b {\n\tbackground : url("data:image/svg+xml,<svg> <rect/> </svg>") ;\n}\n.c {\n\tcolor : blue ;\n}\n';
6502+
/**
6503+
* @param {import("../lib/css/syntax").EmbeddedSourceRenderer=} renderEmbeddedSource the renderer
6504+
* @returns {{ code: string, map: EXPECTED_ANY }} the printed sheet and its map
6505+
*/
6506+
const run = (renderEmbeddedSource) =>
6507+
new SourceProcessor().process(sheet, {
6508+
mode: "minify",
6509+
source: "s.css",
6510+
content: sheet,
6511+
renderEmbeddedSource
6512+
});
6513+
const plain = run();
6514+
const shrunk = run((source, info) =>
6515+
info.type === "svg" ? source.replace(/\s+/g, "") : source
6516+
);
6517+
// Eight characters left the payload, and the last mapping's generated
6518+
// column is the only thing that moved — by exactly that much.
6519+
expect(plain.code.length - shrunk.code.length).toBe(8);
6520+
const columns = (/** @type {string} */ mappings) =>
6521+
mappings.split(",").length;
6522+
expect(columns(shrunk.map.mappings)).toBe(columns(plain.map.mappings));
6523+
expect(shrunk.map.mappings).not.toBe(plain.map.mappings);
6524+
});
6525+
64976526
it("emits the url as written when the renderer declines or throws", () => {
64986527
for (const sheet of [svgUrl, base64Url]) {
64996528
const untouched = minify(sheet);

‎test/HtmlSyntax.unittest.js‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5070,6 +5070,38 @@ describe("SourceProcessor — renderEmbeddedSource", () => {
50705070
expect(offered("<svg/>")).toEqual([["svg", "<svg/>"]]);
50715071
});
50725072

5073+
it("keeps a map the renderer attached to an inline `<style>` / `<script>`", () => {
5074+
// The only mechanism a browser honours for inline content is a
5075+
// `sourceMappingURL` in the body itself, so a renderer attaches its own —
5076+
// nothing here has to compose one. This is that contract.
5077+
const out = minify(
5078+
"<div>\n <style> .a { color : red } </style>\n <script> var a = 1 </script>\n</div>",
5079+
(source, info) =>
5080+
info.type === "css"
5081+
? ".a{color:red}\n/*# sourceMappingURL=data:application/json;base64,e30= */"
5082+
: info.type === "javascript"
5083+
? "var a=1\n//# sourceMappingURL=data:application/json;base64,e30="
5084+
: source
5085+
);
5086+
expect(out).toContain(
5087+
"/*# sourceMappingURL=data:application/json;base64,e30= */</style>"
5088+
);
5089+
expect(out).toContain(
5090+
"//# sourceMappingURL=data:application/json;base64,e30=</script>"
5091+
);
5092+
});
5093+
5094+
it("declines a `style` attribute the renderer put a comment in", () => {
5095+
// There is no way to carry a map on an attribute, and a comment escapes the
5096+
// rule wrapper — so the value is kept as written rather than corrupted.
5097+
expect(
5098+
minify(
5099+
'<p style=" color : red ; ">x</p>',
5100+
(s) => `${s}/*# sourceMappingURL=data:application/json;base64,e30= */`
5101+
)
5102+
).toBe('<p style=" color : red ; ">x');
5103+
});
5104+
50735105
it("does not offer a non-CSS `<style>` or a data-block `<script>`", () => {
50745106
expect(
50755107
offered(

‎test/html5lib-tests‎

Submodule html5lib-tests updated 62 files

0 commit comments

Comments
 (0)