Skip to content

[lexical-link] Bug Fix: the autolink edit transform refreshes the title attribute - #8998

Closed
LeSingh1 wants to merge 2 commits into
meta:mainfrom
LeSingh1:fix/autolink-edit-title
Closed

LeSingh1 wants to merge 2 commits into
meta:mainfrom
LeSingh1:fix/autolink-edit-title

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Description

handleLinkEdit is the transform that keeps an existing AutoLinkNode in sync
with its matcher as the user edits the link text. When the edited text resolves
to a new match it re-applies the url and then walks match.attributes:

if (match.attributes) {
  const rel = linkNode.getRel();
  if (rel !== match.attributes.rel) { ... }

  const target = linkNode.getTarget();
  if (target !== match.attributes.target) { ... }
}

LinkAttributes has exactly three members — rel, target and title — and
the creation path for the very same LinkMatcherResult applies all three:

const linkNode = $createAutoLinkNode(match.url, match.attributes);

$createAutoLinkNode -> AutoLinkNode -> LinkNode's constructor, which
destructures {target, rel, title}. AutoLinkNode.insertNewAfter likewise
carries all three. Only the edit path stopped at two.

So editing an autolink's text updates its url, rel and target from the
new match while leaving the title from the old match on the node forever —
the rendered <a> keeps a tooltip describing a URL it no longer points at. The
onChange handler is not notified either, so integrations that mirror link
attributes elsewhere never see the change.

Apply title with the same !== guard, || null normalization and onChange
notification the two lines above it use.

Test plan

Three new cases in
packages/lexical-link/src/__tests__/unit/LexicalAutoLinkExtension.test.ts,
driven by a matcher that derives all three attributes from the matched host.
The first two are controls that pass before and after — creation applying all
three, and the edit path refreshing rel/target — so the third case is held
to behaviour the same file already guarantees.

Before

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

       ✓ the creation path applies every attribute 1ms
       ✓ editing the link text refreshes rel and target 0ms
       × editing the link text refreshes the title 6ms

⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯
AssertionError: expected 'title-a.com' to be 'title-b.com' // Object.is equality
Expected: "title-b.com"
Received: "title-a.com"

      Tests  1 failed | 8 passed (9)

After

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

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

…le attribute

## Description

`handleLinkEdit` is the transform that keeps an existing `AutoLinkNode` in sync
with its matcher as the user edits the link text. When the edited text resolves
to a new match it re-applies the url and then walks `match.attributes`:

```ts
if (match.attributes) {
  const rel = linkNode.getRel();
  if (rel !== match.attributes.rel) { ... }

  const target = linkNode.getTarget();
  if (target !== match.attributes.target) { ... }
}
```

`LinkAttributes` has exactly three members — `rel`, `target` and `title` — and
the *creation* path for the very same `LinkMatcherResult` applies all three:

```ts
const linkNode = $createAutoLinkNode(match.url, match.attributes);
```

`$createAutoLinkNode` -> `AutoLinkNode` -> `LinkNode`'s constructor, which
destructures `{target, rel, title}`. `AutoLinkNode.insertNewAfter` likewise
carries all three. Only the edit path stopped at two.

So editing an autolink's text updates its `url`, `rel` and `target` from the
new match while leaving the `title` from the *old* match on the node forever —
the rendered `<a>` keeps a tooltip describing a URL it no longer points at. The
`onChange` handler is not notified either, so integrations that mirror link
attributes elsewhere never see the change.

Apply `title` with the same `!==` guard, `|| null` normalization and `onChange`
notification the two lines above it use.

## Test plan

Three new cases in
`packages/lexical-link/src/__tests__/unit/LexicalAutoLinkExtension.test.ts`,
driven by a matcher that derives all three attributes from the matched host.
The first two are controls that pass before and after — creation applying all
three, and the edit path refreshing `rel`/`target` — so the third case is held
to behaviour the same file already guarantees.

### Before

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

       ✓ the creation path applies every attribute 1ms
       ✓ editing the link text refreshes rel and target 0ms
       × editing the link text refreshes the title 6ms

⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯
AssertionError: expected 'title-a.com' to be 'title-b.com' // Object.is equality
Expected: "title-b.com"
Received: "title-a.com"

      Tests  1 failed | 8 passed (9)
```

### 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
const title = linkNode.getTitle();
if (title !== match.attributes.title) {
linkNode.setTitle(match.attributes.title || null);
onChange(match.attributes.title || null, title);

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.

This looks wrong (and some of the above code is too), I don't think onChange should be called for anything but url according to the documentation.

…rting a url change

## Description

`handleLinkEdit` re-runs the matchers after the link text is edited and
refreshes the node from the new match. Two problems.

`title` was never refreshed. It is the third member of `LinkAttributes` and the
creation path applies it via `$createAutoLinkNode(match.url, match.attributes)`,
so after an edit the node kept the title derived from the previous url.

The `rel` and `target` refreshes reported themselves through `onChange`.
`ChangeHandler` is documented as receiving the new url and the previous url, so
an attribute refresh is not something it can describe: `onChange(match.attributes.rel, rel)`
tells the application that the link's url is now a rel value. A handler that
records urls, or navigates using them, receives values that were never urls.

The attribute refreshes now update the node without calling `onChange`, and
`title` is refreshed alongside `rel` and `target`. `onChange` continues to fire
for link creation, removal, and an actual url change.

Refs meta#8998

## Test plan

`LexicalAutoLinkExtension.test.ts` adds a matcher deriving `rel`, `target` and
`title` from the matched host, then edits the link text and asserts all three
follow the new url, plus a test pinning that the only pair reported to
`ChangeHandler` is the url change.

### Before

```
$ npx vitest run packages/lexical-link
 × editing the link text refreshes the title
   expected 'title-a.com' to be 'title-b.com'
 × an attribute refresh is not reported as a url change
   expected [ [ 'rel-b.com', 'rel-a.com' ], … ] to deeply equal [ [ 'https://b.com', 'https://a.com' ] ]
```

### After

```
$ npx vitest run packages/lexical-link
 Test Files  6 passed (6)
      Tests  156 passed (156)
```
@LeSingh1

LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

You are right, and the problem was larger than what I had changed — I was adding a third instance of it.

ChangeHandler is documented as (url, prevUrl), but handleLinkEdit reported the attribute refreshes through it:

onChange(match.attributes.rel || null, rel);
onChange(match.attributes.target || null, target);

so a handler that records urls, or navigates using them, receives rel-b.com and target-b.com as if they were the link's url. My original patch added the same call for title.

Reworked: the attribute refreshes now update the node without calling onChange, and title is refreshed alongside rel and target so it stops going stale after an edit. onChange still fires for creation, removal, and an actual url change.

Added a test pinning that contract — after editing the link text, the only pair reported to ChangeHandler is [https://b.com, https://a.com]. It fails on main with the rel/target pairs included.

packages/lexical-link: 156 passed.

Let me know if you would rather the onChange correction land as its own PR separate from the title refresh; happy to split it.

@LeSingh1

Copy link
Copy Markdown
Contributor Author

Consolidated into #9060 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

Development

Successfully merging this pull request may close these issues.

2 participants