Skip to content

fix(octane): adopt DOM bindings over browser-translated text leaves - #1909

Merged
trueadm merged 10 commits into
mainfrom
fix/translated-text-leaf
Oct 8, 2026
Merged

trueadm merged 10 commits into
mainfrom
fix/translated-text-leaf

Conversation

@trueadm

@trueadm trueadm commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1904

Summary

Chrome's page translation replaces each translated Text node with nested <font style="vertical-align: inherit;"> wrappers. DOM-binding adoption failed on that, in both adopter families:

The fix keeps the translation until the value changes:

Why

  • Chrome's built-in Translate, a common user action, made fixed and structural binding views unadoptable. A page translated before adoption could not activate, and one translated after adoption stopped updating with no error.
  • Rewriting translated text at adoption would flash the source language and shift layout. Dominic therefore chose to keep the translation until the value actually changes, at no runtime cost when values are unchanged.
  • translate="no" stays the explicit opt-out. The compiler does not add it, because that would make all dynamic text untranslatable.

Changes

  • packages/octane/src/dom-bindings.ts (fixed views)
    • Both resolvers share an O(1) isTextLeaf predicate. It reads firstChild once and uses nextSibling instead of childNodes.length. A leaf passes when it is empty or holds exactly one child: a Text node or a font element. Translation replaces one Text node with one wrapper, matching structural text holes.
    • Both resolvers also validate element children with a sibling walk (review follow-up).
      • Before: childNodes.length, children.length and children[i]. In Blink each creates a collection and keeps it on the node.
      • Now each parent keeps a cursor to its next unclaimed child. A final check rejects any child the server topology lacks.
      • A parent without a cursor (opaque, text, or not yet visited) claims nothing, which replaces two explicit parent checks.
      • Error codes are unchanged for compiler-issued descriptors.
    • writeFixedScalar and the general write take an optional previous argument. Both adopters' writePrepared pass previous[i], and the BindingScalarChannels write type includes it.
    • The writer reads nodeValue once. A null nodeValue identifies an element first child, which is replaced via textContent only when previous !== undefined.
  • packages/octane/src/dom-binding-program.ts (structural programs)
    • writeOperation passes instance.previous[index].
    • resolveFragment accepts a <font> element in a static text slot. The marker id already hashes the view's source, including that text (strongHash(...source) in compiler/dom-binding-program.js).
    • adoptRegion accepts a <font>-wrapped text hole and records region.text = undefined.
    • writeText turns undefined into a detached Text node on the first commit, which records the value without writing. When a changed value finds its node displaced, writeText replaces the range content with that node.
    • The handoff valid() treats undefined like null. Translated content makes handoff validation fail, which takes the renderer's existing PresentationAdoptionMiss path.
  • docs/deferred-hydration.md, under "Compiled presentation on existing DOM":
    • Translated text is adopted as it is, in fixed views and structural programs, including conditional arms and list items.
    • Bound text is replaced on its next change; static text keeps its translation.
    • If the data changed between SSR and adoption, translated bound text shows its server value until that change.
    • translate="no" or class="notranslate" is the zero-cost opt-out. The compiler does not add it, because that would make all dynamic text untranslatable.
  • Error documentation for feat(base-ui): port the Menu item family (Phase 3f stage 2) #318, feat(octane): cross-module hook fact extraction (opt-in) #321 and feat(seo): make document metadata reach <head> in SSR #286, with no runtime bytes:
    • Published messages are immutable, so codes.json gains an optional website-only note.
    • The validator checks the field.
    • A generator test pins that a note leaves the generated client and server formatters byte-identical.
    • The /errors/<code> decoder renders the note under the message.
  • Stable member reads (performance-audit docs: teach performance audits to reuse stable member reads #1913):
    • Both adopters' write loops read next[i] and previous[i] once.
    • writeOperation reads instance.previous[index] once, after its disposed guards.
    • writeText no longer reads region.range on the unchanged path.
    • adoptRegion and writeText cache range.end.
    • resolveFixedNodes destructures its open-children flag.
  • Patch changeset for octane.

Validation

  • pnpm format:check: not run locally. pnpm format:files:check passed on every changed file.
  • pnpm typecheck: not run locally; left to CI.
  • pnpm test: not run locally; left to CI.
  • targeted tests, --project octane: 646/646 pass across every runtime file that adopts DOM bindings. That is scalar-dom-bindings, behavior-root, behavior-root-bundle, binding-handoff, binding-handoff-displaced-host, behavior-handoff-bundle, and hydration/{range-boundary,unclosed-range,independent-island,text-sibling-hydrate}.
  • pnpm error-codes:check: 17/17, including the note validation and formatter-identity tests.
  • pnpm sync: no generated changes.

New tests:

  • scalar-dom-bindings.test.ts, using the issue's Panel across the dev/prod × scalar/general lanes:
  • behavior-root.test.ts, "a page translated by the browser", in dev and prod:
    • A Notice view with static text, a text hole, and an @if arm holding a text hole.
    • Translated before adoption: innerHTML is unchanged at adoption and on an unchanged publish. Only changed holes are replaced, static text stays translated, and a re-created arm renders fresh source text.
    • Translated after adoption, then translated again: updates continue.
    • A <span> in place of the static text or of the text hole still throws feat(seo): make document metadata reach <head> in SSR #286 and leaves the DOM untouched.

Perf review: fix/translated-text-leaf (origin/main e29a12be0a..1084f00f1f)

Scan: node scripts/perf-review-scan.mjs → 0 candidates.

Hot paths touched:

  • writeFixedScalar / write: run once per changed binding write, per publication or signal notification.
    • Unchanged values are filtered by next[i] === previous[i] before the writer is called, so unchanged writes do no added work.
    • A changed write still reads nodeValue once, and adds no nodeType read: a null nodeValue identifies the element case.
  • writeText: runs on every commit of every text hole.
    • The unchanged path reads region.text once, then text.data. That is one read fewer than before: the base read region.text twice.
    • A changed write adds one nextSibling comparison.
    • region.text now also holds undefined, an oddball like null, so the field's representation is unchanged.
  • Resolvers and adoptRegion: run once per adoption, per node.
    • The fixed resolvers now validate in O(1) per node with firstChild/nextSibling. They no longer touch childNodes, children or collection indexing, so adoption creates no NodeList or HTMLCollection.
    • The new cursors arrays are packed: one push per node, writes to existing indices only.
    • In adoptRegion and the static-text proof, the font check runs only where the old check would already have thrown.
  • writeOperation: one extra array read per changed program write.
# Severity Location Dimension Finding Required evidence Provided?
1 needs-evidence dom-bindings.ts adopters, dom-binding-program.ts bundle The scalar adopter (the minimal binding path) and the structural program grow Bundle rows from the CI bench report Partly. The CI report has no adopter rows, so the local scratch probe below is the adopter evidence. CI confirms no change anywhere else (see below).
2 note dom-bindings.ts writeFixedScalar, dom-binding-program.ts writeText DOM The replacement write (textContent, or removeRange plus insertBefore) has no read after it, and runs only when translated text changes value none n/a

CI bench report for 025f9575ec, comparing merge 593e4e73f with 385d6aba9 (run):

  • Bundles: application bundle sizes show no change across 60 values, and public-import reachability bundles no change across 111. None of those bundles loads __adoptScalarBindings, __adoptBindings or dom-binding-program: the adopters are reached only from compiled binding artifacts, and the report has no suite for them. So there are no adopter rows to quote. The identical bytes do show the change does not leak into renderer or application bundles.
  • js-framework: all 64 work counters are unchanged. One wall-time verdict: octane-tsrx swap 0.039 → 0.040 ms, +5.5% (CI +3.4% … +7.5%). Its bundle is byte-identical between base and head, and the benchmark never loads the binding runtime, so this is runner noise rather than an effect of this diff. The other 19 operations are within noise.

Local scratch probe: esbuild minify + gzip -9 of each entry, process.env.NODE_ENV=production, origin/main vs head:

Entry min (base → head) gzip (base → head)
__adoptScalarBindings 6368 → 6448 (+80) 2869 → 2917 (+48)
__adoptBindings 13822 → 13854 (+32) 5361 → 5418 (+57)
dom-binding-program (includes the dom-bindings changes) 39975 → 40188 (+213) 13509 → 13591 (+82)
  • Structural code: +38 B gzip of the program's +71. I trimmed it from +46 B by compacting adoptRegion, writeText and the static-text check.

  • Review follow-ups:

    • The O(1) text-leaf check and the single nodeValue read took the scalar adopter to +51 B gzip, from the first push's +30.
    • The sibling walk then brought it back to +45. Letting the cursors encode opaque and text parents removed two explicit parent checks.
    • The general adopter ends at +53.
    • The stable-member-read pass then took minified size down by 12 B (scalar) and 14 B (general), while gzip rose by 4–11 B. That pass was about read counts, not size.
    • Text-leaf variants measured before the walk:
    Variant Scalar gzip General gzip
    Shared helper, chosen +51 +42
    Inline with a loop variable +44 +58
    Helper with the nodeType writer +46 +38

    The shared helper has the smallest combined cost and one predicate for both resolvers.

Compiled output: no compiler change. Reachability: no new imports or references. Scheduling: none.

Not checked: real Chrome Translate in a browser. jsdom reproduces the issue's recorded span → font → font → Text structure.

Risk / follow-ups

  • Stale server text after translation: translated bound text keeps its server value if the data changed between SSR and adoption, until the next change. This is documented, and was Dominic's choice over a flash and layout shift at adoption.

  • Handoff to hydrateRoot: a structural view handed to normal hydration while it still holds translated text fails valid(), which takes the renderer's existing PresentationAdoptionMiss path. What the renderer then does with translated text, including textHoleUpdate, belongs to the separate renderer work.

  • Adopter bytes have no CI rows. The bench report has no suite that loads the binding adopters (see the perf review), so their byte evidence is the local scratch probe above, not CI rows.

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Note

Medium Risk
Touches hot adoption and text-write paths across fixed and structural binding runtimes; behavior is well-tested but edge cases remain (e.g. stale server text until the next bound update after translation).

Overview
DOM binding adoption no longer fails when Chrome’s page translation has replaced server text with nested <font> wrappers. Fixed views and structural programs accept that topology instead of raising #318, #321, or #286; other non-text substitutions still fail.

At adoption, translated text is left in place (no flash back to the source language). Bound text switches to plain text on the next value change so updates work again, including when translation happens after adoption. Writers pass a previous value so <font> leaves are not overwritten until the binding actually changes.

Topology validation in dom-bindings.ts uses an isTextLeaf check and sibling cursors instead of childNodes/children length checks. Structural writeText / adoptRegion track region.text as undefined while translated holes await their first write.

Docs (deferred-hydration.md) document the behavior and translate="no" / notranslate opt-out. Error catalog entries for #286/#318/#321 gain optional website-only note fields (runtime formatters unchanged). New tests cover pre/post-adoption translation for scalar, addressed, and structural views.

Reviewed by Cursor Bugbot for commit 1084f00. Bugbot is set up for automated code reviews on this repo. Configure here.

…1904)

