Skip to content

Commit 69f6931

Browse files
fb55claude
andcommitted
fix(parser): process unmatched foreign end tags at the root of a fragment
The foreign-content end-tag walk stopped before the root `html` element, so in a fragment whose stack is `html, <foreign>…` an unmatched end tag was dropped instead of being processed with the HTML rules (`</form>` did not reset the form element pointer). The loop bound now includes the root. The test parses such a fragment. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent b9093da commit 69f6931

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ describe('parser', () => {
9393
expect(serialize(parseFragment(input))).toBe(expected);
9494
});
9595

96+
// TODO: upstream to html5lib-tests
97+
it('an unmatched foreign end tag at the root of a fragment is handed to the HTML rules', () => {
98+
const div = parseFragment('<div>').childNodes[0] as Element;
99+
const fragment = parseFragment(div, '<div><form></div><svg></form></svg><form>x');
100+
101+
expect(serialize(fragment)).toBe('<div><form></form></div><svg></svg><form>x</form>');
102+
});
103+
96104
describe("Regression - Don't inherit from Object when creating collections (GH-119)", () => {
97105
beforeEach(() => {
98106
// @ts-expect-error Adding unknown prototype method

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3804,7 +3804,10 @@ function endTagInForeignContent<T extends TreeAdapterTypeMap>(p: Parser<T>, toke
38043804

38053805
return;
38063806
}
3807-
for (let i = p.openElements.stackTop; i > 0; i--) {
3807+
// The current node is the root element (fragment case): ignore the token.
3808+
if (p.openElements.stackTop === 0) return;
3809+
3810+
for (let i = p.openElements.stackTop; i >= 0; i--) {
38083811
const element = p.openElements.items[i];
38093812

38103813
if (p.treeAdapter.getNamespaceURI(element) === NS.HTML) {

0 commit comments

Comments
 (0)