Skip to content

[lexical-playground] Bug Fix: $createMentionNode keeps the display text it is given - #8999

Merged
etrepum merged 1 commit into
meta:mainfrom
LeSingh1:fix/mention-text-content
Aug 9, 2026
Merged

etrepum merged 1 commit into
meta:mainfrom
LeSingh1:fix/mention-text-content

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Description

$createMentionNode assigned to its own parameter before using it:

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:

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:

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 #8969. This change is in the same file as #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)

…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)
```
@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 47a3ef2 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