From 525ab758cfe21f32232e40ee8b9a2f15056c2170 Mon Sep 17 00:00:00 2001 From: Ivan Voischev Date: Mon, 30 Nov 2026 18:55:47 +0300 Subject: [PATCH] fix: omit end tags after outside foreign content In HTML, <plaintext> outside SVG/MathML turns the rest of the document into text. posthtml-parser 0.13 (htmlparser2 12) follows this and returns everything after it as the element's content, so the source has no end tags after it. The renderer appended </plaintext> and the ancestors' end tags, which browsers would show as text. Track foreign content like htmlparser2 and stop emitting end tags once such an element is opened. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --- changelog.md | 1 + src/index.ts | 42 +++++++++++++++++++++++++++++++++++------- test/suite.cjs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 7 deletions(-) diff --git a/changelog.md b/changelog.md index 97e184b..b700437 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,6 @@ ## Unreleased +* Do not emit end tags after a `<plaintext>` element outside SVG/MathML. In HTML such an element turns the rest of the document into text, so posthtml-parser 0.13 (htmlparser2 12) returns everything after it as its content and the source cannot contain those end tags. Rendering now reproduces the source instead of appending `</plaintext></div>`. * Replace the legacy build, lint and test toolchain with TypeScript, ESLint and native Node.js tests; retain Node.js 12 runtime support. * Check the full suite and CJS/ESM consumers against a built npm tarball across the runtime matrix. * Make declarations self-contained, including mixed nested content arrays, and check TypeScript 4.3.5 and current consumers. diff --git a/src/index.ts b/src/index.ts index f91071d..9d48142 100644 --- a/src/index.ts +++ b/src/index.ts @@ -83,6 +83,19 @@ export type Options = { quoteStyle?: quoteStyleEnum; }; +const FOREIGN_CONTENT_TAGS = new Set(['svg', 'math']); +const HTML_INTEGRATION_TAGS = new Set([ + 'mi', + 'mo', + 'mn', + 'ms', + 'mtext', + 'annotation-xml', + 'foreignObject', + 'desc', + 'title', +]); + const SINGLE_TAGS: Array<string | RegExp> = [ 'area', 'base', @@ -145,9 +158,14 @@ export function render(tree?: Node | Node[], options: Options = {}): string { tree = [tree]; } - return html(tree); + // Outside SVG/MathML, <plaintext> switches the HTML tokenizer to plain text + // until the end of the document, so no end tag can follow it in the source. + // Mirrors htmlparser2: only exact `svg`/`math` start foreign content. + let inPlaintext = false; + + return html(tree, false); - function html(tree: Array<Node | Node[] | false | null | undefined>) { + function html(tree: Array<Node | Node[] | false | null | undefined>, foreign: boolean) { let result = ''; for (const node of tree) { @@ -168,7 +186,7 @@ export function render(tree?: Node | Node[], options: Options = {}): string { continue; } - result += html(node); + result += html(node, foreign); continue; } @@ -188,12 +206,18 @@ export function render(tree?: Node | Node[], options: Options = {}): string { } if (node.tag === false) { - result += html(node.content); + result += html(node.content, foreign); continue; } const tag = typeof node.tag === 'string' ? node.tag : 'div'; + const contentForeign = FOREIGN_CONTENT_TAGS.has(tag) + || (foreign && !HTML_INTEGRATION_TAGS.has(tag)); + + if (!foreign && tag.toLowerCase() === 'plaintext') { + inPlaintext = true; + } result += `<${tag}`; @@ -228,15 +252,19 @@ export function render(tree?: Node | Node[], options: Options = {}): string { } if (node.content) { - result += html(node.content); + result += html(node.content, contentForeign); } } else if (closingSingleTag === closingSingleTagOptionEnum.closeAs && node.closeAs) { const type = node.closeAs ? closingSingleTagTypeEnum[node.closeAs] : closingSingleTagTypeEnum.default; - result += `${closeAs[type]}${html(node.content)}`; + result += `${closeAs[type]}${html(node.content, contentForeign)}`; } else { - result += `>${html(node.content)}</${tag}>`; + result += `>${html(node.content, contentForeign)}`; + + if (!inPlaintext) { + result += `</${tag}>`; + } } } diff --git a/test/suite.cjs b/test/suite.cjs index 151a4d2..83e133a 100644 --- a/test/suite.cjs +++ b/test/suite.cjs @@ -347,6 +347,51 @@ module.exports = function suite(test, renderer, parser) { assert.equal(render(tree), html); }); + test('{Tags} {plaintext} omits end tags that cannot follow it', () => { + // What posthtml-parser 0.13 (htmlparser2 12) returns for + // <div><plaintext>a</plaintext></div><b>x</b> + const tree = [{tag: 'div', content: [{tag: 'plaintext', content: ['a</plaintext></div><b>x</b>']}]}]; + assert.equal(render(tree), '<div><plaintext>a</plaintext></div><b>x</b>'); + }); + + test('{Tags} {plaintext} is case-insensitive', () => { + assert.equal(render([{tag: 'PLAINTEXT', content: ['a</PLAINTEXT>']}]), '<PLAINTEXT>a</PLAINTEXT>'); + }); + + test('{Tags} {plaintext} omits end tags of later nodes', () => { + const tree = [{tag: 'plaintext', content: ['a']}, {tag: 'b', content: ['x']}]; + assert.equal(render(tree), '<plaintext>a<b>x'); + }); + + test('{Tags} {plaintext} is a regular element inside SVG and MathML', () => { + const tree = [ + {tag: 'svg', content: [{tag: 'plaintext', content: ['a']}, {tag: 'b', content: ['x']}]}, + {tag: 'math', content: [{tag: 'plaintext', content: ['b']}]}, + {tag: 'i', content: ['y']}, + ]; + assert.equal( + render(tree), + '<svg><plaintext>a</plaintext><b>x</b></svg><math><plaintext>b</plaintext></math><i>y</i>', + ); + }); + + test('{Tags} {plaintext} switches to text inside HTML integration points', () => { + const tree = [{tag: 'svg', content: [{tag: 'foreignObject', content: [ + {tag: 'plaintext', content: ['a</plaintext></foreignObject></svg>']}, + ]}]}]; + assert.equal(render(tree), '<svg><foreignObject><plaintext>a</plaintext></foreignObject></svg>'); + }); + + test('{Tags} {plaintext} keeps foreign detection case-sensitive like htmlparser2', () => { + const tree = [{tag: 'SVG', content: [{tag: 'plaintext', content: ['a</plaintext></SVG>']}]}]; + assert.equal(render(tree), '<SVG><plaintext>a</plaintext></SVG>'); + }); + + test('{Tags} {plaintext} state does not leak between render calls', () => { + render([{tag: 'plaintext', content: ['a']}]); + assert.equal(render([{tag: 'b', content: ['x']}]), '<b>x</b>'); + }); + test('{Options} {singleTag} Defaults', () => { const SINGLE_TAGS_LOWERCASE = [ 'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr',