Repository navigation
Fix dead-key and input method typing in Safari shadow roots #6362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vladimir-tikhonov-nutrient
wants to merge
5
commits into
ianstormtaylor:main
Choose a base branch
from
vladimir-tikhonov-nutrient:fix-safari-shadow-dom-composition
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
33b81c2
Fix dead-key and input method typing in Safari shadow roots
vladimir-tikhonov-nutrient 4d79f05
Put the caret where the user clicks during a Safari composition
vladimir-tikhonov-nutrient cc72371
Merge remote-tracking branch 'origin/main' into fix-safari-shadow-dom…
vladimir-tikhonov-nutrient 7b0a70f
Clear composing on mousedown only where Safari ends the composition
vladimir-tikhonov-nutrient 87914ce
Merge remote-tracking branch 'origin/main' into fix-safari-shadow-dom…
vladimir-tikhonov-nutrient File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'slate-react': patch | ||
| --- | ||
|
|
||
| In Safari, when the editor is inside a shadow root, typing with a dead key or an input method no longer loses text, and a click during a composition now moves the caret |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
packages/slate-react/test/composition-state-webkit.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| import React from 'react' | ||
| import { createEditor, Descendant } from 'slate' | ||
| import * as SlateDOM from 'slate-dom' | ||
| import { act, fireEvent, render } from '@testing-library/react' | ||
| import { Editable, ReactEditor, Slate, useComposing, withReact } from '../src' | ||
|
|
||
| // Safari on macOS. Tests for other environments replace these flags. | ||
| jest.mock('slate-dom', () => ({ | ||
| ...jest.requireActual('slate-dom'), | ||
| IS_WEBKIT: true, | ||
| IS_IOS: false, | ||
| })) | ||
|
|
||
| // jsdom does not implement `isContentEditable`. | ||
| Object.defineProperty(HTMLElement.prototype, 'isContentEditable', { | ||
| get() { | ||
| return ( | ||
| this.closest('[contenteditable]')?.getAttribute('contenteditable') === | ||
| 'true' | ||
| ) | ||
| }, | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks() | ||
| delete (window.navigator as { maxTouchPoints?: number }).maxTouchPoints | ||
| }) | ||
|
|
||
| const initialValue: Descendant[] = [ | ||
| { type: 'paragraph', children: [{ text: 'hello world' }] } as Descendant, | ||
| ] | ||
|
|
||
| const setup = (props: React.ComponentProps<typeof Editable> = {}) => { | ||
| const editor = withReact(createEditor()) | ||
| const composing: boolean[] = [] | ||
| const ComposingProbe = () => { | ||
| composing.push(useComposing()) | ||
| return null | ||
| } | ||
| const { getByRole, getByTestId } = render( | ||
| <Slate editor={editor} initialValue={initialValue}> | ||
| <Editable | ||
| tabIndex={0} | ||
| renderElement={({ attributes, children }) => ( | ||
| <p {...attributes}> | ||
| <ComposingProbe /> | ||
| {children} | ||
| <span | ||
| contentEditable={false} | ||
| data-testid="chip" | ||
| onMouseDown={event => event.preventDefault()} | ||
| /> | ||
| </p> | ||
| )} | ||
| {...props} | ||
| /> | ||
| </Slate> | ||
| ) | ||
| const textbox = getByRole('textbox') | ||
| const chip = getByTestId('chip') | ||
| const isComposing = () => ({ | ||
| rendered: composing[composing.length - 1], | ||
| editor: ReactEditor.isComposing(editor), | ||
| }) | ||
| return { textbox, chip, isComposing } | ||
| } | ||
|
|
||
| const startComposition = async (textbox: HTMLElement) => { | ||
| await act(async () => { | ||
| fireEvent.compositionStart(textbox, { data: '' }) | ||
| fireEvent.compositionUpdate(textbox, { data: 'k' }) | ||
| }) | ||
| } | ||
|
|
||
| const mouseDown = async (element: HTMLElement) => { | ||
| await act(async () => { | ||
| fireEvent.mouseDown(element) | ||
| }) | ||
| } | ||
|
|
||
| const composingState = { rendered: true, editor: true } | ||
| const idleState = { rendered: false, editor: false } | ||
|
|
||
| describe('composition state in WebKit', () => { | ||
| test('mousedown clears the composing state', async () => { | ||
| const onMouseDown = jest.fn() | ||
| const { textbox, isComposing } = setup({ onMouseDown }) | ||
|
|
||
| await startComposition(textbox) | ||
| expect(isComposing()).toEqual(composingState) | ||
|
|
||
| await mouseDown(textbox) | ||
|
|
||
| expect(onMouseDown).toHaveBeenCalledTimes(1) | ||
| expect(isComposing()).toEqual(idleState) | ||
| }) | ||
|
|
||
| test.each<[string, (event: React.MouseEvent) => void | boolean]>([ | ||
| ['prevents the default', event => event.preventDefault()], | ||
| [ | ||
| 'prevents the default and returns false', | ||
| event => { | ||
| event.preventDefault() | ||
| return false | ||
| }, | ||
| ], | ||
| ['stops propagation', event => event.stopPropagation()], | ||
| ['returns true', () => true], | ||
| ])( | ||
| 'mousedown keeps the composing state when the handler %s', | ||
| async (_, handler) => { | ||
| const onMouseDown = jest.fn(handler) | ||
| const { textbox, isComposing } = setup({ onMouseDown }) | ||
|
|
||
| await startComposition(textbox) | ||
| await mouseDown(textbox) | ||
|
|
||
| expect(onMouseDown).toHaveBeenCalledTimes(1) | ||
| expect(isComposing()).toEqual(composingState) | ||
| } | ||
| ) | ||
|
|
||
| test('mousedown keeps the composing state when an element in the editor prevents the default', async () => { | ||
| const { textbox, chip, isComposing } = setup() | ||
|
|
||
| await startComposition(textbox) | ||
| await mouseDown(chip) | ||
|
|
||
| expect(isComposing()).toEqual(composingState) | ||
| }) | ||
|
|
||
| test('mousedown keeps the composing state on iOS', async () => { | ||
| jest.replaceProperty( | ||
| jest.requireMock<typeof SlateDOM>('slate-dom'), | ||
| 'IS_IOS', | ||
| true | ||
| ) | ||
| const { textbox, isComposing } = setup() | ||
|
|
||
| await startComposition(textbox) | ||
| await mouseDown(textbox) | ||
|
|
||
| expect(isComposing()).toEqual(composingState) | ||
| }) | ||
|
|
||
| test('mousedown keeps the composing state on iPadOS, which reports a Mac user agent', async () => { | ||
| Object.defineProperty(window.navigator, 'maxTouchPoints', { | ||
| configurable: true, | ||
| value: 5, | ||
| }) | ||
| const { textbox, isComposing } = setup() | ||
|
|
||
| await startComposition(textbox) | ||
| await mouseDown(textbox) | ||
|
|
||
| expect(isComposing()).toEqual(composingState) | ||
| }) | ||
|
|
||
| test('mousedown keeps the composing state outside WebKit', async () => { | ||
| jest.replaceProperty( | ||
| jest.requireMock<typeof SlateDOM>('slate-dom'), | ||
| 'IS_WEBKIT', | ||
| false | ||
| ) | ||
| const { textbox, isComposing } = setup() | ||
|
|
||
| await startComposition(textbox) | ||
| await mouseDown(textbox) | ||
|
|
||
| expect(isComposing()).toEqual(composingState) | ||
| }) | ||
|
|
||
| test('blur clears the composing state', async () => { | ||
| const { textbox, isComposing } = setup() | ||
|
|
||
| await act(async () => { | ||
| textbox.focus() | ||
| }) | ||
| await startComposition(textbox) | ||
| expect(isComposing()).toEqual(composingState) | ||
|
|
||
| await act(async () => { | ||
| textbox.blur() | ||
| }) | ||
|
|
||
| expect(isComposing()).toEqual(idleState) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IS_WEBKITalso matches iOS Safari and every iOS browser, since they all report AppleWebKit without Chrome!IS_IOSThere was a problem hiding this comment.
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
mousedownanyway, so it's gated now, with unit tests.IS_IOSmisses iPad Safari (it reports a Mac user agent), so there's a touch check as well.