Skip to content

Commit fa1eb9b

Browse files
fb55claude
andcommitted
fix(parser): close nested templates at EOF without recursion
At EOF inside templates, `eofInTemplate` popped one template and reprocessed the EOF token, once per nesting level and through recursive calls, so deep nesting exhausted the call stack; the template insertion-mode stack was also kept at the front with `unshift`/`shift`. The loop now pops every open template (open elements, formatting marker and mode) in one `do … while`, and the mode stack is kept at its tail. Nested templates at EOF complete with the same tree; the template-modes test covers the tail-maintained stack. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent ea24015 commit fa1eb9b

3 files changed

Lines changed: 53 additions & 14 deletions

File tree

‎packages/parse5/lib/parser/index.ts‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export class Parser<T extends TreeAdapterTypeMap> implements TokenHandler, Stack
195195
const parser = new this(opts, documentMock, fragmentContext);
196196

197197
if (parser.fragmentContextID === $.TEMPLATE) {
198-
parser.tmplInsertionModeStack.unshift(InsertionMode.IN_TEMPLATE);
198+
parser.tmplInsertionModeStack.push(InsertionMode.IN_TEMPLATE);
199199
}
200200

201201
parser._initTokenizerForFragmentParsing();
@@ -239,8 +239,7 @@ export class Parser<T extends TreeAdapterTypeMap> implements TokenHandler, Stack
239239
protected currentNotInHTML = false;
240240

