Repository navigation
[lexical] Bug Fix: insertText respects canInsertTextBefore/After of inline parents - #9863
Conversation
…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>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
|
AI review status for this pull request.
|
There was a problem hiding this comment.
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:
-
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 carryVerify: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.
-
Consider a browser test for the path people actually hit: the unit tests call
insertTextdirectly, while typing reaches it throughbeforeinputand composition events. Toggling bold and typing at the end of an inline element that keeps the defaults, underpackages/lexical/src/__tests__/browser/, would cover it; per AGENTS.md, browser tests clean up withonTestFinished(() => editor.dispose())rather thanusing. -
Test plan output: AGENTS.md (Commits and Pull Requests) asks for the command and its real failing and passing output under
### Beforeand### After, rather than a summary of the counts. -
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.
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>
There was a problem hiding this comment.
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
-
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 callinginsertTextfrom code..changeset/README.md("Behavior changes") asks theVerify: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.
-
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_CHARthroughinsertText, or$prepareCompositionTargeton 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 inLexicalSelection.test.tsthat calls$setCompositionKeyon 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.
| }); | ||
| }); | ||
|
|
||
| describe('insertText respects canInsertTextBefore/After of inline elements', () => { |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
… 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.
Description
Before #8870,
RangeSelection.insertTextasked an inline parent'scanInsertTextBefore()/canInsertTextAfter()whether text typed at its first or last character could stay inside it. #8870 replaced that with an unconditionalparent.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 inlineElementNodethat keeps the default (true) is affected. The core inline nodes (LinkNode,AutoLinkNode,MarkNode) all returnfalse, which is likely why no existing test caught this.The checks were also inverted.
needsRedirectalready moves the caret out of a parent that refuses text at that edge. SoatStartOfInline/atEndOfInlinein$insertTextcould only take effect when the parent allows the text.Changes:
$insertText: removeatStartOfInline/atEndOfInline. For parents that refuse text they could never be reached, becauseneedsRedirectreturns 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(lexicalpatch,[fix], withVerify:so it is listed under Behavior changes).Test plan
LexicalSelection.test.ts,insertText respects canInsertTextBefore/After of inline elements): an inline node that keeps allElementNodedefaults, 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 aLinkNodeguard checking that replacement text still leaves an element that refuses it.browser/InlineElementEdgeTyping.test.ts): real typing throughbeforeinput. 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 onlyLexicalSelection.tsreverted):After
The full unit suite (
pnpm exec vitest run --project unit) passes: 34,427 tests. Sevenlexical-devtoolsfiles 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