|
6 | 6 | * |
7 | 7 | */ |
8 | 8 | import {$createLinkNode, $isLinkNode, LinkNode} from '@lexical/link'; |
9 | | -import {$createTextNode, $getRoot, ParagraphNode, TextNode} from 'lexical'; |
| 9 | +import { |
| 10 | + $createRangeSelection, |
| 11 | + $createTextNode, |
| 12 | + $getRoot, |
| 13 | + $isTextNode, |
| 14 | + $setSelection, |
| 15 | + DELETE_CHARACTER_COMMAND, |
| 16 | + ParagraphNode, |
| 17 | + TextNode, |
| 18 | +} from 'lexical'; |
10 | 19 | import { |
11 | 20 | expectHtmlToBeEqual, |
12 | 21 | html, |
13 | 22 | initializeUnitTest, |
| 23 | + invariant, |
14 | 24 | } from 'lexical/src/__tests__/utils'; |
15 | 25 | import {waitForReact} from 'packages/lexical-react/src/__tests__/unit/utils'; |
16 | 26 | import {describe, expect, test} from 'vitest'; |
@@ -424,6 +434,95 @@ describe('LexicalListNode tests', () => { |
424 | 434 | expect(bulletList.__listType).toBe('bullet'); |
425 | 435 | }); |
426 | 436 | }); |
| 437 | + |
| 438 | + test('Deleting selection across parent and nested list item should remove nested list', async () => { |
| 439 | + const {editor} = testEnv; |
| 440 | + await editor.update(() => { |
| 441 | + const root = $getRoot(); |
| 442 | + const mainList = $createListNode('number'); |
| 443 | + const parentItem = $createListItemNode(); |
| 444 | + parentItem.append($createTextNode('Parent Item')); |
| 445 | + |
| 446 | + const nestedList = $createListNode('number'); |
| 447 | + const nestedItem = $createListItemNode(); |
| 448 | + nestedItem.append($createTextNode('Child Item')); |
| 449 | + |
| 450 | + //build the tree |
| 451 | + nestedList.append(nestedItem); |
| 452 | + parentItem.append(nestedList); |
| 453 | + mainList.append(parentItem); |
| 454 | + root.append(mainList); |
| 455 | + }); |
| 456 | + |
| 457 | + await editor.update(() => { |
| 458 | + const root = $getRoot(); |
| 459 | + const mainList = root.getFirstChildOrThrow(); |
| 460 | + invariant($isListNode(mainList), 'Expected mainList to be a ListNode'); |
| 461 | + |
| 462 | + const parentItem = mainList.getFirstChildOrThrow(); |
| 463 | + invariant( |
| 464 | + $isListItemNode(parentItem), |
| 465 | + 'Expected parentItem to be a ListItemNode', |
| 466 | + ); |
| 467 | + |
| 468 | + // The text node inside Parent ("Parent") |
| 469 | + const parentText = parentItem.getFirstChildOrThrow(); |
| 470 | + |
| 471 | + // The nested list is the second child of parentItem |
| 472 | + const nestedList = parentItem.getLastChild(); |
| 473 | + invariant($isListNode(nestedList), 'Expected nested list'); |
| 474 | + |
| 475 | + const nestedItem = nestedList.getFirstChildOrThrow(); |
| 476 | + invariant( |
| 477 | + $isListItemNode(nestedItem), |
| 478 | + 'Expected nestedItem to be a ListItemNode', |
| 479 | + ); |
| 480 | + |
| 481 | + const childText = nestedItem.getFirstChildOrThrow(); |
| 482 | + invariant( |
| 483 | + $isTextNode(childText), |
| 484 | + 'Expected childText to be a TextNode', |
| 485 | + ); |
| 486 | + |
| 487 | + const selection = $createRangeSelection(); |
| 488 | + selection.anchor.set(parentText.getKey(), 0, 'text'); // Start of "Parent" |
| 489 | + selection.focus.set( |
| 490 | + childText.getKey(), |
| 491 | + childText.getTextContentSize(), |
| 492 | + 'text', |
| 493 | + ); // End of "Child" |
| 494 | + |
| 495 | + $setSelection(selection); |
| 496 | + |
| 497 | + editor.dispatchCommand(DELETE_CHARACTER_COMMAND, true); |
| 498 | + }); |
| 499 | + |
| 500 | + await editor.update(() => { |
| 501 | + const root = $getRoot(); |
| 502 | + const mainList = root.getFirstChildOrThrow(); |
| 503 | + invariant($isListNode(mainList), 'Expected mainList to be a ListNode'); |
| 504 | + |
| 505 | + // We expect the Parent Item to still exist (maybe empty), |
| 506 | + // but the Nested List inside it should be destroyed. |
| 507 | + const parentItem = mainList.getFirstChildOrThrow(); |
| 508 | + invariant( |
| 509 | + $isListItemNode(parentItem), |
| 510 | + 'Expected parentItem to be a ListItemNode', |
| 511 | + ); |
| 512 | + // Debugging helper: Print what remains |
| 513 | + // console.log(JSON.stringify(parentItem.exportJSON(), null, 2)); |
| 514 | + |
| 515 | + const children = parentItem.getChildren(); |
| 516 | + |
| 517 | + // If the bug exists, this will likely fail because the nested list is still there |
| 518 | + // or the structure is corrupted. |
| 519 | + const hasNestedList = children.some((node) => $isListNode(node)); |
| 520 | + expect(hasNestedList).toBe(false); |
| 521 | + |
| 522 | + // Optionally, check if the text is gone too |
| 523 | + expect(parentItem.getTextContent()).toBe(''); |
| 524 | + }); |
| 525 | + }); |
427 | 526 | }); |
428 | 527 | }); |
429 | 528 |
|
|
0 commit comments