Skip to content

[lexical-rich-text] Bug Fix: use writable node in HeadingNode.setTag#8235

Merged
etrepum merged 5 commits intofacebook:mainfrom
karesansui-u:fix/heading-node-set-tag
Mar 18, 2026
Merged

[lexical-rich-text] Bug Fix: use writable node in HeadingNode.setTag#8235
etrepum merged 5 commits intofacebook:mainfrom
karesansui-u:fix/heading-node-set-tag

Conversation

@karesansui-u
Copy link
Contributor

Summary

HeadingNode.setTag() calls getWritable() but writes to this.__tag instead of the returned writable clone (self). This bypasses Lexical's immutability model — the original (possibly frozen) node is mutated directly.

The fix changes this.__tag = tag to self.__tag = tag, matching the pattern used elsewhere (e.g. CodeNode.setLanguage()).

Test plan

  • Added unit test for HeadingNode.setTag() verifying the tag is updated correctly
  • All existing tests pass (pnpm test-unit: 2112 passed)

@vercel
Copy link

vercel bot commented Mar 18, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview, Comment Mar 18, 2026 3:24pm
lexical-playground Ready Ready Preview, Comment Mar 18, 2026 3:24pm

Request Review

@meta-cla
Copy link

meta-cla bot commented Mar 18, 2026

Hi @karesansui-u!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@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 Mar 18, 2026
@meta-cla
Copy link

meta-cla bot commented Mar 18, 2026

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Copy link
Collaborator

@etrepum etrepum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is useless, it would pass without the fix. You can either fix the test to actually test the condition (requires two updates with an await or using the discrete option) or delete the test altogether.

Split the single-update test into two discrete updates so the test
actually exercises the getWritable() code path. In a single update,
both this.__tag and self.__tag write to the same object, making the
test pass regardless of the fix. With discrete updates, the second
update gets a cloned node from getWritable(), so only self.__tag
correctly mutates the writable copy.
@karesansui-u
Copy link
Contributor Author

@etrepum Thank you for the review — you're absolutely right. The original test ran everything in a single update(), so getWritable() returned the same object and the test would pass regardless of whether this.__tag or self.__tag was used.

I've updated the test to use two separate {discrete: true} updates:

  1. First update: create and append an h1 heading, flush to DOM
  2. Second update: retrieve the existing node via getFirstChildOrThrow(), call setTag('h2'), and verify both the node state and the rendered DOM (<h1><h2>)

With this structure, getWritable() returns a cloned node in the second update, so only self.__tag (the fix) correctly mutates the writable copy. The buggy this.__tag would leave the DOM unchanged, causing the outerHTML assertion to fail.

etrepum
etrepum previously approved these changes Mar 18, 2026
@etrepum etrepum dismissed their stale review March 18, 2026 14:46

Integrity check failure

Copy link
Collaborator

@etrepum etrepum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new test fails

Copy link
Collaborator

@etrepum etrepum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A similar fix needs to happen in getTag to use getLatest

getFirstChildOrThrow() returns a read-only node. setTag() internally
calls getWritable() which returns a new clone, so the original variable
still points to the old read-only node with tag 'h1'. Use the return
value of setTag() to correctly assert the writable clone's tag.
Copy link
Collaborator

@etrepum etrepum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix isn’t complete, it’s not required to use the result of setTag, so you can revert that change. getTag needs to fixed to use getLatest.

HeadingNode.getTag() was reading this.__tag directly, which returns
stale values when called on a non-latest node reference. Use
getLatest().__tag to match the pattern used by ListNode.getTag()
and other node getters.

Also revert the test to use heading.getTag() directly instead of
capturing the setTag() return value, since getLatest() now ensures
the correct value is returned from any node reference.
@karesansui-u
Copy link
Contributor Author

@etrepum Sorry about the back and forth — my test didn't properly validate the fix, and I missed the getLatest() issue entirely. Thank you for catching both.

I've pushed a new commit that:

  • Adds getLatest() to getTag() to match the pattern in ListNode.getTag() and other node getters
  • Reverts the test to assert on heading.getTag() directly, since getLatest() now ensures consistent reads from any node reference

Appreciate the thorough review — learned a lot about Lexical's node versioning model.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should not change, remove this from the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really sorry about that — reverted in the latest push.

@etrepum etrepum added this pull request to the merge queue Mar 18, 2026
Merged via the queue into facebook:main with commit 8a418f0 Mar 18, 2026
40 checks passed
@etrepum etrepum mentioned this pull request Mar 19, 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.

2 participants