Skip to content

fix: escape & and U+00A0 in HTML attributes, keep TextElement text inside the element - #369

Open
mistyhey wants to merge 4 commits into
WebReflection:mainfrom
mistyhey:fix/html-attr-escape-and-text-element
Open

mistyhey wants to merge 4 commits into
WebReflection:mainfrom
mistyhey:fix/html-attr-escape-and-text-element

Conversation

@mistyhey

@mistyhey mistyhey commented Nov 16, 2026 •

Copy link
Copy Markdown

Fixes #340
Fixes #341

Attr.toString() (#340)

In HTML documents ignoreCase(this) is true, so escape(value) is skipped and only " is replaced. A value that contains a character reference, such as the literal A & B, is written back raw and loses one level of references on every serialize + parse cycle.

The HTML branch now keeps a raw & (so href="?a=1&b=2" and content-desc="text3&more" serialize exactly as before) and escapes & as & only when the value would otherwise be decoded on reparse. The check uses decodeHTMLAttribute from entities, the decoder htmlparser2 already uses; entities@^7 is added as a direct dependency and dedupes with htmlparser2's copy. The XML branch is unchanged.

TextElement.toString() (#341)

The text was spliced in at the first >< of the childless clone, which can be inside an attribute value. The boundary between start and end tag is always the last ><, so the text is now inserted there. The function-style replacement from #293 is kept in effect (no special replacement patterns are interpreted).

Tests

  • New cases in test/interface/attr.js (raw & kept, character references escaped and round-tripped) and test/html/style-element.js.
  • No existing expectation changes: anchor-element.js, meta-element.js and element.js pass as on main.
  • npm test passes with 100% coverage on the touched files. cjs/ and worker.js were regenerated with npm run cjs and rollup.

🤖 Generated with Claude Code

@WebReflection

Copy link
Copy Markdown
Owner

this feels like potentially a breaking change (behavior changed) but for HTML & is accepted and so are spaces while escape is used for anything else ... this makes ignoreCase(this) ternary check basically useless ... can I see/have an example where things actually break?

@mistyhey

mistyhey commented Nov 16, 2026 •

Copy link
Copy Markdown
Author

Thanks for looking. Yes, there is a concrete break: the value read back after a serialize/parse cycle differs from the value held in the DOM (the full repro is in #340).

import { parseHTML } from 'linkedom';

const { document } = parseHTML('<!doctype html><html><body><div id="t"></div></body></html>');
const el = document.getElementById('t');
el.setAttribute('data-x', 'A &amp; B'); // the literal 9 characters  A &amp; B

el.outerHTML;
// linkedom today: <div id="t" data-x="A &amp; B"></div>
// browsers:       <div id="t" data-x="A &amp;amp; B"></div>

parseHTML(`<!doctype html><html><body>${el.outerHTML}</body></html>`)
  .document.getElementById('t').getAttribute('data-x');
// linkedom today: "A & B"   (one level of character references lost per cycle)

& is accepted in HTML attributes only when it doesn't start a character reference; for an arbitrary value the serializer can't know that, so the HTML fragment serialization algorithm escapes it in attribute mode: & → &amp;, U+00A0 → &nbsp;, " → &quot;. That is what outerHTML returns in Chrome, Firefox and Safari, and plain spaces are untouched.

So ignoreCase(this) still picks between two escapers: HTML gets the attribute-mode escaping above, while XML keeps the full escape(). Output only changes for values containing & or U+00A0, and in those cases it now matches browsers.

@WebReflection

Copy link
Copy Markdown
Owner

what I mean is that <a href="./page?a=1&b=2">link</a> is how people write links and it's what you'll find in the wild ... here it feels like we're breaking that basic compatibility layer.

…eference (WebReflection#340)

Escaping every & changed the output of common markup such as
href="?a=1&b=2". An & is now escaped only when the value would be decoded
on reparse (checked with the entities attribute decoder htmlparser2 uses),
so plain query strings serialize as before and values like "A &amp; B"
still round-trip. U+00A0 is left raw: it already round-trips.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mistyhey

Copy link
Copy Markdown
Author

Fair point, that output change was not needed. Pushed 813e5c8: the HTML branch now leaves & raw and escapes it only when the value would otherwise be decoded on reparse, checked with decodeHTMLAttribute from entities (the decoder htmlparser2 already uses).

'./page?a=1&b=2'  // -> href="./page?a=1&b=2"          unchanged
'text3&more'      // -> content-desc="text3&more"      unchanged
'A &amp; B'       // -> data-x="A &amp;amp; B"          round-trips

The anchor-element, meta-element and element tests are back to their main expectations, and the U+00A0 escaping is dropped (a raw U+00A0 already round-trips). I also round-tripped 240k random values against the spec decoder with no mismatch.

Session: a1d4bae0-75ed-48d5-8e05-c2e7d38c46f3

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

Projects

None yet

2 participants