Skip to content

Commit 89dceda

Browse files
fb55claude
andcommitted
fix(parser): ignore foreign elements when resetting the insertion mode
"Reset the insertion mode appropriately" only matches HTML elements. An SVG or MathML element named `select`, `template`, `td`, … selected that mode, whose handlers then emptied the open-element stack or dropped the rest of the input (`<table><td><svg><select><desc><table></table><td>x` threw a `TypeError`). The loop now skips elements outside the HTML namespace. It reads the tag first and asks the tree adapter for the namespace only for the fifteen tags that can select a mode (a table indexed by tag ID), so every other element it passes still costs one read. The search below an open `select` for a `table` or `template` reads namespaces only for those two tags. The test covers the foreign names that used to switch modes; the stack tests count one read per element passed, for a reset after a `select` and for one inside it. Co-Authored-By: Claude Fable 5.1 <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0a61bd5 commit 89dceda

3 files changed

Lines changed: 92 additions & 2 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
parseFragment,
44
parse,
55
InsertionMode,
6+
serialize,
67
type ElementCreationContext,
78
type TreeAdapter,
89
type DefaultTreeAdapterMap,
@@ -56,6 +57,33 @@ describe('parser', () => {
5657
);
5758
});
5859

60+
// TODO: upstream to html5lib-tests
61+
describe('foreign elements do not select an insertion mode', () => {
62+
it.each([
63+
[
64+
'<table><td><svg><select><desc><table></table><td>x',
65+
'<table><tbody><tr><td><svg><select><desc><table></table></desc></select></svg></td><td>x</td></tr></tbody></table>',
66+
],
67+
[
68+
'<svg><template><desc><table></table>x<p>y',
69+
'<svg><template><desc><table></table>x<p>y</p></desc></template></svg>',
70+
],
71+
[
72+
'<table><svg><template><desc><select><template></template><td>x',
73+
'<svg><template><desc><select><template></template></select></desc></template></svg><table><tbody><tr><td>x</td></tr></tbody></table>',
74+
],
75+
])('%s', (input, expected) => {
76+
expect(serialize(parse(input))).toBe(`<html><head></head><body>${expected}</body></html>`);
77+
});
78+
79+
it('foreign fragment context', () => {
80+
const svg = parseFragment('<svg><select>').childNodes[0] as Element;
81+
const fragment = parseFragment(svg.childNodes[0] as Element, '<p><table></table><div>x');
82+
83+
expect(serialize(fragment)).toBe('<p></p><table></table><div>x</div>');
84+
});
85+
});
86+
5987
describe("Regression - Don't inherit from Object when creating collections (GH-119)", () => {
6088
beforeEach(() => {
6189
// @ts-expect-error Adding unknown prototype method

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@ import {
3232
} from '../common/token.js';
3333

3434
//Misc constants
35+
// Elements that can select an insertion mode when it is reset, as a table by tag ID (`XMP` is the last tag ID).
36+
const INSERTION_MODE_TAGS = new Uint8Array($.XMP + 1);
37+
for (const tn of [
38+
$.TR,
39+
$.TBODY,
40+
$.THEAD,
41+
$.TFOOT,
42+
$.CAPTION,
43+
$.COLGROUP,
44+
$.TABLE,
45+
$.FRAMESET,
46+
$.SELECT,
47+
$.TEMPLATE,
48+
$.TD,
49+
$.TH,
50+
$.HEAD,
51+
$.BODY,
52+
$.HTML,
53+
]) {
54+
INSERTION_MODE_TAGS[tn] = 1;
55+
}
3556
const HIDDEN_INPUT_TYPE = 'hidden';
3657

3758
// For the integration-point check of elements whose attributes do not matter; never attached to a node.
@@ -738,8 +759,18 @@ export class Parser<T extends TreeAdapterTypeMap> implements TokenHandler, Stack
738759
/** @protected */
739760
_resetInsertionMode(): void {
740761
for (let i = this.openElements.stackTop; i >= 0; i--) {
762+
const tn = i === 0 && this.fragmentContext ? this.fragmentContextID : this.openElements.tagIDs[i];
763+
764+
// The tag first: most open elements select no mode, and their namespace costs more to read.
765+
if (!INSERTION_MODE_TAGS[tn]) continue;
766+
767+
const node = i === 0 && this.fragmentContext ? this.fragmentContext : this.openElements.items[i];
768+
769+
// Only HTML elements select an insertion mode.
770+
if (this.treeAdapter.getNamespaceURI(node) !== NS.HTML) continue;
771+
741772
//Insertion mode reset map
742-
switch (i === 0 && this.fragmentContext ? this.fragmentContextID : this.openElements.tagIDs[i]) {
773+
switch (tn) {
743774
case $.TR: {
744775
this.insertionMode = InsertionMode.IN_ROW;
745776
return;
@@ -809,9 +840,13 @@ export class Parser<T extends TreeAdapterTypeMap> implements TokenHandler, Stack
809840
for (let i = selectIdx - 1; i > 0; i--) {
810841
const tn = this.openElements.tagIDs[i];
811842

843+
if (tn !== $.TEMPLATE && tn !== $.TABLE) continue;
844+
if (this.treeAdapter.getNamespaceURI(this.openElements.items[i]) !== NS.HTML) continue;
845+
812846
if (tn === $.TEMPLATE) {
813847
break;
814-
} else if (tn === $.TABLE) {
848+
} else {
849+
// Only `table` reaches this branch.
815850
this.insertionMode = InsertionMode.IN_SELECT_IN_TABLE;
816851
return;
817852
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { it, expect } from 'vitest';
2+
import { Parser } from './index.js';
3+
4+
function countOpenStackReads(html: string): number {
5+
const parser = new Parser();
6+
let reads = 0;
7+
const handler = {
8+
get(target: unknown[], key: string | symbol): unknown {
9+
if (typeof key === 'string' && /^\d+$/.test(key)) reads++;
10+
return Reflect.get(target, key);
11+
},
12+
};
13+
parser.openElements.items = new Proxy(parser.openElements.items, handler);
14+
parser.openElements.tagIDs = new Proxy(parser.openElements.tagIDs, handler);
15+
parser.tokenizer.write(html, true);
16+
return reads;
17+
}
18+
19+
const n = 1024;
20+
21+
it.each([
22+
['after select', '<span>'.repeat(256) + '<select></select>'.repeat(n)],
23+
['in select', `${'<span>'.repeat(256)}<select>${'<template></template>'.repeat(n)}`],
24+
])('reads one open-stack entry per element passed when resetting the insertion mode %s', (_name, html) => {
25+
// Each token resets the mode past 256 `span`s; it reads only their tags, as no `span` selects a mode.
26+
expect(countOpenStackReads(html)).toBeLessThan(384 * n);
27+
});

0 commit comments

Comments
 (0)