Skip to content

[lexical-playground] Bug Fix: a special text chip re-renders when its text changes - #9014

Closed
LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:fix/special-text-node-update-dom
Closed

LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:fix/special-text-node-update-dom

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Description

SpecialTextNode builds its element from the node's text and returns false from updateDOM, but never re-syncs that text:

createDOM(config: EditorConfig): HTMLElement {
  const dom = $getDocument().createElement('span');
  addClassNamesToElement(dom, config.theme.specialText);
  dom.textContent = this.getTextContent();
  return dom;
}

updateDOM(prevNode: this, dom: HTMLElement, config: EditorConfig): boolean {
  if (prevNode.__text.startsWith('[') && prevNode.__text.endsWith(']')) {
    const strippedText = this.__text.substring(1, this.__text.length - 1); // Strip brackets again
    dom.textContent = strippedText; // Update the text content
  }
  addClassNamesToElement(dom, config.theme.specialText);
  return false;
}

false means "I have already applied every difference between prevNode and this", so the reconciler leaves the element alone. The only write to dom.textContent is behind a guard that cannot hold: the node is created by SpecialTextExtension from the capture group, not the whole match —

const BRACKETED_TEXT_REGEX = /\[([^[\]]+)\]/;
...
const specialTextNode = $createSpecialTextNode(matchedText);   // match[1], brackets already gone

— and the group excludes brackets, so prevNode.__text never starts with [. The branch is dead and the element keeps whatever text it was created with, forever.

SpecialTextNode does not override canInsertTextBefore, so typing at the start of a chip merges the character into the node. Type [foo] to get a foo chip, put the caret at its start and type x (the node's text becomes xfoo), then undo: the editor state goes back to foo, the chip still reads xfoo, and the undo looks like it did nothing. Any programmatic setTextContent, and any collab edit, desync the same way.

The guard is also wrong on its own terms. In the one case it can fire — the user typing brackets back around a chip, which the transform skips because a SpecialTextNode is not isSimpleText() — it writes this.__text minus its first and last character, while createDOM writes the whole string. The two methods disagree about who strips brackets, and createDOM is the one that matches how the node is actually constructed.

Fixed by dropping the dead guard and writing the text the same way createDOM does, only when it changed. $setTextContent, the helper TextNode.updateDOM uses, is module-private, and assigning textContent is exactly what this node's own createDOM already does. Still returns false; no API or serialization change.

Test plan

New unit test packages/lexical-playground/__tests__/unit/SpecialTextNode.test.ts. The second case pins the bracket contradiction: a chip whose text is [bar] must render [bar], which is what createDOM produces.

Before

 ❯ packages/lexical-playground/__tests__/unit/SpecialTextNode.test.ts (2 tests | 2 failed)
   × re-renders the text when it changes
     AssertionError: expected 'foo' to be 'bar' // Object.is equality
   × renders the text verbatim when it is bracketed
     AssertionError: expected 'bar' to be '[bar]' // Object.is equality

 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)

… text changes

## Description

`SpecialTextNode` builds its element from the node's text and returns `false` from `updateDOM`, but never re-syncs that text:

```ts
createDOM(config: EditorConfig): HTMLElement {
  const dom = $getDocument().createElement('span');
  addClassNamesToElement(dom, config.theme.specialText);
  dom.textContent = this.getTextContent();
  return dom;
}

updateDOM(prevNode: this, dom: HTMLElement, config: EditorConfig): boolean {
  if (prevNode.__text.startsWith('[') && prevNode.__text.endsWith(']')) {
    const strippedText = this.__text.substring(1, this.__text.length - 1); // Strip brackets again
    dom.textContent = strippedText; // Update the text content
  }
  addClassNamesToElement(dom, config.theme.specialText);
  return false;
}
```

`false` means "I have already applied every difference between `prevNode` and `this`", so the reconciler leaves the element alone. The only write to `dom.textContent` is behind a guard that cannot hold: the node is created by `SpecialTextExtension` from the *capture group*, not the whole match —

```ts
const BRACKETED_TEXT_REGEX = /\[([^[\]]+)\]/;
...
const specialTextNode = $createSpecialTextNode(matchedText);   // match[1], brackets already gone
```

— and the group excludes brackets, so `prevNode.__text` never starts with `[`. The branch is dead and the element keeps whatever text it was created with, forever.

`SpecialTextNode` does not override `canInsertTextBefore`, so typing at the start of a chip merges the character into the node. Type `[foo]` to get a `foo` chip, put the caret at its start and type `x` (the node's text becomes `xfoo`), then undo: the editor state goes back to `foo`, the chip still reads `xfoo`, and the undo looks like it did nothing. Any programmatic `setTextContent`, and any collab edit, desync the same way.

The guard is also wrong on its own terms. In the one case it *can* fire — the user typing brackets back around a chip, which the transform skips because a `SpecialTextNode` is not `isSimpleText()` — it writes `this.__text` minus its first and last character, while `createDOM` writes the whole string. The two methods disagree about who strips brackets, and `createDOM` is the one that matches how the node is actually constructed.

Fixed by dropping the dead guard and writing the text the same way `createDOM` does, only when it changed. `$setTextContent`, the helper `TextNode.updateDOM` uses, is module-private, and assigning `textContent` is exactly what this node's own `createDOM` already does. Still returns `false`; no API or serialization change.

## Test plan

New unit test `packages/lexical-playground/__tests__/unit/SpecialTextNode.test.ts`. The second case pins the bracket contradiction: a chip whose text is `[bar]` must render `[bar]`, which is what `createDOM` produces.

### Before

```
 ❯ packages/lexical-playground/__tests__/unit/SpecialTextNode.test.ts (2 tests | 2 failed)
   × re-renders the text when it changes
     AssertionError: expected 'foo' to be 'bar' // Object.is equality
   × renders the text verbatim when it is bracketed
     AssertionError: expected 'bar' to be '[bar]' // Object.is equality

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