Chrome's page translation replaces a text leaf's Text node with nested
<font> wrappers. Fixed and addressed adoption rejected that leaf (#318,
#321), and a leaf translated after adoption silently dropped every later
write, because nodeValue is a no-op on an element.

Adoption now accepts a text leaf whose first child is a <font> element and
writes nothing to it, so the page neither flashes back to its source
language nor shifts. The writer receives the channel's previous value and,
once that value exists, replaces any non-Text leaf content on change. Any
other element in a text leaf still throws.

Document the behavior and the translate="no" opt-out in
docs/deferred-hydration.md, and add a website-only catalog note to #318 and
#321 that never reaches a runtime bundle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Oct 8, 2026
@trueadm
trueadm marked this pull request as ready for review October 8, 2026 15:55
Comment thread packages/octane/src/dom-bindings.ts Resolved
Comment thread packages/octane/src/dom-bindings.ts Resolved
trueadm and others added 2 commits October 8, 2026 17:02
…1904)

Structural programs had the same Chrome Translate failure as fixed views.
A static text proof or a text hole whose Text node was replaced by <font>
wrappers failed adoption with #286, and a text hole translated after
adoption silently kept its translation, because writeText set the data of
a detached node.

- resolveFragment accepts a <font> element in a static text slot. The
  marker id already hashes the template source, including that text.
