From 03be16991e58ccab26328d30533f765635646b2c Mon Sep 17 00:00:00 2001 From: Gerard Rovira Date: Thu, 30 Jan 2025 19:20:13 +0000 Subject: [PATCH] Fix ListItemNode serialization throws (#7116) --- .../lexical-list/src/LexicalListItemNode.ts | 2 +- .../unit/LexicalListItemNode.test.ts | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/lexical-list/src/LexicalListItemNode.ts b/packages/lexical-list/src/LexicalListItemNode.ts index 0e5153bc256..e459752256a 100644 --- a/packages/lexical-list/src/LexicalListItemNode.ts +++ b/packages/lexical-list/src/LexicalListItemNode.ts @@ -349,7 +349,7 @@ export class ListItemNode extends ElementNode { getIndent(): number { // If we don't have a parent, we are likely serializing const parent = this.getParent(); - if (parent === null) { + if (parent === null || !this.isAttached()) { return this.getLatest().__indent; } // ListItemNode should always have a ListNode for a parent. diff --git a/packages/lexical-list/src/__tests__/unit/LexicalListItemNode.test.ts b/packages/lexical-list/src/__tests__/unit/LexicalListItemNode.test.ts index d36b8f1cbd5..fb2575c98da 100644 --- a/packages/lexical-list/src/__tests__/unit/LexicalListItemNode.test.ts +++ b/packages/lexical-list/src/__tests__/unit/LexicalListItemNode.test.ts @@ -20,6 +20,7 @@ import { import { $createListItemNode, + $createListNode, $isListItemNode, ListItemNode, ListNode, @@ -1361,5 +1362,24 @@ describe('LexicalListItemNode tests', () => { }); }); }); + + test('Can serialize a node that is not attached', async () => { + const {editor} = testEnv; + await editor.update(() => { + const listItemNode = $createListItemNode(); + const listNode = $createListNode(); + listNode.append(listItemNode); + expect(listItemNode.exportJSON()).toEqual({ + checked: undefined, + children: [], + direction: null, + format: '', + indent: 0, + type: 'listitem', + value: 1, + version: 1, + }); + }); + }); }); });