Skip to content

fix(studio): a sealed preview shows its text caret at screen size - #8857

Open
miguel-heygen wants to merge 4 commits into
mainfrom
fix/sealed-text-caret
Open

miguel-heygen wants to merge 4 commits into
mainfrom
fix/sealed-text-caret

Conversation

@miguel-heygen

Copy link
Copy Markdown
Collaborator

What

A text edited in place in a sealed preview now shows Studio's own caret, 2 px wide at screen size, exactly as a live preview does. Before, Studio drew no caret for a sealed edit, so only the browser's caret inside the frame showed, shrunk below a pixel by the preview's scale.

Why

The sealed frame posted its selection to Studio only when a range was selected (selectedRange returned null for a collapsed selection), and useInlineTextEditing passed no session to InlineTextCaret for a sealed edit. With the sealed preview on (#7846), the inline-text-caret walk in the viewport gate fails on every open: it never sees a caret, presses Escape, and its later read throws Failed to execute 'setStart' on 'Range'.

Related work

Refs #7846 (sealed preview by default), whose viewport gate this clears. Builds on #8183 (sealed in-place text editing).

How

  • core/runtime/editing/textCaret.ts holds the caret's placement, moved from InlineTextCaret.tsx unchanged: the collapsed range's box, the box beside a node boundary, the soft-wrap side a key leaves, the caret colour, and placeCaretAtEnd. A live preview (Studio reading the frame) and a sealed one (the frame reading itself) now use the same code.
  • The frame's text-edit session reads the caret colour, hides the browser's caret while it is open (restored on close), tracks the soft-wrap side and composition, and adds caret: { left, top, height, color, viewportWidth } | null to text-edit-selection. The event's validator takes caret in that exact, bounded shape and nothing else.
  • InlineTextCaret draws a sealed session's caret from that event, mapped by the frame's box over its viewportWidth, and follows the frame's box as the live path does.
  • A sealed edit opened with Enter places its caret with placeCaretAtEnd, inside the last text node, as a live edit already did. Collapsing at the element's edge put the caret past a bold word at the start of the line.

The event changes shape without a protocol version bump: the frame's runtime and Studio ship from one build.

Test plan

  • fails without the fix: packages/core/src/runtime/editing/textEdit.test.ts › "posts the caret's box, in the frame's px, when the selection is a caret in the text" and "opens with the caret at the end of the last text, inside it, not at the element's edge"; packages/studio/src/components/editor/InlineTextCaret.sealed.test.tsx › "draws a sealed frame's caret at screen size where the frame reports it" and "draws nothing for another session's caret, and takes the caret away when the frame has none". Each fails on main 9dc416a.

  • feat(studio): Studio's preview runs sealed by default #7846's version of tests/e2e/inline-text-caret.mjs, run with the preview sealed (VITE_STUDIO_OPAQUE_PREVIEW_ENABLED=true) against this branch: 0 failures, 3 runs in a row. On feat(studio): Studio's preview runs sealed by default #7846 itself it crashes on its first open.

  • Every core and studio unit test file touching text editing or the editing port: core 257/257, studio 220/220, 3 runs each. Typecheck clean for both packages.

  • Not exercised: the unsealed walk passes in CI on main but crashes at charBefore on a Linux machine with or without this change, so this PR's viewport gate is the check for the unsealed path.

  • Unit tests added/updated

  • Manual testing performed

  • Documentation updated (if applicable)

  • Comments follow CONTRIBUTING.md "Comments": they say why, not what, and a bug fix says what the code must do and how to reproduce the bug

A text edited in place in a sealed preview had no caret Studio could draw: the frame posted no
caret for a collapsed selection, so only the browser's own showed, shrunk below a pixel in the
scaled preview. The frame now posts its caret box with the selection event, and Studio draws it
as it does for a live preview. The caret box and the end-of-text caret move into core, so both
paths use one placement; a sealed edit opened with Enter now starts in the last text.
@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Correction to the test plan: on #7846 the walk does not crash on its first open. It sees no drawn caret on any of its nine open attempts (three texts, three tries each), so each counts as failed, and it crashes later at charBefore (Failed to execute 'setStart' on 'Range'), as in the viewport gate on #7846.

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Sealed preview (VITE_STUDIO_OPAQUE_PREVIEW_ENABLED=true), the inline-text-caret fixture's title opened with a press and Enter, the caret moved two letters left, captured at 3x.

Before

main 9dc416a: only the browser's own caret, a hairline in the scaled preview; Studio draws none.

Before: a hairline caret between "Tit" and "le"

After

This branch: Studio's caret, 2 px at screen size, where the frame reports it.

After: a 2 px caret between "Tit" and "le"

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

REQUEST_CHANGES at 70a41b54. One blocker: the sealed frame's caret colour reaches Studio's styles unchecked.

Need / Size / Reuse / Simpler / Structure: real need (the sealed preview showed no usable caret, and #7846's viewport gate fails on it); +412/-157; good reuse, with caret placement moved into core so the live and sealed paths share one copy; the design is about as simple as it gets; structure is fine.
Risk: the event is new frame-to-Studio input, and one of its fields lands in a Studio style unfiltered.

Blocker: caret.color skips the colour-only check.

  • isCaret (textEdit.ts) only checks that color is a short string.
  • InlineTextCaret's sealed effect then passes last.color straight through onScreen into style={{ background: placement.color }}, on Studio's own body.
  • The toolbar already guards this exact boundary. frameColoursOnly in InlineTextToolbar.tsx says: "A sealed frame's colours reach Studio's styles only when they are colours, so nothing else, like a url(), can." The caret path doesn't go through that check.
  • Probe on this head: a test in InlineTextCaret.sealed.test.tsx that posts { ...CARET, color: "url(https://probe.invalid/beacon)" } renders a caret whose style is background: url("https://probe.invalid/beacon"). So a sealed composition can make Studio's page load a URL it chooses, which is what the toolbar's filter exists to prevent.
  • Fix: run caret.color through the same CSS.supports("color", value) test (share the toolbar's helper), and draw nothing, or currentColor, otherwise. Add a test that the url(...) caret above draws no url background; it fails on this head.

Nit (non-blocking): left, top and height are only checked as finite numbers, so a frame can place a 2 px line anywhere on Studio's screen at any height. Clamping the drawn caret to the iframe's box would keep it inside the preview.

Verified:

  • Core textEdit.test.ts: 24/24 pass.
  • Studio InlineText* and useInlineTextEdit: 6 files, 73/73 pass.
  • The "fails without the fix" line is present and names the four new tests.
  • Moving placeCaretAtEnd into core is a verbatim move apart from Node.TEXT_NODE replacing 3. A sealed open with no point now calls it instead of collapsing at the element's edge.
  • Closing the session restores caretColor.

— Rames

@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 273 (base branch 271), smooth 66 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Newly passing (2)

  • seqscaletwice-scale-px-r0-nested-z100
  • seqscaletwice-scale-px-r0-root-z100

Not banked yet, reported and not gated (bank a full-grid run's baseline.json) (2)

  • seqscaletwice-scale-px-r0-nested-z100
  • seqscaletwice-scale-px-r0-root-z100

Quarantined, measured but not gated (0)

…o receives it

The caret colour went into the drawn caret's background unchecked, so a frame could post a url() and
make Studio fetch it. The colour-only filter moves from the toolbar to previewEditingClient, which
now hands every text-edit selection event to listeners with the caret and selection colours kept
only when they are colours.
The frame reports where its caret stands; Studio now clamps the drawn caret to the frame's box
and draws none when nothing of it falls inside, so a frame cannot place a line elsewhere on
Studio's screen.
@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Addressed at 0ac3837.

Blocker: caret.color skipped the colour-only check. Fixed where frame events enter Studio rather than in the caret. frameColoursOnly moves from InlineTextToolbar.tsx to previewEditingClient (player/lib/previewChannel.ts), which now wraps the client's onTextEditSelection. Every listener gets each event with caret kept only when CSS.supports("color", caret.color), and otherwise null, so no caret is drawn. The selection colours go through the same filter as before. The toolbar's own copy is gone, so the toolbar and the caret both read events that already passed the one check.

New test player/lib/previewChannel.frameColours.test.ts, with your probe: a text-edit-selection carrying color: "url(https://probe.invalid/beacon)" reaches listeners with caret: null, and with that url() in the selection's colours none of it reaches them. happy-dom's CSS.supports answers true for any value, so the test stands in Chrome's answer (no url( as a colour). InlineTextToolbar.browser.test.ts already checks in real Chrome that this filter drops url(x). Removing the three wrapping lines in previewChannel.ts turns both cases red.

Nit: the caret could be placed anywhere. Also done. The sealed caret is clamped to the frame's box (insideFrame in InlineTextCaret.tsx), and nothing is drawn when none of it falls inside. Test: "keeps a sealed frame's caret inside the preview's box, wherever the frame says it stands" (red at 04b32d4: left: 250099px).

Checked on Linux: studio typecheck clean; every studio unit test file touching text editing or the editing client, 331/331, 3 runs; the comment ratchet passes.

fails without the fix (all current regression tests): in packages/core/src/runtime/editing/textEdit.test.ts, "posts the caret's box, in the frame's px, when the selection is a caret in the text" and "opens with the caret at the end of the last text, inside it, not at the element's edge" fail on main 9dc416a. In packages/studio/src/components/editor/InlineTextCaret.sealed.test.tsx, "draws a sealed frame's caret at screen size where the frame reports it" and "draws nothing for another session's caret, and takes the caret away when the frame has none" fail on main 9dc416a, and "keeps a sealed frame's caret inside the preview's box, wherever the frame says it stands" fails at 04b32d4. In packages/studio/src/player/lib/previewChannel.frameColours.test.ts, "hands Studio a sealed frame's caret only when its colour is a colour, never a url() beacon" and "hands Studio a sealed frame's selection colours only when they are colours" fail at 70a41b5.

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-review at 0ac3837: approve. My blocker is fixed, and so is my nit about clamping the caret's box.

Need / Size / Reuse / Simpler / Structure: The fix is needed and small.

  • It reuses the existing colour check (CSS.supports("color", …)) and moves it out of InlineTextToolbar into previewEditingClient, at the point where events from the frame enter Studio. InlineTextCaret and InlineTextToolbar both get their client from there, and nothing else in studio subscribes to onTextEditSelection. That means a future reader gets the filter without having to remember to add it.
  • The new insideFrame keeps a sealed caret within the iframe's box. It draws nothing when the caret falls outside that box vertically, and it pins the caret's left edge so the caret's full width stays inside. The frame's numbers are already checked to be finite, with height ≥ 0 and viewportWidth > 0 (isCaret), so the clamp never sees NaN or Infinity.

Risk: Low. A caret whose colour fails the check is dropped entirely, not drawn uncoloured. A real frame's computed colour always passes, so the drop only ever hits a frame posting something that isn't a colour.

Evidence

  • components/editor/ plus previewChannel.frameColours.test.ts: 2209 passed. tsc --noEmit for studio is clean.
  • Fails without the fix: with previewChannel.ts restored to 70a41b5, both frameColours tests fail. A url() caret is passed through, and url( reaches the selection event.
  • Mutants, 6/6 caught:
    • previewChannel.ts: unfiltered caret colour; unfiltered colours; unfiltered painted; unfiltered styles.color; wrapper dropped.
    • InlineTextCaret.tsx: insideFrame clamp removed.

CI was still pending when I posted this, with no failures so far.

— Rames

Core packs 2,559 bytes more: the caret placement Studio and the sealed frame now share, and the
caret the frame reports. Main already stood 0.83% over the old budget, so this tipped it past
the 1% tolerance; the budget is now the packed size CI measured on this branch.
@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Build was red on the package size budget: @hyperframes/core packed 1,968,011 bytes against 1,948,041 (+1% tolerance). Measured locally, main packs 1,964,182 (already 0.83% over) and this branch 1,966,741, so this PR adds 2,559 bytes: the caret placement now shared by Studio and the sealed frame, and the caret the frame reports. At e8533d4 the budget is the size CI measured here. Test reachability (a Git LFS download of an unrelated asset) and both CodeQL jobs (the results upload) failed for reasons outside this change; the push reruns them.

This branch has not been deployed

No deployments
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.

2 participants