Skip to content

[lexical-playground] Bug Fix: Prevent crash importing <details> with loose body content - #8442

Merged
etrepum merged 3 commits into
meta:mainfrom
markselby9:fix-8407-details-import-crash
May 1, 2026
Merged

etrepum merged 3 commits into
meta:mainfrom
markselby9:fix-8407-details-import-crash

Conversation

@markselby9

Copy link
Copy Markdown
Contributor

Description

Closes #8407.

The playground's <details> importer ($convertDetailsElement) returned a bare CollapsibleContainerNode. Because that node is a shadow root, lexical-html's traversal could leave loose text or non-summary elements as direct children, producing a malformed tree that crashed downstream rendering with Only element or decorator nodes can be inserted in to the root node.

The fix adds an after callback that reshapes the imported children into the canonical [CollapsibleTitleNode, CollapsibleContentNode] structure. Loose inline siblings inside the content are wrapped in a ParagraphNode since CollapsibleContentNode is also a shadow root. Round-trip imports of lexical-exported markup produce the same tree.

Scope is strictly the playground's CollapsibleExtension, per the maintainer's triage on the issue.

Test plan

  • New unit suite packages/lexical-playground/__tests__/unit/CollapsibleContainerNode.test.ts covers: loose text body, open attribute preservation, summary-after-content ordering, no-summary input, multi-paragraph body, round-trip shape.
  • Full unit suite (3004 tests) passes.
  • pnpm run prettier / pnpm run tsc / pnpm run lint all clean.

Before

Pasting the issue's HTML and switching back to wysiwyg crashed the editor with Only element or decorator nodes can be inserted in to the root node.

After

Imports as a working Details widget with the canonical structure:

```
root
└ collapsible-container
├ collapsible-title
│ └ text "Details"
└ collapsible-content
└ paragraph
└ text "Something small enough to escape casual notice."
```

No console errors; expanding the disclosure shows the body content.

…loose body content

The playground's <details> importer ($convertDetailsElement) returned
a bare CollapsibleContainerNode. Because that node is a shadow root,
lexical-html's traversal could leave loose text or non-summary
elements as direct children, producing a malformed tree that crashed
downstream rendering with "Only element or decorator nodes can be
inserted in to the root node".

Add an `after` callback that reshapes the imported children into the
canonical [CollapsibleTitleNode, CollapsibleContentNode] structure.
Loose inline siblings inside the content are wrapped in a
ParagraphNode since CollapsibleContentNode is also a shadow root.
Round-trip imports of lexical-exported markup produce the same tree.

Closes meta#8407.
@vercel

vercel Bot commented May 1, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview, Comment May 1, 2026 3:40pm
lexical-playground Ready Ready Preview, Comment May 1, 2026 3:40pm

Request Review

@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 May 1, 2026
Address review feedback on meta#8442:
- Test 1: assert exact child count (toBe(2) instead of >= 2).
- Test 4 (no <summary>): assert the post-transform shape — the empty
  CollapsibleTitleNode created for missing summary is removed by
  CollapsibleTitleNode's $transform, leaving only the
  CollapsibleContentNode under the container.

@etrepum etrepum left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The tests could be cleaned up quite a bit by using assert or just non-null assertions (!) instead of type casts. I only commented on the first few occurrences since this style was repeated throughout the tests.

Comment on lines +52 to +55
expect(container).toBeDefined();
const children = (
container as CollapsibleContainerNode
).getChildren();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
expect(container).toBeDefined();
const children = (
container as CollapsibleContainerNode
).getChildren();
assert(container != null, 'CollapsibleContainerNode must be a child of RootNode');
const children = container.getChildren();

Comment on lines +57 to +58
expect($isCollapsibleTitleNode(children[0])).toBe(true);
expect($isCollapsibleContentNode(children[1])).toBe(true);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
expect($isCollapsibleTitleNode(children[0])).toBe(true);
expect($isCollapsibleContentNode(children[1])).toBe(true);
assert($isCollapsibleTitleNode(children[0]), 'First child must be CollapsibleTitleNode');
assert($isCollapsibleContentNode(children[1]), 'Second child must be CollapsibleContentNode');

const root = $getRoot();
const container = root
.getChildren()
.find($isCollapsibleContainerNode) as CollapsibleContainerNode;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
.find($isCollapsibleContainerNode) as CollapsibleContainerNode;
.find($isCollapsibleContainerNode)!;

const root = $getRoot();
const container = root
.getChildren()
.find($isCollapsibleContainerNode) as CollapsibleContainerNode;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
.find($isCollapsibleContainerNode) as CollapsibleContainerNode;
.find($isCollapsibleContainerNode)!;

Comment on lines +67 to +68
// Lexical-exported markup wraps the body in a CollapsibleContentNode;
// unwrap so we can rebuild a single canonical content node.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We should just re-use this when present, rather than creating more nodes to garbage collect

- Reuse the existing CollapsibleContentNode from lexical-exported
  round-trip markup instead of creating a new one and discarding the
  imported node.
- Replace `as Type` test casts with vitest `assert(...)` and non-null
  assertions for cleaner narrowing.
@etrepum
etrepum added this pull request to the merge queue May 1, 2026
Merged via the queue into meta:main with commit 9787657 May 1, 2026
39 checks passed
@etrepum etrepum mentioned this pull request May 28, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 72e831f9 Deployed May 1, 2026 by vercel[bot]
Preview – lexical-playground — 72e831f9 Deployed May 1, 2026 by vercel[bot]
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.

Bug: Editor crashes after importing <details>

2 participants