Skip to content

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

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

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

Conversation

@agentsfleet-dev

Copy link
Copy Markdown

Fixes the failing step "Check link title decoding" of run 41199403249, attempt 1.

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

unescapeString (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, not as text. 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 the two surrounding spaces: "TypeScript " + "" + " guide & examples". That is exactly the received value in the log.

A link title is rendered as React text — {unescapeString(link.name)} in apps/web/components/LinkViews/LinkComponents/LinkCard.tsx — so the title must never be interpreted as markup. The two passing cases only passed because &amp;, &lt; and &gt; are character references, which the parser decodes.

The defect pre-exists on dev; PR #3 added no production code, it only made CI assert 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 plain text, so escape "<" to keep the HTML parser from
  // treating it as markup while still decoding character references.
  var doc = new DOMParser().parseFromString(
    input.replace(/</g, "&lt;"),
    "text/html"
  );
  return doc.documentElement.textContent;
}

Against the three assertions of the failing run:

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)

Files changed

  • apps/web/lib/client/unescapeString.ts — the fix above.
  • apps/web/lib/client/unescapeString.test.ts — the regression test of the failing run, added unchanged (blob ff0b47366c02cbf165321919c5cb5b2980836c74, identical to the blob in the failing commit 95a173d; the file does not exist on dev).

No workflow file is modified. The step that runs this test (Check link title decoding) lives on PR #3's branch; the regression can be executed locally with yarn test run apps/web/lib/client/unescapeString.test.ts.

Validation

The fix was traced against the three assertions of the failing run; it has not been executed by a test runner in this environment, so the test result is still unverified here. CI on this pull request does not currently contain the Check link title decoding step.

unescapeString passed the saved link title straight to
DOMParser().parseFromString(input, "text/html"), so a title containing
"<T>" was tokenised as an unknown start tag and dropped by
documentElement.textContent: the title rendered as
"TypeScript  guide & examples" instead of "TypeScript <T> guide & examples".

A link title is rendered as React text, never as markup, so escape "<"
before parsing. Character references such as &amp;, &lt; and &gt; are still
decoded, while literal angle brackets are preserved.

Adds the regression test of the failing run unchanged.
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.

1 participant