diff --git a/packages/lexical-list/src/__tests__/unit/formatList.test.ts b/packages/lexical-list/src/__tests__/unit/formatList.test.ts index 4f5915d98ab..37753354521 100644 --- a/packages/lexical-list/src/__tests__/unit/formatList.test.ts +++ b/packages/lexical-list/src/__tests__/unit/formatList.test.ts @@ -14,7 +14,7 @@ import { TableNode, TableRowNode, } from '@lexical/table'; -import {$getRoot} from 'lexical'; +import {$createParagraphNode, $createTextNode, $getRoot} from 'lexical'; import {initializeUnitTest} from 'lexical/src/__tests__/utils'; import {insertList} from '../../formatList'; @@ -42,6 +42,29 @@ describe('insertList', () => { }); }); + test('inserting in root selection with existing child', async () => { + const {editor} = testEnv; + + await editor.update(() => { + $getRoot().select(); + $getRoot().append( + $createParagraphNode().append($createTextNode('hello')), + ); + }); + + insertList(editor, 'number'); + + editor.read(() => { + const root = $getRoot(); + + expect(root.getChildrenSize()).toBe(1); + + const firstChild = root.getFirstChildOrThrow(); + + expect($isListNode(firstChild)).toBe(true); + }); + }); + test('inserting with empty shadow root selection', async () => { const {editor} = testEnv; diff --git a/packages/lexical-list/src/formatList.ts b/packages/lexical-list/src/formatList.ts index 7bd1096a5d1..e4e574a1b93 100644 --- a/packages/lexical-list/src/formatList.ts +++ b/packages/lexical-list/src/formatList.ts @@ -83,9 +83,7 @@ export function $insertList(listType: ListType): void { anchorNode.append(paragraph); nodes = paragraph.select().getNodes(); } - } - - if ($isSelectingEmptyListItem(anchorNode, nodes)) { + } else if ($isSelectingEmptyListItem(anchorNode, nodes)) { const list = $createListNode(listType); if ($isRootOrShadowRoot(anchorNodeParent)) {