Skip to content
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

Use the target range to insert the replacement text #1131

Merged
merged 1 commit into from
Feb 5, 2024
Merged

Conversation

afcapel
Copy link
Contributor

@afcapel afcapel commented Feb 2, 2024

When the browser auto-corrects a misspelled word, it dispatches a beforeinput event with the insertReplacementText input type and a dataTransfer object containing the replacement text. The event also contains a getTargetRanges method that returns an array of DOMRange objects representing the range of text that will be replaced.

When the Level2InputController was originally implemented, not all browsers supported the getTargetRanges method, so it was not used.

Now that all supported browsers do support getTargetRanges, we can use it to insert the replacement text at the correct location. This ensures editor state is updated correctly the cursor is positioned correctly.

Ref.

When the browser auto-corrects a misspelled word, it dispatches a
`beforeinput` event with the `insertReplacementText` input type and a
`dataTransfer` object containing the replacement text. The event also
contains a `getTargetRanges` method that returns an array of `DOMRange`
objects representing the range of text that will be replaced.

When the `Level2InputController` was originally implemented, not all
browsers supported the `getTargetRanges` method, so it was not used.

Now that all supported browsers do support `getTargetRanges`, we can use
it to insert the replacement text at the correct location. This ensures
editor state is updated correctly the cursor is positioned correctly.

Ref.

- https://w3c.github.io/input-events/#overview (search for "insertReplacementText")
- https://developer.mozilla.org/en-US/docs/Web/API/InputEvent/getTargetRanges
@afcapel afcapel merged commit e8dec44 into main Feb 5, 2024
1 check passed
@afcapel afcapel deleted the selected-ranges branch February 5, 2024 10:52
Copy link
Contributor

@kevinmcconnell kevinmcconnell left a comment

Choose a reason for hiding this comment

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

Makes sense! Great job tracking this down, and staying on top of the never-ending cat & mouse game of browser changes.

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