Skip to content

fix(web): decode link-title entities without parsing the title as markup - #6

Draft
agentsfleet-dev[bot] wants to merge 1 commit into
devfrom
agentsfleet-repair/MDFhMTk3MjQtOWU0ZC03ZmM3LWEyMGYtNjdjNDJjZWMxMDA5
Draft

agentsfleet-dev[bot] wants to merge 1 commit into
devfrom
agentsfleet-repair/MDFhMTk3MjQtOWU0ZC03ZmM3LWEyMGYtNjdjNDJjZWMxMDA5

Conversation

@agentsfleet-dev

Copy link
Copy Markdown

Fixes the failed step Check link title decoding of the M224 title regression run.

Evidence

AssertionError: expected 'TypeScript  guide & examples' to be 'TypeScript <T> guide & examples' // Object.is equality
 ❯ apps/web/lib/client/unescapeString.test.ts:21:67

Root cause

apps/web/lib/client/unescapeString.ts handed the saved link title straight to DOMParser().parseFromString(input, "text/html"), so the title was parsed as HTML markup. In TypeScript <T> guide &amp; examples the parser tokenises <T> as an unknown start tag and documentElement.textContent — which concatenates only text nodes — drops it, leaving TypeScript guide & examples. A link title is displayed as React text, so it must never be interpreted as markup; the two passing cases passed only because &amp;, &lt; and &gt; are character references, which the parser decodes.

The defect is present on dev at 154a1b8 (blob fe0807aa); this draft only repairs it.

Fix

Escape < before parsing, so the title is always treated as text while character references are still decoded:

export default function unescapeString(input: string) {
  // A link title is rendered as plain text, so "<" must never open a tag.
  // Escaping it keeps the parser in text mode while character references
  // such as "&amp;" and "&lt;" are still decoded.
  var doc = new DOMParser().parseFromString(
    input.replace(/</g, "&lt;"),
    "text/html"
  );
  return doc.documentElement.textContent;
}

Resulting behaviour against the three unchanged assertions:

input output
Research &amp; development Research & development (unchanged)
TypeScript &lt;T&gt; guide TypeScript <T> guide (unchanged)
TypeScript <T> guide &amp; examples TypeScript <T> guide & examples (was TypeScript guide & examples)

Edge cases checked by inspection: empty input → empty output, non-ASCII text is preserved, a literal </textarea> cannot break out because < is escaped, and &amp;lt; still decodes exactly one level, as before.

Files changed

  • apps/web/lib/client/unescapeString.ts — the fix above.
  • apps/web/lib/client/unescapeString.test.ts — the regression of the failing run, added unchanged (blob ff0b47366c02cbf165321919c5cb5b2980836c74, identical to the blob in the pinned fixture commit 95a173dfb71746d8e5ed01b420815668e34806f3; the file is absent from dev, which the apps/web/lib/client listing at 154a1b8 confirms).

No workflow file is modified, no regression is weakened, skipped or removed.

Validation

The M224 title regression workflow runs on this pull request and checks out the exact pull-request head, restores the pinned regression fixture and runs yarn test run apps/web/lib/client/unescapeString.test.ts. The same three assertions failed at the base head 154a1b8 (run 41281464925); the result of that workflow at this draft head is reported once it has run.

The M224 title regression run failed at "Check link title decoding":
unescapeString("TypeScript <T> guide &amp; examples") returned
"TypeScript  guide & examples" because the saved title was parsed as HTML
markup, so the literal <T> was tokenised as an element and dropped by
documentElement.textContent.

A link title is rendered as React text, so "<" is escaped before parsing.
The parser then stays in text mode while character references such as
"&amp;" and "&lt;" are still decoded, and all three unchanged assertions
of the regression hold. The regression file is added unchanged from the
pinned fixture commit 95a173d.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants