Skip to content

[lexical-playground] Bug Fix: RubyNode.updateDOM updates the element createDOM rendered - #9032

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

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

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Description

RubyNode wraps the element TextNode.createDOM builds, and createDOM is careful to keep everything super produced on the inner element:

createDOM(config: EditorConfig): HTMLElement {
  const inner = super.createDOM(config);          // theme.text.* classes + this.__style land here
  inner.dataset.rubyAnnotation = this.getAnnotation();
  addClassNamesToElement(inner, config.theme.ruby || 'PlaygroundEditorTheme__ruby');
  const wrapper = $getDocument().createElement('span');
  ...
  wrapper.appendChild(inner);
  return wrapper;                                  // the keyed element is the wrapper
}

updateDOM then hands super the wrapper:

updateDOM(prevNode: this, dom: HTMLElement, config: EditorConfig): boolean {
  const updated = super.updateDOM(prevNode, dom, config);   // dom === wrapper

The text itself survives, because the getDOMSlot override redirects $setTextContent to dom.firstElementChild. The other two things TextNode.updateDOM does bypass the slot and write to the element they were handed:

  • setTextThemeClassNames(..., innerDOM, textClassNames)
  • setDOMStyleFromCSS(dom.style, nextStyle, prevStyle)

So the two methods disagree about which element carries the formatting, and updateDOM returns false — promising the reconciler the DOM is already correct.

Rendered on upstream/main, a ruby node with color: red restyled to color: blue and underlined:

<!-- after createDOM -->
<span role="group" aria-label="kanji (kana)">
  <span style="color: red;" data-ruby-annotation="kana" class="theme-ruby">kanji</span>
</span>

<!-- after updateDOM -->
<span role="group" aria-label="kanji (kana)" class="theme-underline" style="color: blue;">
  <span style="color: red;" data-ruby-annotation="kana" class="theme-ruby">kanji</span>
</span>

The inner element's own inline color: red still wins over the wrapper's color: blue, so changing the colour of a ruby annotation does nothing — the toolbar swatch moves and the text stays red. Underline and strikethrough land on the wrapper, whose only child is .PlaygroundEditorTheme__ruby with display: inline-block; text decoration does not propagate into an atomic inline box, so they are not painted either. Removing a format that was carried in from the original text ($toggleRuby copies format and style from the source node) strips the class from the wrapper while the inner keeps it, so the formatting can never be turned off.

Bold, italic, code, highlight, subscript and superscript are unaffected — those change the HTML tag, so TextNode.updateDOM returns true before touching the DOM and the node is rebuilt through createDOM. Only the formats applied in place are broken, which is why this looks arbitrary in use.

The fix resolves the inner element and passes it to super, exactly as createDOM and getDOMSlot already do. The wrapper keeps the role/aria-label it owns. When the inner is missing, updateDOM returns true so the reconciler rebuilds rather than silently promising a DOM it could not update.

Test plan

New unit test packages/lexical-playground/__tests__/unit/RubyNode.test.ts. The first two cases assert the invariant updateDOM returning false actually promises: the DOM after mutating a node must equal the DOM of a freshly created node in the same state. That pins the assertion to createDOM rather than to a hardcoded expectation. The third case is a control covering the annotation and accessible name the wrapper legitimately owns, and passes before and after.

Before

 ❯ packages/lexical-playground/__tests__/unit/RubyNode.test.ts (3 tests | 2 failed)
   × applies a style change to the element createDOM styled
       "innerClass": "theme-ruby",
     -   "innerStyle": "color: blue;",
     +   "innerStyle": "color: red;",
         "wrapperClass": "",
     -   "wrapperStyle": "",
     +   "wrapperStyle": "color: blue;",
   × applies a class-only text format to the element createDOM classed
     -   "innerClass": "theme-ruby theme-underline",
     +   "innerClass": "theme-ruby",
         "innerStyle": "color: red;",
     -   "wrapperClass": "",
     +   "wrapperClass": "theme-underline",
   ✓ leaves the annotation and the accessible name on the wrapper

 Test Files  1 failed (1)
      Tests  2 failed | 1 passed (3)

After

 Test Files  1 passed (1)
      Tests  3 passed (3)

Package suite is unchanged:

$ npx vitest run packages/lexical-playground
 Test Files  19 passed (19)
      Tests  277 passed (277)

…createDOM rendered

## Description

`RubyNode` wraps the element `TextNode.createDOM` builds, and `createDOM` is careful to keep everything super produced on the **inner** element:

```ts
createDOM(config: EditorConfig): HTMLElement {
  const inner = super.createDOM(config);          // theme.text.* classes + this.__style land here
  inner.dataset.rubyAnnotation = this.getAnnotation();
  addClassNamesToElement(inner, config.theme.ruby || 'PlaygroundEditorTheme__ruby');
  const wrapper = $getDocument().createElement('span');
  ...
  wrapper.appendChild(inner);
  return wrapper;                                  // the keyed element is the wrapper
}
```

`updateDOM` then hands super the **wrapper**:

```ts
updateDOM(prevNode: this, dom: HTMLElement, config: EditorConfig): boolean {
  const updated = super.updateDOM(prevNode, dom, config);   // dom === wrapper
```

The text itself survives, because the `getDOMSlot` override redirects `$setTextContent` to `dom.firstElementChild`. The other two things `TextNode.updateDOM` does bypass the slot and write to the element they were handed:

- `setTextThemeClassNames(..., innerDOM, textClassNames)`
- `setDOMStyleFromCSS(dom.style, nextStyle, prevStyle)`

So the two methods disagree about which element carries the formatting, and `updateDOM` returns `false` — promising the reconciler the DOM is already correct.

Rendered on `upstream/main`, a ruby node with `color: red` restyled to `color: blue` and underlined:

```html
<!-- after createDOM -->
<span role="group" aria-label="kanji (kana)">
  <span style="color: red;" data-ruby-annotation="kana" class="theme-ruby">kanji</span>
</span>

<!-- after updateDOM -->
<span role="group" aria-label="kanji (kana)" class="theme-underline" style="color: blue;">
  <span style="color: red;" data-ruby-annotation="kana" class="theme-ruby">kanji</span>
</span>
```

The inner element's own inline `color: red` still wins over the wrapper's `color: blue`, so **changing the colour of a ruby annotation does nothing** — the toolbar swatch moves and the text stays red. Underline and strikethrough land on the wrapper, whose only child is `.PlaygroundEditorTheme__ruby` with `display: inline-block`; text decoration does not propagate into an atomic inline box, so they are not painted either. Removing a format that was carried in from the original text (`$toggleRuby` copies `format` and `style` from the source node) strips the class from the wrapper while the inner keeps it, so the formatting can never be turned off.

Bold, italic, code, highlight, subscript and superscript are unaffected — those change the HTML tag, so `TextNode.updateDOM` returns `true` before touching the DOM and the node is rebuilt through `createDOM`. Only the formats applied in place are broken, which is why this looks arbitrary in use.

The fix resolves the inner element and passes it to super, exactly as `createDOM` and `getDOMSlot` already do. The wrapper keeps the `role`/`aria-label` it owns. When the inner is missing, `updateDOM` returns `true` so the reconciler rebuilds rather than silently promising a DOM it could not update.

## Test plan

New unit test `packages/lexical-playground/__tests__/unit/RubyNode.test.ts`. The first two cases assert the invariant `updateDOM` returning `false` actually promises: the DOM after mutating a node must equal the DOM of a freshly created node in the same state. That pins the assertion to `createDOM` rather than to a hardcoded expectation. The third case is a control covering the annotation and accessible name the wrapper legitimately owns, and passes before and after.

### Before

```
 ❯ packages/lexical-playground/__tests__/unit/RubyNode.test.ts (3 tests | 2 failed)
   × applies a style change to the element createDOM styled
       "innerClass": "theme-ruby",
     -   "innerStyle": "color: blue;",
     +   "innerStyle": "color: red;",
         "wrapperClass": "",
     -   "wrapperStyle": "",
     +   "wrapperStyle": "color: blue;",
   × applies a class-only text format to the element createDOM classed
     -   "innerClass": "theme-ruby theme-underline",
     +   "innerClass": "theme-ruby",
         "innerStyle": "color: red;",
     -   "wrapperClass": "",
     +   "wrapperClass": "theme-underline",
   ✓ leaves the annotation and the accessible name on the wrapper

 Test Files  1 failed (1)
      Tests  2 failed | 1 passed (3)
```

### After

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

Package suite is unchanged:

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