Skip to content

[lexical] Bug Fix: don't leave empty class/style attributes in the editor DOM - #8977

Closed
LeSingh1 wants to merge 1 commit into
facebook:mainfrom
LeSingh1:fix/5307-empty-class-style-attributes
Closed

[lexical] Bug Fix: don't leave empty class/style attributes in the editor DOM#8977
LeSingh1 wants to merge 1 commit into
facebook:mainfrom
LeSingh1:fix/5307-empty-class-style-attributes

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

classList.remove() and style.setProperty(prop, '') keep the attribute in place with an empty value, so clearing the last theme class or the last inline declaration renders <span class=""> / <p style="">.

Repro: p.setFormat('center') then p.setFormat('') gives <p style="">; toggling selection.formatText('underline') twice with a theme.text.underline class gives <span class="">.

Adds an internal removeEmptyDOMAttribute(dom, 'class' | 'style'), which drops the attribute when getAttribute returns '', and calls it from the three reconcile paths that can drop the final token: element format (text-align), element indent (padding-inline-start plus the indent theme class), and TextNode.updateDOM (theme classes and __style). It is not exported from index.ts, so there is no public API or flow change.

Four unit tests, all failing on main with the exact style="" / class="" output.

Three e2e files had recorded the leftover attribute and are updated — ClearFormatting (5), Indentation (6), KeyboardShortcuts (1). Their purpose is unchanged; only the stale attribute is gone, and I confirmed in Chromium beforehand that the received HTML differed from the expectation by exactly style="".

DraggableBlock.spec.mjs keeps its four style="" assertions on purpose: that one comes from LexicalDraggableBlockPlugin writing element.style.transform directly and restoring it, not from the reconciler. Same cosmetic class, plugin-owned DOM, worth its own fix.

Noting @acywatson's "we could probably change this, but I'm not sure it's a bug" from the thread — happy to close this if the answer is still that it isn't worth the churn.

Fixes #5307

…itor DOM

`classList.remove()` and `style.setProperty(prop, '')` keep the attribute in
place with an empty value, so clearing the last theme class or the last inline
declaration rendered `<span class="">` / `<p style="">`.

Remove the attribute once it is empty in the three reconcile paths that can
drop the final token: element format (text-align), element indent
(padding-inline-start and the indent theme class), and TextNode format/style.

The e2e expectations in ClearFormatting, Indentation and KeyboardShortcuts
recorded the leftover `style=""` and are updated. DraggableBlock is left alone:
its `style=""` comes from LexicalDraggableBlockPlugin writing to the DOM
directly, not from the reconciler.
@vercel

vercel Bot commented Aug 8, 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 8, 2026
@LeSingh1

Copy link
Copy Markdown
Contributor Author

Consolidated into #9058 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.

Bug: node.setFormat and selection.formatText leave empty style and class attributes

1 participant