Repository navigation
[lexical-playground] Bug Fix: $createMentionNode keeps the display text it is given - #8999
Merged
Merged
Conversation
…xt it is given
## Description
`$createMentionNode` assigned to its own parameter before using it:
```ts
export function $createMentionNode(
mentionName: string,
textContent?: string,
): MentionNode {
const mentionNode = new MentionNode(mentionName, (textContent = mentionName));
...
```
`(textContent = mentionName)` overwrites the caller's argument and passes
`mentionName` through, so the `textContent` parameter was dead — a mention's
display text was always forced to equal its mention name.
Two other sites cooperate to preserve exactly that distinction, so this
silently defeated both. `MentionNode.exportDOM` goes out of its way to encode
it:
```ts
if (this.__text !== this.__mention) {
element.setAttribute('data-lexical-mention-name', this.__mention);
}
element.textContent = this.__text;
```
and the import rule in `MentionsExtension` decodes it and passes both values
back:
```ts
const textContent = el.textContent ?? '';
const mentionName = el.getAttribute('data-lexical-mention-name') ?? textContent;
return [$createMentionNode(mentionName, textContent)];
```
With the factory discarding the second argument, importing
`<span data-lexical-mention="true" data-lexical-mention-name="luke_skywalker">Luke Skywalker</span>`
produced a mention that renders as `luke_skywalker`. It also made
`exportDOM`'s `this.__text !== this.__mention` branch unreachable for anything
the factory built, so the attribute was never written in the first place and
the encode/decode pair could never round-trip.
`MentionNode`'s constructor already defaults correctly
(`super(text ?? mentionName, key)`), so passing the parameter straight through
preserves the single-argument behaviour exactly.
## Test plan
New `packages/lexical-playground/__tests__/unit/MentionTextContent.test.ts`:
the explicit-text case, a control for the single-argument default (passes
before and after), and a full export -> import round trip.
The test lives in its own file because `MentionHTML.test.ts` is already being
edited by meta#8969. This change is in the same file as meta#8969 but a different
function (`$createMentionNode` vs `exportDOM`/imports); the two apply cleanly
in either order.
The existing mention coverage is unaffected either way:
`PlaygroundNodeImporters.test.ts` calls `$createMentionNode('Luke')` with one
argument, and `Mentions.spec.mjs` pastes a `<span data-lexical-mention="true">`
with no `data-lexical-mention-name`, so `mentionName === textContent` in both.
### Before
```
$ npx vitest run packages/lexical-playground/__tests__/unit/MentionTextContent.test.ts
× $createMentionNode honours an explicit textContent 9ms
✓ $createMentionNode defaults textContent to the mention name 1ms
× round-trips a display text that differs from the mention name 4ms
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 2 ⎯⎯⎯⎯⎯⎯⎯
AssertionError: expected 'luke_skywalker' to be 'Luke Skywalker' // Object.is equality
Expected: "Luke Skywalker"
Received: "luke_skywalker"
AssertionError: expected '<p><span data-lexical-mention="true">…' to contain 'data-lexical-mention-name="luke_skywa…'
Expected: "data-lexical-mention-name="luke_skywalker""
Received: "<p><span data-lexical-mention="true">luke_skywalker</span></p>"
Tests 2 failed | 1 passed (3)
```
### After
```
$ npx vitest run packages/lexical-playground
Test Files 18 passed (18)
Tests 274 passed (274)
```
LeSingh1
requested review from
acywatson,
etrepum,
fantactuka,
ivailop7,
potatowagon and
zurfyx
as code owners
August 9, 2026 02:15
|
@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. |
etrepum
approved these changes
Aug 9, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
$createMentionNodeassigned to its own parameter before using it:(textContent = mentionName)overwrites the caller's argument and passesmentionNamethrough, so thetextContentparameter was dead — a mention'sdisplay text was always forced to equal its mention name.
Two other sites cooperate to preserve exactly that distinction, so this
silently defeated both.
MentionNode.exportDOMgoes out of its way to encodeit:
and the import rule in
MentionsExtensiondecodes it and passes both valuesback:
With the factory discarding the second argument, importing
<span data-lexical-mention="true" data-lexical-mention-name="luke_skywalker">Luke Skywalker</span>produced a mention that renders as
luke_skywalker. It also madeexportDOM'sthis.__text !== this.__mentionbranch unreachable for anythingthe factory built, so the attribute was never written in the first place and
the encode/decode pair could never round-trip.
MentionNode's constructor already defaults correctly(
super(text ?? mentionName, key)), so passing the parameter straight throughpreserves the single-argument behaviour exactly.
Test plan
New
packages/lexical-playground/__tests__/unit/MentionTextContent.test.ts:the explicit-text case, a control for the single-argument default (passes
before and after), and a full export -> import round trip.
The test lives in its own file because
MentionHTML.test.tsis already beingedited by #8969. This change is in the same file as #8969 but a different
function (
$createMentionNodevsexportDOM/imports); the two apply cleanlyin either order.
The existing mention coverage is unaffected either way:
PlaygroundNodeImporters.test.tscalls$createMentionNode('Luke')with oneargument, and
Mentions.spec.mjspastes a<span data-lexical-mention="true">with no
data-lexical-mention-name, somentionName === textContentin both.Before
After