Skip to content

[lexical-html] Bug Fix: apply the after() replacement when exportDOM returns a DocumentFragment - #8997

Merged
etrepum merged 1 commit into
meta:mainfrom
LeSingh1:fix/html-after-fragment
Aug 9, 2026
Merged

etrepum merged 1 commit into
meta:mainfrom
LeSingh1:fix/html-after-fragment

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Description

DOMExportOutput.after may return a replacement element, and
$appendNodesToHTML has a dedicated branch for the case where the exported
element is a DocumentFragment:

parentElementAppend(element);

if (after) {
  const newElement = after.call(target, element);
  if (newElement) {
    if (isDocumentFragment(element)) {
      element.replaceChildren(newElement);
    } else {
      element.replaceWith(newElement);
    }
  }
}

The two lines are in the wrong order for a fragment. parentElementAppend
runs first, and appending a DocumentFragment moves its children into the
parent and leaves the fragment empty and detached
. The subsequent
element.replaceChildren(newElement) therefore writes the replacement into a
fragment that is no longer connected to anything: the replacement is dropped
on the floor and the original children stay in the output. The after hook
silently does nothing.

The HTMLElement path is correct and depends on the current order —
replaceWith needs the element to already be in the tree — so the fix is to
split the two cases rather than move the call unconditionally: resolve after
before appending for a fragment, and leave the element path exactly as it
was.

Test plan

New packages/lexical-html/src/__tests__/unit/ExportAfterFragment.test.ts
exports two custom nodes whose after returns a replacement — one returning a
DocumentFragment as element, one returning an HTMLElement. The element
case is the control: it passes before and after, and is the behaviour the
fragment case is held to.

Before

$ npx vitest run packages/lexical-html/src/__tests__/unit/ExportAfterFragment.test.ts

     × applies the replacement when element is a DocumentFragment 15ms
     ✓ applies the replacement when element is an HTMLElement (control) 2ms

⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯
AssertionError: expected '<span style="white-space: pre-wrap;">…' to contain 'data-replaced="true"'
Expected: "data-replaced="true""
Received: "<span style="white-space: pre-wrap;">body</span>"

      Tests  1 failed | 1 passed (2)

After

$ npx vitest run packages/lexical-html packages/lexical-clipboard packages/lexical-table packages/lexical-playground packages/lexical/src

 Test Files  107 passed (107)
      Tests  1889 passed | 1 skipped (1890)

…returns a DocumentFragment

## Description

`DOMExportOutput.after` may return a replacement element, and
`$appendNodesToHTML` has a dedicated branch for the case where the exported
`element` is a `DocumentFragment`:

```ts
parentElementAppend(element);

if (after) {
  const newElement = after.call(target, element);
  if (newElement) {
    if (isDocumentFragment(element)) {
      element.replaceChildren(newElement);
    } else {
      element.replaceWith(newElement);
    }
  }
}
```

The two lines are in the wrong order for a fragment. `parentElementAppend`
runs first, and appending a `DocumentFragment` **moves its children into the
parent and leaves the fragment empty and detached**. The subsequent
`element.replaceChildren(newElement)` therefore writes the replacement into a
fragment that is no longer connected to anything: the replacement is dropped
on the floor and the original children stay in the output. The `after` hook
silently does nothing.

The `HTMLElement` path is correct and depends on the current order —
`replaceWith` needs the element to already be in the tree — so the fix is to
split the two cases rather than move the call unconditionally: resolve `after`
*before* appending for a fragment, and leave the element path exactly as it
was.

## Test plan

New `packages/lexical-html/src/__tests__/unit/ExportAfterFragment.test.ts`
exports two custom nodes whose `after` returns a replacement — one returning a
`DocumentFragment` as `element`, one returning an `HTMLElement`. The element
case is the control: it passes before and after, and is the behaviour the
fragment case is held to.

### Before

```
$ npx vitest run packages/lexical-html/src/__tests__/unit/ExportAfterFragment.test.ts

     × applies the replacement when element is a DocumentFragment 15ms
     ✓ applies the replacement when element is an HTMLElement (control) 2ms

⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯
AssertionError: expected '<span style="white-space: pre-wrap;">…' to contain 'data-replaced="true"'
Expected: "data-replaced="true""
Received: "<span style="white-space: pre-wrap;">body</span>"

      Tests  1 failed | 1 passed (2)
```

### After

```
$ npx vitest run packages/lexical-html packages/lexical-clipboard packages/lexical-table packages/lexical-playground packages/lexical/src

 Test Files  107 passed (107)
      Tests  1889 passed | 1 skipped (1890)
```
@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
@etrepum
etrepum added this pull request to the merge queue Aug 9, 2026
Merged via the queue into meta:main with commit 474b331 Aug 9, 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants