Skip to content

[lexical-link] Bug Fix: $toggleLink applies the title attribute for a NodeSelection - #8993

Merged
etrepum merged 1 commit into
meta:mainfrom
LeSingh1:fix/toggle-link-title
Aug 9, 2026
Merged

etrepum merged 1 commit into
meta:mainfrom
LeSingh1:fix/toggle-link-title

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Description

$toggleLink documents its second argument as { target, rel, title } and
destructures all three up front:

const {target, title} = attributes;
const rel = attributes.rel === undefined ? 'noreferrer' : attributes.rel;

The RangeSelection path honours all three — it calls setTitle(title) on an
existing link and passes {rel, target, title} to $createLinkNode. The
NodeSelection path handles only two of them:

if (existingLink) {
  existingLink.setURL(url);
  if (target !== undefined) { existingLink.setTarget(target); }
  if (rel !== undefined) { existingLink.setRel(rel); }
  // title is never applied
} else {
  const linkNode = $createLinkNode(url, {rel, target});   // title omitted
  ...
}

So title was silently dropped whenever the selection was a NodeSelection —
which is what you have after clicking a decorator node such as an image, a
video embed or a horizontal rule, the exact case where a link tooltip is most
useful. The same call with a text (range) selection produced title="…"; with
a node selection it produced no title at all.

Apply title in both branches, using the same !== undefined guard the
sibling target/rel lines use, so omitting it still leaves an existing
title untouched.

Test plan

Three new cases in
packages/lexical-link/src/__tests__/unit/LexicalLinkNode.test.ts: creating a
link, updating an existing one, and confirming that omitting title does not
clear a title already on the node. Each also asserts url/rel/target, so
the two attributes that already worked stay pinned.

Before

$ npx vitest run packages/lexical-link/src/__tests__/unit/LexicalLinkNode.test.ts

       × applies the title when creating a link 7ms
       × applies the title when updating an existing link 2ms
       × leaves an existing title alone when none is supplied 2ms

⎯⎯⎯⎯⎯⎯⎯ Failed Tests 3 ⎯⎯⎯⎯⎯⎯⎯
AssertionError: expected null to be 'Lexical' // Object.is equality
AssertionError: expected null to be 'Second' // Object.is equality
AssertionError: expected null to be 'Kept' // Object.is equality

      Tests  3 failed | 71 passed (74)

After

$ npx vitest run packages/lexical-link packages/lexical-playground

 Test Files  23 passed (23)
      Tests  426 passed (426)

… NodeSelection

## Description

`$toggleLink` documents its second argument as `{ target, rel, title }` and
destructures all three up front:

```ts
const {target, title} = attributes;
const rel = attributes.rel === undefined ? 'noreferrer' : attributes.rel;
```

The `RangeSelection` path honours all three — it calls `setTitle(title)` on an
existing link and passes `{rel, target, title}` to `$createLinkNode`. The
`NodeSelection` path handles only two of them:

```ts
if (existingLink) {
  existingLink.setURL(url);
  if (target !== undefined) { existingLink.setTarget(target); }
  if (rel !== undefined) { existingLink.setRel(rel); }
  // title is never applied
} else {
  const linkNode = $createLinkNode(url, {rel, target});   // title omitted
  ...
}
```

So `title` was silently dropped whenever the selection was a `NodeSelection` —
which is what you have after clicking a decorator node such as an image, a
video embed or a horizontal rule, the exact case where a link tooltip is most
useful. The same call with a text (range) selection produced `title="…"`; with
a node selection it produced no `title` at all.

Apply `title` in both branches, using the same `!== undefined` guard the
sibling `target`/`rel` lines use, so omitting it still leaves an existing
title untouched.

## Test plan

Three new cases in
`packages/lexical-link/src/__tests__/unit/LexicalLinkNode.test.ts`: creating a
link, updating an existing one, and confirming that omitting `title` does not
clear a title already on the node. Each also asserts `url`/`rel`/`target`, so
the two attributes that already worked stay pinned.

### Before

```
$ npx vitest run packages/lexical-link/src/__tests__/unit/LexicalLinkNode.test.ts

       × applies the title when creating a link 7ms
       × applies the title when updating an existing link 2ms
       × leaves an existing title alone when none is supplied 2ms

⎯⎯⎯⎯⎯⎯⎯ Failed Tests 3 ⎯⎯⎯⎯⎯⎯⎯
AssertionError: expected null to be 'Lexical' // Object.is equality
AssertionError: expected null to be 'Second' // Object.is equality
AssertionError: expected null to be 'Kept' // Object.is equality

      Tests  3 failed | 71 passed (74)
```

### After

```
$ npx vitest run packages/lexical-link packages/lexical-playground

 Test Files  23 passed (23)
      Tests  426 passed (426)
```
@vercel

vercel Bot commented Aug 9, 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 9, 2026
@etrepum
etrepum added this pull request to the merge queue Aug 9, 2026
Merged via the queue into meta:main with commit 0ed182c Aug 9, 2026
44 checks passed
@etrepum etrepum mentioned this pull request Aug 31, 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

Development

Successfully merging this pull request may close these issues.

2 participants