Skip to content

Commit a89cf36

Browse files
alexander-akaitclaude
authored andcommitted
fix(javascript,html): keep parseProgram's callback state its own, ack frame where inserted
A parseProgram started from its own parseError callback replaced the running call's callback and released its tree; it now throws instead. A SyntaxError the callback throws is rethrown rather than read as the parse failing. An ignored <frame/> in body now reports its trailing solidus: only frameset mode acknowledges it, so the name-wide exemption goes. END_TAG_OPTIONAL_AT_BODY_END spreads IMPLIED_END_TAG_ELEMENTS. Claude-Session: https://claude.ai/code/session_015gdnghKbLJ7VvJ5nNCNHhH
1 parent 11f73f2 commit a89cf36

6 files changed

Lines changed: 43 additions & 18 deletions

File tree

‎lib/html/data.js‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,12 +2001,7 @@ const JAVASCRIPT_SCRIPT_TYPES = new Set([
20012001
* Start tags besides the void elements whose self-closing flag tree construction acknowledges.
20022002
* @type {Set<string>}
20032003
*/
2004-
const ACKNOWLEDGED_NON_VOID = new Set([
2005-
"basefont",
2006-
"bgsound",
2007-
"frame",
2008-
"image"
2009-
]);
2004+
const ACKNOWLEDGED_NON_VOID = new Set(["basefont", "bgsound", "image"]);
20102005

20112006
/**
20122007
* What the end of the body may leave open without a parse error: elements whose end tag is optional.

‎lib/html/syntax-parser.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9447,6 +9447,7 @@ modes.inFrameset = (t) => {
94479447
if (t.type === TOKEN_START_TAG && t.name === "frame") {
94489448
insertHtmlElement("frame", t.attrs, t.pos);
94499449
popOpen();
9450+
selfClosingAcknowledged = true;
94509451
return;
94519452
}
94529453
if (t.type === TOKEN_START_TAG && t.name === "noframes") {

‎lib/javascript/syntax-parser.js‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21139,6 +21139,8 @@ const A = {
2113921139
let programParsing = false;
2114021140
/** @type {JavascriptParseOptions["parseError"]} */
2114121141
let programParseError;
21142+
// Whether the callback threw, so a `SyntaxError` it threw is rethrown.
21143+
let programParseErrorThrew = false;
2114221144
/** @type {Set<string>} */
2114321145
const programErrorsSeen = new Set();
2114421146
/** @type {[RegExp, string][] | undefined} */
@@ -21178,7 +21180,9 @@ const reportProgramError = (parser, pos, message, severity) => {
2117821180
if (programErrorsSeen.has(key)) return;
2117921181
programErrorsSeen.add(key);
2118021182
const end = parser.start === pos && parser.end > pos ? parser.end : pos;
21183+
programParseErrorThrew = true;
2118121184
programParseError(parser.input, code, pos, end, severity);
21185+
programParseErrorThrew = false;
2118221186
};
2118321187

2118421188
// Whether a `SourceProcessor` walk is running, its tree then the one `A` reads.
@@ -21198,6 +21202,11 @@ const parseProgram = (input, pos = 0, options = {}) => {
2119821202
"javascript syntax: process() is running; a visitor cannot start another JavaScript parse"
2119921203
);
2120021204
}
21205+
if (programParsing) {
21206+
throw new Error(
21207+
"javascript syntax: parseProgram() is running; its parseError callback cannot start another JavaScript parse"
21208+
);
21209+
}
2120121210
releaseParsedTree();
2120221211
const tree = new SyntaxTree();
2120321212
programParsing = true;
@@ -21222,11 +21231,14 @@ const parseProgram = (input, pos = 0, options = {}) => {
2122221231
);
2122321232
} catch (error) {
2122421233
tree.release();
21225-
if (error instanceof SyntaxError) return undefined;
21234+
if (error instanceof SyntaxError && !programParseErrorThrew) {
21235+
return undefined;
21236+
}
2122621237
throw error;
2122721238
} finally {
2122821239
programParsing = false;
2122921240
programParseError = undefined;
21241+
programParseErrorThrew = false;
2123021242
}
2123121243
walkTree = tree;
2123221244
walkInput = input;

‎test/unitCases/html/tokenizer.unittest.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3067,6 +3067,8 @@ describe("tokenize", () => {
30673067
]
30683068
],
30693069
["<!DOCTYPE html><div/><br/><svg/><math/><svg><g/></svg>", undefined, ["non-void-html-element-start-tag-with-trailing-solidus", "expected-closing-tag-but-got-eof"]],
3070+
["<!DOCTYPE html><frameset><frame/></frameset>", undefined, []],
3071+
["<!DOCTYPE html><body><frame/>", undefined, ["unexpected-start-tag-ignored", "non-void-html-element-start-tag-with-trailing-solidus"]],
30703072
["</frameset>x", "frameset", ["unexpected-frameset-in-frameset-innerhtml", "unexpected-char-in-frameset"]],
30713073
["<body></body></html>", "html", ["unexpected-end-tag-after-body-innerhtml"]],
30723074
])("parseHtml reports the tree's errors in %j", (source, fragmentContext, codes) => {

‎test/unitCases/javascript/parser.unittest.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5180,6 +5180,30 @@ describe("parseProgram", () => {
51805180
expect(parseProgram("a ~ ")).toBeUndefined();
51815181
});
51825182

5183+
it("rethrows a syntax error the callback throws", () => {
5184+
expect(() =>
5185+
parseProgram("let a; let a", 0, {
5186+
parseError: () => {
5187+
throw new SyntaxError("callback");
5188+
}
5189+
})
5190+
).toThrow("callback");
5191+
});
5192+
5193+
it("refuses a parse started from its own callback", () => {
5194+
/** @type {string[]} */
5195+
const codes = [];
5196+
expect(() =>
5197+
parseProgram("let a; let a", 0, {
5198+
parseError: (input, code) => {
5199+
codes.push(code);
5200+
parseProgram("x");
5201+
}
5202+
})
5203+
).toThrow("parseProgram() is running");
5204+
expect(codes).toEqual(["var-redeclaration"]);
5205+
});
5206+
51835207
it("rethrows what is not a syntax error", () => {
51845208
expect(() =>
51855209
parseProgram("a", 0, {

‎tooling/generate-html-data.js‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,23 +1705,14 @@ const PARSER_TABLES = [
17051705
"ACKNOWLEDGED_NON_VOID",
17061706
"set",
17071707
"Start tags besides the void elements whose self-closing flag tree construction acknowledges.",
1708-
["basefont", "bgsound", "frame", "image"]
1708+
["basefont", "bgsound", "image"]
17091709
],
17101710
[
17111711
"END_TAG_OPTIONAL_AT_BODY_END",
17121712
"set",
17131713
"What the end of the body may leave open without a parse error: elements whose end tag is optional.",
17141714
[
1715-
"dd",
1716-
"dt",
1717-
"li",
1718-
"optgroup",
1719-
"option",
1720-
"p",
1721-
"rb",
1722-
"rp",
1723-
"rt",
1724-
"rtc",
1715+
...IMPLIED_END_TAG_ELEMENTS,
17251716
"tbody",
17261717
"td",
17271718
"tfoot",

0 commit comments

Comments
 (0)