Skip to content

Commit be31281

Browse files
fix(html): halt streaming on any mid-stack open-stack removal
`removeOpenAt` only halted when the removed element was already entered. An element the walk had not entered yet is just as unsafe: it leaves the stack while descendants stay open, and the next sync enters those descendants against an open stack that no longer describes their parent. A document that crosses the streaming threshold before reaching `<form><div></form><p>y</p></div>` lost the div and its paragraph entirely — main visits eight nodes there, the streamed walk visited four. Key the halt on the mid-stack test alone. Removing the top of the stack, which is the common `</form>` after implied end tags, still streams: 40k forms retain 2.80 MiB against main's 9.50 MiB.
1 parent 657f73d commit be31281

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

‎lib/html/syntax.js‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5280,22 +5280,19 @@ const cur = () =>
52805280
* @returns {HtmlElement} the popped element
52815281
*/
52825282
/**
5283-
* Remove an element from the middle of the open element stack. Only a removal
5284-
* that takes an *entered* element out from under still-open descendants breaks
5285-
* the streamed walk: the element leaves the stack without its subtree ending,
5286-
* which the entered/open comparison cannot express. Removing the top is just a
5287-
* pop, and an element the walk never entered costs it nothing — neither needs
5288-
* to stop streaming.
5283+
* Remove an element from the middle of the open element stack. A removal that
5284+
* leaves still-open descendants above it breaks the streamed walk whether or
5285+
* not the walk entered the element: it drops off the stack without its subtree
5286+
* ending, so those descendants would be entered under a parent the walk has no
5287+
* open-stack evidence for. Removing the top is just a pop and keeps streaming —
5288+
* which is the common `</form>` after implied end tags.
52895289
* @param {number} index position in `open` to remove
52905290
*/
52915291
const removeOpenAt = (index) => {
5292-
const el = open[index];
52935292
open.splice(index, 1);
52945293
_openSpliced = true;
52955294
_openStackChanged = true;
5296-
if (_streaming && index < open.length && _entered.includes(el)) {
5297-
_streamHalted = true;
5298-
}
5295+
if (_streaming && index < open.length) _streamHalted = true;
52995296
};
53005297

53015298
const popOpen = () => {

‎test/HtmlSyntax.unittest.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4428,6 +4428,27 @@ describe("SourceProcessor — streamed walk recycling", () => {
44284428
expect(log.indexOf("-form")).toBeGreaterThan(divExit);
44294429
});
44304430

4431+
it("visits a form subtree the walk never entered before `</form>`", () => {
4432+
// Same mid-stack removal, but the bulk sits *before* the form, so the walk
4433+
// is streaming yet has never entered the form when `</form>` takes it off
4434+
// the stack. Keying the halt on "was it entered" loses the whole subtree.
4435+
const log = walk(
4436+
`<!DOCTYPE html><html><body>${"<p>x</p>".repeat(
4437+
BIG
4438+
)}<form><div></form><p>y</p></div></body></html>`
4439+
);
4440+
expect(log.slice(log.indexOf("+form"))).toEqual([
4441+
"+form",
4442+
"+div",
4443+
"+p",
4444+
"-p",
4445+
"-div",
4446+
"-form",
4447+
"-body",
4448+
"-html"
4449+
]);
4450+
});
4451+
44314452
it("streams tables, where flushing is held back", () => {
44324453
const log = walk(bigBody("<table><tr><td>a</td></tr></table>", BIG_ROWS));
44334454
expect(log.filter((l) => l === "+table")).toHaveLength(BIG_ROWS);

0 commit comments

Comments
 (0)