Skip to content

fix: keep text elements' content out of attribute values (#341) - #347

Open
vsolano9 wants to merge 2 commits into
WebReflection:mainfrom
vsolano9:fix/text-element-attribute-gt-lt
Open

vsolano9 wants to merge 2 commits into
WebReflection:mainfrom
vsolano9:fix/text-element-attribute-gt-lt

Conversation

@vsolano9

Copy link
Copy Markdown

Fixes #341.

TextElement.toString() put the element text back at the first >< of its childless clone. That can be inside an attribute value, so a <style>, <script>, <title> or <textarea> whose attribute contains >< was serialized with its text inside the attribute and an empty body. A parse → toString() round trip loses the stylesheet the same way:

const {document} = parseHTML('<!doctype html><html><head><style data-src="a><b.css">.c{}</style></head></html>');
document.toString();
// main:   ...<style data-src="a>.c{}<b.css"></style>...
// branch: ...<style data-src="a><b.css">.c{}</style>...

In an HTML document the clone serializes as <tag attrs></tag>, so the text now goes right before the end tag, at the last ></. It's a plain lastIndexOf and slice, so $ patterns in the text stay literal, as #293 intended.

A text element imported into an XML document serializes self-closed (<style ... />). That clone is now returned unchanged, so its attributes stay intact. On main the text there was dropped, or written into an attribute containing ><. That check is the one new branch, under /* c8 ignore next */.

main @ 3fcf145 this branch
reporter's script from #341, pointed at this checkout (style, script, title, textarea with data-src="a><b.css") 4 of 4 corrupted text and attribute intact (esm, cjs and worker.js)
<textarea placeholder="<a></a>">hi placeholder="<a>hi</a>", empty body unchanged attribute, hi in the body
no attributes, empty element, $& / $` in the text, </script> in an attribute correct same output
imported into an XML document (self-closing) <style />; with >< in an attribute, the text goes into that attribute the clone unchanged: <style />, or <style data-src="a><b" /> with its attribute intact

cjs/html/text-element.js and worker.js carry the same change. A full rebuild of worker.js also pulls in #339's getAttributeValue change, which I left out to keep this to one concern.

Validation

  • npm run build (tsc, cjs, rollup, then npm test: eslint, benchmark, both test passes, c8) exits 0 on Windows 11 with Node 24.19. On Linux (WSL Ubuntu) with Node 20.20.2, the version CI uses, npm ci, npm run build, npm test and npm run coverage all exit 0. Coverage stays at 100% statements, branches and lines (functions 99.75%, the same as main).
  • The existing tests cover the changed lines. I didn't add a test case.
  • Serializing 200,000 text elements with attributes: no slowdown visible against main (within the noise of repeated runs).

Prepared with AI assistance (Claude Code); verified by the reporter's script from #341 against esm, cjs and worker.js, an edge-case comparison against main, and the npm runs above.

🤖 Generated with Claude Code

vsolano9 and others added 2 commits October 16, 2026 19:06
…on#341)

TextElement.toString() put the element text back at the first "><" of its
childless clone, which can be inside an attribute value, so a style,
script, title or textarea with such an attribute serialized its text into
the attribute and an empty element body.

The text now goes right before the end tag, the last "></" of the clone.
A clone that self-closes (in an XML document) is returned as before.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Check for the self-closing form first, so an attribute value with "></"
in a text element imported into an XML document cannot take the text.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant