Skip to content

[lexical] Bug Fix: insertText respects canInsertTextBefore/After of inline parents - #9863

Merged
zurfyx merged 3 commits into
meta:mainfrom
james-atticus:fix-inline-insert-text-can-insert
Nov 11, 2026
Merged

zurfyx merged 3 commits into
meta:mainfrom
james-atticus:fix-inline-insert-text-can-insert

Conversation

@james-atticus

@james-atticus james-atticus commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Description

Before #8870, RangeSelection.insertText asked an inline parent's canInsertTextBefore() / canInsertTextAfter() whether text typed at its first or last character could stay inside it. #8870 replaced that with an unconditional parent.isInline() check, taken from the old commented-out WIP sketch. Since v0.49.0, text at either edge of an inline element always goes outside it, even when the element allows it. Any inline ElementNode that keeps the default (true) is affected. The core inline nodes (LinkNode, AutoLinkNode, MarkNode) all return false, which is likely why no existing test caught this.

The checks were also inverted. needsRedirect already moves the caret out of a parent that refuses text at that edge. So atStartOfInline / atEndOfInline in $insertText could only take effect when the parent allows the text.

Changes:

  • $insertText: remove atStartOfInline / atEndOfInline. For parents that refuse text they could never be reached, because needsRedirect returns first. For parents that allow text they were the bug.
  • $insertTextAtPoint (used for range replacement and format changes): only move the new text outside an inline parent that refuses it at that edge.

Overlaps with #9287, which rewrites the same redirect/boundary code in $insertText. Whichever lands second will need a rebase, and its expected trees for the inline-element cases should be rechecked.

Changeset

.changeset/insert-text-inline-can-insert.md (lexical patch, [fix], with Verify: so it is listed under Behavior changes).

Test plan

  • Unit tests (LexicalSelection.test.ts, insertText respects canInsertTextBefore/After of inline elements): an inline node that keeps all ElementNode defaults, with text "ab". They cover inserting with a collapsed selection at its start and end, replacing a range at either edge or all of its text, and inserting at the end with a pending bold format. They also include a LinkNode guard checking that replacement text still leaves an element that refuses it.
  • Browser test (browser/InlineElementEdgeTyping.test.ts): real typing through beforeinput. It covers toggling bold at the end of such an element and typing, and retyping a selection that ends at its edge.

Before

On main (8c369d793, with only LexicalSelection.ts reverted):

$ pnpm exec vitest run --project unit packages/lexical/src/__tests__/unit/LexicalSelection.test.ts -t "canInsertTextBefore/After of inline" --reporter=verbose
 × |unit| packages/lexical/src/__tests__/unit/LexicalSelection.test.ts > insertText respects canInsertTextBefore/After of inline elements > keeps text inside an inline element that allows it when collapsed at the end 13ms
 × |unit| packages/lexical/src/__tests__/unit/LexicalSelection.test.ts > insertText respects canInsertTextBefore/After of inline elements > keeps text inside an inline element that allows it when collapsed at the start 1ms
 × |unit| packages/lexical/src/__tests__/unit/LexicalSelection.test.ts > insertText respects canInsertTextBefore/After of inline elements > keeps text inside an inline element that allows it when replacing the last character 2ms
 × |unit| packages/lexical/src/__tests__/unit/LexicalSelection.test.ts > insertText respects canInsertTextBefore/After of inline elements > keeps text inside an inline element that allows it when replacing the first character 1ms
 × |unit| packages/lexical/src/__tests__/unit/LexicalSelection.test.ts > insertText respects canInsertTextBefore/After of inline elements > keeps text inside an inline element that allows it when replacing all of the text 1ms
 × |unit| packages/lexical/src/__tests__/unit/LexicalSelection.test.ts > insertText respects canInsertTextBefore/After of inline elements > keeps differently formatted text inside an inline element that allows it 1ms
 ✓ |unit| packages/lexical/src/__tests__/unit/LexicalSelection.test.ts > insertText respects canInsertTextBefore/After of inline elements > moves replacement text out of an inline element that refuses it 1ms
 Test Files  1 failed (1)
      Tests  6 failed | 1 passed | 103 skipped (110)
$ pnpm run test-browser packages/lexical/src/__tests__/browser/InlineElementEdgeTyping.test.ts
   × typing after toggling a format at the end of an inline element keeps the text inside it 1131ms
   × retyping a selection that ends at the edge of an inline element keeps the text inside it 1064ms
 Test Files  1 failed (1)
      Tests  2 failed (2)

 FAIL  |browser (chromium)| packages/lexical/src/__tests__/browser/InlineElementEdgeTyping.test.ts > typing after toggling a format at the end of an inline element keeps the text inside it
AssertionError: expected [ { text: 'ab', …(2) }, …(1) ] to deeply equal [ { bold: [ false, true ], …(2) } ]


- Expected
+ Received

  [
    {
      "bold": [
        false,
-       true,
      ],
-     "text": "abx",
+     "text": "ab",
      "type": "test_edge_typing_inline",
+   },
+   {
+     "text": "x",
+     "type": "text",
    },
  ]

After

$ pnpm exec vitest run --project unit packages/lexical/src/__tests__/unit/LexicalSelection.test.ts -t "canInsertTextBefore/After of inline" --reporter=verbose
 ✓ |unit| packages/lexical/src/__tests__/unit/LexicalSelection.test.ts > insertText respects canInsertTextBefore/After of inline elements > keeps text inside an inline element that allows it when collapsed at the end 6ms
 ✓ |unit| packages/lexical/src/__tests__/unit/LexicalSelection.test.ts > insertText respects canInsertTextBefore/After of inline elements > keeps text inside an inline element that allows it when collapsed at the start 1ms
 ✓ |unit| packages/lexical/src/__tests__/unit/LexicalSelection.test.ts > insertText respects canInsertTextBefore/After of inline elements > keeps text inside an inline element that allows it when replacing the last character 1ms
 ✓ |unit| packages/lexical/src/__tests__/unit/LexicalSelection.test.ts > insertText respects canInsertTextBefore/After of inline elements > keeps text inside an inline element that allows it when replacing the first character 0ms
 ✓ |unit| packages/lexical/src/__tests__/unit/LexicalSelection.test.ts > insertText respects canInsertTextBefore/After of inline elements > keeps text inside an inline element that allows it when replacing all of the text 1ms
 ✓ |unit| packages/lexical/src/__tests__/unit/LexicalSelection.test.ts > insertText respects canInsertTextBefore/After of inline elements > keeps differently formatted text inside an inline element that allows it 0ms
 ✓ |unit| packages/lexical/src/__tests__/unit/LexicalSelection.test.ts > insertText respects canInsertTextBefore/After of inline elements > moves replacement text out of an inline element that refuses it 1ms
 Test Files  1 passed (1)
      Tests  7 passed | 103 skipped (110)
$ pnpm run test-browser packages/lexical/src/__tests__/browser/InlineElementEdgeTyping.test.ts
 Test Files  1 passed (1)
      Tests  2 passed (2)

The full unit suite (pnpm exec vitest run --project unit) passes: 34,427 tests. Seven lexical-devtools files fail to load in my local environment both with and without this change. pnpm run tsc, ESLint and Prettier are clean.

🤖 Generated with Claude Code

james-atticus and others added 2 commits November 10, 2026 13:16
…nline parents

meta#8870 made insertText always move new text outside an inline parent when
the caret was at the parent's first or last character, without asking the
parent's canInsertTextBefore()/canInsertTextAfter(). Inline elements that
keep ElementNode's default (true) lost text typed at their edges, and
replacing all of their text left them empty.

needsRedirect already moves the caret out of a parent that refuses text,
so the inline checks in $insertText could only fire when the parent
allows it; drop them. $insertTextAtPoint, used for range replacement and
format changes, now only escapes an inline parent that refuses text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Nov 10, 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 11, 2026 3:46am UTC
lexical-playground Ready Ready Preview Nov 11, 2026 3:46am UTC

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 Nov 10, 2026
@pkg-pr-new

pkg-pr-new Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
lexical

npm i https://pkg.pr.new/lexical@9863

@lexical/a11y

npm i https://pkg.pr.new/@lexical/a11y@9863

@lexical/clipboard

npm i https://pkg.pr.new/@lexical/clipboard@9863

@lexical/code

npm i https://pkg.pr.new/@lexical/code@9863

@lexical/code-core

npm i https://pkg.pr.new/@lexical/code-core@9863

@lexical/code-prism

npm i https://pkg.pr.new/@lexical/code-prism@9863

@lexical/code-shiki

npm i https://pkg.pr.new/@lexical/code-shiki@9863

@lexical/compiler

npm i https://pkg.pr.new/@lexical/compiler@9863

@lexical/devtools-core

npm i https://pkg.pr.new/@lexical/devtools-core@9863

@lexical/dragon

npm i https://pkg.pr.new/@lexical/dragon@9863

@lexical/eslint-plugin

npm i https://pkg.pr.new/@lexical/eslint-plugin@9863

@lexical/extension

npm i https://pkg.pr.new/@lexical/extension@9863

@lexical/fast-check

npm i https://pkg.pr.new/@lexical/fast-check@9863

@lexical/file

npm i https://pkg.pr.new/@lexical/file@9863

@lexical/hashtag

npm i https://pkg.pr.new/@lexical/hashtag@9863

@lexical/headless

npm i https://pkg.pr.new/@lexical/headless@9863

@lexical/history

npm i https://pkg.pr.new/@lexical/history@9863

@lexical/html

npm i https://pkg.pr.new/@lexical/html@9863

@lexical/internal

npm i https://pkg.pr.new/@lexical/internal@9863

@lexical/link

npm i https://pkg.pr.new/@lexical/link@9863

@lexical/list

npm i https://pkg.pr.new/@lexical/list@9863

@lexical/mark

npm i https://pkg.pr.new/@lexical/mark@9863

@lexical/markdown

npm i https://pkg.pr.new/@lexical/markdown@9863

@lexical/mdast

npm i https://pkg.pr.new/@lexical/mdast@9863

@lexical/offset

npm i https://pkg.pr.new/@lexical/offset@9863

@lexical/overflow

npm i https://pkg.pr.new/@lexical/overflow@9863

@lexical/plain-text

npm i https://pkg.pr.new/@lexical/plain-text@9863

@lexical/react

npm i https://pkg.pr.new/@lexical/react@9863

@lexical/rich-text

npm i https://pkg.pr.new/@lexical/rich-text@9863

@lexical/selection

npm i https://pkg.pr.new/@lexical/selection@9863

@lexical/table

npm i https://pkg.pr.new/@lexical/table@9863

@lexical/tailwind

npm i https://pkg.pr.new/@lexical/tailwind@9863

@lexical/text

npm i https://pkg.pr.new/@lexical/text@9863

@lexical/utils

npm i https://pkg.pr.new/@lexical/utils@9863

@lexical/y

npm i https://pkg.pr.new/@lexical/y@9863

@lexical/yjs

npm i https://pkg.pr.new/@lexical/yjs@9863

commit: bd033eb

@james-atticus
james-atticus marked this pull request as ready for review November 10, 2026 02:34
@rootnode-bot

rootnode-bot Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 11, 2026, 4:12 AM UTC

@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Nov 11, 2026

@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, this looks good to me. The reasoning holds up: needsRedirect already moves text out of an inline parent that refuses it at that edge, so the removed atStartOfInline/atEndOfInline branches in $insertText only ever fired for parents that accept text, and the added canInsertTextBefore()/canInsertTextAfter() check in $insertTextAtPoint keeps range replacement moving text out of links and marks as before. The new tests fail without the source change (all but the LinkNode guard) and pass here. The change also lines up Lexical's own insertion path (typing after toggling a format, retyping a selection that ends at the edge, a composition that starts with a pending format) with native typing, which already keeps text inside such an element.

A few optional, non-blocking suggestions:

  1. Add a Verify: paragraph to the changeset (.changeset/insert-text-inline-can-insert.md). CONTRIBUTING.md and .changeset/README.md ("Behavior changes") ask a fix that changes editing behavior to carry Verify: so it is listed under Behavior changes, which matters here because apps on v0.49.0 to v0.53.0 will see text move back inside these elements. The detail in the summary line fits better there, for example:

    [fix] `RangeSelection.insertText` keeps text inside an inline element that accepts it at its edge
    
    Verify: typing, retyping a selection, or composing at the start or end of a custom inline `ElementNode` that keeps the default `canInsertTextBefore()`/`canInsertTextAfter()`: the text now stays inside the element, as it did before v0.49.0. Return `false` from those methods to keep it outside, as `LinkNode` and `MarkNode` do.
  2. Consider a browser test for the path people actually hit: the unit tests call insertText directly, while typing reaches it through beforeinput and composition events. Toggling bold and typing at the end of an inline element that keeps the defaults, under packages/lexical/src/__tests__/browser/, would cover it; per AGENTS.md, browser tests clean up with onTestFinished(() => editor.dispose()) rather than using.

  3. Test plan output: AGENTS.md (Commits and Pull Requests) asks for the command and its real failing and passing output under ### Before and ### After, rather than a summary of the counts.

  4. Heads-up on #9287: it rewrites the same redirect and boundary code in $insertText (text typed at a link's edge from an inline element inside the link). Whichever lands second will need a rebase, and its expected trees for the inline-element cases are worth rechecking against this change.

Automated review (maintainer link)

Add a browser test that types at the edge of an inline element keeping the
default canInsertTextBefore/After(), through real beforeinput events: after
toggling bold at its end, and retyping a selection that ends at its edge.

Move the detail of the changeset into a Verify: paragraph so it is listed
under Behavior changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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.

This looks good, thanks for the follow-up. The Verify: paragraph and the new browser test cover the earlier suggestions, and the new unit and browser tests fail without the fix and pass with it. Both suggestions below are optional and non-blocking.

Optional suggestions

  1. Narrow the Verify: line (.changeset/insert-text-inline-can-insert.md:13). Plain collapsed typing in the caret's own format goes through the browser's native input, so on v0.49.0 to v0.53.0 it already left the text inside the element. What changes is typing after toggling a format, retyping a selection, starting an IME composition, and calling insertText from code. .changeset/README.md ("Behavior changes") asks the Verify: paragraph to say what to test, so naming those paths points people who upgrade at the right checks. For example:

    Verify: typing after toggling a format, retyping a selection, starting an IME composition, or calling `insertText` at the start or end of a custom inline `ElementNode` that keeps the default `canInsertTextBefore()`/`canInsertTextAfter()`: the text now stays inside the element, as it did before v0.49.0. Return `false` from those methods to keep it outside, as `LinkNode` and `MarkNode` do.
  2. Cover composition start. The changeset names composing. At an edge that accepts text, the composition-start write (packages/lexical/src/LexicalEvents.ts:1858-1889: COMPOSITION_START_CHAR through insertText, or $prepareCompositionTarget on desktop WebKit) now splices into the element's text instead of creating a text node after it. Neither new test reaches that path. A unit test in LexicalSelection.test.ts that calls $setCompositionKey on the anchor before inserting at the edge would pin it (AGENTS.md, "Testing Strategy").

The description already covers the overlap with #9287: whichever lands second should recheck its expected trees for the inline-element cases.

Automated review (maintainer link)

});
});

