Let me trace through what happens when:
- We have a DecoratorBlockNode followed by a ListNode containing a ListItemNode
- The cursor is at the start of the ListItemNode (e.g., offset 0 in the element)
- Backspace is pressed
The deleteCharacter(true) function is called.
The anchor is in the ListItemNode. When we go backward:
initialRange.anchor.origin would be the ListItemNode
- We iterate through
initialRange.iterNodeCarets('shadowRoot') going backwards
As we go backward from the ListItemNode:
- First we'd see ChildCarets going through ListItemNode -> ListNode
- Then we'd hit a SiblingCaret for the DecoratorBlockNode
Wait, the issue is that when the cursor is at offset 0 of a ListItemNode element:
- The initialRange starts from the ListItemNode and goes backward
- It goes through the ListItemNode (which may or may not be empty)
- It goes through the ListNode (parent of ListItemNode)
- Then it hits the DecoratorBlockNode as a sibling
The key question is: what is initialRange.anchor.origin in this case?
If the cursor is in a text node inside the ListItemNode at offset 0, then:
initialRange.anchor.origin would be the text node or the ListItemNode
Let me think about this more carefully. The issue states "delete the first list item" - so the scenario is:
- Cursor is at the beginning of the first list item (before any text)
- Backspace is pressed
In this case:
anchor.getNode() returns the ListItemNode (if element selection) or a text node
initialRange.anchor.origin would be the node where the anchor is
- As we iterate backward, we eventually find the DecoratorBlockNode as a sibling
When we find the decorator as a sibling:
$isDecoratorNode(caret.origin) is true
state.type depends on what happened before
The bug is in this condition:
} else if (
state.type === 'merge-next-block' &&
(caret.origin.isKeyboardSelectable() ||
!caret.origin.isInline()) &&
$isElementNode(initialRange.anchor.origin) &&
initialRange.anchor.origin.isEmpty()
) {
If this condition is NOT met, it falls into the else branch which removes the decorator:
} else {
// When the anchor is not an empty element then the
// adjacent decorator is removed
caret.origin.remove();
}
So the bug is that when the anchor is a ListItemNode inside a ListNode:
state.type might be 'merge-next-block' because the ListNode is a block element
- But
initialRange.anchor.origin is the ListItemNode, not the ListNode
- The ListItemNode might not be empty (it has text), so the condition fails
- OR the ListItemNode is empty but the ListNode is its parent, not the initial anchor
Wait, let me reconsider. The anchor is inside the ListItemNode. When going backward:
- First, we descend into the ListItemNode via ChildCaret
- If ListItemNode has a text node as first child, we might descend further
- Then we go up through the ListNode
- Then we hit the DecoratorBlockNode as a sibling
The issue is that when the ListItemNode is the first child of the ListNode and the cursor is at its start:
state becomes 'merge-next-block' when we encounter the ListNode going backward
- Then when we find the DecoratorBlockNode, we check if
initialRange.anchor.origin.isEmpty()
initialRange.anchor.origin is likely the text node or the ListItemNode
Actually, let me re-read the code more carefully. The issue might be different.
When backspace is pressed at the start of a list item:
- The anchor is at position 0 in the list item
initialCaret = $caretFromPoint(anchor, 'previous') - this gets a caret pointing backward
initialRange = $extendCaretToRange(initialCaret) - extends the caret to a range
As we iterate through initialRange.iterNodeCarets('shadowRoot'):
- We traverse from the anchor backward
- When we hit a sibling that's a block element (ListNode),
state becomes {block: listNode, type: 'merge-next-block'}
- When we then hit the DecoratorBlockNode as a sibling:
$isDecoratorNode(caret.origin) is true
state.type === 'merge-next-block' is true
!caret.origin.isInline() is true (DecoratorBlockNode is not inline)
$isElementNode(initialRange.anchor.origin) - this depends on what the anchor is
The key question: what is initialRange.anchor.origin?
If the anchor is in a text node, then initialRange.anchor.origin would be the text node, which is NOT an ElementNode. So the condition $isElementNode(initialRange.anchor.origin) would be false, and we'd fall into the else branch that removes the decorator!
Wait, but that's not right either. Let me think again...
Actually, I think the issue is this: when the cursor is at the start of a ListItemNode, going backward:
- We iterate and find the ListNode as a block sibling (state becomes 'merge-next-block')
- We continue and find the DecoratorBlockNode
The problem is: initialRange.anchor.origin is the element where the selection starts. If the selection is at position 0 of a text node inside a ListItemNode:
initialRange.anchor.origin might be the text node
$isElementNode(initialRange.anchor.origin) would be false
- So we fall into the else branch and remove the decorator
OR if the anchor is an element selection at position 0 of the ListItemNode:
initialRange.anchor.origin is the ListItemNode
$isElementNode(initialRange.anchor.origin) is true
- But
initialRange.anchor.origin.isEmpty() depends on whether the ListItemNode is empty
Actually wait, the bug description says "Delete the first list item" - this implies the list item might not be empty. If it has content, then isEmpty() returns false, and we fall into the else branch that removes the decorator.
Hmm, but that doesn't make sense from a user perspective. If the list item has content, pressing backspace at its start shouldn't delete the previous block.
Let me re-read the code...
Oh! I see the issue now. When state.type === 'merge-next-block' and we encounter a decorator:
} else if (
state.type === 'merge-next-block' &&
(caret.origin.isKeyboardSelectable() ||
!caret.origin.isInline()) &&
$isElementNode(initialRange.anchor.origin) &&
initialRange.anchor.origin.isEmpty()
) {
// If the anchor is an empty element that is adjacent to a
// decorator then we remove the paragraph and select the
// decorator
initialRange.anchor.origin.remove();
...
} else {
// When the anchor is not an empty element then the
// adjacent decorator is removed
caret.origin.remove();
}
The comment says "When the anchor is not an empty element then the adjacent decorator is removed". This is wrong behavior when the "anchor" is actually a ListItemNode inside a ListNode.
The issue is:
- User is at the start of a ListItemNode (first item in a list)
- User presses backspace
- The code finds that the previous sibling is a DecoratorBlockNode
- The code removes the DecoratorBlockNode
But this is wrong! The expected behavior should be:
- If the ListItemNode is the first (and possibly only) item in a ListNode
- And the user presses backspace at the start of the ListItemNode
- The ListNode should be replaced with a ParagraphNode (or the content should just move out)
- The DecoratorBlockNode should NOT be deleted
The problem is that the code doesn't account for the ListNode wrapper. When going backward from a ListItemNode, the code sees the DecoratorBlockNode as a sibling of the ListNode, but the deletion logic doesn't properly handle the case where the anchor is nested inside a ListNode.
Let me look more carefully at the iteration. When we go backward from a ListItemNode:
- First we'd see ChildCarets inside the ListItemNode
- Then we go to the parent ListNode
- The ListItemNode is the first child, so we need to go to the previous sibling of the ListNode
- The previous sibling is the DecoratorBlockNode
So when $isSiblingCaret(caret) and caret.origin is the DecoratorBlockNode:
state.type could be 'initial' or 'merge-next-block' depending on what happened in between
Actually, I think I need to trace through this more carefully. Let me look at how $caretFromPoint and $extendCaretToRange work.
Actually, let me just look at what happens when the ListItemNode IS empty (which is probably the common case mentioned in the bug - user creates a list, then tries to delete it).
If the ListItemNode is empty and the anchor is the ListItemNode itself (element selection at offset 0):
initialRange.anchor.origin = ListItemNode
- Going backward from ListItemNode:
- We hit the ListNode as a ChildCaret
- Since ListNode is not inline and not shadow root, and state is 'initial', we keep going
- Wait, no. Let me re-read the code.
for (const caret of initialRange.iterNodeCarets('shadowRoot')) {
if ($isChildCaret(caret)) {
if (caret.origin.isInline()) {
// fall through when descending an inline
} else if (caret.origin.isShadowRoot()) {
// ...
return;
} else if (
state.type === 'merge-next-block' ||
state.type === 'merge-block'
) {
// Keep descending ChildCaret to find which block to merge with
state = {block: state.block, caret, type: 'merge-block'};
}
} else if (state.type === 'merge-block') {
break;
} else if ($isSiblingCaret(caret)) {
// Handle sibling
}
}
Hmm, when state is 'initial' and we see a ChildCaret for a non-inline, non-shadow-root element, we just continue (no state change). This means we're going UP through the parent elements.
Then when we hit a SiblingCaret (going from ListNode to its previous sibling, the DecoratorBlockNode):
state.type is still 'initial'
$isElementNode(caret.origin) - is DecoratorBlockNode an ElementNode? No, it's a DecoratorNode!
- So we check
$isDecoratorNode(caret.origin) - yes, this is true!
caret.origin.isIsolated() - DecoratorBlockNode is not isolated by default
state.type === 'merge-next-block' - NO! state is 'initial'!
- So we fall into the else branch:
caret.origin.remove()
THIS IS THE BUG! When state.type === 'initial' and we encounter a DecoratorBlockNode, we just remove it!
The fix should be: when state.type === 'initial' and we encounter a non-isolated DecoratorBlockNode, we should NOT remove it. We should instead let the normal merge/delete logic handle the ListItemNode/ListNode.
Actually wait, let me reconsider. When the anchor is at the start of a ListItemNode and we're going backward:
If the ListItemNode is empty:
- anchor is the ListItemNode at offset 0
- Going backward, we go up through ListNode
- Then we hit the DecoratorBlockNode as a sibling of ListNode
If the ListItemNode has text:
- anchor is a text node at offset 0 (or ListItemNode at offset 0 if no text)
- Going backward, we might first see the text, then the ListItemNode, then the ListNode
- Then we hit the DecoratorBlockNode
In both cases, when we hit the DecoratorBlockNode, state is 'initial' because we haven't seen a non-inline sibling element yet (we went up through parents).
So the fix should handle the case where state.type === 'initial' and we encounter a DecoratorBlockNode that's a sibling of a ListNode.
Wait, but actually the iteration goes from the anchor outward. Let me think about the order:
If the anchor is inside a ListItemNode which is inside a ListNode:
- We might first see child carets going from the anchor to its parent
- Then sibling carets at various levels
- The DecoratorBlockNode is a sibling of the ListNode (not the ListItemNode)
So the order would be:
- ChildCaret: anchor -> ListItemNode (going up)
- ChildCaret: ListItemNode -> ListNode (going up)
- SiblingCaret: previous sibling of ListNode = DecoratorBlockNode
At step 1 and 2, state remains 'initial' because we're going up through non-inline, non-shadow-root elements.
At step 3, we hit the DecoratorBlockNode and state is still 'initial'.
Now, the question is: what should happen?
The current behavior is:
- state is 'initial', so
state.type === 'merge-next-block' is false
- We fall into the else branch and remove the DecoratorBlockNode
The expected behavior is:
- The DecoratorBlockNode should NOT be removed
- Instead, the ListNode (or ListItemNode) should be handled properly (e.g., replaced with a ParagraphNode)
I think the fix should be: when state.type === 'initial' and we encounter a non-isolated DecoratorBlockNode, we should NOT remove it. Instead, we should break out of the loop and let the code continue to handle the normal deletion (which would be handled by the later code or by the merge-block case).
Actually, let me look at what happens if we just break instead of removing the decorator:
After the loop, if state.type === 'initial', the code doesn't enter the if (state.type === 'merge-block') block. Then it continues to:
// Handle the deletion around decorators.
const focus = this.focus;
this.modify('extend', isBackward, 'character');
Hmm, this might not handle the case properly either.
Let me think about this differently. The issue is that when the cursor is at the start of a ListItemNode inside a ListNode:
- Pressing backspace should NOT delete the previous DecoratorBlockNode
- It should either:
a. Delete the text/content of the ListItemNode
b. If the ListItemNode is empty and it's the only item, convert the ListNode to a ParagraphNode
c. If the ListItemNode has siblings, merge with them or remove it
The current code is incorrectly deleting the DecoratorBlockNode because it doesn't recognize that the anchor is nested inside a ListNode.
Looking at the code again:
} else if ($isDecoratorNode(caret.origin)) {
if (caret.origin.isIsolated()) {
// do nothing, shouldn't delete an isolated decorator
} else if (
state.type === 'merge-next-block' &&
(caret.origin.isKeyboardSelectable() ||
!caret.origin.isInline()) &&
$isElementNode(initialRange.anchor.origin) &&
initialRange.anchor.origin.isEmpty()
) {
// If the anchor is an empty element that is adjacent to a
// decorator then we remove the paragraph and select the
// decorator
initialRange.anchor.origin.remove();
const nodeSelection = $createNodeSelection();
nodeSelection.add(caret.origin.getKey());
$setSelection(nodeSelection);
} else {
// When the anchor is not an empty element then the
// adjacent decorator is removed
caret.origin.remove();
}
// always stop when a decorator is encountered
return;
}
The bug is in the else branch. When state.type === 'initial', it means we're at the start of a block and we haven't found any block to merge with yet. In this case, removing the decorator is wrong because:
- The anchor might be nested inside a ListNode (or other container)
- The decorator is not adjacent to the anchor - it's adjacent to the ListNode
The fix should be: when state.type === 'initial' and we encounter a non-isolated, non-inline DecoratorBlockNode, we should NOT remove it. Instead, we should just return without doing anything (or break to let other code handle it).
Actually, I think the issue is that when the anchor is nested (e.g., inside a ListItemNode -> ListNode), the code should recognize that the decorator is not truly "adjacent" to the anchor in a way that warrants deletion.
Let me look at a simpler fix: when state.type === 'initial' and the previous block (where the decorator lives) is not at the same nesting level as the anchor's top-level block, we should not delete the decorator.
Actually, the simplest fix might be: when state.type === 'initial' and we encounter a decorator, just break or return without removing anything. The normal deletion flow will handle the current block correctly.
But wait, that might break the case where an empty paragraph is adjacent to a decorator and pressing backspace should select the decorator.
Let me look at the condition more carefully:
} else if (
state.type === 'merge-next-block' &&
(caret.origin.isKeyboardSelectable() ||
!caret.origin.isInline()) &&
$isElementNode(initialRange.anchor.origin) &&
initialRange.anchor.origin.isEmpty()
) {
This condition checks:
state.type === 'merge-next-block' - we've found a previous block to merge with
- The decorator is keyboard selectable or not inline
- The anchor origin is an ElementNode
- The anchor origin is empty
If all these are true, it removes the empty paragraph and selects the decorator.
If any of these are false (including when state is 'initial'), it falls to the else branch and removes the decorator. This is wrong!
The fix should be: when state.type === 'initial', we should NOT enter this decorator removal logic at all. We should just break or return without removing anything.
Wait, but the comment says "When the anchor is not an empty element then the adjacent decorator is removed". This suggests the intent is that when you press backspace at the start of a non-empty block, it should delete the previous decorator. But this doesn't make sense when the block is inside a ListNode.
I think the correct fix is:
When state.type === 'initial' and we encounter a decorator:
- Don't remove the decorator
- Just return (stop processing)
- Let the normal deletion flow handle the anchor block
This way, if the anchor is an empty ListItemNode inside a ListNode, the normal flow will handle it (e.g., by removing the ListItemNode or converting the ListNode to a ParagraphNode).
Let me check what the normal flow does after the loop when state is 'initial'...
After the for loop, if state is 'initial':
if (state.type === 'merge-block') - not entered
- The code continues to:
const focus = this.focus;
this.modify('extend', isBackward, 'character');
Hmm, this modifies the selection to extend backward. Then if it's not collapsed, it calls removeText(). But if it's still collapsed (because there's no text to extend to), it might not do anything useful.
Let me look at the rest of the function to see what happens...
Actually, I think I need to understand the flow better. Let me look at what happens when you press backspace at the start of an empty paragraph:
deleteCharacter(true) is called
- Selection is collapsed, anchor is the empty ParagraphNode
- We iterate through carets going backward
- We find a sibling block (maybe another ParagraphNode or a decorator)
- If it's a paragraph,
state becomes 'merge-next-block'
- If it's a decorator, we handle it specially
The key insight is: when the anchor is an empty paragraph and the previous sibling is a decorator, the current behavior is to select the decorator. This makes sense because there's nothing to merge with.
But when the anchor is a ListItemNode inside a ListNode:
- The previous sibling of the ListNode is the decorator
- The ListItemNode itself might not be empty, or even if it is, removing it shouldn't delete the decorator
- The correct behavior would be to either:
a. Convert the ListNode to a ParagraphNode
b. Or let the normal paragraph-merging behavior handle it
I think the issue is that the code treats the decorator as if it's adjacent to the anchor, when in reality it's separated by the ListNode wrapper.
The fix should be: when iterating backward and we encounter a decorator, check if the decorator is a sibling of a parent of the anchor (i.e., nested inside a container like ListNode). If so, don't remove the decorator.
Actually, I think a simpler fix is: when state.type === 'initial', just don't remove the decorator. Instead, break out of the loop and let other code handle the deletion. The decorator removal should only happen when we've actually found a block to merge with (state.type === 'merge-next-block').
Let me look at the fix more carefully. The condition should be:
} else if ($isDecoratorNode(caret.origin)) {
if (caret.origin.isIsolated()) {
// do nothing, shouldn't delete an isolated decorator
} else if (
state.type === 'merge-next-block' &&
(caret.origin.isKeyboardSelectable() ||
!caret.origin.isInline()) &&
$isElementNode(initialRange.anchor.origin) &&
initialRange.anchor.origin.isEmpty()
) {
// If the anchor is an empty element that is adjacent to a
// decorator then we remove the paragraph and select the
// decorator
initialRange.anchor.origin.remove();
const nodeSelection = $createNodeSelection();
nodeSelection.add(caret.origin.getKey());
$setSelection(nodeSelection);
} else if (state.type === 'initial') {
// Don't delete the decorator when we haven't found a block to merge with.
// The anchor might be nested inside a container (like ListNode).
// Just stop here and let the normal deletion flow handle the anchor.
return;
} else {
// When the anchor is not an empty element then the
// adjacent decorator is removed
caret.origin.remove();
}
return;
}
Wait, but this changes the behavior for cases where state.type === 'merge-next-block' but other conditions fail. Let me think about when that could happen...
If state.type === 'merge-next-block' and:
- The decorator is inline (not keyboard selectable and isInline())
- OR the anchor origin is not an ElementNode (e.g., it's a TextNode)
- OR the anchor origin is not empty
In these cases, the current code removes the decorator. Is that correct?
I think the issue is specifically when state.type === 'initial'. In that case, we haven't found any block to merge with yet, and removing the decorator is definitely wrong.
Let me refine the fix:
} else if ($isDecoratorNode(caret.origin)) {
if (caret.origin.isIsolated()) {
// do nothing, shouldn't delete an isolated decorator
return;
} else if (
state.type === 'merge-next-block' &&
(caret.origin.isKeyboardSelectable() ||
!caret.origin.isInline()) &&
$isElementNode(initialRange.anchor.origin) &&
initialRange.anchor.origin.isEmpty()
) {
// If the anchor is an empty element that is adjacent to a
// decorator then we remove the paragraph and select the
// decorator
initialRange.anchor.origin.remove();
const nodeSelection = $createNodeSelection();
nodeSelection.add(caret.origin.getKey());
$setSelection(nodeSelection);
} else if (state.type === 'initial') {
// Don't delete the decorator when we haven't found a block to merge with.
// The anchor might be nested inside a container (like ListNode).
// Just stop here and let the normal deletion flow handle the anchor.
return;
} else {
// When the anchor is not an empty element then the
// adjacent decorator is removed
caret.origin.remove();
}
return;
}
Actually wait, there's another case to consider. What if state.type === 'merge-next-block' but initialRange.anchor.origin is a ListItemNode inside a ListNode? In that case, removing the decorator is still wrong because the decorator is not adjacent to the ListItemNode - it's adjacent to the ListNode.
So the fix should also check: when the anchor is nested inside a container that has the decorator as a sibling, don't remove the decorator.
Hmm, this is getting complex. Let me think about a different approach.
The root cause is that when iterating backward from the anchor, we go UP through parent elements before finding siblings. The decorator is a sibling of the ListNode, not the ListItemNode. But the code treats it as if it's adjacent to the anchor.
One way to fix this is to check: when we find a decorator as a sibling, is the decorator actually a sibling of a parent of the anchor? If so, we should not remove the decorator because the deletion is happening inside a container.
Actually, let me re-think this. The iteration goes:
- Start at the anchor
- Go backward through carets
- When we hit a sibling caret, the sibling is at the same level as the current node
So if the anchor is inside ListItemNode -> ListNode:
- We go up through ListItemNode (child caret)
- We go up through ListNode (child caret)
- We hit the decorator as a sibling of ListNode
At step 3, caret.origin is the decorator, and it's a sibling of the ListNode. The state at this point is 'initial' because we haven't found any block to merge with yet (we've only gone up through parents).
Wait, actually I need to reconsider. The iteration doesn't go "up" - it goes "back" in document order. Let me look at how iterNodeCarets works.
Actually, the iteration is about traversing from the anchor backward in the document. It goes through:
- ChildCarets: going from a node to its children
- SiblingCarets: going from a node to its previous sibling
But the direction matters. If we're going backward:
- We first go to the previous sibling of the anchor's node (if any)
- If there's no previous sibling, we go to the parent and then to the parent's previous sibling
- And so on
So if the anchor is at position 0 inside a ListItemNode:
- There's no previous sibling of the text inside the ListItemNode at position 0
- We go up to the ListItemNode
- The ListItemNode is the first child of the ListNode, so no previous sibling
- We go up to the ListNode
- The ListNode has a previous sibling: the DecoratorBlockNode
So when we find the DecoratorBlockNode as a sibling caret:
caret.direction is 'previous'
caret.origin is the DecoratorBlockNode
- The parent of the caret is the root
And state at this point is 'initial' because we haven't found any block to merge with.
OK so my fix is correct: when state.type === 'initial' and we encounter a non-isolated decorator, we should NOT remove it. Just return without doing anything.
But wait, what happens after we return? The code continues to:
// Handle the deletion around decorators.
const focus = this.focus;
this.modify('extend', isBackward, 'character');
if (!this.isCollapsed()) {
// ...
} else if (isBackward && anchor.offset === 0) {
// Special handling around rich text nodes
if ($collapseAtStart(this, anchor.getNode())) {
return;
}
}
Hmm, this might not properly handle the case where the ListItemNode is empty. Let me think...
If the ListItemNode is empty and the anchor is at offset 0:
- We iterate backward and find the DecoratorBlockNode
- We return without removing the decorator
- We continue to
modify('extend', 'backward', 'character')
- But there's no character to extend to, so the selection remains collapsed
isBackward && anchor.offset === 0 is true
$collapseAtStart is called
Hmm, I'm not sure what $collapseAtStart does in this case. Let me check.
Actually, I think the issue might be more nuanced. Let me re-read the deleteCharacter function to see what happens in each case.
OK let me just implement the fix and test it. The fix is:
When state.type === 'initial' and we encounter a DecoratorNode (that's not isolated), we should NOT remove it. Instead, we should just return to let the normal deletion flow handle the anchor block.
Actually, looking at the code more carefully, I realize there might be another issue. Let me check if the iteration order is correct.
When we call initialRange.iterNodeCarets('shadowRoot'):
- This iterates from the anchor backward to the range's end
- Since
initialCaret = $caretFromPoint(anchor, 'previous'), the caret points backward
$extendCaretToRange(initialCaret) extends this caret to a range
The iteration would go:
- Starting from the anchor point
- Going backward through the document structure
- Eventually hitting the DecoratorBlockNode as a sibling of the ListNode
OK, I'm fairly confident in my analysis now. Let me implement the fix.
The fix is in /lexical/packages/lexical/src/LexicalSelection.ts around line 1830. I need to add a check for when state.type === 'initial' and we encounter a DecoratorNode:
} else if ($isDecoratorNode(caret.origin)) {
if (caret.origin.isIsolated()) {
// do nothing, shouldn't delete an isolated decorator
} else if (
state.type === 'merge-next-block' &&
...
) {
// ...
} else if (state.type === 'initial') {
// Don't delete the decorator when we haven't found a block to merge with.
// The anchor might be nested inside a container (like ListNode).
// Just stop here and let the normal deletion flow handle the anchor.
return;
} else {
// When the anchor is not an empty element then the
// adjacent decorator is removed
caret.origin.remove();
}
// always stop when a decorator is encountered
return;
}
Wait, but there's a subtlety. If state.type === 'merge-next-block' and other conditions fail, we still fall into the else branch and remove the decorator. Is that correct?
For example, if:
state.type === 'merge-next-block' (we found a previous block)
- The decorator is inline
- The anchor is not empty
In this case, removing the decorator might be correct because:
- The user is at the start of a non-empty block
- The previous element is an inline decorator
- Backspace should delete the inline decorator
But wait, the issue is specifically about DecoratorBlockNode which is NOT inline. Let me re-check:
(caret.origin.isKeyboardSelectable() || !caret.origin.isInline())
This means: keyboard selectable OR not inline. For DecoratorBlockNode:
isInline() returns false (it's a block)
- So
!caret.origin.isInline() is true
- The condition is satisfied
So the issue is when:
state.type === 'merge-next-block'
- Decorator is not inline (condition passes)
- But the anchor is not an ElementNode, OR the anchor is not empty
If the anchor is a TextNode:
$isElementNode(initialRange.anchor.origin) is false
- We fall into the else branch and remove the decorator
If the anchor is an ElementNode but not empty:
$isElementNode(initialRange.anchor.origin) is true
initialRange.anchor.origin.isEmpty() is false
- We fall into the else branch and remove the decorator
In both cases, removing the decorator seems wrong if the anchor is nested inside a ListNode.
Actually, I think the issue is different. Let me re-think.
The key condition is state.type === 'merge-next-block'. When is this set?
if ($isElementNode(caret.origin)) {
if (!caret.origin.isInline()) {
state = {block: caret.origin, type: 'merge-next-block'};
}
...
}
So state.type becomes 'merge-next-block' when we find a non-inline ElementNode as a sibling. This happens BEFORE we reach the decorator.
Wait, that means the iteration would go:
- Start at anchor
- Go backward
- Find some non-inline ElementNode (like a ParagraphNode) as a sibling -> state becomes 'merge-next-block'
- Continue and find the DecoratorBlockNode as another sibling
But that doesn't match our scenario. In our scenario:
- DecoratorBlockNode
- ListNode -> ListItemNode (with cursor at start)
So the order going backward from the ListItemNode is:
- Go up through ListItemNode
- Go up through ListNode
- Find DecoratorBlockNode as sibling of ListNode
There's no intermediate ElementNode before the DecoratorBlockNode. So state.type is still 'initial' when we reach the decorator.
OK so my fix is correct for this scenario. When state.type === 'initial' and we encounter a DecoratorBlockNode, we should NOT remove it.
But what about the case where there IS a block before the decorator? Like:
- ParagraphNode
- DecoratorBlockNode
- ListNode -> ListItemNode
Going backward from ListItemNode:
- Go up through ListItemNode
- Go up through ListNode
- Find DecoratorBlockNode as sibling -> state is still 'initial'
In this case, we should still not remove the decorator because it's not directly adjacent to the anchor.
What if the structure is:
- DecoratorBlockNode
- ParagraphNode
- ListNode -> ListItemNode
Going backward from ListItemNode:
- Go up through ListItemNode
- Go up through ListNode
- Find ParagraphNode as sibling -> state becomes 'merge-next-block'
- Find DecoratorBlockNode as sibling
Hmm, in this case, would the ParagraphNode be processed before the DecoratorBlockNode? I think the iteration would process siblings in order, so:
- ParagraphNode -> state = 'merge-next-block'
- DecoratorBlockNode -> check the conditions
At step 2, state is 'merge-next-block', so the conditions are:
state.type === 'merge-next-block' -> true
!caret.origin.isInline() -> true
$isElementNode(initialRange.anchor.origin) -> depends on anchor
initialRange.anchor.origin.isEmpty() -> depends on anchor
If the anchor is a non-empty text node:
$isElementNode(initialRange.anchor.origin) is false
- We fall into the else branch and remove the decorator
But in this case, removing the decorator might actually be correct! The user pressed backspace at the start of a non-empty block, and there's a decorator before it (separated by a ParagraphNode).
Hmm, but the ParagraphNode is between the decorator and the ListNode. Shouldn't the ParagraphNode be processed first?
Actually, I think the iteration order is from the anchor outward. So if the anchor is inside ListItemNode -> ListNode:
- First, we go up through the parents
- Then we find siblings in order (ParagraphNode, then DecoratorBlockNode)
But the ParagraphNode is a sibling of the ListNode, so it would be encountered first. And since it's a non-inline ElementNode, state becomes 'merge-next-block'.
Wait, but actually, when state becomes 'merge-next-block', the code continues to iterate:
if ($isElementNode(caret.origin)) {
if (!caret.origin.isInline()) {
state = {block: caret.origin, type: 'merge-next-block'};
} else if (!caret.origin.isParentOf(initialRange.anchor.origin)) {
break;
}
continue;
}
Notice the continue at the end! So after setting state to 'merge-next-block', the loop continues and finds the DecoratorBlockNode next.
OK so in the scenario where ParagraphNode is between DecoratorBlockNode and ListNode:
- ParagraphNode -> state = 'merge-next-block', continue
- DecoratorBlockNode -> check conditions
At step 2:
state.type === 'merge-next-block' -> true
!caret.origin.isInline() -> true (DecoratorBlockNode is not inline)
$isElementNode(initialRange.anchor.origin) -> depends
initialRange.anchor.origin.isEmpty() -> depends
If the anchor is a text node, $isElementNode is false, and we remove the decorator. But is this correct?
I think the answer is: it depends on the user's intent. If the user is at the start of a non-empty block and presses backspace, should it delete the previous decorator? I think that's a reasonable expectation.
But the bug reported is specifically about the case where there's NO intermediate block between the decorator and the ListNode. In that case, the decorator is adjacent to the ListNode, and removing it would be wrong.
OK let me focus on the specific bug. The fix is:
When state.type === 'initial' and we encounter a DecoratorNode:
- Don't remove the decorator
- Just return
This handles the case where the decorator is directly adjacent to a container (like ListNode) that wraps the anchor.
Let me also consider: what if state.type === 'merge-next-block' but the decorator is adjacent to a container? For example:
- ParagraphNode
- DecoratorBlockNode
- ListNode -> ListItemNode
In this case, the ParagraphNode is adjacent to the ListNode, and the DecoratorBlockNode is between them. When we iterate:
- ParagraphNode -> state = 'merge-next-block'
- DecoratorBlockNode -> check conditions
But wait, would we even reach the DecoratorBlockNode? The ParagraphNode is the sibling of the ListNode. When we go up from ListItemNode to ListNode, the sibling of ListNode going backward would be... well, it depends on the structure.
If the structure is:
Root
├── ParagraphNode
├── DecoratorBlockNode
└── ListNode
└── ListItemNode (cursor here)
Going backward from ListItemNode:
- Go up through ListItemNode (child caret)
- Go up through ListNode (child caret)
- Find DecoratorBlockNode as sibling (sibling caret) -> state is 'initial'
- Find ParagraphNode as sibling (sibling caret) -> state is 'initial' (wait, this is wrong)
Hmm, I think the iteration order goes from the anchor backward. So at step 3, we first find DecoratorBlockNode (which is the immediate previous sibling of ListNode), then ParagraphNode.
So:
3. DecoratorBlockNode -> state is 'initial', $isDecoratorNode is true
- If it's not isolated and state is 'initial', we should NOT remove it
- We return without removing it
With my fix, the decorator would not be removed. Then what happens?
The code continues to:
const focus = this.focus;
this.modify('extend', isBackward, 'character');
This modifies the selection to extend backward. Since the decorator is not inline and we're at the start of the ListItemNode, the selection might extend to include... hmm, I'm not sure what exactly happens here.
Actually, I think the issue is more nuanced. Let me just implement the fix and test it.
Actually wait, I just realized something. Looking at the code flow again:
for (const caret of initialRange.iterNodeCarets('shadowRoot')) {
if ($isChildCaret(caret)) {
// ...
} else if (state.type === 'merge-block') {
break;
} else if ($isSiblingCaret(caret)) {
if ($isElementNode(caret.origin)) {
if (!caret.origin.isInline()) {
state = {block: caret.origin, type: 'merge-next-block'};
} else if (!caret.origin.isParentOf(initialRange.anchor.origin)) {
break;
}
continue;
} else if ($isDecoratorNode(caret.origin)) {
// ...
return;
}
break;
}
}
When we find a non-inline ElementNode, we set state to 'merge-next-block' and continue. This means we keep iterating and might find more siblings.
But when we find a DecoratorNode, we either remove it or the anchor element and then return. We don't continue.
So if the order is:
- DecoratorBlockNode (sibling of ListNode)
- ParagraphNode (sibling of DecoratorBlockNode)
We'd find the DecoratorBlockNode first, and if we return from there, we never reach the ParagraphNode.
OK, I think my fix is correct. Let me implement it.
Actually, I realize I should also consider: after my fix (returning when state is 'initial' and we find a decorator), what happens next?
The code after the loop:
if (state.type === 'merge-block') {
// ...
}
// Handle the deletion around decorators.
const focus = this.focus;
this.modify('extend', isBackward, 'character');
if (!this.isCollapsed()) {
// ... delete extended text
} else if (isBackward && anchor.offset === 0) {
// Special handling around rich text nodes
if ($collapseAtStart(this, anchor.getNode())) {
return;
}
}
If the selection remains collapsed after modify, and isBackward && anchor.offset === 0, we call $collapseAtStart. Let me check what that does.
function $collapseAtStart(
selection: RangeSelection,
node: TextNode | ElementNode,
): boolean {
...
}
I need to see this function. Let me search for it.
Actually, let me just implement my fix and test it. The key fix is:
When state.type === 'initial' and we encounter a non-isolated DecoratorNode, don't remove it. Just return.
Let me implement this.
Actually, I realize I should first run the existing tests to see the current state. Let me also check if there are any existing tests for this scenario.
Hmm, actually let me think about this more carefully. Looking at the code:
When state.type === 'initial' and we encounter a DecoratorNode that's not isolated:
- Current behavior: remove the decorator
- New behavior: don't remove the decorator, just return
After returning, the code continues with the normal deletion flow:
modify('extend', 'backward', 'character') - tries to extend the selection backward
- If selection is still collapsed, check if we're at offset 0 and call
$collapseAtStart
For the case of an empty ListItemNode:
- The anchor is the ListItemNode at offset 0
- After returning,
modify('extend', 'backward', 'character') doesn't extend anything (we're already at the start)
$collapseAtStart might handle this case
For the case of a non-empty ListItemNode:
- The anchor is a text node at offset 0
- After returning,
modify('extend', 'backward', 'character') doesn't extend anything (we're at the start of the text)
- But wait, the code earlier handles this case...
Actually, looking at the code flow again:
deleteCharacter(isBackward: boolean): void {
const wasCollapsed = this.isCollapsed();
if (this.isCollapsed()) {
const anchor = this.anchor;
let anchorNode: TextNode | ElementNode | null = anchor.getNode();
if (this.forwardDeletion(anchor, anchorNode, isBackward)) {
return;
}
const direction = isBackward ? 'previous' : 'next';
const initialCaret = $caretFromPoint(anchor, direction);
const initialRange = $extendCaretToRange(initialCaret);
if (
initialRange
.getTextSlices()
.every(slice => slice === null || slice.distance === 0)
) {
// ... our code is in this block
}
// Handle the deletion around decorators (OUTSIDE the if block!)
const focus = this.focus;
this.modify('extend', isBackward, 'character');
// ...
}
this.removeText();
}
Wait, there's an if block. If the text slices check passes, we enter the block with the decorator handling. If we return from inside that block, we skip the code after it.
But if the text slices check fails, we skip the entire block and go directly to the "Handle the deletion around decorators" code.
Hmm, let me re-read:
if (
initialRange
.getTextSlices()
.every(slice => slice === null || slice.distance === 0)
) {
// There's no text in the direction of the deletion so we can explore our options
// ... our code is in this block
for (const caret of initialRange.iterNodeCarets('shadowRoot')) {
// ...
if ($isDecoratorNode(caret.origin)) {
// ...
return; // This returns from the function!
}
}
// After the for loop, if state.type === 'merge-block', we handle merging
if (state.type === 'merge-block') {
// ...
return this.removeText();
}
}
// After the if block
// Handle the deletion around decorators.
// ...
Wait, if we return from inside the if block, we exit the deleteCharacter function entirely. So the code after the if block (the "Handle the deletion around decorators" part) is only reached if we DON'T return from inside the if block.
So with my fix:
} else if ($isDecoratorNode(caret.origin)) {
if (caret.origin.isIsolated()) {
// do nothing, shouldn't delete an isolated decorator
} else if (
state.type === 'merge-next-block' &&
// ...
) {
// ...
} else if (state.type === 'initial') {
return; // <-- My fix: return without removing the decorator
} else {
caret.origin.remove();
}
return;
}
When state.type === 'initial' and we find a non-isolated DecoratorNode, we return from the function. This means:
- The decorator is NOT removed
- The function exits
- No text is deleted
Is this the right behavior? Let me think...
If the user presses backspace at the start of a ListItemNode:
- If the ListItemNode is empty, ideally the ListNode should be converted to a ParagraphNode
- If the ListItemNode has content, ideally the first character should be deleted
But with my fix, pressing backspace at the start of a ListItemNode (even with content) would do nothing! That's not right.
Hmm, I need to reconsider the fix. Let me look at the code flow again.
When we return from inside the if block:
- The decorator is not removed
- But the deletion is also not performed
- The user expects some deletion to happen
The issue is that the decorator handling code is inside the if block that checks for no text in the direction. When there IS text, the code skips this block and goes to the normal deletion code.
Wait, but the if condition checks initialRange.getTextSlices().every(slice => slice === null || slice.distance === 0). This means: every slice in the initial range is null or has distance 0. If the cursor is at the start of a text node, the slice going backward would be null or distance 0 (because there's nothing before).
So for the case where the cursor is at the start of a non-empty text node inside a ListItemNode:
initialRange goes from the anchor backward
- The text slice going backward would have distance 0 (we're at offset 0)
- So the condition is true, and we enter the if block
And inside the if block, we find the DecoratorNode and (with my fix) return without doing anything.
But the user expects some action! If the text node has content, pressing backspace at its start should... do nothing (can't go further back without affecting the previous block). If the text node is empty, it should merge with the previous block.
Wait, actually, if the cursor is at the start of a non-empty text node:
- Pressing backspace should NOT delete anything (we're at the start of the text)
- But it SHOULD try to merge with the previous block
If the cursor is at the start of an empty ListItemNode:
- Pressing backspace should merge with the previous block
- If the previous block is a DecoratorNode, the behavior is debatable
Hmm, actually, the current behavior (with my fix) might be correct for the case where the text node has content. Pressing backspace at the start of a non-empty block should not delete the previous block.
But for the case where the ListItemNode is empty, the behavior should be:
- Remove the ListItemNode (and possibly the ListNode if it's the only item)
- But NOT delete the DecoratorNode
OK let me re-think the fix.
The issue is: when the anchor is inside a container (like ListNode), pressing backspace at its start should:
- NOT delete the previous sibling of the container (the DecoratorNode)
- Handle the deletion of the current block (ListItemNode or ListNode) appropriately
The current code's problem is that it doesn't distinguish between:
- The anchor being directly adjacent to a decorator (e.g., in a ParagraphNode next to a DecoratorNode)
- The anchor being inside a container whose sibling is a decorator
When the anchor is directly adjacent:
- It makes sense to select or delete the decorator when pressing backspace at the start
When the anchor is inside a container:
- Deleting the decorator is wrong
So the fix should check: is the anchor directly adjacent to the decorator, or is it nested inside a container?
Looking at the code, initialRange.anchor.origin is the node where the anchor is located. If the anchor is inside a ListItemNode, initialRange.anchor.origin would be the ListItemNode or a text node inside it.
The decorator is a sibling of the ListNode (the parent of the ListItemNode). So the anchor is NOT directly adjacent to the decorator - it's nested inside a container.
One way to check this: is caret.origin (the decorator) a sibling of an ancestor of initialRange.anchor.origin? If so, the anchor is nested, and we should not delete the decorator.
Actually, let me check what the relationship is between caret.origin and initialRange.anchor.origin:
initialRange.anchor.origin is the node where the anchor is (e.g., text node or ListItemNode)
caret.origin is the DecoratorNode
- They don't share a parent (DecoratorNode is a child of root, text node is a child of ListItemNode)
So one check could be: is caret.origin a sibling of any ancestor of initialRange.anchor.origin? If so, the anchor is nested, and we should be more careful about deleting the decorator.
But this is getting complex. Let me look for a simpler fix.
Actually, I think the issue might be simpler. Let me look at the condition again:
} else if (
state.type === 'merge-next-block' &&
(caret.origin.isKeyboardSelectable() ||
!caret.origin.isInline()) &&
$isElementNode(initialRange.anchor.origin) &&
initialRange.anchor.origin.isEmpty()
) {
// If the anchor is an empty element that is adjacent to a
// decorator then we remove the paragraph and select the
// decorator
initialRange.anchor.origin.remove();
...
} else {
// When the anchor is not an empty element then the
// adjacent decorator is removed
caret.origin.remove();
}
The comment says "adjacent decorator" but the decorator is only "adjacent" if it's a sibling of initialRange.anchor.origin. If the anchor is nested, the decorator is not adjacent.
But actually, looking at the iteration, the decorator is found as a sibling caret. This means it's a sibling of some node in the traversal path from the anchor to the root. It might be a sibling of the anchor itself, or a sibling of the anchor's parent, grandparent, etc.
The key insight is: when state.type === 'initial', we haven't found any block-level ElementNode sibling yet. This means the decorator is the first thing we found going backward from the anchor. If the anchor is nested inside a container (like ListNode), the decorator would be a sibling of that container, not the anchor itself.
So the fix should be: when state.type === 'initial' and the decorator is found, check if the anchor is nested inside a non-trivial container (i.e., a container that's not just a ParagraphNode or similar). If so, don't delete the decorator.
But how do we define "non-trivial container"? A ListNode is a container that wraps ListItemNodes. A ParagraphNode is a container that wraps text nodes. If the anchor is in a text node inside a ParagraphNode, the ParagraphNode is a trivial container.
Hmm, I think the key difference is:
- ParagraphNode: a simple container, its siblings are what matter
- ListNode: a structural container, its siblings are separate from its children's behavior
When the anchor is inside a ParagraphNode:
- Deleting at the start of the paragraph can merge with the previous sibling
- If the previous sibling is a decorator, it makes sense to handle the decorator specially
When the anchor is inside a ListItemNode -> ListNode:
- Deleting at the start of the list item should NOT affect siblings of the ListNode
- The list item's behavior is managed by the list structure
So the fix could be: check if the anchor is inside a ListItemNode. If so, don't delete the decorator.
But that's too specific. What about other structural containers?
Actually, I think the issue is more fundamental. The current code assumes that when we find a decorator as a sibling caret, the anchor is "adjacent" to the decorator. But when the anchor is nested inside a container, the decorator is a sibling of the container, not the anchor.
The fix should be: when we find a decorator as a sibling caret, check if the caret's parent is the same as the anchor's parent (directly or via an ElementNode chain). If not, the decorator is not truly "adjacent" to the anchor, and we should not delete it.
Let me think about this more carefully. When we find a SiblingCaret for the decorator:
caret.origin is the DecoratorNode
caret.direction is 'previous'
- The parent of the caret is the container that both the decorator and the current node share
If the anchor is inside a ListItemNode -> ListNode:
- The parent of the sibling caret (decorator) is the root
- The ListNode is also a child of the root
- So the decorator and ListNode share the same parent (root)
The anchor is NOT a direct sibling of the decorator. The anchor is nested inside a ListItemNode which is inside a ListNode.
One check: is initialRange.anchor.origin a direct child of the same parent as caret.origin?
If initialRange.anchor.origin is a ListItemNode and caret.origin is a DecoratorNode:
- Parent of ListItemNode is ListNode
- Parent of DecoratorNode is root
- They don't share a parent, so the decorator is not adjacent
If initialRange.anchor.origin is a ParagraphNode and caret.origin is a DecoratorNode:
- Parent of ParagraphNode is root
- Parent of DecoratorNode is root
- They share a parent, so the decorator is adjacent
So the fix could be: when we find a decorator, check if initialRange.anchor.origin.getParent() === caret.origin.getParent(). If not, the decorator is not adjacent, and we should not delete it.
But wait, what if initialRange.anchor.origin is a text node? Then:
- Parent of text node is ListItemNode
- Parent of DecoratorNode is root
- They don't share a parent
So the fix should check the parent of the anchor's containing element, not the anchor itself.
Hmm, this is getting complex. Let me try a different approach.
The issue is specifically when the anchor is inside a ListItemNode. So maybe the fix should check: is the anchor inside a ListItemNode?
} else if ($isDecoratorNode(caret.origin)) {
if (caret.origin.isIsolated()) {
// do nothing, shouldn't delete an isolated decorator
} else if (
state.type === 'merge-next-block' &&
(caret.origin.isKeyboardSelectable() ||
!caret.origin.isInline()) &&
$isElementNode(initialRange.anchor.origin) &&
initialRange.anchor.origin.isEmpty()
) {
// ...
} else {
// When the anchor is not an empty element then the
// adjacent decorator is removed
caret.origin.remove();
}
return;
}
The fix: add a check to see if the anchor (or any of its ancestors up to the sibling's parent) is inside a structural container.
Actually, let me think about this differently. The state variable tracks what we've seen so far. When state.type === 'initial', we haven't found any block-level sibling yet. This means the first thing we found going backward from the anchor is a decorator.
If the anchor is at the root level (e.g., in a ParagraphNode), the decorator is a direct sibling. In this case, deleting the decorator (or the paragraph) makes sense.
If the anchor is nested (e.g., in a ListItemNode inside a ListNode), the decorator is a sibling of the ListNode, not the anchor. In this case, deleting the decorator is wrong.
The key is: when state.type === 'initial' and we go through child carets before finding a sibling caret, we know the anchor is nested. The number of child carets tells us how deep the nesting is.
Actually, a simpler check: when state.type === 'initial' and we find a decorator, check if we've gone through any non-trivial parent elements. If the anchor is directly in a ParagraphNode or at the root, it's OK to handle the decorator. If the anchor is inside a ListItemNode or other structural element, we should not delete the decorator.
OK let me just implement a simple fix and test it. The fix is:
When state.type === 'initial' and we find a decorator, don't remove it. Instead, just break from the loop and continue with normal deletion.
Actually, looking at the code again, after the for loop:
if (state.type === 'merge-block') {
// ...
return this.removeText();
}
// Code after the if block is outside the outer if block!
Wait, let me re-read the structure:
if (
initialRange.getTextSlices().every(...)
) {
// ...
for (const caret of initialRange.iterNodeCarets('shadowRoot')) {
// ...
if ($isDecoratorNode(caret.origin)) {
// ...
return; // This returns from the function
}
break; // This is for non-decorator sibling cases
}
if (state.type === 'merge-block') {
// ...
return this.removeText();
}
}
// This code is OUTSIDE the if block
Wait, I need to look at the actual structure more carefully. Let me read the full function again.
Actually, I realize the key issue is that after the if block (no text in direction), the code does:
// Handle the deletion around decorators.
const focus = this.focus;
this.modify('extend', isBackward, 'character');
if (!this.isCollapsed()) {
// ...
} else if (isBackward && anchor.offset === 0) {
// ...
}
}
this.removeText();
Wait, this code is INSIDE the if (this.isCollapsed()) block but OUTSIDE the if (initialRange.getTextSlices()...) block. Let me verify by checking the actual indentation/braces.
Let me re-read the function structure:
deleteCharacter(isBackward: boolean): void {
const wasCollapsed = this.isCollapsed();
if (this.isCollapsed()) { // Line 1765
const anchor = this.anchor;
let anchorNode: TextNode | ElementNode | null = anchor.getNode();
if (this.forwardDeletion(anchor, anchorNode, isBackward)) {
return;
}
const direction = isBackward ? 'previous' : 'next';
const initialCaret = $caretFromPoint(anchor, direction);
const initialRange = $extendCaretToRange(initialCaret);
if (
initialRange.getTextSlices().every(...)
) { // Line 1774
// ... decorator handling code ...
for (const caret of ...) {
// ...
}
if (state.type === 'merge-block') {
// ...
}
} // End of inner if block around line 1893
// Handle the deletion around decorators. // Line 1895
const focus = this.focus;
this.modify('extend', isBackward, 'character');
// ...
} // End of outer if block
this.removeText();
}
Hmm, so if we return from inside the inner if block, we exit the function. The code after the inner if block (line 1895) is only reached if we don't return from inside the inner if block.
But wait, looking at the for loop:
for (const caret of initialRange.iterNodeCarets('shadowRoot')) {
if ($isChildCaret(caret)) {
// ...
} else if (state.type === 'merge-block') {
break;
} else if ($isSiblingCaret(caret)) {
if ($isElementNode(caret.origin)) {
// ...
continue;
} else if ($isDecoratorNode(caret.origin)) {
// ...
return;
}
break;
}
}
After the for loop, if state.type === 'merge-block', we handle the merge. Otherwise, we fall through to the code after the inner if block.
So if we change the code to break instead of return when we find a decorator and state.type === 'initial':
- We break out of the for loop
- state.type is still 'initial'
- We don't enter the
if (state.type === 'merge-block') block
- We fall through to the code after the inner if block
At step 4, the code does:
// Handle the deletion around decorators.
const focus = this.focus;
this.modify('extend', isBackward, 'character');
This modifies the selection to extend backward. If the selection extends to include the decorator, removeText() would delete it. But if the selection doesn't extend (because we're at the start and can't go further), nothing happens.
Hmm, this might not be the right behavior either. Let me think...
If the cursor is at the start of an empty ListItemNode and we press backspace:
- We find the decorator as a sibling
- We
break instead of return
- The inner if block ends
- We call
modify('extend', 'backward', 'character')
- The selection might extend to include the decorator
removeText() would delete the decorator
That's still wrong! The decorator would still be deleted.
So break is not the right fix. We need to return to prevent any further action. But then the ListItemNode is not handled at all.
Hmm, this is tricky. The issue is that the current code structure assumes that when there's no text in the direction, it handles all the edge cases (including decorators) inside the if block. If we want to add a case where the decorator is not deleted but the anchor block is still processed, we need to handle it differently.
One approach: when we find a decorator and state.type === 'initial', we should check if the anchor is nested inside a structural container (like ListNode). If so:
- Don't delete the decorator
- Delete the anchor block (if it's empty)
For an empty ListItemNode:
- Remove the ListItemNode
- If the ListNode is now empty, replace it with a ParagraphNode (or remove it too)
But this is getting complex. Let me look at how similar cases are handled elsewhere.
Actually, let me re-read the bug description:
Delete the first list item, and the previous DecoratorBlockNode will also be removed
So the issue is specifically that the DecoratorBlockNode is being deleted when it shouldn't be. The expected behavior is:
Only ListNode would be deleted. (I will be more than happy to see the ListNode be replaced by a ParagraphNode if possible! That's the default behavior in other editors.)
So the expected behavior is:
- The ListItemNode (or ListNode) should be deleted/replaced with a ParagraphNode
- The DecoratorBlockNode should NOT be deleted
Now, the fix should:
- When the anchor is inside a ListItemNode and we find a DecoratorBlockNode as a sibling of the ListNode, don't delete the DecoratorBlockNode
- Instead, handle the deletion of the ListItemNode/ListNode
Let me think about how to implement this. The key insight is:
When state.type === 'initial' and we find a non-isolated DecoratorNode:
- If the anchor is directly adjacent to the decorator (same parent), delete/select as before
- If the anchor is nested (decorator is a sibling of a parent), don't delete the decorator
To check if the anchor is nested: check if the parent of initialRange.anchor.origin (or its containing block) is the same as the parent of caret.origin.
Wait, actually, I realize there's a simpler approach. The state variable tells us what we've seen so far:
state.type === 'initial': we haven't found any block-level sibling yet
state.type === 'merge-next-block': we found a block-level sibling and want to merge with it
When state.type === 'initial', the first sibling we find could be:
- A block-level ElementNode (ParagraphNode, etc.) - state becomes 'merge-next-block'
- A DecoratorNode - we handle it specially
- An inline ElementNode - we continue/break
The issue is: when the anchor is nested (inside a container), the first sibling we find is a sibling of the container, not the anchor itself. So we shouldn't treat it as "adjacent" to the anchor.
One check: when we find a sibling caret, the sibling's parent is the same as the parent of the node we just went through. If we went through multiple child carets (going up from the anchor to a parent), the sibling is a sibling of that parent, not the anchor.
Let me check: when state.type === 'initial' and we find a DecoratorNode as a sibling, has the iteration gone through any parent elements?
The iteration goes:
- Start at the anchor
- For each caret in
initialRange.iterNodeCarets('shadowRoot'):
- If it's a ChildCaret, we're going through a parent
- If it's a SiblingCaret, we've found a sibling
If we see ChildCarets before the SiblingCaret, the anchor is nested. If we see the SiblingCaret immediately, the anchor is not nested.
So the fix could be: when state.type === 'initial' and we find a DecoratorNode as a sibling, check if the anchor is a direct sibling of the decorator. If not (i.e., the anchor is nested), don't delete the decorator.
How to check this: compare the parent of initialRange.anchor.origin with the parent of caret.origin.
Actually, the anchor might be a text node, in which case we need to find its containing block.
Hmm, let me try a different approach. Instead of checking if the anchor is nested, let me check if the decorator is actually "reachable" from the anchor in a single merge operation.
If the anchor is in a ParagraphNode:
- The ParagraphNode and DecoratorNode are siblings
- Pressing backspace at the start of the ParagraphNode should merge with or select the DecoratorNode
If the anchor is in a ListItemNode inside a ListNode:
- The ListNode and DecoratorNode are siblings
- The ListItemNode is NOT a direct sibling of the DecoratorNode
- Pressing backspace at the start of the ListItemNode should NOT affect the DecoratorNode
So the fix could be: when state.type === 'initial' and we find a DecoratorNode, check if initialRange.anchor.origin is a sibling of the decorator (same parent). If not, don't delete the decorator.
Let me implement this check:
} else if ($isDecoratorNode(caret.origin)) {
if (caret.origin.isIsolated()) {
// do nothing, shouldn't delete an isolated decorator
} else if (
state.type === 'merge-next-block' &&
(caret.origin.isKeyboardSelectable() ||
!caret.origin.isInline()) &&
$isElementNode(initialRange.anchor.origin) &&
initialRange.anchor.origin.isEmpty()
) {
// ...
} else {
// Check if the anchor is a direct sibling of the decorator
// (i.e., the anchor and decorator share the same parent)
const anchorParent = initialRange.anchor.origin.getParent();
const decoratorParent = caret.origin.getParent();
if (anchorParent === decoratorParent) {
// Anchor is adjacent to decorator, remove the decorator
caret.origin.remove();
}
// Otherwise, anchor is nested, don't remove the decorator
}
return;
}
Wait, but what if initialRange.anchor.origin is a text node? Then anchorParent would be the containing element (e.g., ParagraphNode or ListItemNode).
For the ParagraphNode case:
- Text node parent is ParagraphNode
- DecoratorNode parent is root
- They're NOT the same, so we wouldn't delete the decorator
But wait, the ParagraphNode IS a sibling of the DecoratorNode. If the user presses backspace at the start of the ParagraphNode, should the decorator be deleted?
Hmm, the current behavior for a ParagraphNode:
- The ParagraphNode is the
initialRange.anchor.origin (if the selection is at element level)
- Or a text node inside the ParagraphNode
If the selection is at element level (anchor is the ParagraphNode):
initialRange.anchor.origin is the ParagraphNode
- ParagraphNode parent is root
- DecoratorNode parent is root
- They ARE the same, so we delete the decorator
If the selection is at text level (anchor is a text node inside the ParagraphNode):
initialRange.anchor.origin is the text node
- Text node parent is ParagraphNode
- DecoratorNode parent is root
- They're NOT the same, so we wouldn't delete the decorator
Hmm, that doesn't seem right. If the user is at the start of a text node in a ParagraphNode and presses backspace, we might want to merge with or select the decorator.
But wait, in this case, the iteration would go:
- Start at the text node (at offset 0)
- There's no previous sibling of the text node at offset 0 (or there's nothing before it)
- Go up to the ParagraphNode
- Find the DecoratorNode as a sibling of the ParagraphNode
So when we find the DecoratorNode:
initialRange.anchor.origin is the text node
- Text node parent is ParagraphNode
- DecoratorNode parent is root
- They're NOT the same
With my check, we wouldn't delete the decorator. But should we?
Actually, I think the issue is that initialRange.anchor.origin might be different from what I assumed. Let me check what initialRange.anchor.origin is.
Looking at the code:
const initialCaret = $caretFromPoint(anchor, direction);
const initialRange = $extendCaretToRange(initialCaret);
The initialCaret is created from the anchor point. The initialRange is created by extending the caret. The initialRange.anchor.origin would be... let me check.
Actually, I think I need to look at the $caretFromPoint and $extendCaretToRange functions to understand what initialRange.anchor.origin is.
Let me search for these functions.
OK, I think I've been overthinking this. Let me look at the actual issue more carefully.
The bug is: when you have a DecoratorBlockNode followed by a ListNode (with ListItemNode), and you delete the first ListItemNode, the DecoratorBlockNode is also deleted.
The key scenario:
- DecoratorBlockNode (child of root)
- ListNode (child of root)
- ListItemNode (child of ListNode)
- TextNode "item text" (child of ListItemNode)
When the cursor is at the start of the ListItemNode (e.g., at the start of "item text"):
- Pressing backspace should NOT delete the DecoratorBlockNode
- It should either delete the first character of "item text" or merge the ListItemNode with the previous block
But currently, pressing backspace at the start of "item text" deletes the DecoratorBlockNode. This is wrong.
Wait, actually, if the cursor is at the start of "item text" (offset 0 of the TextNode), pressing backspace should try to merge with the previous block. But there's no text before it in the ListItemNode. So it should try to go to the previous block.
The "previous block" is the DecoratorBlockNode (via the ListNode). But the DecoratorBlockNode is a block-level element, and you can't merge a list item with a decorator. So what should happen?
I think the expected behavior is:
- If the ListItemNode is empty, remove it (and possibly convert the ListNode to a ParagraphNode)
- If the ListItemNode has content, do nothing (or move the cursor)
But currently, the DecoratorBlockNode is being deleted, which is wrong.
OK so the fix should be: when we find a DecoratorNode and the anchor is nested inside a structural container (like ListNode), don't delete the decorator. Just return without doing anything.
Let me implement this fix and see if it works.
The fix is to add a check in the else branch:
} else {
// When the anchor is not an empty element then the
// adjacent decorator is removed
// BUT: only if the anchor is actually adjacent to the decorator
// (i.e., not nested inside a container like ListNode)
// Check: is the anchor's top-level block at the same level as the decorator?
// If not, the anchor is nested, and we should not delete the decorator
const anchorOrigin = initialRange.anchor.origin;
let anchorBlock: LexicalNode | null = anchorOrigin;
// Walk up to find the top-level block (sibling of the decorator)
while (anchorBlock != null && anchorBlock.getParent() !== caret.origin.getParent()) {
anchorBlock = anchorBlock.getParent();
}
if (anchorBlock !== null) {
// The anchor is nested; the decorator is a sibling of the container
// Don't delete the decorator
} else {
// This shouldn't happen, but just in case
caret.origin.remove();
}
}
Hmm, this is getting complicated. Let me simplify.
Actually, I think the simplest fix is to check if state.type === 'initial'. When state.type === 'initial' and we find a decorator:
- If the anchor is at the root level (directly in a ParagraphNode or similar), delete the decorator
- If the anchor is nested (inside a container like ListNode), don't delete the decorator
The way to check if the anchor is nested is to see if the anchor's parent chain goes through more than just a ParagraphNode before reaching the decorator's parent.
But wait, maybe there's an even simpler check. When state.type === 'initial' and we find a decorator as a sibling, check if the decorator's parent is the same as the anchor origin's parent. If not, the anchor is nested.
Actually, let me try a different approach. Instead of checking if the anchor is nested, let me check if the decorator is a sibling of the anchor's topmost element in the current traversal.
When the iteration finds a SiblingCaret:
caret.origin is the sibling
- The sibling's parent is the container we're traversing
If we went through ChildCarets before finding the SiblingCaret, we know we traversed up through parent elements. The SiblingCaret's parent is the topmost element we reached.
So: when state.type === 'initial' and we find a decorator as a SiblingCaret:
- If we haven't gone through any non-inline, non-shadow-root ChildCarets, the anchor is at the same level as the decorator
- If we have gone through such ChildCarets, the anchor is nested
But I don't have an easy way to check if we've gone through any ChildCarets.
Let me try a simpler approach: check if initialRange.anchor.origin is a child of the same parent as caret.origin.
For the ParagraphNode case (anchor is in a ParagraphNode at the root level):
initialRange.anchor.origin could be a text node or the ParagraphNode
- If it's the ParagraphNode, its parent is root, same as the decorator's parent
- If it's a text node, its parent is ParagraphNode, different from the decorator's parent (root)
Hmm, this doesn't work for the text node case.
Let me try yet another approach: find the topmost block element of the anchor and compare its parent with the decorator's parent.
// Find the topmost block element of the anchor
let anchorBlock = initialRange.anchor.origin;
while ($isElementNode(anchorBlock) && !anchorBlock.isInline()) {
const parent = anchorBlock.getParent();
if (parent === null || $isRootOrShadowRoot(parent)) {
break;
}
anchorBlock = parent;
}
// Now compare anchorBlock's parent with the decorator's parent
if ($isElementNode(anchorBlock) && anchorBlock.getParent() !== caret.origin.getParent()) {
// The anchor is nested; don't delete the decorator
return;
}
Hmm, this is still getting complicated. Let me think about this from a different angle.
Actually, I just realized something. When state.type === 'initial', we haven't found any block-level ElementNode sibling yet. This means we went straight from the anchor (or its containing elements) to the decorator. If the anchor is inside a ListNode, the ListNode is the parent we traversed through to find the decorator.
So when state.type === 'initial' and we find a decorator:
- If we went through a ListNode (or other structural container), the anchor is nested
- If we went through a ParagraphNode (or directly at the root), the anchor is at the same level
The way to check if we went through a ListNode is to look at the parent of initialRange.anchor.origin.
For the ListItemNode case:
initialRange.anchor.origin is the ListItemNode (or a text node inside it)
- The parent of the ListItemNode is the ListNode
- The parent of the decorator is the root
- The ListNode is NOT the same as the root, so the anchor is nested
For the ParagraphNode case:
initialRange.anchor.origin is the ParagraphNode (or a text node inside it)
- The parent of the ParagraphNode is the root
- The parent of the decorator is the root
- The root is the same as the root, so the anchor is at the same level
Wait, but what if the anchor is a text node inside the ParagraphNode? Then:
initialRange.anchor.origin is the text node
- The parent of the text node is the ParagraphNode
- The parent of the decorator is the root
- The ParagraphNode is NOT the same as the root
So the check should find the topmost parent of the anchor origin and compare it with the decorator's parent.
Let me try this:
// Find the topmost parent of the anchor origin that is a child of root/shadow root
let topBlock = initialRange.anchor.origin;
let parent = topBlock.getParent();
while (parent !== null && !$isRootOrShadowRoot(parent)) {
topBlock = parent;
parent = parent.getParent();
}
// Now parent is root/shadow root (or null)
// And topBlock is a child of root/shadow root
if ($isElementNode(topBlock)) {
// topBlock is a direct child of root/shadow root
// Compare its parent with the decorator's parent
if (topBlock.getParent() !== caret.origin.getParent()) {
// The anchor is nested; don't delete the decorator
return;
}
}
This should work:
- For the ParagraphNode case: topBlock is the ParagraphNode, its parent is root, same as the decorator's parent
- For the ListItemNode case: topBlock is the ListNode, its parent is root, same as the decorator's parent
Wait, that doesn't distinguish between the two cases! Both have the same parent (root).
Hmm, the issue is that both the ParagraphNode and the ListNode are direct children of the root. So their parents are the same.
The difference is:
- ParagraphNode: the anchor is directly in the ParagraphNode (or a text node inside it)
- ListItemNode: the anchor is in a ListItemNode which is in a ListNode
So I need to check if the anchor is directly in the top block (ParagraphNode) or nested inside a child of the top block (ListItemNode inside ListNode).
The check should be: is initialRange.anchor.origin a direct child of topBlock?
- ParagraphNode case: the text node is a direct child of the ParagraphNode -> yes
- ListItemNode case: the text node is NOT a direct child of the ListNode (it's in a ListItemNode) -> no
But what if initialRange.anchor.origin is the ParagraphNode itself? Then it IS the top block.
And what if initialRange.anchor.origin is the ListItemNode? Then its parent is the ListNode, which is the top block, so it IS a direct child.
Hmm, but in the ListItemNode case, the ListItemNode IS a direct child of the ListNode. So the check "is anchor origin a direct child of top block" would be true.
Wait, I need to reconsider. Let me think about what initialRange.anchor.origin is in each case.
If the cursor is at element selection (offset 0 of the ListItemNode):
anchor.getNode() returns the ListItemNode
initialCaret is created from this anchor
initialRange.anchor.origin would be the ListItemNode
If the cursor is at text selection (offset 0 of a text node inside the ListItemNode):
anchor.getNode() returns the text node
initialCaret is created from this anchor
initialRange.anchor.origin would be the text node (or maybe the ListItemNode?)
I'm not sure what $caretFromPoint does exactly. Let me check.
OK I think I need to take a step back and just implement a fix based on my best understanding, then test it.
The simplest fix that I'm fairly confident about:
When state.type === 'initial' and we find a non-isolated DecoratorNode as a sibling, DON'T delete the decorator. Just return without doing anything.
This might not handle all edge cases, but it should fix the reported bug (DecoratorBlockNode being deleted when it shouldn't be).
The reasoning is:
- When
state.type === 'initial', we haven't found any block-level ElementNode sibling
- The first thing we found going backward is a DecoratorNode
- If the anchor is at the same level as the decorator (e.g., in a ParagraphNode), the current code's behavior of deleting the decorator might be intentional
- If the anchor is nested (e.g., in a ListItemNode inside a ListNode), deleting the decorator is wrong
But the problem is: how do we distinguish between these two cases?
Actually, I just realized: when the anchor is at the same level as the decorator (e.g., in a ParagraphNode), the ParagraphNode would be found first as a non-inline ElementNode, setting state.type to 'merge-next-block'. Then the decorator would be found with state.type === 'merge-next-block', not 'initial'.
Wait, that's not right either. Let me think about the iteration order again.
When going backward from an anchor in a ParagraphNode:
- If the ParagraphNode is the anchor itself (element selection), we start at the ParagraphNode
- We look for a sibling of the ParagraphNode (or its parent)
- The first sibling found could be the DecoratorNode
In this case, the DecoratorNode is found directly, without finding any other block first. So state.type would be 'initial'.
When going backward from an anchor in a ListItemNode inside a ListNode:
- We start at the ListItemNode (or a text node inside it)
- We go up through the ListItemNode and the ListNode
- We find the DecoratorNode as a sibling of the ListNode
In this case, the DecoratorNode is also found directly, without finding any other block first. So state.type would also be 'initial'.
So both cases have state.type === 'initial'. We can't distinguish them based on the state alone.
OK, I need a different approach. Let me check if the decorator is a direct sibling of the anchor's containing block.
For the ParagraphNode case:
- The ParagraphNode is a direct child of root
- The DecoratorNode is a direct child of root
- They are direct siblings
For the ListItemNode case:
- The ListItemNode is a child of the ListNode
- The ListNode is a direct child of root
- The DecoratorNode is a direct child of root
- The DecoratorNode is a sibling of the ListNode, not the ListItemNode
So the fix is: when we find a DecoratorNode as a sibling caret, check if the anchor's containing block (the topmost element that's a direct child of the decorator's parent) is the same as the node whose sibling we're looking at.
Actually, the SiblingCaret tells us exactly which node's sibling we're looking at. Let me check if there's a way to get that information.
Looking at the $isSiblingCaret function, a SiblingCaret has:
origin: the sibling node
direction: 'previous' or 'next'
But I don't think it has information about which node's sibling it is.
Hmm, let me try a different approach. Instead of checking the relationship between the anchor and the decorator, let me check if the anchor is inside a ListItemNode.
} else if ($isDecoratorNode(caret.origin)) {
if (caret.origin.isIsolated()) {
// do nothing, shouldn't delete an isolated decorator
} else if (
state.type === 'merge-next-block' &&
// ...
) {
// ...
} else {
// Check if the anchor is inside a ListItemNode
let node = initialRange.anchor.origin;
let isInsideListItem = false;
while (node != null) {
if ($isListItemNode(node)) {
isInsideListItem = true;
break;
}
node = node.getParent();
}
if (isInsideListItem) {
// Don't delete the decorator when the anchor is inside a ListItemNode
return;
}
caret.origin.remove();
}
return;
}
This is simple and specific to the reported bug. But it might not be general enough for other similar cases.
A more general fix would be to check if the anchor is inside any structural container (not just ListItemNode). But what defines a "structural container"?
One approach: a container is "structural" if it's not a ParagraphNode, HeadingNode, QuoteNode, or similar simple block elements. In other words, if the container is a ListNode, TableNode, or other complex container, it's structural.
But this is hard to define generically.
Let me think about what the expected behavior is for different cases:
- ParagraphNode -> DecoratorNode (backspace at start of ParagraphNode): delete the ParagraphNode and select the decorator
- ParagraphNode (empty) -> DecoratorNode (backspace at start of ParagraphNode): delete the ParagraphNode and select the decorator
- ListItemNode inside ListNode -> DecoratorNode (backspace at start of ListItemNode): do NOT delete the decorator
- TableCellNode inside TableNode -> DecoratorNode (backspace at start of TableCellNode): probably do NOT delete the decorator
So the pattern is: when the anchor is inside a structural container (ListNode, TableNode), don't delete the decorator. When the anchor is in a simple block (ParagraphNode), handle the decorator.
One way to distinguish: check if the anchor's containing block (the direct child of the decorator's parent) is the same as the anchor's "current block" (the node at the selection level).
For the ParagraphNode case:
- Anchor's containing block: ParagraphNode
- Anchor's current block: ParagraphNode (or text node inside it)
- They're the same, so handle the decorator
For the ListItemNode case:
- Anchor's containing block: ListNode (the direct child of root that contains the anchor)
- Anchor's current block: ListItemNode (or text node inside it)
- They're different, so don't delete the decorator
Wait, but in the ParagraphNode case, the anchor's containing block IS the ParagraphNode, and the anchor's current block is also the ParagraphNode (if element selection) or a text node inside it.
If the anchor is a text node inside the ParagraphNode:
- Anchor's containing block: ParagraphNode (direct child of root)
- Anchor's current block: text node
- They're different!
So this check doesn't work for the text node case.
Hmm, let me think again. When the anchor is a text node inside a ParagraphNode:
- Pressing backspace at the start of the text node
- We go backward and find the DecoratorNode as a sibling of the ParagraphNode
In this case, should the decorator be deleted? I think it depends:
- If the ParagraphNode is empty (just a text node with no content), deleting the ParagraphNode and selecting the decorator makes sense
- If the ParagraphNode has content (text before the cursor), pressing backspace at the start should NOT delete the decorator
But wait, if the cursor is at the start of the text node (offset 0), there's no text before it. So pressing backspace would try to merge with the previous block.
OK, I think I've been overthinking this. Let me focus on the specific bug: when the anchor is inside a ListItemNode, don't delete the DecoratorNode.
The fix: check if the anchor (or any of its ancestors up to the decorator's parent) is inside a ListItemNode.
Actually, I just thought of an even simpler fix. The issue is that when state.type === 'initial' and we find a decorator, we always delete it (unless it's isolated). But the "initial" state means we haven't found any block to merge with yet. In this case, we should be more conservative and NOT delete the decorator.
The fix: when state.type === 'initial' and we find a non-isolated decorator, just return without doing anything. Let the normal deletion flow handle the anchor.
This might change the behavior for other cases, but I think it's a safer default. If the anchor is in a ParagraphNode and the previous sibling is a decorator, the behavior would be:
- We find the decorator
- We return without doing anything
- The normal deletion flow handles the ParagraphNode
What would the normal deletion flow do? Let me check...
After the inner if block:
// Handle the deletion around decorators.
const focus = this.focus;
this.modify('extend', isBackward, 'character');
if (!this.isCollapsed()) {
// ...
} else if (isBackward && anchor.offset === 0) {
// Special handling around rich text nodes
if ($collapseAtStart(this, anchor.getNode())) {
return;
}
}
And then this.removeText() at the end.
If modify('extend', 'backward', 'character') doesn't extend the selection (because we're at the start), the selection remains collapsed. Then $collapseAtStart is called, which might handle the case.
Let me check what $collapseAtStart does.
OK let me search for $collapseAtStart function.
I need to continue investigating. Let me search for the $collapseAtStart function to understand what happens after my proposed fix.
<tool_call>
<function=bash_terminal>
<parameter=command>
grep -n "collapseAtStart" /lexical/packages/lexical/src/LexicalSelection.ts
</tool_call>