Skip to content

[lexical][lexical-playground] Bug Fix: Add workarounds for buggy browser behavior around macOS text replacements#8417

Merged
etrepum merged 13 commits into
facebook:mainfrom
nmggithub:fix-macos-text-replacements-accepted-by-backspace
May 1, 2026
Merged

[lexical][lexical-playground] Bug Fix: Add workarounds for buggy browser behavior around macOS text replacements#8417
etrepum merged 13 commits into
facebook:mainfrom
nmggithub:fix-macos-text-replacements-accepted-by-backspace

Conversation

@nmggithub

@nmggithub nmggithub commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

Description

This adds workaround behavior where typing a character to accept a macOS text replacement used to leave the caret before that character instead of after. It also adds workaround behavior where pressing Backspace (and performing Select All, apparently) would accept a pending macOS text replacement instead of deleting a character.

Closes #8413
Closes #8416

Test plan

There are tests added for the caret placement bug. The Backspace / Select All acceptance bug relies on native behavior that can't easily be synthesized, so there are no tests around that.

Before

Screen.Recording.2026-04-28.at.14.11.05.mov
Screen.Recording.2026-04-28.at.16.02.05.mov

After

Screen.Recording.2026-04-28.at.14.11.32.mov
Screen.Recording.2026-04-28.at.16.02.35.mov

@vercel

vercel Bot commented Apr 28, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview, Comment May 1, 2026 4:35am
lexical-playground Ready Ready Preview, Comment May 1, 2026 4:35am

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 28, 2026
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Apr 28, 2026
@nmggithub

nmggithub commented Apr 28, 2026

Copy link
Copy Markdown
Contributor Author

This might not actually be possible due to Chrome's bad heuristics. Still looking into it myself. EDIT: I think I figured it out now. It's a bit wonky but it does seem to work.

@etrepum etrepum left a comment

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.

I think we should try and smooth out all of the wonky text replacement behavior we can find in one pass, the behavior in chrome with this one is still a bit wonky. For example pressing return or space should accept the replacement then do the behavior of that key. Right now it just accepts the replacement and effectively prevents default (haven't done a thorough audit of that section of the events code to step through what it's doing, just QA). The best justification to do it all at once is to make sure that we add the minimum amount of implicit state machine to make it work rather than add a little bit for each workaround.

Safari seems to have the right behavior before this patch, can also be tested in chrome in any textarea.

Comment thread packages/lexical/src/LexicalEvents.ts Outdated
Comment on lines +624 to +627
handledSelectionCommandTimeoutId = setTimeout(() => {
isInsertTextAfterHandledSelectionCommand = false;
handledSelectionCommandTimeoutId = null;
}, 0);

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.

Looks like this is effectively the same as the above function, should save a few bytes to just call it

Suggested change
handledSelectionCommandTimeoutId = setTimeout(() => {
isInsertTextAfterHandledSelectionCommand = false;
handledSelectionCommandTimeoutId = null;
}, 0);
handledSelectionCommandTimeoutId = setTimeout(clearHandledSelectionCommandInsertText, 0);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should be resolved by 2e29782.

