Skip to content

Fix dead-key and input method typing in Safari shadow roots - #6362

Open
vladimir-tikhonov-nutrient wants to merge 5 commits into
ianstormtaylor:mainfrom
vladimir-tikhonov-nutrient:fix-safari-shadow-dom-composition
Open

vladimir-tikhonov-nutrient wants to merge 5 commits into
ianstormtaylor:mainfrom
vladimir-tikhonov-nutrient:fix-safari-shadow-dom-composition

Conversation

@vladimir-tikhonov-nutrient

@vladimir-tikhonov-nutrient vladimir-tikhonov-nutrient commented Nov 4, 2026 •

Copy link
Copy Markdown

Description

In Safari, typing with a dead key or an input method in an editor inside a shadow root loses text. With a US layout, typing "C", Option+U, "o" and then "mo" should give "Cömo". Instead the editor ends with "Cö". The accented letter arrives, but the caret is gone and the letters typed after it are lost.

Safari's getSelection() doesn't return nodes inside a shadow root, so for these editors onDOMSelectionChange reads the selection from the beforeinput event that document.execCommand('indent') triggers. On main that workaround also runs during a composition, when the caret sits after text the input method hasn't committed yet, and moves the Slate selection past the end of the stored text. Once the composition is committed, the selection can't be mapped back to the DOM, so the DOM selection is removed and the editor has no caret.

This PR skips the workaround while composing, as the regular selection path already does. In Safari, Slate also removes the DOM selection on blur, which drops an ongoing composition without a compositionend event and leaves the composing state set. With the workaround skipped, Slate would then ignore selection changes until the next key press or click in the editor, so onBlur now clears the composing state after removing the selection.

With these two changes, a click in the editor during a composition still didn't move the caret. On macOS, WebKit ends the composition right after mousedown, and in a shadow root the re-render after compositionend wrote the old selection back over the click. onMouseDown now clears the composing state, so the re-render runs before the click moves the caret. It runs after the app's own onMouseDown and is skipped when the event is handled or prevented, because a prevented mousedown keeps the composition open. It does nothing on iOS and iPadOS.

Issue

None that I could find. The workaround was added in #5648 and enabled for Safari 17 in #5659.

Example

macOS input methods can't reach Playwright's WebKit build (see playwright/devices/README.md), so four new WebKit-only tests in shadow-dom.test.ts replay the events and DOM changes of a composition.

  • Type "C", a replayed Option+E and "o", then "mo". On main the editor ends with "Có" in 5 of 5 runs, so the "mo" is lost. With this PR the editor has "Cómo".
  • Type "A", replay a Japanese conversion to "漢字", then type "b". On main the "b" is lost in 5 of 5 runs, and with this PR the editor has "A漢字b".
  • Type "Hello" and a replayed Option+E, then click before "Hello" and type "Oh ". Without the onMouseDown change the letters go to the old caret ("HelloOh "), and with it the editor has "Oh Hello".
  • Type "Hello" and a replayed Option+E, focus a link, click before "Hello" and type "Oh ". With only the onDOMSelectionChange change, the letters go to the old caret ("HelloOh ") in 5 of 5 runs. With the onBlur or the onMouseDown change added, the editor has "Oh Hello", because either one clears the composing state before the click. On main this test passes too, because the workaround still runs during a composition.

composition-state-webkit.spec.tsx covers the onMouseDown and onBlur changes in jest, which CI runs.

The replayed events are untrusted and have no target ranges, so I also ran the shadow DOM example in a macOS app that embeds WebKit (the engine of Safari 27.2) and sends key events through the system input method. On main, "C", the Option+U dead key and "omo" leave "Cö" with no caret, and a Japanese-style composition sent through the calls an input method makes loses the letter typed after it. With this PR they give "Cömo" and "A漢字b". With an Option+U accent pending after "Cxmo", I clicked before the "x", holding the button for 100 ms. Without the onMouseDown change the caret stayed after "Cxmo", where the composition started; with it, the caret went before the "x". With mousedown prevented, the composition survives the click. On iOS 27.2 simulators (iPhone and iPad, Japanese keyboard), a tap during a composition never reached the onMouseDown change. The rest of the Playwright suite on Chromium, mobile and WebKit gives the same results as on main. The webkit project only runs on macOS, so CI won't run the four Playwright tests.

Context

The onBlur change sits in the existing IS_WEBKIT block, so it also runs outside shadow roots. Replaying the blur test in the plaintext example, which has no shadow root, gives "HelloOh " on main and "Oh Hello" with this PR. That case isn't in the new tests. The onMouseDown change also runs outside shadow roots. In the plaintext example, the same click in the macOS app puts the caret before the "x" with and without it.

Checks

  • The new code matches the existing patterns and styles.
  • The tests pass with yarn test.
  • The linter passes with yarn lint. (Fix errors with yarn fix.)
  • The relevant examples still work. (Run examples with yarn start.)
  • You've added a changeset if changing functionality. (Add one with yarn changeset add.)

@changeset-bot

changeset-bot Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 87914ce

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
slate-react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

In WebKit, a click ends an open composition right after mousedown. In a
shadow root, the re-render after compositionend then wrote the old Slate
selection back over the click, so the caret stayed where the composition
started. Clear the composing state on mousedown, so the re-render runs
before the click moves the caret, and add a WebKit replay test for it.
// started. Clear the composing state now, so the re-render
// happens before the caret moves.
if (
IS_WEBKIT &&

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.

  • IS_WEBKIT also matches iOS Safari and every iOS browser, since they all report AppleWebKit without Chrome
  • a tap sends a synthesized mousedown, so tapping in the editor during a kana composition clears composing here
  • the new tests skip everything but desktop WebKit, so nothing covers iOS
  • was this tried on iOS? if it doesn't end the composition there, gate it on !IS_IOS

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Tried it on iPhone and iPad simulators with a Japanese keyboard: a tap never gets this far mid-composition. Only macOS WebKit ends a composition on mousedown anyway, so it's gated now, with unit tests. IS_IOS misses iPad Safari (it reports a Mac user agent), so there's a touch check as well.

setIsComposing(false)
}

isEventHandled(event, attributes.onMouseDown)

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.

  • the reset runs before attributes.onMouseDown, so a handler that calls preventDefault can't stop it
  • if WebKit keeps the composition open when mousedown is prevented, composing is now false mid-composition, intended?
  • other handlers in this file check isEventHandled first and skip the editor's own work when it returns true

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Not intended, good catch. Safari keeps the composition open when mousedown is prevented, and the early reset then cancelled it. The handler runs first now, and a handled or prevented event skips the reset.

@dylans

dylans commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Still planning to pick this up? My review comments above are open. If you don't have time, let me know and I'll take it over.

Safari on macOS ends an open composition when a mousedown runs its default
action, so the editor clears its composing state then. It did that before
calling the app's onMouseDown and without looking at the event. A prevented
mousedown keeps the composition open, though. Clearing the state made the
editor write its selection back to the DOM, and that cancelled the
composition.

- Call onMouseDown first. Skip the reset when it handles the event, or when
  the default is prevented, also by an element inside the editor.
- Skip the reset on iOS and iPadOS, where WebKit doesn't end a composition
  on mousedown. iPadOS reports a Mac user agent, so check for touch too.
- Use setComposing for the blur and mousedown resets.
- Add unit tests for the mousedown and blur resets.
@vladimir-tikhonov-nutrient

Copy link
Copy Markdown
Author

Yes, sorry for the delay. Just pushed, with main merged in.

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