Repository navigation
Fix dead-key and input method typing in Safari shadow roots - #6362
vladimir-tikhonov-nutrient wants to merge 5 commits into
Conversation
🦋 Changeset detectedLatest commit: 87914ce The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 && |
There was a problem hiding this comment.
IS_WEBKITalso 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
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
- the reset runs before
attributes.onMouseDown, so a handler that callspreventDefaultcan'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
isEventHandledfirst and skip the editor's own work when it returns true
There was a problem hiding this comment.
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.
|
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.
|
Yes, sorry for the delay. Just pushed, with main merged in. |
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 editorsonDOMSelectionChangereads the selection from thebeforeinputevent thatdocument.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
compositionendevent 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, soonBlurnow 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 aftercompositionendwrote the old selection back over the click.onMouseDownnow clears the composing state, so the re-render runs before the click moves the caret. It runs after the app's ownonMouseDownand is skipped when the event is handled or prevented, because a preventedmousedownkeeps 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 inshadow-dom.test.tsreplay the events and DOM changes of a composition.onMouseDownchange the letters go to the old caret ("HelloOh "), and with it the editor has "Oh Hello".onDOMSelectionChangechange, the letters go to the old caret ("HelloOh ") in 5 of 5 runs. With theonBluror theonMouseDownchange 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.tsxcovers theonMouseDownandonBlurchanges 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
onMouseDownchange the caret stayed after "Cxmo", where the composition started; with it, the caret went before the "x". Withmousedownprevented, the composition survives the click. On iOS 27.2 simulators (iPhone and iPad, Japanese keyboard), a tap during a composition never reached theonMouseDownchange. The rest of the Playwright suite on Chromium, mobile and WebKit gives the same results as on main. Thewebkitproject only runs on macOS, so CI won't run the four Playwright tests.Context
The
onBlurchange sits in the existingIS_WEBKITblock, 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. TheonMouseDownchange 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
yarn test.yarn lint. (Fix errors withyarn fix.)yarn start.)yarn changeset add.)