- adoptRegion accepts a <font>-wrapped text hole and records `undefined`.
  The first commit stores the value in a detached Text node and writes
  nothing.
- writeText replaces the range content with its node only when the value
  changes and the node is no longer the range's only child.

Any other element in either slot still throws #286. Extend the
deferred-hydration paragraph and the website-only error note, now also on
#286, to structural views.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…1904)

Review follow-up. The fixed and addressed resolvers share an isTextLeaf
predicate. It reads firstChild once and uses nextSibling instead of
childNodes.length, which is not O(1).

The fixed-scalar writer reads nodeValue once. A null nodeValue identifies
an element first child, such as Chrome Translate's <font>, so the changed
write no longer reads nodeType.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

🟢 No byte growth, no added work, and no timing changes beyond ±3%.

Compares e29a12be0, the merge commit's first parent with 24541ba4a on the same runner. Any increase in a js-framework work counter fails this check; byte changes are a report. Wall time is a report: base and head samples alternate in one browser, and an operation is called slower or faster only when the 95% confidence interval of its ratio excludes ±3%. Workflow run

js-framework: 1k/10k row operations

All 20 timed operations (20 without a verdict)
target operation base ms head ms head/base 95% CI
octane-tsrx run 4.725 5.092 +1.6% −13.9% … +13.4% ⚪ within noise
octane-tsrx replace 17.992 16.283 −5.8% −15.3% … ±0.0% ⚪ within noise
octane-tsrx add 4.517 4.442 −3.2% −8.8% … +2.4% ⚪ within noise
octane-tsrx update 0.190 0.209 +11.2% +0.5% … +23.5% ⚪ within noise
octane-tsrx select 0.009 0.009 −3.2% −9.1% … +4.1% ⚪ within noise
octane-tsrx swap 0.043 0.042 +0.5% −7.2% … +5.3% ⚪ within noise
octane-tsrx remove 0.027 0.028 −0.9% −4.1% … +11.2% ⚪ within noise
octane-tsrx runlots 42.500 42.358 −0.0% −9.9% … +12.0% ⚪ within noise
octane-tsrx select_lots 0.010 0.011 +9.1% −2.2% … +14.6% ⚪ within noise
octane-tsrx clear 98.708 94.108 +0.8% −4.2% … +6.1% ⚪ within noise
octane-jsx run 8.300 7.692 −3.5% −14.0% … +4.5% ⚪ within noise
octane-jsx replace 21.475 22.850 +6.8% −3.1% … +13.4% ⚪ within noise
octane-jsx add 7.608 7.842 +1.3% −5.6% … +9.6% ⚪ within noise
octane-jsx update 0.619 0.659 +3.0% −8.6% … +13.2% ⚪ within noise
octane-jsx select 0.251 0.268 −0.5% −6.7% … +8.9% ⚪ within noise
octane-jsx swap 0.449 0.475 +4.3% −6.0% … +10.5% ⚪ within noise
octane-jsx remove 0.442 0.354 −8.3% −13.1% … +2.2% ⚪ within noise
octane-jsx runlots 58.725 59.142 +2.3% −1.7% … +9.6% ⚪ within noise
octane-jsx select_lots 4.775 4.835 +8.8% −13.4% … +34.1% ⚪ within noise
octane-jsx clear 103.667 102.133 +0.5% −6.8% … +8.8% ⚪ within noise

