Skip to content

fix: omit end tags after <plaintext> outside foreign content - #78

Open
voischev wants to merge 1 commit into
masterfrom
fix/plaintext
Open

voischev wants to merge 1 commit into
masterfrom
fix/plaintext

Conversation

@voischev

Copy link
Copy Markdown
Member

In HTML, a <plaintext> start tag outside SVG/MathML switches the tokenizer to plain text until the end of the document. posthtml-parser 0.13 (htmlparser2 12) follows the spec and returns everything after it as the element's text content:

input:   <div><plaintext>text</plaintext></div><b>x</b>
parsed:  [{ tag: 'div', content: [{ tag: 'plaintext', content: ['text</plaintext></div><b>x</b>'] }] }]
before:  <div><plaintext>text</plaintext></div><b>x</b></plaintext></div>
after:   <div><plaintext>text</plaintext></div><b>x</b>

The appended </plaintext></div> never existed in the source and browsers render it as text.

Fix. render() tracks foreign content like htmlparser2 (exact svg/math start it; foreignObject, desc, title, mi, mo, mn, ms, mtext, annotation-xml return to HTML). Once a plaintext element (any case) is opened outside foreign content, no further end tags are emitted: not for it, its ancestors, or later nodes. Inside SVG/MathML plaintext is a regular element. The state is per render() call.

Foreign detection is intentionally case-sensitive to mirror htmlparser2 with lowerCaseTags: false (posthtml-parser's default): it does not treat <SVG> as foreign, and the renderer must agree to reproduce the parsed source.

Tests: end tags omitted for the element, ancestors and later nodes; case-insensitive PLAINTEXT; regular element inside SVG and MathML; text mode again inside foreignObject; case-sensitive SVG; no state leak between calls. Five of them fail without the fix. npm run check passes.

Round trip with posthtml-parser master (incl. posthtml/posthtml-parser#113): 10 of 11 plaintext/SVG/HTML samples render back identical; the remaining one differs only by the pre-existing explicit </li> for an implied end tag.

Downstream: htmlnano master with posthtml master + posthtml/posthtml#421, posthtml-parser master and this renderer: 1866/1867 tests pass, types pass. The remaining test uses uppercase <SVG>, which htmlnano treats as foreign per spec while htmlparser2 does not (case-sensitive foreign detection); the renderer reproduces the parsed source there.

🤖 Generated with Claude Code

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant