Repository navigation
fix(metadata): decode numeric HTML references with browser scalar rules - #50345
Conversation
|
Important Review skippedReview was skipped as selected files did not have any reviewable changes.
|
Link-preview and workspace-port page titles passed numeric character references straight to String.fromCodePoint, so legacy references such as &stablyai#146; became invisible C1 controls, � produced a lone surrogate, and � or � stayed literal. Follow the WHATWG numeric character reference rules: 0x80-0x9F map through the Windows-1252 replacement table, and NUL, surrogates and values above U+10FFFF become U+FFFD. Fixes stablyai#50344
5a5db77 to
b11da71
Compare
ELI5
Web pages sometimes write special characters as number codes, like
“for a curly quote. Orca read some of these codes differently from a browser, so link previews and detected port page titles could show invisible junk, broken characters, or the raw code. Now Orca reads them the same way a browser does, so the titles look right.What Changed
decodeHtmlEntitiesinsrc/main/network/page-metadata/html-head-tags.tsis the shared HTML head parser used by link previews (readLinkPreviewMetadata) and workspace port page probes. It now follows the WHATWG rules for numeric character references:�, surrogates (0xD800-0xDFFF), values above 0x10FFFF and numeric overflow become U+FFFD.Before:
<title>€ “quoted” � � � 😀</title>decoded to"\u0080 \u0093quoted\u0094 � \ud800 � 😀". That is invisible C1 control characters, a literal�, a lone surrogate, and a literal�.After: it decodes to
€ “quoted” � � � 😀, which is what a browser shows.Named-entity decoding and tag scanning are unchanged.
Why
The old code passed every numeric reference straight to
String.fromCodePoint. Pages that still use legacy references like’for an apostrophe showed control characters instead. A table was chosen overTextDecoder('windows-1252')so the decoder doesn't depend on ICU in non-Electron runtimes (orcad/server). The table matchesTextDecoder('windows-1252')for every code from 0x80 to 0x9F.Linked Issue
Fixes #50344
Visual Proof
N/A. This changes how text is decoded, not layout or interaction. The exact before/after strings are shown above and are pinned by the regression test.
Testing
I manually tested these changes locally
Automated tests added/updated, or explained why not below
Every added test fails without this change and avoids the test anti-patterns
Reproduced on
main: the newhtml-head-tags.test.tscase fails (it gets raw C1 controls, a literal�, a lone surrogate and a literal�). It passes with this change.Compared the decoder against the
entitiespackage'sdecodeHTMLfor every code point from 0 to 0x110010, in decimal and hex, plus overflow: 0 mismatches.Neighbouring suites pass (
src/main/network/page-metadata,src/main/link-preview,src/main/ports: 8 files, 17 tests). oxlint, oxfmt, the anti-slop, code-quality and ast-grep gates, and the changed-code quality gate all pass on the touched files.macOS (Node 24). The change is pure string handling with no platform-specific code, so Linux, Windows, SSH and mobile behave the same.
Performance
None. This runs in the main process once per link-preview fetch or port probe, not on a hot path. Median of
extractHtmlTitle, old vs new (Node 24):decodeHtmlEntities): 0.030 ms vs 0.030 ms.The change adds one module-level lookup table and a few constant comparisons per numeric reference. It adds no I/O, polling, subprocess work or per-page allocation.
Risk
Level: Low. Only numeric-reference decoding changes, and every consumer only displays the text.
findIconHrefalso decodes hrefs through this function, and mapping invalid references to U+FFFD there matches what a browser does. A plain revert restores the old behavior, and no saved data is involved.Review
Reviewed against the WHATWG numeric character reference rules and checked against the Windows-1252 decoder for every code from 0x80 to 0x9F. Dropping
Number.isIntegeris safe because the regex only matches digits, soNaNcan't happen and overflow becomesInfinity, which maps to U+FFFD.Agent skill upstream boundary
Notes
Security, cross-platform (Linux, Windows, Mac), remote SSH, mobile, backwards compatibility and performance were checked. This is pure string handling in the shared main-process parser, with no protocol or saved-data changes.
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)Original fix by @Afloat16 in #50345