Repository navigation
[lexical-html] Bug Fix: apply the after() replacement when exportDOM returns a DocumentFragment - #8997
Merged
Conversation
…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)
```
LeSingh1
requested review from
acywatson,
etrepum,
fantactuka,
ivailop7,
potatowagon and
zurfyx
as code owners
August 9, 2026 01:55
|
@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. |
etrepum
approved these changes
Aug 9, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
DOMExportOutput.aftermay return a replacement element, and$appendNodesToHTMLhas a dedicated branch for the case where the exportedelementis aDocumentFragment:The two lines are in the wrong order for a fragment.
parentElementAppendruns first, and appending a
DocumentFragmentmoves its children into theparent and leaves the fragment empty and detached. The subsequent
element.replaceChildren(newElement)therefore writes the replacement into afragment that is no longer connected to anything: the replacement is dropped
on the floor and the original children stay in the output. The
afterhooksilently does nothing.
The
HTMLElementpath is correct and depends on the current order —replaceWithneeds the element to already be in the tree — so the fix is tosplit the two cases rather than move the call unconditionally: resolve
afterbefore appending for a fragment, and leave the element path exactly as it
was.
Test plan
New
packages/lexical-html/src/__tests__/unit/ExportAfterFragment.test.tsexports two custom nodes whose
afterreturns a replacement — one returning aDocumentFragmentaselement, one returning anHTMLElement. The elementcase is the control: it passes before and after, and is the behaviour the
fragment case is held to.
Before
After