Skip to content

[lexical-html] Bug Fix: apply the slot frame redirect to a NodeSelection when exporting HTML - #9010

Closed
LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:html-slot-nodeselection
Closed

LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:html-slot-nodeselection

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Description

Slots are shadow-root isolated, so a selection wholly inside a slot subtree
never includes its host. A walk over the root's children therefore misses the
selected nodes and exports nothing. $generateDOMFromNodes handles that by
redirecting the walk through the selection's slot frame — but only for a
RangeSelection:

const slotFrame = $isRangeSelection(selection)
  ? $getSlotFrame(selection.anchor.getNode())
  : null;

Its sibling $generateJSONFromSelectedNodes in @lexical/clipboard handles
both selection types, and says why in a comment that applies verbatim here:

NodeSelection participates here too — a click that selects a decorator
nested in a slot needs the same frame redirect, otherwise its export
pipeline silently produces an empty clipboard.

The fix for #8712 (2f2bc97c1) only updated @lexical/clipboard;
$generateDOMFromNodes was never brought along. $isNodeSelection is already
imported in this file, so the omission is not a missing import.

The result is a split between the two clipboard channels: selecting a node
nested in a slot and copying puts a correct application/x-lexical-editor
payload on the clipboard and an empty text/html one, so pasting into any
other application yields nothing. The existing regression test for #8712
(SlotClipboardExport.test.ts) only asserts the JSON channel, which is why the
gap was not caught — the RangeSelection test beside it deliberately asserts
"on both channels".

This mirrors the clipboard implementation exactly, including anchoring on the
first selected node for a NodeSelection.

Test plan

New unit test packages/lexical-html/src/__tests__/unit/SlotNodeSelectionExport.test.ts.
The RangeSelection case is included as a control: it passes before and after,
which is what pins the asymmetry to the selection type.

Before

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

     × a NodeSelection inside a slot exports the node, not empty HTML 10ms

AssertionError: expected '' to contain 'SlotNodeTarget'

      Tests  1 failed | 1 passed (2)

After

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

 Test Files  1 passed (1)
      Tests  2 passed (2)

$ npx vitest run packages/lexical-html packages/lexical-clipboard

 Test Files  14 passed (14)
      Tests  114 passed (114)

…ion when exporting HTML

## Description

Slots are shadow-root isolated, so a selection wholly inside a slot subtree
never includes its host. A walk over the root's children therefore misses the
selected nodes and exports nothing. `$generateDOMFromNodes` handles that by
redirecting the walk through the selection's slot frame — but only for a
`RangeSelection`:

```js
const slotFrame = $isRangeSelection(selection)
  ? $getSlotFrame(selection.anchor.getNode())
  : null;
```

Its sibling `$generateJSONFromSelectedNodes` in `@lexical/clipboard` handles
both selection types, and says why in a comment that applies verbatim here:

> NodeSelection participates here too — a click that selects a decorator
> nested in a slot needs the same frame redirect, otherwise its export
> pipeline silently produces an empty clipboard.

The fix for meta#8712 (`2f2bc97c1`) only updated `@lexical/clipboard`;
`$generateDOMFromNodes` was never brought along. `$isNodeSelection` is already
imported in this file, so the omission is not a missing import.

The result is a split between the two clipboard channels: selecting a node
nested in a slot and copying puts a correct `application/x-lexical-editor`
payload on the clipboard and an **empty** `text/html` one, so pasting into any
other application yields nothing. The existing regression test for meta#8712
(`SlotClipboardExport.test.ts`) only asserts the JSON channel, which is why the
gap was not caught — the RangeSelection test beside it deliberately asserts
"on both channels".

This mirrors the clipboard implementation exactly, including anchoring on the
first selected node for a `NodeSelection`.

## Test plan

New unit test `packages/lexical-html/src/__tests__/unit/SlotNodeSelectionExport.test.ts`.
The RangeSelection case is included as a control: it passes before and after,
which is what pins the asymmetry to the selection type.

### Before

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

     × a NodeSelection inside a slot exports the node, not empty HTML 10ms

AssertionError: expected '' to contain 'SlotNodeTarget'

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

### After

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

 Test Files  1 passed (1)
      Tests  2 passed (2)

$ npx vitest run packages/lexical-html packages/lexical-clipboard

 Test Files  14 passed (14)
      Tests  114 passed (114)
```
@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
@LeSingh1

Copy link
Copy Markdown
Contributor Author

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

1 participant