Skip to content

Commit 99fc364

Browse files
fix(html): read a lone @ as a delimiter when closing a sheet to join
A lone @ opens an ordinary rule's prelude, which only {} ends, so closing it with ; let the next sheet's first rule be swallowed. Found by cutting every test and corpus stylesheet and comparing the joined sheet with the two apart in Chromium.
1 parent 184a82e commit 99fc364

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎lib/html/syntax-parser.js‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9926,6 +9926,35 @@ const _mergeableStyleAfter = (path, element) => {
99269926
return 0;
99279927
};
99289928

9929+
/**
9930+
* Whether a CSS name starts at `at` (§4.3.9), which makes an `@` before it an
9931+
* at-keyword; a lone `@` is a delimiter opening an ordinary rule's prelude.
9932+
* @param {string} css the sheet's text
9933+
* @param {number} at where the name would start
9934+
* @returns {boolean} true when an identifier sequence starts there
9935+
*/
9936+
const _startsCssName = (css, at) => {
9937+
/**
9938+
* @param {number} c a code unit
9939+
* @returns {boolean} true for a name-start code point
9940+
*/
9941+
const nameStart = (c) =>
9942+
(c | 0x20) >= 0x61 && (c | 0x20) <= 0x7a ? true : c === 0x5f || c >= 0x80;
9943+
/**
9944+
* @param {number} i where a backslash would be
9945+
* @returns {boolean} true for a valid escape there
9946+
*/
9947+
const escape = (i) =>
9948+
css.charCodeAt(i) === CC_REVERSE_SOLIDUS &&
9949+
i + 1 < css.length &&
9950+
css.charCodeAt(i + 1) !== CC_LF;
9951+
const first = css.charCodeAt(at);
9952+
if (nameStart(first) || escape(at)) return true;
9953+
if (first !== CC_HYPHEN_MINUS) return false;
9954+
const second = css.charCodeAt(at + 1);
9955+
return nameStart(second) || second === CC_HYPHEN_MINUS || escape(at + 1);
9956+
};
9957+
99299958
/**
99309959
* What a sheet needs appended to end where a rule does, as §5.4 ends it at the
99319960
* end of input: the quote, comment end and closers it left open, then `{}` or `;`
@@ -9958,7 +9987,7 @@ const _sheetClosing = (css) => {
99589987
i += c === CC_LESS_THAN ? 3 : 2;
99599988
continue;
99609989
}
9961-
atRule = c === CC_COMMERCIAL_AT;
9990+
atRule = c === CC_COMMERCIAL_AT && _startsCssName(css, i + 1);
99629991
}
99639992
if (top) ended = false;
99649993
if (c === CC_REVERSE_SOLIDUS) {

‎test/unitCases/HtmlSyntax.unittest.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4230,6 +4230,9 @@ describe("SourceProcessor — merging adjacent <style>", () => {
42304230
for (const [sheet, printed] of [
42314231
["a{color:red}b", "a{color:red}"],
42324232
["a{color:red}}", "a{color:red}"],
4233+
// A lone `@` is a delimiter opening a rule's prelude, not an at-rule.
4234+
["a{color:red}@", "a{color:red}"],
4235+
["a{color:red}@-", "a{color:red}"],
42334236
["@import url(x.css)", "@import url(x.css);"],
42344237
["a[x", ""]
42354238
]) {

0 commit comments

Comments
 (0)