Skip to content

[lexical-mark] Bug Fix: Commenting on part of a link no longer marks the whole link - #9427

Open
Vedthakar wants to merge 2 commits into
meta:mainfrom
Vedthakar:fix/5606-mark-partial-link
Open

Vedthakar wants to merge 2 commits into
meta:mainfrom
Vedthakar:fix/5606-mark-partial-link

Conversation

@Vedthakar

Copy link
Copy Markdown
Contributor

Description

This solves issue #5606: adding a comment over part of a link marks the whole link.

Current behavior. $wrapSelectionInMarkNode (which the playground's CommentPlugin uses) has a case that adds any inline element node in the selection to the mark whole. When a selection starts before a link and ends inside it, extract() returns the LinkNode itself as well as its selected text, so the whole link goes into the mark. With before link after, selecting ore li gives bef<mark>ore <a>link</a></mark> after instead of marking only li. A selection that starts inside the link never hits this case, so only selections entering a link from before it were wrong.

What this changes. An inline ElementNode is wrapped whole only when it is fully selected. extract() splits text at the selection boundaries, so that's the case when its first and last descendants are both in the extracted nodes (or it has none). A partly selected inline element is skipped, the same way a mark is, and its selected children are wrapped by the text case, inside it: bef<mark>ore </mark><a><mark>li</mark>nk</a> after.

  • A LinkNode stays one node, and its unselected part stays unmarked.

  • A fully selected link, or an empty inline element, is still wrapped whole as before.

  • Decorators and block elements are unchanged.

  • Autolinks. An AutoLinkNode with a mark inside it is still unlinked by the auto-link extension (handleLinkEdit). This already happens on main for a selection that starts inside an autolink, and it affects only autolinks. I checked with AutoLinkExtension on before http://a.dev after:

    • Selecting ore htt gives bef,mark[ore ],mark[htt],p://a.dev after with this change. On main it gave bef,mark[ore ,autolink[http://a.dev]], after, with the whole autolink marked.
    • Selecting tp: gives before ht,mark[tp:],//a.dev after both with this change and on main.

    Keeping autolinks intact under a mark is Bug: Overlapping Mark Nodes and Link Nodes #3266-type work and is left out of this PR.

This is also point 2 of #3266 ("wrapping half of the linked word with a mark node will wrap the entire link"). Points 1 and 3 there (link styling under a mark, and creating a link over a mark) are separate, so this PR doesn't close #3266.

Closes #5606

Test plan

New tests in packages/lexical-mark/__tests__/unit/LexicalMarkNode.test.ts on before link after, rendering the paragraph as eg. bef,mark[ore ],link[mark[li],nk], after:

  • "marks only the selected start of the link": the issue's case (from before the link into it).
  • "marks a partly selected link with a backward selection": the same range, backwards.
  • "marks the selected children of a link with several text nodes": the link is li + bold nk, and the selection ends inside nk.
  • "marks the selected parts of two links": from inside one link to inside another, across plain text.
  • Controls that already passed: the selected end and middle of the link, a fully selected link (still wrapped whole), and a link whose text is exactly selected.

The existing $wrapSelectionInMarkNode tests (inline element and decorator nodes, block elements) pass unchanged.

Before

On main with the tests applied and the source change reverted:

$ pnpm exec vitest run packages/lexical-mark/__tests__
  × marks only the selected start of the link
    Expected: "bef,mark[ore ],link[mark[li],nk], after"
    Received: "bef,mark[ore ,link[li,nk]], after"
  × marks a partly selected link with a backward selection
  × marks the selected children of a link with several text nodes
  × marks the selected parts of two links
    Expected: "link[o,mark[ne]],mark[ and ],link[mark[tw],o]"
    Received: "link[o,mark[ne]],mark[ and ,link[tw,o]]"
  Tests  4 failed | 14 passed (18)

After

On current main:

  • LexicalMarkNode.test.ts: 18 passed (the 4 new behaviour tests fail on main).
  • pnpm run test-unit: all 9,580 tests pass
  • tsc, pnpm run flow (0 errors), and eslint/prettier on the changed files: clean

Author: Ved Thakar (@Vedthakar)

🤖 Generated with Claude Code

@vercel

vercel Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
lexical Ready Ready Preview Nov 16, 2026 11:57pm UTC
lexical-playground Ready Ready Preview Nov 16, 2026 11:57pm UTC

Request Review

@pkg-pr-new

pkg-pr-new Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
lexical

npm i https://pkg.pr.new/lexical@9427

@lexical/a11y

npm i https://pkg.pr.new/@lexical/a11y@9427

@lexical/clipboard

npm i https://pkg.pr.new/@lexical/clipboard@9427

@lexical/code

npm i https://pkg.pr.new/@lexical/code@9427

@lexical/code-core

npm i https://pkg.pr.new/@lexical/code-core@9427

@lexical/code-prism

npm i https://pkg.pr.new/@lexical/code-prism@9427

@lexical/code-shiki

npm i https://pkg.pr.new/@lexical/code-shiki@9427

@lexical/compiler

npm i https://pkg.pr.new/@lexical/compiler@9427

@lexical/devtools-core

npm i https://pkg.pr.new/@lexical/devtools-core@9427

@lexical/dragon

npm i https://pkg.pr.new/@lexical/dragon@9427

@lexical/eslint-plugin

npm i https://pkg.pr.new/@lexical/eslint-plugin@9427

@lexical/extension

npm i https://pkg.pr.new/@lexical/extension@9427

@lexical/fast-check

npm i https://pkg.pr.new/@lexical/fast-check@9427

@lexical/file

npm i https://pkg.pr.new/@lexical/file@9427

@lexical/hashtag

npm i https://pkg.pr.new/@lexical/hashtag@9427

@lexical/headless

npm i https://pkg.pr.new/@lexical/headless@9427

@lexical/history

npm i https://pkg.pr.new/@lexical/history@9427

@lexical/html

npm i https://pkg.pr.new/@lexical/html@9427

@lexical/internal

npm i https://pkg.pr.new/@lexical/internal@9427

@lexical/link

npm i https://pkg.pr.new/@lexical/link@9427

@lexical/list

npm i https://pkg.pr.new/@lexical/list@9427

@lexical/mark

npm i https://pkg.pr.new/@lexical/mark@9427

@lexical/markdown

npm i https://pkg.pr.new/@lexical/markdown@9427

@lexical/mdast

npm i https://pkg.pr.new/@lexical/mdast@9427

@lexical/offset

npm i https://pkg.pr.new/@lexical/offset@9427

@lexical/overflow

npm i https://pkg.pr.new/@lexical/overflow@9427

@lexical/plain-text

npm i https://pkg.pr.new/@lexical/plain-text@9427

@lexical/react

npm i https://pkg.pr.new/@lexical/react@9427

@lexical/rich-text

npm i https://pkg.pr.new/@lexical/rich-text@9427

@lexical/selection

npm i https://pkg.pr.new/@lexical/selection@9427

@lexical/table

npm i https://pkg.pr.new/@lexical/table@9427

@lexical/tailwind

npm i https://pkg.pr.new/@lexical/tailwind@9427

@lexical/text

npm i https://pkg.pr.new/@lexical/text@9427

@lexical/utils

npm i https://pkg.pr.new/@lexical/utils@9427

@lexical/y

npm i https://pkg.pr.new/@lexical/y@9427

@lexical/yjs

npm i https://pkg.pr.new/@lexical/yjs@9427

commit: af60d58

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This looks good to me. Thanks for the careful fix and the clear before/after tests. $wrapSelectionInMarkNode now skips a partly selected inline element and lets the text case wrap its selected children, so a comment that enters a link from before behaves like one that starts inside it. The four new tests fail without the change and pass with it.

A few optional, non-blocking suggestions:

  1. Playground link colour under a comment (packages/lexical-playground/src/themes/PlaygroundEditorTheme.css:639-643). Now that the mark sits inside the <a>, the marked part of a link (for example Lexi in Lexical website) renders black. .PlaygroundEditorTheme__mark sets no color, so the browser's default mark colour applies. main already does this for a comment that starts inside a link (point 1 of #3266), so leaving it for that follow-up is fine; adding color: inherit to the rule would fix it in the playground if you want it here.
  2. Pin the AutoLink trade-off with a test. The description explains that the auto-link extension unlinks a partly marked AutoLinkNode until the mark is removed. A small unit test with AutoLinkExtension and MarkExtension asserting bef,mark[ore ],mark[htt],p://a.dev after, and the autolink back after $unwrapMarkNode, would keep that behaviour visible for the #3266 work.
  3. Test plan output. AGENTS.md (Commits and Pull Requests) asks for the real command output under ### After. Your Before section pastes it, but After describes the results ("18 passed", "all 9,580 tests pass"); pasting the output there would match the template.

Vedthakar and others added 2 commits November 16, 2026 18:54
…the whole link

$wrapSelectionInMarkNode wrapped any inline element in the selection
whole, so commenting on part of a link marked the entire link. Wrap an
inline element whole only when both its first and last descendants are
selected; otherwise wrap its selected children inside it.

Signed-off-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Vedthakar

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I rebased onto main and added a changeset; the code is unchanged. The link colour under a mark is already the case on main for a comment that starts inside a link, so I left it for the #3266 follow-up.

— Ved Thakar

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks, this looks good. Marking a partly selected inline element's selected children, instead of wrapping the element itself, fixes #5606 in the right place. The four new tests fail on main and pass with the change. Everything below is optional and non-blocking.

  1. Add a Verify: paragraph to the changeset (.changeset/mark-partial-link.md:5). The fix changes observable output, and CONTRIBUTING.md ("Changesets", the Verify: bullet) and .changeset/README.md ("Behavior changes") ask for a Verify: paragraph in that case. For example:

    Verify: marks created over a selection that starts before a link and ends inside it. The part inside the link is now a separate mark nested in the link, so a custom createNode runs once more, and an autolink covered this way is unlinked until the mark is removed.

  2. Autolinks (packages/lexical-mark/src/index.ts:105-112). As the description notes, an AutoLinkNode entered from before is now skipped here, and AutoLinkExtension's handleLinkEdit unlinks it while the mark exists. It relinks once the mark is unwrapped. That matches main for selections that start inside an autolink. If maintainers would rather keep autolinks intact until #3266, wrapping a partly selected AutoLinkNode whole would keep the old behaviour for that one node type.
  3. Caret after a backward wrap (packages/lexical-mark/src/index.ts:156-161). With isBackward, the helper calls selectStart() on the last mark it created. For a backward selection from inside a link to before it, that is now the mark inside the link, so the caret lands at the start of li rather than at the start of the commented range. Remembering the first created mark for the backward case would fix it.
  4. Extra test cases. Two would be worth pinning: a selection that ends at offset 0 of the link's text or at the element point (link, 0), where nothing of the link is selected and main also wrapped the whole link; and the autolink outcome above.
  5. Playground styling, possibly as a follow-up. A mark inside a link takes the browser's default mark text colour, so the commented part of a link turns black in the playground. main already does this for selections that start inside a link. Adding .PlaygroundEditorTheme__link .PlaygroundEditorTheme__mark { color: inherit; } to packages/lexical-playground/src/themes/PlaygroundEditorTheme.css would fix it. This is the first point of #3266, which the description scopes out.

A small process note: AGENTS.md ("Commits and Pull Requests") asks the test plan's ### After section to carry the pasted output of the command, not a summary of it.

Automated review (maintainer link)

'@lexical/mark': patch
---

[fix] `$wrapSelectionInMarkNode` marks only the selected part of a link when the selection starts before the link and ends inside it, instead of the whole link

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional: this fix changes observable output, so CONTRIBUTING.md ("Changesets", the Verify: bullet) and .changeset/README.md ("Behavior changes") ask for a Verify: paragraph, for example:

Verify: marks created over a selection that starts before a link and ends inside it. The part inside the link is now a separate mark nested in the link, so a custom `createNode` runs once more, and an autolink covered this way is unlinked until the mark is removed.

Comment on lines +105 to +112
} else if (
$isElementNode(node) &&
node.isInline() &&
!$isFullySelected(node, selectedKeys)
) {
// Case 3: a partly selected inline element node (eg. a link) is not
// wrapped whole. Its selected children are wrapped instead, inside it.
continue;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional, non-blocking: an AutoLinkNode that a selection enters from before is now skipped here, and AutoLinkExtension's handleLinkEdit then unlinks it while the mark exists, relinking it once the mark is removed. main already does this for selections that start inside an autolink, so this is consistent. If maintainers prefer to keep autolinks intact until #3266, treating a partly selected AutoLinkNode as case 4 (wrap it whole) would preserve the old behaviour for that one node type.

This branch was successfully deployed

2 active deployments
Preview – lexical — af60d582 Deployed Nov 16, 2026 by vercel[bot]
Preview – lexical-playground — af60d582 Deployed Nov 16, 2026 by vercel[bot]
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

Development

Successfully merging this pull request may close these issues.

Bug: Incorrect range when adding comment over link Bug: Overlapping Mark Nodes and Link Nodes

1 participant