Skip to content

[lexical-playground] Bug Fix: a keyword keeps the text format it was typed with - #9008

Closed
LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:fix/keyword-node-theme-classes
Closed

LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:fix/keyword-node-theme-classes

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Description

KeywordNode.createDOM replaces the class name that TextNode.createDOM just set instead of adding to it:

createDOM(config: EditorConfig): HTMLElement {
  const dom = super.createDOM(config);
  dom.style.cursor = 'default';
  dom.className = 'keyword';   // <- discards everything super applied
  return dom;
}

TextNode.createDOM renders the node's text formats through $createTextInnerDOM, which applies config.theme.text.* to the element. Every format the playground theme implements as a class — underline, strikethrough, highlight, subscript, superscript, code, uppercase/lowercase/capitalize — is written onto that element and then thrown away by the assignment.

The formats really are there to render: registerLexicalTextEntity, which is what promotes a TextNode into a KeywordNode, explicitly carries the format over.

replacementNode.setFormat(nodeToReplace.getFormat());

So turning on underline and typing congrats gives a node whose state, toolbar and exportDOM all say underlined, rendered without an underline. It is not permanently broken, which is what makes it confusing: TextNode.updateDOM re-runs setTextThemeClassNames on a format change, so toggling the format off and back on paints it correctly, and it stays correct until something re-runs createDOM — pasting the keyword elsewhere, an undo/redo across it, reloading a saved document, or receiving it over collab — at which point the styling disappears again. The same document renders differently depending on how it was arrived at.

Fixed by using addClassNamesToElement, which is what the sibling SpecialTextNode in the same directory already does for the same reason. The outer .keyword class is unchanged, so the CSS still matches; formats are simply no longer dropped.

Test plan

New unit test packages/lexical-playground/__tests__/unit/KeywordNode.test.ts renders a formatted keyword with a theme and reads the class list off the rendered element.

Before

 ❯ packages/lexical-playground/__tests__/unit/KeywordNode.test.ts (2 tests | 2 failed)
   × keeps the theme class of the text format it was created with
     AssertionError: expected [ 'keyword' ] to deeply equal [ 'keyword', 'theme-underline' ]

     - Expected
     + Received

       Array [
         "keyword",
     -   "theme-underline",
       ]

   × keeps the theme class across a re-render of the keyword
     AssertionError: expected [ 'keyword' ] to deeply equal [ 'keyword', 'theme-bold' ]

 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)

…typed with

## Description

`KeywordNode.createDOM` replaces the class name that `TextNode.createDOM` just set instead of adding to it:

```ts
createDOM(config: EditorConfig): HTMLElement {
  const dom = super.createDOM(config);
  dom.style.cursor = 'default';
  dom.className = 'keyword';   // <- discards everything super applied
  return dom;
}
```

`TextNode.createDOM` renders the node's text formats through `$createTextInnerDOM`, which applies `config.theme.text.*` to the element. Every format the playground theme implements as a class — underline, strikethrough, highlight, subscript, superscript, code, uppercase/lowercase/capitalize — is written onto that element and then thrown away by the assignment.

The formats really are there to render: `registerLexicalTextEntity`, which is what promotes a `TextNode` into a `KeywordNode`, explicitly carries the format over.

```ts
replacementNode.setFormat(nodeToReplace.getFormat());
```

So turning on underline and typing `congrats` gives a node whose state, toolbar and `exportDOM` all say underlined, rendered without an underline. It is not permanently broken, which is what makes it confusing: `TextNode.updateDOM` re-runs `setTextThemeClassNames` on a *format change*, so toggling the format off and back on paints it correctly, and it stays correct until something re-runs `createDOM` — pasting the keyword elsewhere, an undo/redo across it, reloading a saved document, or receiving it over collab — at which point the styling disappears again. The same document renders differently depending on how it was arrived at.

Fixed by using `addClassNamesToElement`, which is what the sibling `SpecialTextNode` in the same directory already does for the same reason. The outer `.keyword` class is unchanged, so the CSS still matches; formats are simply no longer dropped.

## Test plan

New unit test `packages/lexical-playground/__tests__/unit/KeywordNode.test.ts` renders a formatted keyword with a theme and reads the class list off the rendered element.

### Before

```
 ❯ packages/lexical-playground/__tests__/unit/KeywordNode.test.ts (2 tests | 2 failed)
   × keeps the theme class of the text format it was created with
     AssertionError: expected [ 'keyword' ] to deeply equal [ 'keyword', 'theme-underline' ]

     - Expected
     + Received

       Array [
         "keyword",
     -   "theme-underline",
       ]

   × keeps the theme class across a re-render of the keyword
     AssertionError: expected [ 'keyword' ] to deeply equal [ 'keyword', 'theme-bold' ]

 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