Skip to content

[lexical-text][lexical-mark][lexical-link][lexical-playground] Bug Fix: inline node transforms keep the node and its DOM in step - #9060

Merged
etrepum merged 7 commits into
meta:mainfrom
LeSingh1:consol/text-entity-inline-dom
Aug 10, 2026
Merged

etrepum merged 7 commits into
meta:mainfrom
LeSingh1:consol/text-entity-inline-dom

Conversation

@LeSingh1

Copy link
Copy Markdown
Contributor

Description

Ten fixes for one defect class: an inline or custom node and the DOM/HTML it
renders drift apart because the code that rewrites the node only re-applies
some of what built it. A text-entity transform rebuilds a TextNode and drops
properties the original carried, or misses a match that ends the node; an
updateDOM returns false — promising the reconciler every difference is
already in the DOM — while writing to a different element than createDOM did,
or to none at all; a class or attribute is refreshed on only some of the
transitions that should refresh it; an export path loses formats or throws on
non-Latin-1 text. Same shape as #9049 (nodes overwriting what super.createDOM
applied), so they are consolidated into one review.

  • $replaceWithSimpleText in packages/lexical-text/src/registerLexicalTextEntity.ts
    carried only format onto the replacement node, so an entity reverting to
    plain text silently lost its style and detail.
  • $textNodeTransform in the same file re-ran getMatch over the whole
    combined text of the node and its sibling and then required
    nextMatch.start === 0. Matches already replaced or skipped are part of that
    text, so a match that ends the node without starting it — typically the last
    of several in one node — was never replaced. It now compares against the text
    still to be processed and against the current match's own start (Bug: Inconsistent behaviour from useLexicalTextEntity when there are several matches within the same node #5466).
  • MarkNode.updateDOM (packages/lexical-mark/src/MarkNode.ts) enumerated
    id-count transitions instead of re-applying the ids.length > 1 rule
    createDOM states, so 1 -> 3 never gained theme.markOverlap and 2 -> 0
    never lost it. It now derives the overlap state from the same rule.
  • handleLinkEdit (packages/lexical-link/src/LexicalAutoLinkExtension.ts)
    refreshed rel and target from the new match but never title, the third
    member of LinkAttributes that the creation path already applies. It also
    reported those attribute refreshes through onChange, which is documented as
    receiving (url, prevUrl); attribute refreshes no longer fire it, while
    creation, removal and real url changes still do.
  • TOGGLE_LINK_COMMAND's object-payload branch in
    packages/lexical-link/src/LexicalLinkExtension.ts never called
    validateUrl, so a configured policy applied to
    dispatchCommand(TOGGLE_LINK_COMMAND, url) and was skipped for
    dispatchCommand(TOGGLE_LINK_COMMAND, {url, target}). The same gate now
    covers both shapes.
  • MentionNode.exportDOM
    (packages/lexical-playground/src/nodes/MentionNode.ts) built a bare
    <span data-lexical-mention> from this.__text, dropping the node's inline
    style and text formats from the exported HTML. It now carries the style and
    wraps the span in the same <b>/<i>/<s>/<u> elements TextNode.exportDOM
    uses; the mention stays a <span> so the span[data-lexical-mention] import
    rule still matches (Bug: Mentions Format was not applied, the Font size style was not applied in html  #6453).
  • SpecialTextNode.updateDOM
    (packages/lexical-playground/src/nodes/SpecialTextNode.tsx) returned false
    but only wrote dom.textContent behind a prevNode.__text.startsWith('[')
    guard that cannot hold — the node is built from the regex capture group, with
    the brackets already stripped — so the chip kept the text it was created with
    forever. It now writes the text the way createDOM does, when it changed.
  • EquationNode.exportDOM
    (packages/lexical-playground/src/nodes/EquationNode.tsx) base64-encoded
    free-form LaTeX with btoa, which throws InvalidCharacterError on any code
    point above U+00FF, so copying or exporting \text{α} failed outright; the
    importer's atob had the mirror-image limit. Both sides now go through the
    UTF-8 bytes with shared encodeEquation/decodeEquation helpers, the same
    approach utils/docSerialization.ts already uses. Pure ASCII encodes byte for
    byte, so previously exported HTML still decodes.
  • RubyNode.updateDOM
    (packages/lexical-playground/src/plugins/RubyExtension/RubyNode.ts) handed
    super.updateDOM the wrapper element, while createDOM puts the text theme
    classes and the node's inline style on the inner element. The two disagreed
    about which element carries the formatting, so restyling a ruby annotation did
    nothing (the inner element's own color still won) and underline/strikethrough
    landed on a wrapper whose only child is an inline-block. It now resolves the
    inner element and passes it to super, and returns true when the inner is
    missing so the reconciler rebuilds instead.
  • $canShowPlaceholder (packages/lexical-text/src/canShowPlaceholder.ts)
    indexed its innermost loop with the outer loop's i instead of s. i is
    always 0 there, so the scan tested topBlockChildren[0] N times and never
    looked past index 0 — the one thing it exists to do. A paragraph of
    [TextNode(""), <inline DecoratorNode>] reported that the placeholder may be
    shown, rendering it on top of real content.

Test plan

Seven new unit test files and three extended ones, covering every fix:
registerLexicalTextEntity.test.ts, Issue5466Repro.test.ts and
canShowPlaceholder.test.ts in packages/lexical-text;
LexicalMarkNode.test.ts in packages/lexical-mark;
LexicalAutoLinkExtension.test.ts and LinkExtensionValidateUrl.test.ts in
packages/lexical-link; and MentionHTML.test.ts, SpecialTextNode.test.ts,
EquationNodeHTML.test.ts and RubyNode.test.ts in
packages/lexical-playground. Several cases are deliberate controls that pass
before and after — the createDOM reference the updateDOM cases are held to,
the legacy ASCII equation that must still decode, and the string-payload
validateUrl case that pins the asymmetry with the object payload.

Before

Source fixes reverted, new tests kept:

$ npx vitest run --project unit packages/lexical-text packages/lexical-mark packages/lexical-link packages/lexical-playground

     × replaces a match that ends the node and does not start at offset 0 265ms
     × replaces the last of several matches in the same node 23ms
     × replaces every match when the first one starts at offset 0 53ms
     × exports the font-size applied to a mention (#6453) 258ms
     × exports the text formats applied to a mention (#6453) 90ms
     × updateDOM adds the overlap class when going from 1 to 3 ids 27ms
     × updateDOM removes the overlap class when going from 2 to 0 ids 22ms
     × preserves style and detail when reverting an entity to simple text 191ms
     × re-renders the text when it changes 98ms
     × renders the text verbatim when it is bracketed 5ms
     × applies a style change to the element createDOM styled 71ms
     × applies a class-only text format to the element createDOM classed 4ms
     × exports an equation with non Latin-1 characters without throwing 53ms
     × round trips an equation with non Latin-1 characters 4ms
     × round trips an equation outside the basic multilingual plane 1ms
     × rejects a URL the validator refuses, for an object payload 64ms
     × is false when a decorator follows an empty token text node 29ms
     × is false when a decorator follows an empty segmented text node 4ms
     × is false when the decorator is several positions along 1ms
       × editing the link text refreshes the title 28ms
       × an attribute refresh is not reported as a url change 3ms

 Test Files  10 failed | 27 passed (37)
      Tests  21 failed | 895 passed (916)

After

$ npx vitest run --project unit packages/lexical-text packages/lexical-mark packages/lexical-link packages/lexical-playground

 Test Files  37 passed (37)
      Tests  916 passed (916)

$ npx tsc --noEmit -p .
(clean)

Supersedes #8919, #8969, #8982, #8987, #8998, #9014, #9019, #9032, #9036, #9046,
consolidated per the review feedback on #9027 and #9035.

…x: inline node transforms keep the node and its DOM in step

## Description

Ten fixes for one defect class: an inline or custom node and the DOM/HTML it
renders drift apart because the code that rewrites the node only re-applies
*some* of what built it. A text-entity transform rebuilds a `TextNode` and drops
properties the original carried, or misses a match that ends the node; an
`updateDOM` returns `false` — promising the reconciler every difference is
already in the DOM — while writing to a different element than `createDOM` did,
or to none at all; a class or attribute is refreshed on only some of the
transitions that should refresh it; an export path loses formats or throws on
non-Latin-1 text. Same shape as meta#9049 (nodes overwriting what `super.createDOM`
applied), so they are consolidated into one review.

- `$replaceWithSimpleText` in `packages/lexical-text/src/registerLexicalTextEntity.ts`
  carried only `format` onto the replacement node, so an entity reverting to
  plain text silently lost its `style` and `detail`.
- `$textNodeTransform` in the same file re-ran `getMatch` over the *whole*
  combined text of the node and its sibling and then required
  `nextMatch.start === 0`. Matches already replaced or skipped are part of that
  text, so a match that ends the node without starting it — typically the last
  of several in one node — was never replaced. It now compares against the text
  still to be processed and against the current match's own `start` (meta#5466).
- `MarkNode.updateDOM` (`packages/lexical-mark/src/MarkNode.ts`) enumerated
  id-count transitions instead of re-applying the `ids.length > 1` rule
  `createDOM` states, so `1 -> 3` never gained `theme.markOverlap` and `2 -> 0`
  never lost it. It now derives the overlap state from the same rule.
- `handleLinkEdit` (`packages/lexical-link/src/LexicalAutoLinkExtension.ts`)
  refreshed `rel` and `target` from the new match but never `title`, the third
  member of `LinkAttributes` that the creation path already applies. It also
  reported those attribute refreshes through `onChange`, which is documented as
  receiving `(url, prevUrl)`; attribute refreshes no longer fire it, while
  creation, removal and real url changes still do.
- `TOGGLE_LINK_COMMAND`'s object-payload branch in
  `packages/lexical-link/src/LexicalLinkExtension.ts` never called
  `validateUrl`, so a configured policy applied to
  `dispatchCommand(TOGGLE_LINK_COMMAND, url)` and was skipped for
  `dispatchCommand(TOGGLE_LINK_COMMAND, {url, target})`. The same gate now
  covers both shapes.
- `MentionNode.exportDOM`
  (`packages/lexical-playground/src/nodes/MentionNode.ts`) built a bare
  `<span data-lexical-mention>` from `this.__text`, dropping the node's inline
  style and text formats from the exported HTML. It now carries the style and
  wraps the span in the same `<b>/<i>/<s>/<u>` elements `TextNode.exportDOM`
  uses; the mention stays a `<span>` so the `span[data-lexical-mention]` import
  rule still matches (meta#6453).
- `SpecialTextNode.updateDOM`
  (`packages/lexical-playground/src/nodes/SpecialTextNode.tsx`) returned `false`
  but only wrote `dom.textContent` behind a `prevNode.__text.startsWith('[')`
  guard that cannot hold — the node is built from the regex capture group, with
  the brackets already stripped — so the chip kept the text it was created with
  forever. It now writes the text the way `createDOM` does, when it changed.
- `EquationNode.exportDOM`
  (`packages/lexical-playground/src/nodes/EquationNode.tsx`) base64-encoded
  free-form LaTeX with `btoa`, which throws `InvalidCharacterError` on any code
  point above U+00FF, so copying or exporting `\text{α}` failed outright; the
  importer's `atob` had the mirror-image limit. Both sides now go through the
  UTF-8 bytes with shared `encodeEquation`/`decodeEquation` helpers, the same
  approach `utils/docSerialization.ts` already uses. Pure ASCII encodes byte for
  byte, so previously exported HTML still decodes.
- `RubyNode.updateDOM`
  (`packages/lexical-playground/src/plugins/RubyExtension/RubyNode.ts`) handed
  `super.updateDOM` the wrapper element, while `createDOM` puts the text theme
  classes and the node's inline style on the *inner* element. The two disagreed
  about which element carries the formatting, so restyling a ruby annotation did
  nothing (the inner element's own `color` still won) and underline/strikethrough
  landed on a wrapper whose only child is an inline-block. It now resolves the
  inner element and passes it to super, and returns `true` when the inner is
  missing so the reconciler rebuilds instead.
- `$canShowPlaceholder` (`packages/lexical-text/src/canShowPlaceholder.ts`)
  indexed its innermost loop with the *outer* loop's `i` instead of `s`. `i` is
  always `0` there, so the scan tested `topBlockChildren[0]` N times and never
  looked past index 0 — the one thing it exists to do. A paragraph of
  `[TextNode(""), <inline DecoratorNode>]` reported that the placeholder may be
  shown, rendering it on top of real content.

## Test plan

Seven new unit test files and three extended ones, covering every fix:
`registerLexicalTextEntity.test.ts`, `Issue5466Repro.test.ts` and
`canShowPlaceholder.test.ts` in `packages/lexical-text`;
`LexicalMarkNode.test.ts` in `packages/lexical-mark`;
`LexicalAutoLinkExtension.test.ts` and `LinkExtensionValidateUrl.test.ts` in
`packages/lexical-link`; and `MentionHTML.test.ts`, `SpecialTextNode.test.ts`,
`EquationNodeHTML.test.ts` and `RubyNode.test.ts` in
`packages/lexical-playground`. Several cases are deliberate controls that pass
before and after — the `createDOM` reference the `updateDOM` cases are held to,
the legacy ASCII equation that must still decode, and the string-payload
`validateUrl` case that pins the asymmetry with the object payload.

### Before

Source fixes reverted, new tests kept:

```
$ npx vitest run --project unit packages/lexical-text packages/lexical-mark packages/lexical-link packages/lexical-playground

     × replaces a match that ends the node and does not start at offset 0 265ms
     × replaces the last of several matches in the same node 23ms
     × replaces every match when the first one starts at offset 0 53ms
     × exports the font-size applied to a mention (meta#6453) 258ms
     × exports the text formats applied to a mention (meta#6453) 90ms
     × updateDOM adds the overlap class when going from 1 to 3 ids 27ms
     × updateDOM removes the overlap class when going from 2 to 0 ids 22ms
     × preserves style and detail when reverting an entity to simple text 191ms
     × re-renders the text when it changes 98ms
     × renders the text verbatim when it is bracketed 5ms
     × applies a style change to the element createDOM styled 71ms
     × applies a class-only text format to the element createDOM classed 4ms
     × exports an equation with non Latin-1 characters without throwing 53ms
     × round trips an equation with non Latin-1 characters 4ms
     × round trips an equation outside the basic multilingual plane 1ms
     × rejects a URL the validator refuses, for an object payload 64ms
     × is false when a decorator follows an empty token text node 29ms
     × is false when a decorator follows an empty segmented text node 4ms
     × is false when the decorator is several positions along 1ms
       × editing the link text refreshes the title 28ms
       × an attribute refresh is not reported as a url change 3ms

 Test Files  10 failed | 27 passed (37)
      Tests  21 failed | 895 passed (916)
```

### After

```
$ npx vitest run --project unit packages/lexical-text packages/lexical-mark packages/lexical-link packages/lexical-playground

 Test Files  37 passed (37)
      Tests  916 passed (916)

$ npx tsc --noEmit -p .
(clean)
```

Supersedes meta#8919, meta#8969, meta#8982, meta#8987, meta#8998, meta#9014, meta#9019, meta#9032, meta#9036, meta#9046,
consolidated per the review feedback on meta#9027 and meta#9035.
@vercel

vercel Bot commented Aug 10, 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.

@vercel

vercel Bot commented Aug 10, 2026 •

Copy link
Copy Markdown

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

2 Skipped Deployments
Project Deployment Actions Updated (UTC)
lexical Ignored Ignored Aug 10, 2026 2:30pm
lexical-playground Ignored Ignored Aug 10, 2026 2:30pm

Request Review

@etrepum
etrepum added this pull request to the merge queue Aug 10, 2026
Merged via the queue into meta:main with commit 2929ef3 Aug 10, 2026
44 checks passed
@etrepum etrepum mentioned this pull request Aug 31, 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. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants