[lexical-code-prism][lexical-code-shiki] Bug Fix: don't restore a negative selection offset when a code line starts with a line break - #8947
Closed
LeSingh1 wants to merge 1 commit into
Conversation
…ative selection offset when a code line starts with a line break $updateAndRetainSelection walks the children produced by the highlight transform looking for the pre-update text offset. It subtracted a LineBreakNode's size even when the remaining offset was already 0, driving the offset negative; the next text node then matched `size >= offset` and was selected at that out-of-range position. Walk the children explicitly instead: stop on a line break when the remaining offset is 0 and use an element point on the code node, since an empty line has no text node to anchor to, and clamp to the end of the code node if the offset outruns the content. Fixes facebook#8943
LeSingh1
requested review from
acywatson,
etrepum,
fantactuka,
ivailop7,
potatowagon and
zurfyx
as code owners
August 7, 2026 09:24
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Collaborator
|
If these functions are the same we should move it to the code-core parent package rather than maintain two identical copies. |
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.
$updateAndRetainSelectionmeasures the caret's text offset against the code node's children before the highlight transform replaces them, then walks the new children to find that offset again. ALineBreakNodehad its size subtracted even when the remaining offset was already 0, so the offset went negative; the following text node then matchedsize >= offsetand was selected at an out-of-range position. A code block starting with a line break always produces that layout, since$plainifyCodeContent('\nfoo')yields[LineBreakNode, CodeHighlightNode('foo')].The result is a
RangeSelectionpointing outside its node — typed characters land on the wrong line, and the invalid point can throw during reconciliation.This walks the children explicitly instead: stop on a line break when the remaining offset is 0 and use an element point on the code node (an empty line has no text node to anchor to), and clamp to the end of the code node if the offset outruns the content. The same function is duplicated in
@lexical/code-shiki, so both copies are fixed, each with a regression test.Fixes #8943