Repository navigation
[lexical] Bug Fix: insertText no longer throws when the caret is inside a segmented TextNode - #9104
Merged
etrepum merged 2 commits intoAug 29, 2026
Conversation
…de a segmented TextNode
Om-singhaI
requested review from
acywatson,
etrepum,
fantactuka,
ivailop7,
potatowagon and
zurfyx
as code owners
August 28, 2026 19:39
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
etrepum
approved these changes
Aug 28, 2026
Merged
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[lexical] Bug Fix: insertText no longer throws when the caret is inside a segmented TextNode
Description
Current behavior: typing one character with the caret anywhere strictly inside a segmented
TextNodethrowsExpected node N to have a parent.out ofRangeSelection.insertText. The character is dropped and the document is left untouched.Expected, and what this PR produces:
JohnXSmith, the node no longer segmented, caret collapsed at offset 5.That is also what the documentation promises.
packages/lexical-website/docs/concepts/nodes.mdxdescribes segmented mode as one where the node "is editable although node becomes non-segmented once its content is updated".This is reached by an ordinary keystroke, not only by API callers.
$shouldPreventDefaultAndInsertTextcounts$isTokenOrSegmented(anchorNode)among the reasons to preventDefault (packages/lexical/src/LexicalEvents.ts), so a caret on a segmented node routes the character throughCONTROLLED_TEXT_INSERTION_COMMAND, and the rich text handler for that command callsselection.insertText(...)on$getSelection()(packages/lexical-rich-text/src/index.ts). That is the call that throws, and because the selection there is the current one it is the exact shape the repro above uses. Segmented mode is the usual way to build mention and entity nodes, and the playground's ownMentionNodeopts into it (mentionNode.setMode('segmented')), so putting the caret inside a mention and typing raises an uncaught invariant and fires the editor'sonError. Applications with mentions, chips, or hashtags as entities are affected.Mechanism
Inside the
needsRedirectbranch ofinsertText, a segmented node with the caret inside it is converted to a plainTextNodeand theninsertTextrecurses. The branch without composition did:LexicalNode.replace()clones the active selection when it is entered and commits that clone with$setSelectionbefore it returns, so once it returns the editor's current selection is a different object fromthis.TextNode.select()then repositions whatever$getSelection()gives it, which is that clone, andthisis left pointing at the segmented nodereplace()has just detached. The recursivethis.insertText(text)runs against the stale anchor,anchorNode.getParentOrThrow()finds no parent, and the invariant fires.I confirmed the object identity directly rather than inferring it. Immediately after
replace()returns, with the caret at offset 4 ofJohnSmith: the segmented node is key 46 and detached,this.anchor.keyis still 46,$getSelection() === thisis false, and the current selection anchors on key 48, the replacement.The branch with composition is unaffected because
setMode('normal')keeps the same node key attached, which is why the asymmetry went unnoticed.The three existing tests in
describe('Segmented node composition (#5065)')all callinsertText(''), and the empty string is exactly the value that skipsif (text !== '') { this.insertText(text) }, so the recursion is never exercised.Regression window
The recursion arrived with #8870, which was titled a refactor. Before it, this branch assigned
firstNode = textNodeand carried on inline in the same call, so there was no stalethisto go wrong. I ran the same offset sweep against its parent commit and every offset inserted correctly there; the numbers are in the test plan below.The change
It moves
thisonto the replacement before recursing, and makes it current again when it was current on entry. That is the same sequenceremoveText()already performs a few lines further down the same file:One deliberate difference worth flagging for review:
replacement.select(offset, offset)is dropped rather than kept alongside the new positioning. Whenthisis the current selection, which is the case on the keystroke path above, the two are equivalent. When it is not, the old line reached pastthisand moved an unrelated active selection onto the replacement, or installed a new one when there was no selection at all; now an existing selection stays wherereplace()left it and a null selection stays null. The composition key transfer insideTextNode.select()cannot apply here either, since this branch only runs when$getCompositionKey()is null.Test plan
Before
Sweeping every caret offset of a nine character segmented node on
main, inserting one character at each:Every interior offset throws. The two boundaries are fine because they take the other paths out of
needsRedirect. The same sweep against the parent of #8870 returns no error at any offset and givesJohnXSmithat offset 4, so this is a behavior that regressed rather than one that was never there.The new test is
insertText without composition types into a segmented node, added next to the existing non composition test indescribe('Segmented node composition (#5065)'). Reverting onlypackages/lexical/src/LexicalSelection.tsto its previous state and keeping the test:The two
insertTextframes in that stack are the recursion described above.After
Restoring the change, the same file is green:
vitest --project unit --project scripts-unit --no-watchover the whole repository:The one skipped test predates this change.
tsc,eslint ./,prettier --list-different .and the flow check are all clean. The browser, integration and e2e suites were not run.