[lexical-link] Bug Fix: Preserve cursor position when merging adjacent identical links#8309
Merged
etrepum merged 2 commits intoApr 8, 2026
Conversation
…t identical links When two adjacent LinkNodes with identical attributes merge, the cursor jumps to the end of the merged link instead of staying at the merge boundary. This happens because the saved caret points to the gap between the two links, and after the merge removes one link, the gap resolves to the end of the remaining content. Fix by re-anchoring the caret to the first text child of the link being merged in before the merge occurs. That child survives the move and correctly represents the merge boundary position. Closes facebook#8305
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
etrepum
reviewed
Apr 8, 2026
Collaborator
There was a problem hiding this comment.
To ensure that this is a complete fix, it should test the behavior of all five equivalent points (they are all indistinguishable visually in the browser)
- A 'text' point at the last position of the first link's text
- A 'text' point at the first position of the second link's text
- An 'element' point at the last child position of the first link
- An 'element' point between the two links
- An 'element' point at the first child position of the second link
etrepum
approved these changes
Apr 8, 2026
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.
Description
When two adjacent LinkNodes with identical attributes merge (e.g., after deleting the plain text between them), the cursor jumps to the end of the merged link instead of staying at the deletion point.
The root cause is in
$linkNodeTransform: the saved caret points to the gap between the two links (SiblingCaret(linkA, 'next')), and after the merge removes one link, the gap resolves to the end of the remaining content.Fixed by re-anchoring the caret to the first text child of the link being merged in before the merge occurs. That child survives the move and correctly represents the merge boundary position.
Closes #8305
Test plan
Before
Cursor jumps to the end of the merged link after deleting text between two identical links.
After
Cursor stays at the merge boundary (the point where the two links joined).
Added two unit tests covering both merge directions (nextSibling merge and prevSibling merge).
All existing tests pass (2741 passed, 0 failed).