Bug: selection points to missing nodes after calling $deleteTableRow__EXPERIMENTAL() on all table rows
Calling $deleteTableRow__EXPERIMENTAL() when all table rows are selected produces an editor state (processed by update listeners) that has an invalid selection. The selection points to nodes that have been removed from the editor, so if you call $getSelection()?.getNodes() you will get an error Point.getNode: node not found.
Note: in the recording below the rendered HTML table doesn't show the selection, but the tree view below the editor shows the table cells are getting selected.
https://github.com/user-attachments/assets/30f574d1-1684-4672-a006-c814342307fc
Lexical version: 0.23.1
Steps To Reproduce
- Have an editor state with a
tableand at least one other sibling node before it (e.g.paragraph). - Have an update listener that calls
$getSelection?.getNodes()on every update. - Select all rows of the table, then take an action that runs
$deleteTableRow__EXPERIMENTAL(). - Observe the error
Point.getNode: node not foundis thrown.
Link to code example: https://stackblitz.com/edit/facebook-lexical-akmwfitx?file=src%2FApp.tsx
The current behavior
The error Point.getNode: node not found is thrown because the selection is pointing to nodes that have been removed from the document.
The expected behavior
The selection should be fixed up (pointing to valid nodes) before update listeners are called with new updates.
Impact of fix
The most severe impact of this bug exists when you also use CollaborationPlugin, as this error causes Lexical and the Yjs doc to get out of sync. The table deletion doesn't propagate to the Yjs doc, so until the page is refreshed there is risk that user edits are corrupting the document in this unexpected state, and upon refresh users will see the table re-appear unexpectedly.
As a workaround, the error can be caught and handled to avoid this problem.
I haven't fixed the problem but here are the relevant changes to the theme so you can see the selection properly
https://stackblitz.com/edit/facebook-lexical-akmwfitx?file=src%2Fstyles.css,src%2FExampleTheme.ts,index.html,src%2Fmain.tsx
Thanks! Have updated the link in the original description.