lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Bug: First issues with named slots

Open levensta opened this issue 5 months ago • 4 comments

Lexical version: 0.45.0+dev

Description

I decided not to wait for the 0.46 release and immediately leave feedback on the slot issues I discovered at the playground. I discovered many of these bugs after this issue https://github.com/facebook/lexical/issues/8707 But I don’t deny that they may not reproduce artificially, but by users after the first integrations of named slots

Below I'll describe several bugs I found, mostly related to shadow root slots. All tests were conducted on Google Chrome 149.0 on MacOS Tahoe 26.4

Selected DecoratoreNode is not deleted via Backspace

Steps To Reproduce

  1. Insert a HorizontalRuleNode into the slot shadow-root-container
  2. Click on HorizontalRuleNode to select the node
  3. Press Backspace

Link to code example

The current behavior

The selected decorator is not deleted. However, if you press Ctrl + C, then Backspace, it will be deleted.

https://github.com/user-attachments/assets/a131a21b-b19a-4326-9b3d-0ed4aa228d96

The expected behavior

The selected decorator is deleted

Copying a DecoratoreNode from slot shadow-root to an external document

Steps To Reproduce

  1. Insert a HorizontalRuleNode into the slot shadow-root-container
  2. Click on HorizontalRuleNode to select the node
  3. Press Ctrl + C to copy
  4. Click to outside document
  5. Press Ctrl + V to paste

Link to code example

The current behavior

  • The node is not copied and therefore not pasted. The clipboard is empty
  • Ctrl + X deletes the selected node, but the clipboard is still empty.

https://github.com/user-attachments/assets/9703118d-94a2-405e-a18d-cc6196e4db40

btw, the text is copied correctly

The expected behavior

DecoratorNode is copied to the clipboard and successfully pasted outside the slot

Copying a decorator from outside and pasting it into a slot shadow-root in block cursor

Steps To Reproduce

  1. Insert a HorizontalRuleNode outside the slot shadow-root-container
  2. Click on the external HorizontalRuleNode to select the node
  3. Press Ctrl + C to copy
  4. Click to HorizontalRuleNode in slot
  5. Press ArrowLeft to show block cursor (!)
  6. Press Ctrl + V to paste

Link to code example

The current behavior

The decorator isn't inserted into a block cursor (there's no error with a regular cursor). An exception is thrown: Uncaught RangeError: Maximum call stack size exceeded

https://github.com/user-attachments/assets/7f9eea09-e378-4c66-99b4-5ba3cefb2720

The expected behavior

The decorator is correctly inserted into the block cursor inside the slot-root-container

SELECT_ALL_COMMAND in slot-root-container with decorator and text

Steps To Reproduce

  1. Insert a HorizontalRuleNode in the slot shadow-root-container
  2. Write the text before (!) the HorizontalRuleNode via block cursor
  3. Press Ctrl + A when caret at the text

Link to code example

The current behavior

The content inside the slot is not selected. An exception is thrown: Uncaught Error: Children of root nodes must be elements or decorators

(!) No error occurs if text was entered after the HorizontalRuleNode

https://github.com/user-attachments/assets/cbe2f492-1d80-46c8-a58c-d7afade560c8

The expected behavior

Correct selection occurs within the slot root

An empty slot element is removed if an empty paragraph is removed after it

Steps To Reproduce

  1. Insert any node with slot
  2. Create an empty paragraph after the node
  3. Press Backspace in a paragraph

Link to code example

The current behavior

If the node with slots is empty, it is deleted along with the paragraph

https://github.com/user-attachments/assets/5c27122d-817d-4e88-9a08-e5fb3fd901a5

The expected behavior

Only the paragraph is deleted, the caret moves to the previous node with the slot

levensta avatar Jun 17 '26 17:06 levensta

@mayrang fyi

levensta avatar Jun 17 '26 17:06 levensta

Opened #8716 for four of these. Quick summary:

  • Selected DecoratoreNode is not deleted via Backspace — KEY_BACKSPACE_COMMAND was passing through on a NodeSelection because the click target lives inside the decorator. Now skips the pass-through on NodeSelection. Same fix for forward-Delete.
  • Copying a DecoratoreNode from slot shadow-root to an external document — $generateJSONFromSelectedNodes only redirected through the slot frame for RangeSelection. Extended to NodeSelection.
  • Copying a decorator from outside and pasting it into a slot shadow-root in block cursor — RangeSelection.insertNodes slot-host redirect re-entered itself when the slot's first child was a non-element. Seeds a paragraph before the redirect.
  • SELECT_ALL_COMMAND in slot-root-container with decorator and text — fixed at two layers: the typing path that produced this shape (defensive guard symmetric to #8686), and a hydrate-time normalize that wraps any raw text in slot values parsed in from outside (URL doc, imported JSON, etc.) so existing invalid states like the URL in this issue also stop throwing.

The last one (An empty slot element is removed if an empty paragraph is removed after it) reads to me as intentional — registerSlotHostBackspace was designed for the empty-host case, and the deleteCharacter guard at LexicalSelection.ts:2004-2008 (a slot-bearing host is removed only as a unit by an explicit host deletion, never silently via backspace) only covers the non-empty case — so I left it as-is in this PR. But I'm not certain about the policy here; what's your read? Should the empty-host case also stay protected?

mayrang avatar Jun 17 '26 22:06 mayrang

The last one (An empty slot element is removed if an empty paragraph is removed after it) reads to me as intentional — registerSlotHostBackspace was designed for the empty-host case, and the deleteCharacter guard at LexicalSelection.ts:2004-2008 (a slot-bearing host is removed only as a unit by an explicit host deletion, never silently via backspace) only covers the non-empty case — so I left it as-is in this PR. But I'm not certain about the policy here; what's your read? Should the empty-host case also stay protected?

From a user's perspective, I think it would be more natural to see that pressing Backspace deletes only the current empty node where the cursor is positioned.

For example, if there were an empty heading before an empty paragraph, pressing Backspace on the paragraph should not delete the heading. Details such as the node type, whether it contains slots or not, and so on, are not that important to the end user of the editor

levensta avatar Jun 17 '26 22:06 levensta

Added that fix too — PR #8716 updated. Host stays, paragraph only is removed.

mayrang avatar Jun 18 '26 00:06 mayrang