Repository navigation
Conversation
…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)
```
|
@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. |
| const title = linkNode.getTitle(); | ||
| if (title !== match.attributes.title) { | ||
| linkNode.setTitle(match.attributes.title || null); | ||
| onChange(match.attributes.title || null, title); |
There was a problem hiding this comment.
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) ```
|
You are right, and the problem was larger than what I had changed — I was adding a third instance of it.
onChange(match.attributes.rel || null, rel);
onChange(match.attributes.target || null, target);so a handler that records urls, or navigates using them, receives Reworked: the attribute refreshes now update the node without calling Added a test pinning that contract — after editing the link text, the only pair reported to
Let me know if you would rather the |
Description
handleLinkEditis the transform that keeps an existingAutoLinkNodein syncwith 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:LinkAttributeshas exactly three members —rel,targetandtitle— andthe creation path for the very same
LinkMatcherResultapplies all three:$createAutoLinkNode->AutoLinkNode->LinkNode's constructor, whichdestructures
{target, rel, title}.AutoLinkNode.insertNewAfterlikewisecarries all three. Only the edit path stopped at two.
So editing an autolink's text updates its
url,relandtargetfrom thenew match while leaving the
titlefrom the old match on the node forever —the rendered
<a>keeps a tooltip describing a URL it no longer points at. TheonChangehandler is not notified either, so integrations that mirror linkattributes elsewhere never see the change.
Apply
titlewith the same!==guard,|| nullnormalization andonChangenotification 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 heldto behaviour the same file already guarantees.
Before
After