Skip to content

[lexical-code-prism][lexical-code-shiki] Bug Fix: don't drag the caret into a code block while highlighting - #8972

Closed
LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:fix/6305-markdown-caret-code-block
Closed

LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:fix/6305-markdown-caret-code-block

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

$updateAndRetainSelection restores the selection by walking the highlighted code node's children. That is only meaningful when the selection was inside that code node, but it ran for every RangeSelection, so a selection anywhere else in the document was moved into the code block.

$convertFromMarkdownString leaves the caret at the start of the document (root.selectStart()). The highlight transform then immediately pulled it into the imported code block — and with several code blocks, into the last one, scrolling long documents down to it.

The fix skips the retain/restore when the anchor is not inside the code node being highlighted; the highlighting itself still runs. Both the Prism and Shiki highlighters carry the same copy of this function, so both are fixed.

Tests: new unit tests in each package import the exact markdown from the issue and assert the caret stays at the start of the first paragraph, plus a control asserting a caret genuinely inside the code block is still retained across tokenization. Reverting the two product files fails 4 of the 5.

Fixes #6305

…t into a code block while highlighting

`$updateAndRetainSelection` restores the selection by walking the code node's
children, which is only meaningful when the selection was inside that code
node to begin with. It ran unconditionally for any RangeSelection, so a
selection living elsewhere in the document got moved into the code block.

`$convertFromMarkdownString` leaves the caret at the start of the document,
and the highlight transform then immediately pulled it into the last imported
code block — scrolling long documents down to it.

Skip the retain/restore when the anchor is not inside the code node being
highlighted.
@vercel

vercel Bot commented Aug 8, 2026

Copy link
Copy Markdown

@LeSingh1 is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@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 Aug 8, 2026
@LeSingh1

Copy link
Copy Markdown
Contributor Author

Consolidated into #9056 with the other PRs that share this defect, per @etrepum's note on #9027 and @mayrang's on #9035. Same fix and same tests, one review.

@LeSingh1 LeSingh1 closed this Aug 10, 2026
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.

Projects

None yet

1 participant