Skip to content

[lexical-link] Bug Fix: Preserve cursor position when merging adjacent identical links#8309

Merged
etrepum merged 2 commits into
facebook:mainfrom
Sathvik-Chowdary-Veerapaneni:cursor-link-merge-8305
Apr 8, 2026
Merged

[lexical-link] Bug Fix: Preserve cursor position when merging adjacent identical links#8309
etrepum merged 2 commits into
facebook:mainfrom
Sathvik-Chowdary-Veerapaneni:cursor-link-merge-8305

Conversation

@Sathvik-Chowdary-Veerapaneni

Copy link
Copy Markdown
Contributor

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).

…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
@vercel

vercel Bot commented Apr 8, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview, Comment Apr 8, 2026 6:52am
lexical-playground Ready Ready Preview, Comment Apr 8, 2026 6:52am

Request Review

@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 Apr 8, 2026
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Apr 8, 2026

@etrepum etrepum left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 etrepum added this pull request to the merge queue Apr 8, 2026
Merged via the queue into facebook:main with commit b9c93a1 Apr 8, 2026
37 checks passed
@etrepum etrepum mentioned this pull request Apr 9, 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. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: The cursor moves to the end of the LinkNode when merging identical links

2 participants