Skip to content

[lexical-playground] Bug Fix: an emoji renders the text format applied to it - #9009

Closed
LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:fix/emoji-node-format
Closed

LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:fix/emoji-node-format

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Description

EmojiNode renders a wrapper span around the element TextNode.createDOM builds, and drops the format work that element carries in two places.

updateDOM discards super's return value.

updateDOM(prevNode: this, dom: HTMLElement, config: EditorConfig): boolean {
  const inner = dom.firstChild;
  if (inner === null) {
    return true;
  }
  super.updateDOM(prevNode, inner as HTMLElement, config);
  return false;   // <- super may have returned true
}

TextNode.updateDOM returns true when the new format needs a different tag, and it does so before touching the DOM:

if (prevTag !== nextTag) {
  return true;
}

true means "I could not apply this, recreate the element". EmojiNode throws that away and returns false, which promises the reconciler that every difference is already in the DOM — so nothing is recreated. Selecting a range that contains an emoji and pressing Ctrl+B formats the emoji ($updateTextFormat formats token nodes wholesale, and an EmojiNode is a token), the surrounding text goes bold, and the emoji does not. The editor state and exportJSON/exportDOM all say bold: what you see is not what you copy. Same for italic, code, highlight, subscript and superscript — every format that changes the tag. Underline and strikethrough happen to work, because they are applied as classes on the same tag.

The if (inner === null) return true line above shows the intent was already understood here; the super result is simply dropped.

createDOM replaces the inner element's class name.

const inner = super.createDOM(config);
inner.className = 'emoji-inner';   // <- discards theme.text classes

TextNode.createDOM applies config.theme.text.* to that element for the class-based formats. Assigning className throws them away, so an underlined or highlighted emoji comes back unstyled every time createDOM runs again — pasting it elsewhere, undo/redo across it, reloading a saved document, or receiving it over collab.

Both are fixed without changing the emoji's own markup: updateDOM now returns what TextNode.updateDOM returned, and createDOM uses addClassNamesToElement (as the sibling SpecialTextNode already does) so .emoji-inner is added to the theme classes rather than replacing them. The CSS selectors .emoji and .emoji-inner still match.

Test plan

New unit test packages/lexical-playground/__tests__/unit/EmojiNode.test.ts, one case per defect.

Before

 ❯ packages/lexical-playground/__tests__/unit/EmojiNode.test.ts (2 tests | 2 failed)
   × renders a tag-changing format applied after the emoji exists
     AssertionError: expected null not to be null // Object.is equality
   × keeps the theme class of a class-only format on the inner element
     AssertionError: expected [ 'emoji-inner' ] to deeply equal [ 'emoji-inner', 'theme-underline' ]

 Test Files  1 failed (1)
      Tests  2 failed (2)

After

 Test Files  1 passed (1)
      Tests  2 passed (2)

Package suite is unchanged:

$ npx vitest run packages/lexical-playground
 Test Files  18 passed (18)
      Tests  273 passed (273)

…d to it

## Description

`EmojiNode` renders a wrapper span around the element `TextNode.createDOM` builds, and drops the format work that element carries in two places.

**`updateDOM` discards `super`'s return value.**

```ts
updateDOM(prevNode: this, dom: HTMLElement, config: EditorConfig): boolean {
  const inner = dom.firstChild;
  if (inner === null) {
    return true;
  }
  super.updateDOM(prevNode, inner as HTMLElement, config);
  return false;   // <- super may have returned true
}
```

`TextNode.updateDOM` returns `true` when the new format needs a different tag, and it does so *before touching the DOM*:

```ts
if (prevTag !== nextTag) {
  return true;
}
```

`true` means "I could not apply this, recreate the element". `EmojiNode` throws that away and returns `false`, which promises the reconciler that every difference is already in the DOM — so nothing is recreated. Selecting a range that contains an emoji and pressing Ctrl+B formats the emoji (`$updateTextFormat` formats token nodes wholesale, and an `EmojiNode` is a token), the surrounding text goes bold, and the emoji does not. The editor state and `exportJSON`/`exportDOM` all say bold: what you see is not what you copy. Same for italic, code, highlight, subscript and superscript — every format that changes the tag. Underline and strikethrough happen to work, because they are applied as classes on the same tag.

The `if (inner === null) return true` line above shows the intent was already understood here; the `super` result is simply dropped.

**`createDOM` replaces the inner element's class name.**

```ts
const inner = super.createDOM(config);
inner.className = 'emoji-inner';   // <- discards theme.text classes
```

`TextNode.createDOM` applies `config.theme.text.*` to that element for the class-based formats. Assigning `className` throws them away, so an underlined or highlighted emoji comes back unstyled every time `createDOM` runs again — pasting it elsewhere, undo/redo across it, reloading a saved document, or receiving it over collab.

Both are fixed without changing the emoji's own markup: `updateDOM` now returns what `TextNode.updateDOM` returned, and `createDOM` uses `addClassNamesToElement` (as the sibling `SpecialTextNode` already does) so `.emoji-inner` is added to the theme classes rather than replacing them. The CSS selectors `.emoji` and `.emoji-inner` still match.

## Test plan

New unit test `packages/lexical-playground/__tests__/unit/EmojiNode.test.ts`, one case per defect.

### Before

```
 ❯ packages/lexical-playground/__tests__/unit/EmojiNode.test.ts (2 tests | 2 failed)
   × renders a tag-changing format applied after the emoji exists
     AssertionError: expected null not to be null // Object.is equality
   × keeps the theme class of a class-only format on the inner element
     AssertionError: expected [ 'emoji-inner' ] to deeply equal [ 'emoji-inner', 'theme-underline' ]

 Test Files  1 failed (1)
      Tests  2 failed (2)
```

### After

```
 Test Files  1 passed (1)
      Tests  2 passed (2)
```

Package suite is unchanged:

```
$ npx vitest run packages/lexical-playground
 Test Files  18 passed (18)
      Tests  273 passed (273)
```
@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
@LeSingh1

Copy link
Copy Markdown
Contributor Author

Consolidated into #9049 with the other two nodes that share this defect, per @mayrang's note on #9035 and @etrepum's on #9027. 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.

1 participant