describe('insertText respects canInsertTextBefore/After of inline elements', () => {

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: the changeset names composing, but no case here reaches the composition-start write (LexicalEvents.ts:1858-1889: COMPOSITION_START_CHAR through insertText, or $prepareCompositionTarget). At an accepting edge it now splices into the element's text instead of creating a node after it. A case that calls $setCompositionKey(text.getKey()) before inserting at the edge would pin that.

@zurfyx zurfyx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Verified this is a regression fix: on the commit before #8870, insertText at either edge of an inline ElementNode that keeps the default canInsertTextBefore()/canInsertTextAfter() keeps the text inside it (collapsed start/end, replacing the first/last/all text, bold at the end); on main it goes outside; with this PR it matches the pre-#8870 results again. The reasoning about needsRedirect holds: the removed atStartOfInline/atEndOfInline branches were only reachable for parents that accept text.

Merged with current main locally: full unit suite passes (916 files, 34,608 tests), tsc, Flow, ESLint and Prettier are clean, and CI on the head is green across the browser and e2e matrix.

@zurfyx
zurfyx added this pull request to the merge queue Nov 11, 2026
Merged via the queue into meta:main with commit 5790233 Nov 11, 2026
57 checks passed
rinarakaki added a commit to rinarakaki/lexical that referenced this pull request Nov 20, 2026
… parent when looking for an element that refuses text, and keep main's separate text node

## Description

Follow-up to the review, after merging `main`:

- `$getOutermostAncestorRefusingText` recorded an element as refusing before it checked whether the element was inline, so it could return a block reached through an inline parent. With a custom block that takes no text at its edges holding an inline element directly, typing at either edge of that inline element threw `Only element or decorator nodes can be inserted in to the root node`. It now counts a block only as the text's own parent, as its JSDoc says, and the text stays in the inline element, as on `main`.
- `main`'s meta#9863 keeps text typed at the edge of an inline element that takes it inside that element. The redirect now looks for a text node to reuse beside the anchor, as `main` does, and starts a new one beside the element that refuses text. Reusing the text beside that element instead joined `X` to the next text node where the core doesn't merge them, which failed `MarkStateExtension.test.ts`'s "the class that replaces TextNode is split like TextNode, like MarkNode" (`"Xdef"` where `main` gives `"X" "def"`).
- New unit tests: typing at the end of a link that ends a `MarkNode` writes after the `MarkNode` (on `main` it went before it), and typing at either edge of an inline element directly inside a block that refuses text keeps the text in the inline element. The test for an inline element away from the link's edge now expects meta#9863's result, the text inside that element, which the browser test already expected.
- The comment on the redirect names every case it handles.
- The changeset.

## Changeset

`.changeset/typing-at-link-edge-in-inline-element.md`

## Test plan

### Before

With `main`'s `LexicalSelection.ts` and `LexicalUtils.ts`, `pnpm exec vitest --project unit --no-watch packages/lexical-link/src/__tests__/unit/LexicalLinkNode.test.ts -t "meta#9280"`:

```
Expected: "paragraph["ab ", link["cd ", mark["xy"]], "X"]"
Received: "paragraph["ab X", link["cd ", mark["xy"]]]"
Expected: "paragraph["ab ", link["cd ", test_inline["xy"]], "X"]"
Received: "paragraph["ab ", link["cd ", test_inline["xyX"]]]"
Expected: "paragraph["ab ", link["cd ", test_inline["xy"]], "X ef"]"
Received: "paragraph["ab ", link["cd ", test_inline["xyX"]], " ef"]"
Expected: "paragraph["X", link[test_inline["xy"], " cd"], " ef"]"
Received: "paragraph[link[test_inline["Xxy"], " cd"], " ef"]"
Expected: "paragraph["ab ", mark["cd ", link["ef"]], "X"]"
Received: "paragraph["ab X", mark["cd ", link["ef"]]]"
Expected: "paragraph["ab ", link["cd", "X", "ef"], " gh"]"
Received: "paragraph["ab ", link["cd", "ef"], "X gh"]"
 Test Files  1 failed (1)
      Tests  6 failed | 6 passed | 82 skipped (94)
```

The same, `VITEST_BROWSER=chromium,firefox,webkit pnpm exec vitest --project browser --no-watch packages/lexical/src/__tests__/browser/Issue9280TypingAtLinkEdge.test.ts`: the character is still lost in all three engines.

```
 × |browser (chromium)| writes after the link, at the end of an inline element that ends it
 × |browser (chromium)| writes into the text after the link, at the end of an inline element that ends it
 × |browser (chromium)| writes before the link, at the start of an inline element that starts it
 × |browser (chromium)| writes after the link, at the end of a MarkNode that ends it
 (the same four in firefox and webkit)
 Test Files  3 failed (3)
      Tests  12 failed | 6 passed (18)
```

The PR's previous helper, with the new block tests:

```
 × writes in an inline element at the start of a block that takes no text there
 × writes in an inline element at the end of a block that takes no text there
Error: Only element or decorator nodes can be inserted in to the root node
      Tests  2 failed | 92 skipped (94)
```

### After

```
 ✓ |unit| writes after the link, at the end of a MarkNode that ends it
 ✓ |unit| writes after the link, at the end of an inline element that ends it
 ✓ |unit| writes into the text after the link, at the end of an inline element that ends it
 ✓ |unit| writes before the link, at the start of an inline element that starts it
 ✓ |unit| writes after the link, at the end of its own text
 ✓ |unit| writes before the link, at the start of its own text
 ✓ |unit| writes before the link, at the start of a MarkNode that starts it
 ✓ |unit| writes in an inline element away from the link's edge
 ✓ |unit| writes after the MarkNode, at the end of a link that ends it
 ✓ |unit| writes in an inline element at the start of a block that takes no text there
 ✓ |unit| writes in an inline element at the end of a block that takes no text there
 ✓ |unit| writes between two tokens inside a link, in the link
 Test Files  1 passed (1)
      Tests  12 passed | 82 skipped (94)
```

`VITEST_BROWSER=chromium,firefox,webkit pnpm exec vitest --project browser --no-watch packages/lexical/src/__tests__/browser/Issue9280TypingAtLinkEdge.test.ts packages/lexical/src/__tests__/browser/InlineElementEdgeTyping.test.ts` (meta#9863's browser test included):

```
 Test Files  6 passed (6)
      Tests  24 passed (24)
```

`pnpm exec vitest --project unit --no-watch packages/lexical*` (every package):

```
 Test Files  3 failed | 984 passed | 6 skipped (993)
      Tests  5 failed | 36420 passed | 423 expected fail | 174 skipped (37022)
```

All five failures are in `@lexical/y`. Two are "deterministic across processes" in `Migrate2to3`/`Migrate3to4`: tsx can't listen on its pipe (`listen EINVAL`), as this machine's `TMPDIR` makes the socket path too long, and they fail the same way with `main`'s source. The three in `LoadAcrossGap.test.ts` ran out of time in the full run, and the file passes when it runs alone, with this change and with `main`'s source. `pnpm run tsc` and `pnpm run check-changesets` pass, and Prettier and ESLint are clean on the changed files.
@potatowagon potatowagon mentioned this pull request Dec 2, 2026

This branch was successfully deployed

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

3 participants