Skip to content

[lexical] Bug Fix: Preserve multiline IME text commits - #9991

Open
wrvsrx wants to merge 1 commit into
meta:mainfrom
wrvsrx:fix/multiline-ime-commit
Open

wrvsrx wants to merge 1 commit into
meta:mainfrom
wrvsrx:fix/multiline-ime-commit

Conversation

@wrvsrx

@wrvsrx wrvsrx commented Nov 28, 2026

Copy link
Copy Markdown

Description

Chromium can split a multiline insertText commit into multiple DOM edits when the caret is inside an existing text node. Lexical can then retain only the first line. Route cancelable, non-composing multiline text commits through the existing controlled insertion command before Chromium changes the DOM. Single-line input and the existing newline-only branches are unchanged.

Reproduce on Android Chrome with Fcitx5-android: open the plain-text Lexical Playground, type X, and insert an AAA/BBB two-line entry from the keyboard's clipboard panel. Only XAAA remains; a native contenteditable with the same prefix retains both lines.

Test plan

Chromium: vitest run --project browser LexicalMultilineInput.test.ts LexicalComposition.test.ts (system Chromium via local launch configuration).

Before

× handles a multiline text commit in plain text
× handles a multiline text commit in rich text
AssertionError: expected false to be true // Object.is equality
Tests  2 failed (2)

After

✓ LexicalMultilineInput.test.ts (2 tests)
✓ LexicalComposition.test.ts (24 tests | 1 skipped)
Tests  25 passed | 1 skipped (26)

Firefox and WebKit were not run locally. An equivalent downstream workaround was verified with the original Fcitx5-android reproduction; the core change is covered by the Chromium regression above.

## Description

Chromium can split a multiline `insertText` commit into multiple DOM edits when the caret is inside an existing text node. Lexical can then retain only the first line. Route cancelable, non-composing multiline text commits through the existing controlled insertion command before Chromium changes the DOM. Single-line input and the existing newline-only branches are unchanged.

Reproduce on Android Chrome with Fcitx5-android: open the [plain-text Lexical Playground](https://playground.lexical.dev/?isRichText=false&emptyEditor=true), type `X`, and insert an `AAA`/`BBB` two-line entry from the keyboard's clipboard panel. Only `XAAA` remains; a native `contenteditable` with the same prefix retains both lines.

## Test plan

Chromium: `vitest run --project browser LexicalMultilineInput.test.ts LexicalComposition.test.ts` (system Chromium via local launch configuration).

### Before

```text
× handles a multiline text commit in plain text
× handles a multiline text commit in rich text
AssertionError: expected false to be true // Object.is equality
Tests  2 failed (2)
```

### After

```text
✓ LexicalMultilineInput.test.ts (2 tests)
✓ LexicalComposition.test.ts (24 tests | 1 skipped)
Tests  25 passed | 1 skipped (26)
```

Firefox and WebKit were not run locally. An equivalent downstream workaround was verified with the original Fcitx5-android reproduction; the core change is covered by the Chromium regression above.
@vercel

vercel Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
lexical Ready Ready Preview Nov 28, 2026 5:14pm UTC
lexical-playground Ready Ready Preview Nov 28, 2026 5:14pm UTC

Request Review

@meta-cla

meta-cla Bot commented Nov 28, 2026

Copy link
Copy Markdown

Hi @wrvsrx!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@rootnode-bot

rootnode-bot Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 28, 2026, 5:41 PM UTC

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for the fix, this looks good. Sending only cancelable, non-composing multiline insertText commits through CONTROLLED_TEXT_INSERTION_COMMAND reuses the path Lexical already takes for the same commit in an empty paragraph, and single-line typing, '\n', '\n\n' and composition commits keep their existing branches. Everything below is optional and non-blocking.

  1. Let the test reach the native path (packages/lexical/src/__tests__/browser/LexicalMultilineInput.test.ts:39-52). A synthetic beforeinput has no default action, so on main the test fails because nothing inserts the text, not because Chromium splits the commit, and defaultPrevented pins the new routing rather than the bug. AGENTS.md's Testing Strategy puts behavior that depends on the real editing engine in browser or e2e tests: Playwright's keyboard.insertText sends a trusted, non-composing text commit that emulates IME input (the playground e2e specs already use it, for example Markdown.spec.mjs), and a browser-test command next to typeText in scripts/vitest/browserCommands.ts could wrap it.
  2. Assert structure and caret, not only text content. getTextContent() reads the same for a newline inside one TextNode, a LineBreakNode or a paragraph split. Asserting the paragraph's children and the selection (anchor at offset 8 of the text node) would pin the intended result.
  3. Newline shape. The commit lands as one TextNode containing \n, while a plain-text paste of the same text produces LineBreakNodes (insertRawText). That matches what the controlled path already writes in an empty paragraph, so it is fine as is; flagging it in case maintainers prefer the paste shape.
  4. Caret heuristic. The branch shares the trailing $maybeMoveSelectionPastTrailingAcceptanceBoundary(data) call (LexicalEvents.ts:1513), which reads the key of the last keydown. When no keydown precedes the commit (as with keyboard.insertText) and the last key was Enter, a commit that ends a text node moves the caret to the next line or block. Skipping that call for this branch, or asserting the caret, would avoid the surprise.
  5. Changeset wording (.changeset/preserve-multiline-ime-commits.md:5). The new branch is not Android-specific, and .changeset/README.md asks for a summary of the change from the user's side, for example [fix] Insert the whole text of a multiline input commit, such as an Android keyboard's clipboard entry, inside existing text.

Automated review (maintainer link)

Comment on lines +39 to +52
const event = new InputEvent('beforeinput', {
bubbles: true,
cancelable: true,
data,
inputType: 'insertText',
});
root.dispatchEvent(event);

// Allowing the default would let Chromium split this commit into multiple
// DOM edits, including edits outside the current text node.
expect(event.defaultPrevented).toBe(true);
expect(editor.read('force-commit', () => $getRoot().getTextContent())).toBe(
`X${data}Y`,
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional, non-blocking: a synthetic beforeinput has no default action, so on main this fails because nothing inserts the text, not because Chromium splits the commit, and defaultPrevented pins the new routing rather than the bug. Playwright's keyboard.insertText sends a trusted, non-composing text commit that emulates IME input (the playground e2e specs already use it); a browser-test command next to typeText in scripts/vitest/browserCommands.ts could wrap it, per AGENTS.md's guidance to test real-engine behavior in browser tests. Asserting the paragraph's children and the caret (anchor at offset 8) would also pin the result more tightly than getTextContent(), which reads the same for a \n inside one TextNode, a LineBreakNode or a paragraph split.

'lexical': patch
---

[fix] Preserve multiline text committed by an Android input method into existing text.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit, optional: the new branch is not Android-specific (any cancelable, non-composing multiline insertText takes it), and .changeset/README.md asks for a summary from the user's side. For example: [fix] Insert the whole text of a multiline input commit, such as an Android keyboard's clipboard entry, inside existing text.

This branch was successfully deployed

2 active deployments
Preview – lexical — eba95549 Deployed Nov 28, 2026 by vercel[bot]
Preview – lexical-playground — eba95549 Deployed Nov 28, 2026 by vercel[bot]
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.

1 participant