All 64 work counters are unchanged.

Application bundle size (bytes)

No changes across 60 measured values.

Public-import reachability bundles (bytes)

No changes across 111 measured values.

@trueadm trueadm closed this Oct 8, 2026
@trueadm trueadm reopened this Oct 8, 2026
trueadm and others added 2 commits October 8, 2026 18:34
…g walk (#1904)

Review follow-up. The element branch of both resolvers still counted live
child lists (`childNodes.length`, `children.length`) and indexed
`children[i]`. In Blink each of those creates a collection and keeps it on
the node.

Each parent now keeps a cursor to its next unclaimed child. Validation
reads `firstChild` and `nextSibling` only, and a final check rejects any
child the server topology does not have. A parent without a cursor
(opaque, text, or not yet visited) claims nothing, which replaces two
explicit parent checks.

Error codes are unchanged for compiler-issued descriptors. New tests pin
#318 and #321 for an extra child, a stray text node, a missing child,
reordered children, and a child under a childless element. Unbound
children still adopt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
octane-website 6856723 Commit Preview URL

Branch Preview URL
Oct 08 2026, 06:54 PM

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
octane-website-mcp 1ed0f16 Commit Preview URL

Branch Preview URL
Oct 08 2026, 05:55 PM

trueadm and others added 2 commits October 8, 2026 20:03
…tion paths (#1904)

Apply performance-audit's "Reuse stable member reads" (#1913) to the code
this PR adds.

- Both adopters' write loops read `next[i]` and `previous[i]` once.
- writeOperation reads `instance.previous[index]` once, after its
  disposed guards.
- writeText no longer reads `region.range` on the unchanged path, and
  caches `range.end` in the branches that use it.
- adoptRegion caches `range.end`.
- resolveFixedNodes destructures its open-children flag rather than
  indexing `descriptor.nodes[i]` again.

Minified adopters shrink by 12–14 B. gzip moves 4–11 B, so this is a
read-count change, not a size claim.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Text leaf allows extra font siblings
    • Added the missing !first.nextSibling guard to the font branch of isTextLeaf, so a <font> followed by extra siblings is now correctly rejected, matching the Text-node branch behavior and the old childNodes.length > 1 check.

Create PR

Or push these changes by commenting:

@cursor push a575c05c4c
Preview (a575c05c4c)
diff --git a/packages/octane/src/dom-bindings.ts b/packages/octane/src/dom-bindings.ts
--- a/packages/octane/src/dom-bindings.ts
+++ b/packages/octane/src/dom-bindings.ts
@@ -242,7 +242,9 @@
 	const first = node.firstChild;
 	return (
 		first === null ||
-		(first.nodeType === 3 ? !first.nextSibling : (first as Element).localName === 'font')
+		(first.nodeType === 3
+			? !first.nextSibling
+			: (first as Element).localName === 'font' && !first.nextSibling)
 	);
 }

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit 9593d6b. Configure here.

Comment thread packages/octane/src/dom-bindings.ts Resolved
…1904)

Bugbot review: isTextLeaf accepted a leading <font> even when other nodes
followed it. Those leaves used to fail #318/#321, and a structural text
hole already requires a single node.

Chrome Translate replaces one Text node with one wrapper, so a text leaf
now holds no child, or exactly one: a Text node or a <font> element. The
negative tests cover a <font> followed by text in every fixed lane, and
two wrappers in an addressed view.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@trueadm
trueadm merged commit e2044cb into main Oct 8, 2026
32 checks passed
@trueadm
trueadm deleted the fix/translated-text-leaf branch October 8, 2026 19:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DOM bindings: native Chrome Translate causes #318 before adoption, including hidden views

1 participant