Comment thread packages/lexical/src/LexicalEvents.ts Outdated
Comment on lines +1346 to +1350
} else if (isSelectAll(event)) {
event.preventDefault();
dispatchCommand(editor, SELECT_ALL_COMMAND, event);
if (dispatchCommand(editor, SELECT_ALL_COMMAND, event)) {
markHandledSelectionCommandInsertText();
}

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.

As far as I can tell this can be lifted up after isRedo and not repeated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should be resolved by 2e29782.

@nmggithub

Copy link
Copy Markdown
Contributor Author

@etrepum yeah, I'm not particularly happy with fix.

This patch of Chromium I submitted for Electron fixes it for Chrome: electron/electron#50432

To note, Chrome is the only browser between it, Firefox, and Safari, that has this bug.

#8414 is a different story. Firefox and Chrome exhibit that bug, but Safari doesn't.

Both bugs closely related, and there's spec questions to be had at how all three browsers implement InputEvents around macOS text replacements, but we have to work around those for both bugs here.

@nmggithub

nmggithub commented Apr 30, 2026

Copy link
Copy Markdown
Contributor Author

I think we should try and smooth out all of the wonky text replacement behavior we can find in one pass,

I find

  1. Backspace accepts text replacement, and
  2. caret ends up before the acceptance boundary,

to be two separate bugs (which is why I filed them separately). Bug 1 only exists in Chrome and is arguably because it has the worst implementation of InputEvents here (I could go into detail, but I'll save space here). Bug 2 exists in Chrome and Firefox because they both fire the Space, Return, Etc. event before the event that replaces the text. Safari fires the event that replaces the text first (hence how I wrote the test for #8414).

Again, we can't really change any of this, only work around it.

@nmggithub nmggithub requested a review from etrepum April 30, 2026 02:05
@etrepum

etrepum commented Apr 30, 2026

Copy link
Copy Markdown
Collaborator

I agree that conceptually these are different things because there are different keystrokes involved and have slightly different browser compatibility matrices. However, as far as reviewing goes it would be much easier and more efficient for me if I had one PR to review that worked around all of the known bad behavior with macOS text replacements. This PR for example is shipping without any sort of test infrastructure (playwright doesn't really make it feasible to write real tests for IMEs anyway) and requires manual QA in all of the browsers, so it would be way better if I only had to go through that process for one PR.

@nmggithub

Copy link
Copy Markdown
Contributor Author

I agree that conceptually these are different things because there are different keystrokes involved and have slightly different browser compatibility matrices. However, as far as reviewing goes it would be much easier and more efficient for me if I had one PR to review that worked around all of the known bad behavior with macOS text replacements. This PR for example is shipping without any sort of test infrastructure (playwright doesn't really make it feasible to write real tests for IMEs anyway) and requires manual QA in all of the browsers, so it would be way better if I only had to go through that process for one PR.

Understandable, I can combine everything into this PR if you prefer, since we're having the convo here already.

@etrepum

etrepum commented Apr 30, 2026

Copy link
Copy Markdown
Collaborator

What I would like to see working (with the selection on the rightmost character except in the arrow key cases):

  • "omw<space>" results in "On My Way! "
  • "omw<backspace>" results in "om" (dismisses the text replacement and removes w)
  • "omw!" results in "On My Way!!" (most punctuation and symbols should accept and continue according to platform behavior elsewhere)
  • "omw<enter>" results in "On My Way!\n" (newline, in the default lexical case this would be a new paragraph)
  • "omw<arrowkey>" results in "omw" (dismisses the text replacement) and results in the default navigation for that key
  • "omw<esc>" results in "omw" (dismisses the text replacement)

Ideally this would just be one PR where I can carefully read through the code and all of the required context and run through all of the scenarios in each browser to verify expected behavior. With multiple PRs I have to repeat all of this.

In the current playground all of these work as expected in Safari, but the behavior is mixed in the other browsers

@etrepum

etrepum commented Apr 30, 2026

Copy link
Copy Markdown
Collaborator

Rolling it up into this PR works for me

@nmggithub nmggithub changed the title [lexical][lexical-playground] Bug Fix: Stop Backspace from accepting macOS text replacements [lexical][lexical-playground] Bug Fix: Add workarounds for buggy browser behavior around macOS text replacements Apr 30, 2026
@nmggithub

nmggithub commented Apr 30, 2026

Copy link
Copy Markdown
Contributor Author

A long bit of context for these bugs.

The bugs

The two bugs:

  1. Backspace (and Select All, apparently) accepts a pending text replacement, and
  2. caret ends up before the acceptance boundary after accepting a pending text replacement,

The InputEvents

Safari (No Bugs)

Safari has, arguably, the most sensible behavior around these replacements. It exhibits neither of these bugs.

Typing omw<space> in Safari results in the last two InputEvents being:

  1. { inputType: "insertReplacementText", dataTransfer: { ... "On my way!" (equivalently) ... }), and
  2. { inputType: "insertText", data: " " } .

This gets two things right:

  • it uses insertReplacementText for the event that replaces the text (matching Input Events Level 2), and
  • it fires the Space event after the replacement event (which plays nicely with Lexical's event handling, leaving the caret in the correct spot).

Firefox (Bug 2)

Firefox flips the order, but at least gets insertReplacementText right:

  1. { inputType: "insertText", data: " " } , and
  2. { inputType: "insertReplacementText", dataTransfer: { ... "On my way!" (equivalently) ... }).

Chrome (Bug 1, Bug 2)

Chrome doesn't even use insertReplacementText for this:

  1. { inputType: "insertText", data: " " } , and
  2. { inputType: "insertText", data: "On my way!" }.

Chrome also is the only one that exhibits Bug 1.
From what I can tell, its due to some internal logic error. I've squashed Bug 1 in Electron by adding a Electron-local Chromium patch, but it's also possible that logic I patched was/is in plumbing that wouldn't even be hit if Chrome chose to use insertReplacementText for this instead (but I can't confirm that yet).

Regardless, Chrome's choice to not use insertReplacementText is apparently deliberate, with them only using it for their own first-party replacements. There is an open issue trying to get them to use insertReplacementText for maOS text replacements, though.

Proper Long-Term Solutions

  • Getting all browsers to adopt Safari's event ordering would likely finally squash Bug 2 and remove the need for our workaround.
  • Getting Chromium to use the insertReplacementText plumbing for macOS text replacements may squash Bug 1 and remove the need for our workaround, but it might entail a drift in deliberate policy by the Chromium team to not use insertReplacementText for third-party replacements (despite how arguably out-of-spec that is).

@etrepum etrepum left a comment

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.

This is much improved! I've found two edge cases that we should consider handling before this is merged:

Chrome - if you complete the text replacement with the same character that the replacement ends with, the selection ends up before the last character you typed.

Screen.Recording.2026-04-30.at.07.48.07.mov

Firefox - punctuation doesn't accept the text replacement

Screen.Recording.2026-04-30.at.07.48.57.mov

@nmggithub

nmggithub commented Apr 30, 2026

Copy link
Copy Markdown
Contributor Author

Firefox - punctuation doesn't accept the text replacement

This appears to be the current behavior before my patches in both contenteditable and regular textarea and input elements in Firefox. I don't know if we want to deliberately stray from that. I would liken this to the fact that Tab only accepts the text replacement in Chrome. It seems to be a consistent browser difference.

I will investigate the Chrome bug, though.

@nmggithub

Copy link
Copy Markdown
Contributor Author

The lingering Chrome bug has been squashed! I left the Firefox bug as-is because it appears to be consistent across all kinds of inputs in that browser.

@etrepum etrepum left a comment

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.

QA looks great, agreed that it's fine to leave the Firefox behavior as-is. Will merge after taking a closer look at the code when I have the time.

@etrepum etrepum added this pull request to the merge queue May 1, 2026
Merged via the queue into facebook:main with commit f50bfbb May 1, 2026
38 checks passed
@etrepum etrepum mentioned this pull request May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Backspace accepts macOS text replacement Bug: pressing space to accept a macOS text replacement leaves the caret before the space

2 participants