Repository navigation
fix(studio): a sealed preview shows its text caret at screen size - #8857
miguel-heygen wants to merge 4 commits into
Conversation
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.
|
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 |
|
Sealed preview ( Beforemain 9dc416a: only the browser's own caret, a hairline in the scaled preview; Studio draws none. AfterThis branch: Studio's caret, 2 px at screen size, where the frame reports it. |
jrusso1020
left a comment
There was a problem hiding this comment.
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 thatcoloris a short string.InlineTextCaret's sealed effect then passeslast.colorstraight throughonScreenintostyle={{ background: placement.color }}, on Studio's own body.- The toolbar already guards this exact boundary.
frameColoursOnlyinInlineTextToolbar.tsxsays: "A sealed frame's colours reach Studio's styles only when they are colours, so nothing else, like aurl(), can." The caret path doesn't go through that check. - Probe on this head: a test in
InlineTextCaret.sealed.test.tsxthat posts{ ...CARET, color: "url(https://probe.invalid/beacon)" }renders a caret whose style isbackground: 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.colorthrough the sameCSS.supports("color", value)test (share the toolbar's helper), and draw nothing, orcurrentColor, otherwise. Add a test that theurl(...)caret above draws nourlbackground; 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*anduseInlineTextEdit: 6 files, 73/73 pass. - The "fails without the fix" line is present and names the four new tests.
- Moving
placeCaretAtEndinto core is a verbatim move apart fromNode.TEXT_NODEreplacing3. A sealed open with no point now calls it instead of collapsing at the element's edge. - Closing the session restores
caretColor.
— Rames
Edit accuracy: accurate 273 (base branch 271), smooth 66 of thoseThe gate passes. Newly passing (2)
Not banked yet, reported and not gated (bank a full-grid run's baseline.json) (2)
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.
|
Addressed at 0ac3837. Blocker: New test Nit: the caret could be placed anywhere. Also done. The sealed caret is clamped to the frame's box ( 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 |
jrusso1020
left a comment
There was a problem hiding this comment.
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 ofInlineTextToolbarintopreviewEditingClient, at the point where events from the frame enter Studio.InlineTextCaretandInlineTextToolbarboth get their client from there, and nothing else in studio subscribes toonTextEditSelection. That means a future reader gets the filter without having to remember to add it. - The new
insideFramekeeps 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/pluspreviewChannel.frameColours.test.ts: 2209 passed.tsc --noEmitfor studio is clean.- Fails without the fix: with
previewChannel.tsrestored to 70a41b5, both frameColours tests fail. A url() caret is passed through, andurl(reaches the selection event. - Mutants, 6/6 caught:
previewChannel.ts: unfiltered caret colour; unfilteredcolours; unfilteredpainted; unfilteredstyles.color; wrapper dropped.InlineTextCaret.tsx:insideFrameclamp 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.
|
Build was red on the package size budget: |


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 (
selectedRangereturned null for a collapsed selection), anduseInlineTextEditingpassed no session toInlineTextCaretfor 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 throwsFailed 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.tsholds the caret's placement, moved fromInlineTextCaret.tsxunchanged: the collapsed range's box, the box beside a node boundary, the soft-wrap side a key leaves, the caret colour, andplaceCaretAtEnd. A live preview (Studio reading the frame) and a sealed one (the frame reading itself) now use the same code.caret: { left, top, height, color, viewportWidth } | nulltotext-edit-selection. The event's validator takescaretin that exact, bounded shape and nothing else.InlineTextCaretdraws a sealed session's caret from that event, mapped by the frame's box over itsviewportWidth, and follows the frame's box as the live path does.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
charBeforeon 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