Skip to content

Commit 184a82e

Browse files
fix(html): close a sheet the input left open before joining the next
A <style> the input left inside a string, comment or block, or with a statement unfinished, is closed as its own end of input closed it (the quote, comment end and closers, then `{}` or `;`) and the next sheet is appended to that, so every run joins again and is minified once, with no reprint. Only a sheet ending on a lone backslash is not joined, and none is joined printed as written, where the closing would stay in a custom property's value. A syntax-equivalence test holds each such run to the separate sheets' computed styles in the real browser the job runs.
1 parent 72c62b1 commit 184a82e

7 files changed

Lines changed: 194 additions & 65 deletions

File tree

‎lib/html/syntax-parser.js‎

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9927,26 +9927,28 @@ const _mergeableStyleAfter = (path, element) => {
99279927
};
99289928

99299929
/**
9930-
* Whether a sheet ends where a rule does: outside every string, comment and
9931-
* block, after a `}` or an at-rule's `;`. A sheet appended to it then starts
9932-
* rules of its own (§5.4) rather than finishing this one's last.
9930+
* What a sheet needs appended to end where a rule does, as §5.4 ends it at the
9931+
* end of input: the quote, comment end and closers it left open, then `{}` or `;`
9932+
* for a statement it did not finish. Null where it ends on a lone backslash,
9933+
* which would escape whatever followed.
99339934
* @param {string} css the sheet's text
9934-
* @returns {boolean} true when another sheet may follow it as written
9935+
* @returns {string | null} the text to append before another sheet, empty where it already ends at a rule
99359936
*/
9936-
const _sheetEndsAtRule = (css) => {
9937+
const _sheetClosing = (css) => {
99379938
/** @type {number[]} */
99389939
const closers = [];
99399940
// Whether nothing has been read since a rule ended, and whether the
99409941
// statement being read is an at-rule, which a `;` ends.
99419942
let ended = true;
99429943
let atRule = false;
9943-
for (let i = 0; i < css.length; i++) {
9944+
let open = "";
9945+
for (let i = 0; i < css.length && open.length === 0; i++) {
99449946
const c = css.charCodeAt(i);
99459947
if (isAsciiWhitespace(c)) continue;
99469948
if (c === CC_SOLIDUS && css.charCodeAt(i + 1) === CC_ASTERISK) {
99479949
const close = css.indexOf("*/", i + 2);
9948-
if (close === -1) return false;
9949-
i = close + 1;
9950+
if (close === -1) open = "*/";
9951+
else i = close + 1;
99509952
continue;
99519953
}
99529954
const top = closers.length === 0;
@@ -9960,15 +9962,18 @@ const _sheetEndsAtRule = (css) => {
99609962
}
99619963
if (top) ended = false;
99629964
if (c === CC_REVERSE_SOLIDUS) {
9963-
i++;
9965+
if (++i >= css.length) return null;
99649966
} else if (c === CC_QUOTATION_MARK || c === CC_APOSTROPHE) {
99659967
// A string ends at its quote, or at a newline as a bad string does.
99669968
for (i++; i < css.length; i++) {
99679969
const d = css.charCodeAt(i);
9968-
if (d === CC_REVERSE_SOLIDUS) i++;
9969-
else if (d === c || d === CC_LF) break;
9970+
if (d === CC_REVERSE_SOLIDUS) {
9971+
if (++i >= css.length) return null;
9972+
} else if (d === c || d === CC_LF) {
9973+
break;
9974+
}
99709975
}
9971-
if (i >= css.length) return false;
9976+
if (i >= css.length) open = String.fromCharCode(c);
99729977
} else if (c === CC_LEFT_CURLY_BRACKET) {
99739978
closers.push(CC_RIGHT_CURLY_BRACKET);
99749979
} else if (c === CC_LEFT_PARENTHESIS) {
@@ -9983,25 +9988,34 @@ const _sheetEndsAtRule = (css) => {
99839988
ended = true;
99849989
}
99859990
}
9986-
return ended;
9991+
for (let i = closers.length - 1; i >= 0; i--) {
9992+
open += String.fromCharCode(closers[i]);
9993+
}
9994+
if (closers.length !== 0) ended = closers[0] === CC_RIGHT_CURLY_BRACKET;
9995+
return ended ? open : `${open}${atRule ? ";" : "{}"}`;
99879996
};
99889997

99899998
/**
9990-
* The text a `<style>` run prints as one sheet, minified once. Each sheet's
9991-
* source is appended while the one before it ends at a rule, so the run stops
9992-
* after a sheet the input left open.
9999+
* The text a `<style>` run prints as one sheet, minified once: each sheet's
10000+
* source, the one before it closed first as its own end of input closed it.
999310001
* @param {HtmlPath} path the accessor
999410002
* @param {HtmlElement} element the run's first `<style>`
999510003
* @param {string} css its own text
999610004
* @param {boolean} absorb whether to mark the rest of the run as printed here
999710005
* @returns {string} the run's text
999810006
*/
999910007
const _styleRunText = (path, element, css, absorb) => {
10008+
// Printed as written, a closing would stay in the value a custom property
10009+
// keeps as authored (`--x: f(` reads back `f()`); minified, it is closed anyway.
10010+
const asWritten =
10011+
_S._renderEmbeddedSource === undefined &&
10012+
_S._deferEmbeddedSource === undefined;
1000010013
let text = css;
10001-
let ends = _sheetEndsAtRule(css);
10014+
let closing = _sheetClosing(css);
10015+
if (asWritten && closing !== "") closing = null;
1000210016
for (
1000310017
let next = _mergeableStyleAfter(path, element);
10004-
ends && next !== 0;
10018+
closing !== null && next !== 0;
1000510019
next = _mergeableStyleAfter(path, next)
1000610020
) {
1000710021
const source = _nodeStrings[_onlyTextChild(next)];
@@ -10010,8 +10024,9 @@ const _styleRunText = (path, element, css, absorb) => {
1001010024
if (_S._absorbed === null) _S._absorbed = new Set();
1001110025
_S._absorbed.add(next);
1001210026
}
10013-
text += source;
10014-
ends = _sheetEndsAtRule(source);
10027+
text += closing + source;
10028+
closing = _sheetClosing(source);
10029+
if (asWritten && closing !== "") closing = null;
1001510030
}
1001610031
return text;
1001710032
};
@@ -13643,7 +13658,7 @@ module.exports._sheetDecides = _sheetDecides;
1364313658
module.exports._SHEET_LEADING_AT_RULE_REGEXP = _SHEET_LEADING_AT_RULE_REGEXP;
1364413659
module.exports._mergeableStyleAfter = _mergeableStyleAfter;
1364513660
module.exports._onlyTextChild = _onlyTextChild;
13646-
module.exports._sheetEndsAtRule = _sheetEndsAtRule;
13661+
module.exports._sheetClosing = _sheetClosing;
1364713662
module.exports._styleRunText = _styleRunText;
1364813663
module.exports.baseTag = baseTag;
1364913664
module.exports.buildHeadTags = buildHeadTags;

‎test/configCases/html/minimize-merge-styles/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`ConfigCacheTestCases html minimize-merge-styles minimize-merge-styles should compile 1`] = `
4-
"<!doctype html><head><title>Merge styles</title><style>.merge-a{color:red}.merge-b{color:#00f}</style><meta name=sep content=1><style>.ws-a{color:red}.ws-b{color:#00f}</style><meta name=sep content=2><style>.link-a{color:red}</style><link rel=stylesheet href=x.css><style>.link-b{color:#00f}</style><meta name=sep content=3><style media=screen>.media-a{color:red}</style><style>.media-b{color:#00f}</style><meta name=sep content=4><style media=screen>.same-a{color:red}.same-b{color:#00f}</style><meta name=sep content=5><style>.cmt-a{color:red}.cmt-b{color:#00f}</style><meta name=sep content=6><style type=text/template>.tpl-a{ not css </style><style type=text/template>.tpl-b{ not css </style><meta name=sep content=7><style nonce=n1>.nonce-a{color:red}</style><style nonce=n2>.nonce-b{color:#00f}</style><meta name=sep content=8><style>.three-a{color:red}.three-b{color:#00f}.three-c{color:#0f0}</style><meta name=sep content=9><style>.empty-a{color:red}</style><style></style><meta name=sep content=10><style>.open-a{--x:f()}</style><style>.open-b{color:red}</style><meta name=sep content=11><style>.fold-a,.fold-b{color:red}</style><meta name=sep content=12><style>.blank-b{color:red}</style></head><body>
4+
"<!doctype html><head><title>Merge styles</title><style>.merge-a{color:red}.merge-b{color:#00f}</style><meta name=sep content=1><style>.ws-a{color:red}.ws-b{color:#00f}</style><meta name=sep content=2><style>.link-a{color:red}</style><link rel=stylesheet href=x.css><style>.link-b{color:#00f}</style><meta name=sep content=3><style media=screen>.media-a{color:red}</style><style>.media-b{color:#00f}</style><meta name=sep content=4><style media=screen>.same-a{color:red}.same-b{color:#00f}</style><meta name=sep content=5><style>.cmt-a{color:red}.cmt-b{color:#00f}</style><meta name=sep content=6><style type=text/template>.tpl-a{ not css </style><style type=text/template>.tpl-b{ not css </style><meta name=sep content=7><style nonce=n1>.nonce-a{color:red}</style><style nonce=n2>.nonce-b{color:#00f}</style><meta name=sep content=8><style>.three-a{color:red}.three-b{color:#00f}.three-c{color:#0f0}</style><meta name=sep content=9><style>.empty-a{color:red}</style><style></style><meta name=sep content=10><style>.open-a{--x:f()}.open-b{color:red}</style><meta name=sep content=11><style>.fold-a,.fold-b{color:red}</style><meta name=sep content=12><style>.blank-b{color:red}</style></head><body>
55
<svg><style>.svg-a{color:red}</style><style>.svg-b{color:#00f}</style></svg>
66
<p>x</p>
77

‎test/configCases/html/minimize-merge-styles/__snapshots__/ConfigTest.snap‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`ConfigTestCases html minimize-merge-styles minimize-merge-styles should compile 1`] = `
4-
"<!doctype html><head><title>Merge styles</title><style>.merge-a{color:red}.merge-b{color:#00f}</style><meta name=sep content=1><style>.ws-a{color:red}.ws-b{color:#00f}</style><meta name=sep content=2><style>.link-a{color:red}</style><link rel=stylesheet href=x.css><style>.link-b{color:#00f}</style><meta name=sep content=3><style media=screen>.media-a{color:red}</style><style>.media-b{color:#00f}</style><meta name=sep content=4><style media=screen>.same-a{color:red}.same-b{color:#00f}</style><meta name=sep content=5><style>.cmt-a{color:red}.cmt-b{color:#00f}</style><meta name=sep content=6><style type=text/template>.tpl-a{ not css </style><style type=text/template>.tpl-b{ not css </style><meta name=sep content=7><style nonce=n1>.nonce-a{color:red}</style><style nonce=n2>.nonce-b{color:#00f}</style><meta name=sep content=8><style>.three-a{color:red}.three-b{color:#00f}.three-c{color:#0f0}</style><meta name=sep content=9><style>.empty-a{color:red}</style><style></style><meta name=sep content=10><style>.open-a{--x:f()}</style><style>.open-b{color:red}</style><meta name=sep content=11><style>.fold-a,.fold-b{color:red}</style><meta name=sep content=12><style>.blank-b{color:red}</style></head><body>
4+
"<!doctype html><head><title>Merge styles</title><style>.merge-a{color:red}.merge-b{color:#00f}</style><meta name=sep content=1><style>.ws-a{color:red}.ws-b{color:#00f}</style><meta name=sep content=2><style>.link-a{color:red}</style><link rel=stylesheet href=x.css><style>.link-b{color:#00f}</style><meta name=sep content=3><style media=screen>.media-a{color:red}</style><style>.media-b{color:#00f}</style><meta name=sep content=4><style media=screen>.same-a{color:red}.same-b{color:#00f}</style><meta name=sep content=5><style>.cmt-a{color:red}.cmt-b{color:#00f}</style><meta name=sep content=6><style type=text/template>.tpl-a{ not css </style><style type=text/template>.tpl-b{ not css </style><meta name=sep content=7><style nonce=n1>.nonce-a{color:red}</style><style nonce=n2>.nonce-b{color:#00f}</style><meta name=sep content=8><style>.three-a{color:red}.three-b{color:#00f}.three-c{color:#0f0}</style><meta name=sep content=9><style>.empty-a{color:red}</style><style></style><meta name=sep content=10><style>.open-a{--x:f()}.open-b{color:red}</style><meta name=sep content=11><style>.fold-a,.fold-b{color:red}</style><meta name=sep content=12><style>.blank-b{color:red}</style></head><body>
55
<svg><style>.svg-a{color:red}</style><style>.svg-b{color:#00f}</style></svg>
66
<p>x</p>
77

‎test/configCases/html/minimize-merge-styles/test.config.js‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@ module.exports = {
4040
expect(page).toContain("<style nonce=n1>.nonce-a{color:red}</style>");
4141
// An empty sheet has no text node to fold into the one before it.
4242
expect(page).toContain("<style>.empty-a{color:red}</style><style></style>");
43-
// Nothing is appended to a sheet the source left open, which would take
44-
// the next sheet in; the renderer closes it in its own element.
45-
expect(page).toContain(
46-
"<style>.open-a{--x:f()}</style><style>.open-b{color:red}</style>"
47-
);
43+
// A sheet the source left open is closed as its end of input closed it
44+
// before the next is appended, so the next is not read inside it.
45+
expect(page).toContain("<style>.open-a{--x:f()}.open-b{color:red}</style>");
4846
// Joined, the run is minified as one sheet, so rules fold across it; a
4947
// blank sheet joined is nothing.
5048
expect(page).toContain("<style>.fold-a,.fold-b{color:red}</style>");

‎test/specCases/syntaxEquivalence.spectest.js‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,4 +2319,119 @@ describe("a color rewrite paints as the color it replaced", () => {
23192319
},
23202320
FILE_TIMEOUT
23212321
);
2322+
2323+
it(
2324+
"reads a merged <style> run as the sheets it joined",
2325+
async () => {
2326+
const {
2327+
builtinEmbeddedRenderer
2328+
} = require("../../lib/html/builtinEmbeddedRenderer");
2329+
2330+
const renderer = builtinEmbeddedRenderer();
2331+
// Sheets the input left open or unfinished, each joined to the next only
2332+
// once closed as its own end of input closed it.
2333+
const firsts = [
2334+
"a{}",
2335+
"a{color:red}/*x",
2336+
'a{content:"x',
2337+
'a{content:"x\n}',
2338+
"a{color:red",
2339+
"a{color:rgb(1,2,3",
2340+
"a{}b",
2341+
"a{}b,c",
2342+
"@import url(x.css)",
2343+
"a{}}",
2344+
'a{color:red}\\"',
2345+
"@media screen{a{color:red",
2346+
"a[x",
2347+
'a[x="y',
2348+
"@media (min-width:1px",
2349+
"@layer base",
2350+
"@font-face{font-family:x",
2351+
"a{color:red;/*",
2352+
"a{color:red!",
2353+
".a{--x:f(",
2354+
".a{--x:{",
2355+
"a{background:url(x",
2356+
'a{background:url(x"',
2357+
"<!-- a{color:red} -->",
2358+
"a{color:red} <!--",
2359+
"@supports (display:grid){a{color:red",
2360+
"a{color:red;b",
2361+
":is(",
2362+
"a:not(.b",
2363+
"@keyframes k{from{opacity:0",
2364+
"a{}.b{color:red}.c",
2365+
"a{color:red}}}",
2366+
"a{content:'\\'"
2367+
];
2368+
const seconds = [
2369+
"b{color:#00f}",
2370+
"@media screen{c{color:red}}",
2371+
".x,.y{margin:1px;animation-name:k}"
2372+
];
2373+
const body =
2374+
'<a class="a b" x="y">a</a><b class="b x">b</b><c class="c y">c</c><p class="a">p</p><div>d</div>';
2375+
/**
2376+
* @param {string} html a head holding both sheets
2377+
* @param {boolean} mergeStyles whether to join them
2378+
* @returns {string} the head, minified
2379+
*/
2380+
const print = (html, mergeStyles) =>
2381+
new HtmlSourceProcessor().process(html, {
2382+
mode: "minify",
2383+
mergeStyles,
2384+
renderEmbeddedSource: renderer
2385+
}).code;
2386+
const page = await browser.newPage();
2387+
try {
2388+
/**
2389+
* @param {string} head the document's head
2390+
* @returns {Promise<string>} every element's style, as the engine computes it
2391+
*/
2392+
const computed = async (head) => {
2393+
await page.setContent(
2394+
`<!doctype html><html><head>${head}</head><body>${body}</body></html>`
2395+
);
2396+
return page.evaluate(() =>
2397+
[...document.body.querySelectorAll("*")]
2398+
.map((element) => {
2399+
const style = getComputedStyle(element);
2400+
return [
2401+
"color",
2402+
"margin-top",
2403+
"background-image",
2404+
"content",
2405+
"opacity",
2406+
"animation-name",
2407+
"font-family",
2408+
"--x"
2409+
]
2410+
.map((name) => style.getPropertyValue(name))
2411+
.join("|");
2412+
})
2413+
.join("\n")
2414+
);
2415+
};
2416+
/** @type {string[]} */
2417+
const differences = [];
2418+
for (const first of firsts) {
2419+
for (const second of seconds) {
2420+
const html = `<style>${first}</style><style>${second}</style>`;
2421+
const joined = print(html, true);
2422+
if (joined.split("<style").length !== 2) {
2423+
differences.push(`not joined: ${html}`);
2424+
}
2425+
if ((await computed(print(html, false))) !== (await computed(joined))) {
2426+
differences.push(`reads differently: ${html}`);
2427+
}
2428+
}
2429+
}
2430+
expect(differences).toEqual([]);
2431+
} finally {
2432+
await page.close();
2433+
}
2434+
},
2435+
FILE_TIMEOUT
2436+
);
23222437
});

0 commit comments

Comments
 (0)