241241
/**
242-
* The template insertion mode stack is maintained from the left.
243-
* Ie. the topmost element will always have index 0.
242+
* The topmost template insertion mode is the last element.
244243
*
245244
* @internal
246245
*/
@@ -755,7 +754,7 @@ export class Parser<T extends TreeAdapterTypeMap> implements TokenHandler, Stack
755754
return;
756755
}
757756
case $.TEMPLATE: {
758-
this.insertionMode = this.tmplInsertionModeStack[0];
757+
this.insertionMode = this.tmplInsertionModeStack[this.tmplInsertionModeStack.length - 1];
759758
return;
760759
}
761760
case $.HTML: {
@@ -1771,7 +1770,7 @@ function startTagInHead<T extends TreeAdapterTypeMap>(p: Parser<T>, token: TagTo
17711770
p.activeFormattingElements.insertMarker();
17721771
p.framesetOk = false;
17731772
p.insertionMode = InsertionMode.IN_TEMPLATE;
1774-
p.tmplInsertionModeStack.unshift(InsertionMode.IN_TEMPLATE);
1773+
p.tmplInsertionModeStack.push(InsertionMode.IN_TEMPLATE);
17751774
break;
17761775
}
17771776
case $.HEAD: {
@@ -1817,7 +1816,7 @@ function templateEndTagInHead<T extends TreeAdapterTypeMap>(p: Parser<T>, token:
18171816

18181817
p.openElements.popUntilTagNamePopped($.TEMPLATE);
18191818
p.activeFormattingElements.clearToLastMarker();
1820-
p.tmplInsertionModeStack.shift();
1819+
p.tmplInsertionModeStack.pop();
18211820
p._resetInsertionMode();
18221821
} else {
18231822
p._err(token, ERR.endTagWithoutMatchingOpenElement);
@@ -3498,32 +3497,32 @@ function startTagInTemplate<T extends TreeAdapterTypeMap>(p: Parser<T>, token: T
34983497
case $.TBODY:
34993498
case $.TFOOT:
35003499
case $.THEAD: {
3501-
p.tmplInsertionModeStack[0] = InsertionMode.IN_TABLE;
3500+
p.tmplInsertionModeStack[p.tmplInsertionModeStack.length - 1] = InsertionMode.IN_TABLE;
35023501
p.insertionMode = InsertionMode.IN_TABLE;
35033502
startTagInTable(p, token);
35043503
break;
35053504
}
35063505
case $.COL: {
3507-
p.tmplInsertionModeStack[0] = InsertionMode.IN_COLUMN_GROUP;
3506+
p.tmplInsertionModeStack[p.tmplInsertionModeStack.length - 1] = InsertionMode.IN_COLUMN_GROUP;
35083507
p.insertionMode = InsertionMode.IN_COLUMN_GROUP;
35093508
startTagInColumnGroup(p, token);
35103509
break;
35113510
}
35123511
case $.TR: {
3513-
p.tmplInsertionModeStack[0] = InsertionMode.IN_TABLE_BODY;
3512+
p.tmplInsertionModeStack[p.tmplInsertionModeStack.length - 1] = InsertionMode.IN_TABLE_BODY;
35143513
p.insertionMode = InsertionMode.IN_TABLE_BODY;
35153514
startTagInTableBody(p, token);
35163515
break;
35173516
}
35183517
case $.TD:
35193518
case $.TH: {
3520-
p.tmplInsertionModeStack[0] = InsertionMode.IN_ROW;
3519+
p.tmplInsertionModeStack[p.tmplInsertionModeStack.length - 1] = InsertionMode.IN_ROW;
35213520
p.insertionMode = InsertionMode.IN_ROW;
35223521
startTagInRow(p, token);
35233522
break;
35243523
}
35253524
default: {
3526-
p.tmplInsertionModeStack[0] = InsertionMode.IN_BODY;
3525+
p.tmplInsertionModeStack[p.tmplInsertionModeStack.length - 1] = InsertionMode.IN_BODY;
35273526
p.insertionMode = InsertionMode.IN_BODY;
35283527
startTagInBody(p, token);
35293528
}
@@ -3538,9 +3537,12 @@ function endTagInTemplate<T extends TreeAdapterTypeMap>(p: Parser<T>, token: Tag
35383537

35393538
function eofInTemplate<T extends TreeAdapterTypeMap>(p: Parser<T>, token: EOFToken): void {
35403539
if (p.openElements.tmplCount > 0) {
3541-
p.openElements.popUntilTagNamePopped($.TEMPLATE);
3542-
p.activeFormattingElements.clearToLastMarker();
3543-
p.tmplInsertionModeStack.shift();
3540+
// EOF closes every open template.
3541+
do {
3542+
p.openElements.popUntilTagNamePopped($.TEMPLATE);
3543+
p.activeFormattingElements.clearToLastMarker();
3544+
p.tmplInsertionModeStack.pop();
3545+
} while (p.openElements.tmplCount > 0);
35443546
p._resetInsertionMode();
35453547
p.onEof(token);
35463548
} else {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { parse, parseFragment, type TreeAdapter } from 'parse5';
3+
import { treeAdapters } from 'parse5-test-utils/utils/common.js';
4+
5+
describe.each(Object.entries(treeAdapters) as [string, TreeAdapter][])('template EOF (%s)', (_name, treeAdapter) => {
6+
it.each([
7+
'<template>',
8+
'<template><div>',
9+
'<template><table>',
10+
'<template><svg><html><desc>',
11+
'<template><math><html><annotation-xml encoding=text/html>',
12+
])('%s', (markup) => {
13+
const input = markup.repeat(12_000);
14+
15+
expect(() => parse(input, { treeAdapter })).not.toThrow();
16+
expect(() => parseFragment(input, { treeAdapter })).not.toThrow();
17+
});
18+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { it, expect } from 'vitest';
2+
import { Parser } from 'parse5';
3+
4+
it.each([
5+
['EOF', false],
6+
['end tags', true],
7+
])('uses linear work on the template-mode stack (%s)', (_label, close) => {
8+
const parser = new Parser();
9+
let reads = 0;
10+
parser.tmplInsertionModeStack = new Proxy(parser.tmplInsertionModeStack, {
11+
get(target, key, receiver): unknown {
12+
if (typeof key === 'string' && /^\d+$/.test(key)) reads++;
13+
return Reflect.get(target, key, receiver);
14+
},
15+
});
16+
parser.tokenizer.write('<template>'.repeat(128) + (close ? '</template>'.repeat(128) : ''), true);
17+
expect(reads).toBeLessThan(1024);
18+
expect(parser.tmplInsertionModeStack).toHaveLength(0);
19+
});

0 commit comments

Comments
 (0)