Let me look at the issue more carefully. The bug is that when deleting the first ListItemNode in a list, the previous DecoratorBlockNode is also removed.
Let me trace through what happens:
Document: Root -> [DecoratorBlockNode, ListNode -> [ListItemNode, ListItemNode]]
When we're at the first ListItemNode and press backspace:
- The
deleteCharacter function is called
- Since the selection is collapsed and there's no text to delete, it enters the exploration loop
iterNodeCarets iterates through the carets
Let me think about the carets:
- If we're at the first ListItemNode, the initial caret points to it
- Then
iterNodeCarets would give us:
- First: the ListItemNode itself (ChildCaret or NodeCaret pointing to it)
- Then: parent carets going up to ListNode
- Then: sibling carets going to DecoratorBlockNode
Actually, let me look at the getAdjacentChildCaret and getParentCaret functions to understand the iteration better.
Wait, let me re-read the deleteCharacter code. The key issue is what happens when we hit the DecoratorNode in the iteration.
Looking at lines 1830-1853:
} else if ($isDecoratorNode(caret.origin)) {
if (caret.origin.isIsolated()) {
// do nothing
} 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();
}
// always stop when a decorator is encountered
return;
}
The issue is in the else branch - it's removing the decorator unconditionally when certain conditions aren't met.
Let me think about what state.type would be when we hit the DecoratorNode...
Actually, I think I need to understand the exact conditions under which this happens. Let me look at the full flow:
- We start with
state = {type: 'initial'}
- We iterate through carets
- For each ChildCaret (non-inline, non-shadowRoot), we update the state
- For each SiblingCaret pointing to an ElementNode, we set
state = {block: caret.origin, type: 'merge-next-block'}
- For each SiblingCaret pointing to a DecoratorNode, we handle it
The key question is: what state are we in when we hit the DecoratorNode?
Let me trace through:
- First caret: ChildCaret pointing to the first ListItemNode (or somewhere inside it)
- It's not inline, not shadowRoot
state updates to merge-block or stays as initial
Actually wait, let me re-read the loop more carefully:
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()) {
// ... handle shadow root
} 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)) {
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)) {
// Handle decorator
...
}
break;
}
}
So when we're at the first ListItemNode and press backspace:
- Initial caret points to the first ListItemNode
- We iterate...
- First we might get ChildCaret pointing to the ListItemNode itself
- Then we get ChildCaret pointing to the ListNode (parent)
- Then we get SiblingCaret pointing to the DecoratorBlockNode
When we hit the SiblingCaret pointing to DecoratorBlockNode:
$isSiblingCaret(caret) is true
$isDecoratorNode(caret.origin) is true
- Check conditions...
What is state.type at this point? If we've been descending through ChildCarets, state.type might be 'merge-block'.
Wait, there's this condition: else if (state.type === 'merge-block') { break; }
So if state.type is 'merge-block' and we hit a non-ChildCaret, non-SiblingCaret, we break. But if we hit a SiblingCaret, we continue processing it...
Actually no, looking at the structure:
if ($isChildCaret(caret)) {
// ...
} else if (state.type === 'merge-block') {
break;
} else if ($isSiblingCaret(caret)) {
// process sibling
}
So if state.type is 'merge-block', we break BEFORE checking if it's a sibling caret. This means we won't process the DecoratorNode as a sibling if we're already in merge-block state.
Hmm, but the issue is that the DecoratorNode is being removed. So state must NOT be 'merge-block' when we hit the DecoratorNode.
Let me think about this differently. What if the ListItemNode is EMPTY? Then the initial caret would point to the ListItemNode itself (an element caret), and iterNodeCarets would start from there.
Actually wait, let me re-read the condition for entering the main loop:
if (
initialRange
.getTextSlices()
.every(slice => slice === null || slice.distance === 0)
) {
This condition checks if there's no text to delete (all slices are null or distance 0). If this is true, we enter the loop.
If the first ListItemNode is empty, then the text slices would all be distance 0, so we enter the loop.
Now, what does iterNodeCarets yield? Let me think...
Actually, I think I need to understand the iteration better. Let me look at how $extendCaretToRange and iterNodeCarets work together.
Actually, let me just look at the existing tests to understand the expected behavior, and then trace through the code.
Let me also look at the issue from a different angle. The bug is that when deleting the first list item, the previous DecoratorBlockNode is also removed. This could happen if:
- The
iterNodeCarets yields a caret pointing to the DecoratorBlockNode as a previous sibling
- The code at line 1830+ handles the DecoratorNode by removing it
Let me look at what conditions would cause the DecoratorNode to be removed:
if ($isDecoratorNode(caret.origin)) {
if (caret.origin.isIsolated()) {
// do nothing
} else if (
state.type === 'merge-next-block' &&
(caret.origin.isKeyboardSelectable() ||
!caret.origin.isInline()) &&
$isElementNode(initialRange.anchor.origin) &&
initialRange.anchor.origin.isEmpty()
) {
// Remove the anchor element and select the decorator
initialRange.anchor.origin.remove();
...
} else {
// Remove the decorator
caret.origin.remove();
}
// always stop when a decorator is encountered
return;
}
The decorator is removed if:
- It's not isolated
- AND either:
- state.type is NOT 'merge-next-block', OR
- caret.origin.isKeyboardSelectable() is false AND caret.origin.isInline() is true, OR
- initialRange.anchor.origin is NOT an ElementNode, OR
- initialRange.anchor.origin is NOT empty
In the case where we're deleting an empty ListItemNode and the previous sibling is a DecoratorBlockNode:
- The DecoratorBlockNode is not isolated
- state.type might not be 'merge-next-block' (depends on the iteration order)
- initialRange.anchor.origin might be the empty ListItemNode
If state.type is 'merge-block' instead of 'merge-next-block', we'd fall into the else branch and remove the decorator.
Wait, but I also need to understand the iteration order. Let me think about this:
Document structure:
Root
├── DecoratorBlockNode
└── ListNode
└── ListItemNode ← anchor is here
If anchor is at the first ListItemNode, and we're going backwards (isBackward = true, direction = 'previous'):
The initialRange would be extended from the anchor caret in the 'previous' direction.
Actually, I realize I need to understand what $extendCaretToRange does. Let me look at it.
Actually, I think the key insight is this: when we're iterating through the carets and we encounter the DecoratorBlockNode as a sibling of the ListNode (parent of our current position), the code sees it as "the previous block" and tries to handle it.
But the current block (the ListItemNode being deleted) and the DecoratorBlockNode don't share the same parent! The ListItemNode's parent is ListNode, and the DecoratorBlockNode is a sibling of ListNode.
So the condition that should protect against removing the DecoratorBlockNode is... what?
Looking at the code again, the DecoratorNode removal doesn't check for shared parents. It just removes the decorator if it's not isolated and the anchor conditions aren't met.
I think the bug might be in this logic. The issue is that when we're inside a list item and press backspace, the code is reaching out to the DecoratorBlockNode that's a sibling of the ListNode, not a sibling of the ListItemNode.
Let me look at what the correct behavior should be:
- If we're at an empty ListItemNode at the beginning of a list
- And the previous sibling of the ListNode is a DecoratorBlockNode
- Pressing backspace should only remove the empty ListItemNode (and possibly the ListNode if it becomes empty)
- It should NOT remove the DecoratorBlockNode
The fix should be to not remove the DecoratorBlockNode in this case. The issue is that the code treats the DecoratorBlockNode as a "previous sibling block" that should be handled, but it's actually a sibling of the ListNode, not the ListItemNode.
Let me look at the specific condition that's being checked:
} else if ($isDecoratorNode(caret.origin)) {
if (caret.origin.isIsolated()) {
// do nothing
} 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();
}
// always stop when a decorator is encountered
return;
}
The issue is in the else branch - it's removing the decorator without checking if it's actually adjacent to the anchor in a meaningful way.
Actually, I think the issue might be more subtle. Let me re-read the logic.
When state.type === 'merge-next-block' AND the decorator is keyboard selectable or not inline AND the anchor is an empty element, we remove the anchor element and select the decorator.
But in our case:
- state.type might not be 'merge-next-block'
- The anchor (first ListItemNode) IS an empty element
- The decorator is NOT inline and IS keyboard selectable
So if state.type is NOT 'merge-next-block', we go to the else branch and remove the decorator.
The question is: why is state.type not 'merge-next-block'?
Let me trace through the iteration more carefully:
- Initial caret: ChildCaret pointing to the ListItemNode (or an element caret if the ListItemNode is selected)
- We iterate:
- First item: ChildCaret pointing to the ListItemNode
- It's not inline, not shadowRoot
- state is 'initial', so we don't enter the
else if (state.type === 'merge-next-block' || state.type === 'merge-block') branch
- We just skip this ChildCaret
- Next: ChildCaret pointing to the ListNode
- Same as above, state is still 'initial'
- Next: SiblingCaret pointing to the DecoratorBlockNode (previous sibling of ListNode)
$isSiblingCaret(caret) is true
$isDecoratorNode(caret.origin) is true
- state.type is 'initial', not 'merge-next-block'
- So we enter the
else branch and remove the decorator!
That's the bug! When state.type is 'initial', we still process the DecoratorNode and remove it.
The fix should be to check if the decorator is actually adjacent to the anchor in a meaningful way, not just as a sibling of some ancestor.
Looking 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 if we're trying to merge with a next block and the anchor is an empty element. If both are true, we handle it specially.
But if state.type is 'initial', we fall into the else branch and remove the decorator unconditionally.
I think the fix should be to check if the decorator is truly adjacent to the anchor (not just through parent relationships). One way to do this is to check if they share the same parent.
Let me look at the other conditions in the loop. There's a check for caret.origin.getParent() === block.getParent() at line 1871:
if (
caret.origin.isEmpty() &&
!block.isEmpty() &&
caret.origin.getParent() === block.getParent()
) {
caret.origin.remove(true);
return;
}
This check ensures that the decorator is removed only if it shares the same parent as the anchor block. But this check is inside the if (state.type === 'merge-block') block, which comes after the decorator handling.
So the fix should be to add a similar check in the decorator handling code, or to not enter the decorator handling code when state.type is 'initial'.
Actually, looking at the comment: "always stop when a decorator is encountered" - the intention seems to be that we should stop when we encounter a decorator. But the issue is that we're removing it first.
I think the fix should be to only remove the decorator if it's truly adjacent to the anchor (shares the same parent), not if it's just a sibling of some ancestor.
Let me look at how the state transitions work more carefully to understand when we should and shouldn't remove the decorator.
Actually, I think the real issue is that when we're inside a nested structure (ListItemNode inside ListNode), we shouldn't be reaching across to the DecoratorBlockNode that's a sibling of ListNode. The iteration is going too far.
Let me look at the comment at line 1830-1832:
} else if ($isDecoratorNode(caret.origin)) {
if (caret.origin.isIsolated()) {
// do nothing, shouldn't delete an isolated decorator
}
The comment says "shouldn't delete an isolated decorator." But it's deleting non-isolated decorators even when they're not truly adjacent to the anchor.
I think the fix should be to check if the decorator is actually adjacent to the anchor's immediate parent, not just anywhere in the tree.
Let me think about what "adjacent" means in this context:
- If the anchor is inside a ListItemNode, and the ListItemNode is inside a ListNode
- The "adjacent" blocks would be siblings of the ListNode (same level as ListNode)
- But the DecoratorBlockNode is indeed a sibling of the ListNode
Wait, but the issue is that the DecoratorBlockNode is NOT "adjacent" to the ListItemNode in the same way that a sibling ListItemNode would be. The ListItemNode is inside the ListNode, and the DecoratorBlockNode is a sibling of the ListNode.
So when we press backspace on the first ListItemNode, we should:
- If the ListItemNode is empty, remove it
- If the ListNode becomes empty, remove it
- NOT touch the DecoratorBlockNode
The issue is that the code is treating the DecoratorBlockNode as something to remove/merge with.
I think the fix should be to add a condition that checks if the decorator is at the same level as the anchor (i.e., if it's a direct sibling, not a sibling of an ancestor).
Let me look 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();
...
} 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 comment in the else branch says "the adjacent decorator is removed." But the decorator isn't truly adjacent in this case - it's a sibling of the ListNode, not a sibling of the ListItemNode.
I think the fix should be to check if the decorator is truly adjacent. One way to do this is to check if the decorator's parent is the same as the anchor's parent's parent (i.e., they're siblings at the same level).
Actually, wait. Let me reconsider. The DecoratorBlockNode IS a sibling of the ListNode, which means it's at the same level. The issue is whether we should be deleting it when we're inside the ListNode.
I think the key insight is that when we're inside a ListNode and delete a ListItemNode, we should only affect the ListNode and its children, not siblings of the ListNode.
The fix should be to check if the decorator is a direct sibling of the anchor's immediate block parent, not just any sibling in the iteration path.
Let me look at the condition state.type === 'merge-next-block'. This condition is true when we've found a merge-next-block element. If we're at a ListItemNode and iterate, the merge-next-block would be the previous sibling of the ListItemNode's parent (ListNode).
So when state.type is 'merge-next-block' and we hit a DecoratorNode that's a sibling of the ListNode, the code correctly handles it.
But when state.type is 'initial', we shouldn't be removing the decorator at all, because we haven't determined that we should merge with anything.
I think the fix should be to not process the decorator when state.type is 'initial'. Let me look at the code structure:
} 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)) {
// Handle decorator
...
}
break;
}
Notice that for ElementNodes, there's a check: else if (!caret.origin.isParentOf(initialRange.anchor.origin)) { break; }. This means if the inline element is not a parent of the anchor, we break.
But for DecoratorNodes, there's no such check. We process them unconditionally.
I think the fix should be similar: if the decorator is not a sibling at the same level as the anchor's immediate block, we should break instead of removing it.
Let me think about what the correct condition would be. The decorator should only be processed if it's a sibling of the anchor's immediate block parent.
Wait, but the decorator IS a sibling of the anchor's block parent (ListNode). So the condition would be true...
Actually, I think the issue is different. Let me re-read the flow.
When state.type is 'initial' and we hit a SiblingCaret pointing to a DecoratorNode:
- We're not in a merge state
- We shouldn't be removing the decorator
But the current code removes it anyway in the else branch.
I think the fix is to check if state.type is NOT 'initial' before removing the decorator. If state.type is 'initial', we should break instead of removing.
Or alternatively, we should check if the anchor is at the "top" level of its structure, not nested inside other elements.
Actually, let me look at the comment again: "When the anchor is not an empty element then the adjacent decorator is removed"
The intention is: if the anchor is NOT an empty element, we remove the adjacent decorator.
But in our case, the anchor (first ListItemNode) IS an empty element. So we should be checking:
- state.type === 'merge-next-block'
- anchor is an empty element
- decorator is keyboard selectable or not inline
If all these conditions are true, we remove the anchor and select the decorator.
But if state.type is NOT 'merge-next-block', we still enter the else branch and remove the decorator.
I think the issue is that state.type should never be 'initial' when we hit a DecoratorNode at the top level. Let me trace through the iteration again.
Actually, I think the issue might be that the ListItemNode is being treated as the anchor, and when we iterate, we go:
- ListItemNode (ChildCaret)
- ListNode (ChildCaret)
- DecoratorBlockNode (SiblingCaret)
But we should also be iterating through:
4. Root (ChildCaret)
5. ...
And the state should be updated as we go through the ChildCaret for ListNode.
Wait, let me re-read the ChildCaret handling:
if ($isChildCaret(caret)) {
if (caret.origin.isInline()) {
// fall through when descending an inline
} else if (caret.origin.isShadowRoot()) {
// ...
} 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'};
}
}
So for a ChildCaret pointing to ListNode:
- It's not inline
- It's not shadowRoot
- If state.type is 'merge-next-block' or 'merge-block', we update the state
- Otherwise, we do nothing
So if state.type is 'initial' when we hit the ChildCaret for ListNode, we do nothing and continue to the next caret.
Then we hit the SiblingCaret for DecoratorBlockNode. At this point, state.type is still 'initial'.
So the issue is clear: state.type is 'initial' when we hit the DecoratorNode, and we remove it.
The fix should be to check if state.type is NOT 'initial' before removing the decorator. If state.type is 'initial', we should break out of the loop.
Actually wait, let me re-read the code flow. After the loop, there's:
if (state.type === 'merge-block') {
// ... handle merge
}
So if state.type is 'initial', we just continue without doing anything.
But the issue is that we're entering the decorator handling code before we exit the loop.
Let me look at the code structure 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)) {
// ...
} else if ($isDecoratorNode(caret.origin)) {
// REMOVE THE DECORATOR
return;
}
break;
}
}
So when we hit a SiblingCaret pointing to a DecoratorNode, we immediately remove it and return.
The fix should be to check if we should actually remove the decorator. If state.type is 'initial', we shouldn't be removing it.
Let me think about what the correct behavior should be:
- If we're in an empty element at the beginning of a list
- And we press backspace
- We should remove the empty element (and possibly the list)
- We should NOT touch siblings of the list
The current code is treating the DecoratorBlockNode as something to merge with, but it shouldn't be, because we're inside a list structure.
I think the fix should be to add a condition: if state.type is 'initial', we should break instead of processing the decorator.
Let me draft the 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' &&
(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') {
// 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 this changes the behavior. Let me think more carefully.
Actually, I think the issue is different. Let me re-read the original code:
} else if ($isDecoratorNode(caret.origin)) {
if (caret.origin.isIsolated()) {
// do nothing
} 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();
}
return;
}
The else branch says "When the anchor is not an empty element then the adjacent decorator is removed."
But the condition for the if branch is:
- state.type === 'merge-next-block'
- decorator is keyboard selectable or not inline
- anchor is an element
- anchor is empty
If ANY of these conditions is false, we go to the else branch.
So the else branch is entered when:
- state.type !== 'merge-next-block', OR
- decorator is NOT keyboard selectable AND is inline, OR
- anchor is NOT an element, OR
- anchor is NOT empty
In our case:
- state.type is 'initial' (not 'merge-next-block')
- anchor is the empty ListItemNode (an element, empty)
So we enter the else branch because state.type is not 'merge-next-block'.
The comment in the else branch says "When the anchor is not an empty element then the adjacent decorator is removed." But this comment doesn't match the code - the else branch is entered even when the anchor IS an empty element (because state.type is not 'merge-next-block').
I think the issue is that the comment is misleading, and the code has a bug.
The fix should be to change the condition so that we don't remove the decorator when state.type is 'initial'.
Let me think about the correct behavior:
- If state.type is 'initial', we're not trying to merge with anything, so we shouldn't remove the decorator
- If state.type is 'merge-next-block' and the decorator is truly adjacent (keyboard selectable, not inline) and the anchor is an empty element, we remove the anchor and select the decorator
- If state.type is 'merge-next-block' and the decorator is NOT truly adjacent (inline, not keyboard selectable), we might still want to handle it somehow
I think the simplest fix is:
} else if ($isDecoratorNode(caret.origin)) {
if (caret.origin.isIsolated()) {
// do nothing
} else if (state.type === 'initial') {
// Don't remove the decorator if we're not in a merge state
break;
} 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();
}
return;
}
Wait, but adding a break here would still exit the loop without removing the decorator. Let me think about whether that's the correct behavior.
Actually, if we break when state.type is 'initial', we exit the loop and continue to the code after the loop:
if (state.type === 'merge-block') {
// ... handle merge
}
Since state.type is 'initial', we don't enter this block either. So we just fall through without doing anything.
But wait, we're in the deleteCharacter method, and we've already checked that there's no text to delete. If we don't do anything, the backspace operation would effectively be a no-op, which might not be the desired behavior.
Let me think about what should happen:
- We're at an empty ListItemNode
- We press backspace
- We should remove the ListItemNode
But the ListItemNode is inside a ListNode. If we remove the ListItemNode, the ListNode might become empty, and then the ListNode would be removed too.
So the correct behavior is to remove the ListItemNode (and potentially the ListNode), not the DecoratorBlockNode.
The issue is that the code is reaching across the ListNode boundary and removing the DecoratorBlockNode.
I think the fix is to not process the DecoratorBlockNode when state.type is 'initial'. Instead, we should break out of the loop and let the default behavior handle removing the empty ListItemNode.
But wait, the default behavior for removing an empty element is different. Let me look at the code after the loop...
Actually, looking at the code structure, if state.type is 'initial' after the loop, we just exit the entire block and continue with the rest of the deleteCharacter method:
// Handle the deletion around decorators.
const focus = this.focus;
this.modify('extend', isBackward, 'character');
So we extend the selection and try to delete more. This might be the correct behavior for non-empty elements, but for empty elements, we should have removed them already.
Hmm, let me re-think this. The issue is that we're in the "exploration" phase of deleteCharacter, where we're looking for opportunities to merge blocks. If we don't find a valid merge opportunity (state.type is 'initial'), we should exit this phase and do something else.
But the current code is finding a "merge opportunity" with the DecoratorBlockNode and removing it.
I think the fix is to not treat the DecoratorBlockNode as a merge opportunity when state.type is 'initial'.
Actually, let me look at the code again. The issue might be simpler than I thought.
Looking at the condition:
} else if ($isDecoratorNode(caret.origin)) {
if (caret.origin.isIsolated()) {
// do nothing
} else if (
state.type === 'merge-next-block' &&
(caret.origin.isKeyboardSelectable() ||
!caret.origin.isInline()) &&
$isElementNode(initialRange.anchor.origin) &&
initialRange.anchor.origin.isEmpty()
) {
// ...
} else {
// remove the decorator
}
return;
}
The issue is the else branch. It says "When the anchor is not an empty element then the adjacent decorator is removed." But the code removes the decorator whenever the condition is not met, which includes when state.type is 'initial'.
I think the fix is to only enter the else branch when state.type is not 'initial' and we're actually trying to merge with something.
Let me also consider: what is the intended behavior when we're inside a list and press backspace on the first item?
If the list item is empty and it's the only item, the entire list should be removed (but not the previous sibling).
If the list item is empty and there are other items, only the list item should be removed.
In both cases, the previous sibling (DecoratorBlockNode) should NOT be touched.
So the fix should be to not remove the DecoratorBlockNode. The simplest fix is to add a condition that checks if the decorator is truly adjacent to the anchor (at the same level).
Let me look at what makes a decorator "truly adjacent":
- The decorator and the anchor share the same parent, OR
- The decorator is a sibling of the anchor's immediate block parent, AND the anchor is at the top level of its structure
For case 1, this would be: decorator and anchor are siblings of the same parent.
For case 2, this would be: decorator is a sibling of the ListNode, and the anchor is a direct child of the ListNode (not nested deeper).
In our case, the anchor is a direct child of the ListNode, and the decorator is a sibling of the ListNode. So case 2 applies.
But wait, the code IS entering this path. The issue is what we do after entering.
Let me re-read the condition:
state.type === 'merge-next-block' &&
(caret.origin.isKeyboardSelectable() ||
!caret.origin.isInline()) &&
$isElementNode(initialRange.anchor.origin) &&
initialRange.anchor.origin.isEmpty()
For the DecoratorNode to be treated as a merge target (where we remove the anchor and select the decorator), ALL of these must be true:
- state.type is 'merge-next-block'
- decorator is keyboard selectable or not inline
- anchor is an element
- anchor is empty
If ALL of these are true, we remove the anchor (empty ListItemNode) and select the decorator.
But if any of these is false, we go to the else branch and remove the decorator instead.
The issue is that state.type is 'initial', not 'merge-next-block'. So we go to the else branch and remove the decorator.
I think the fix is to check if state.type is 'initial' before removing the decorator. If state.type is 'initial', we should break out of the loop and let the default behavior handle the deletion.
Actually, I realize that the issue might be more nuanced. Let me think about when we SHOULD remove a decorator:
- When we're inside a decorator and press backspace - we should enter the decorator
- When we're at an empty element and the previous sibling is a non-isolated decorator - we might want to select the decorator instead
For case 2, the anchor should be at the same level as the decorator (sibling relationship). If the anchor is nested inside a ListNode, it's not truly at the same level as the decorator.
I think the fix should be to check if the decorator is at the same level as the anchor's immediate block parent. If not, we should break instead of removing.
One way to check this is to compare the parents. The decorator should be a sibling of the anchor's immediate parent.
In our case:
- Anchor's immediate parent is ListNode
- Decorator's parent is Root
- ListNode's parent is Root
- So decorator is a sibling of ListNode, which is the anchor's parent
So the decorator IS at the same level as the anchor's parent. This is exactly what we're dealing with.
But the issue is that we shouldn't be removing the decorator just because it's a sibling of the anchor's parent. The reason is that the anchor is nested inside the ListNode, and we're trying to delete the ListItemNode. The DecoratorBlockNode is not related to the deletion.
I think the key insight is: the decorator should only be removed if the anchor is at the "top" level, not nested inside other elements.
But wait, the anchor (ListItemNode) IS at the top level of the ListNode. It's a direct child of the ListNode, not nested deeper.
Hmm, let me think about this differently.
The issue is that when we're iterating through the carets, we're crossing boundaries (from ListItemNode to ListNode to DecoratorBlockNode). The code treats the DecoratorBlockNode as a sibling of the ListNode and decides to remove it.
But the correct behavior should be: when we're inside a ListItemNode and press backspace, we should only affect the ListItemNode and its parent (ListNode), not the siblings of the ListNode.
I think the fix should be to check if the decorator is a sibling of the anchor (not just a sibling of any ancestor). If it's a sibling of an ancestor, we should break instead of removing it.
One way to check this is:
- Get the anchor's immediate parent
- Check if the decorator is a sibling of the anchor's immediate parent
- If the decorator is a sibling of the anchor's immediate parent, AND the anchor is empty, AND we're not in a merge state, we should NOT remove the decorator.
Actually, let me re-read the comment:
} else {
// When the anchor is not an empty element then the
// adjacent decorator is removed
}
The comment says "When the anchor is not an empty element." But the condition for the if branch includes initialRange.anchor.origin.isEmpty(). So the else branch is entered when the anchor IS an empty element AND state.type is not 'merge-next-block'.
I think the comment is wrong. The else branch is entered when:
- state.type is NOT 'merge-next-block', OR
- decorator is inline AND not keyboard selectable, OR
- anchor is NOT an element, OR
- anchor is NOT empty
In our case, the anchor IS an empty element, but state.type is 'initial'. So we enter the else branch.
I think the fix is to add a condition that checks if we're in a merge state before removing the decorator:
} else if ($isDecoratorNode(caret.origin)) {
if (caret.origin.isIsolated()) {
// do nothing
} else if (state.type === 'initial') {
// Don't process the decorator if we're not in a merge state
break;
} else if (
state.type === 'merge-next-block' &&
(caret.origin.isKeyboardSelectable() ||
!caret.origin.isInline()) &&
$isElementNode(initialRange.anchor.origin) &&
initialRange.anchor.origin.isEmpty()
) {
// Remove the anchor and select the decorator
} else {
// Remove the decorator
}
return;
}
Wait, but if we break here, we exit the loop and continue with the rest of the deleteCharacter method. What happens after that?
Let me look at the code after the loop:
// Handle the deletion around decorators.
const focus = this.focus;
this.modify('extend', isBackward, 'character');
if (!this.isCollapsed()) {
// ... handle non-collapsed selection
}
So if we break out of the loop, we extend the selection and try to delete. This might cause the empty ListItemNode to be selected and deleted.
Actually, let me trace through what happens after we break:
- We extend the selection by one character in the backward direction
- The selection now includes the caret before the first ListItemNode
- We try to delete this extended selection
But the caret before the first ListItemNode is the caret between the DecoratorBlockNode and the ListNode. Extending the selection here would select the "space" between them, which might not be a valid target.
Hmm, this is getting complicated. Let me think about a different approach.
Actually, I think the issue might be simpler. Let me re-read the code:
} 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)) {
// ...
}
break;
}
For ElementNodes, there's a continue statement that skips the final break. This means we continue iterating to find more sibling carets.
But for DecoratorNodes, we process them and then return (after the if-else chain). This means we don't continue iterating.
The issue is that we're processing the DecoratorNode before we've had a chance to update state.type to 'merge-next-block' or 'merge-block'.
Wait, let me re-read the flow. The iteration goes:
- First, we iterate through ChildCaret (descending into the structure)
- Then, we iterate through SiblingCaret (checking siblings)
For our document:
Root
├── DecoratorBlockNode
└── ListNode
└── ListItemNode ← anchor
The carets would be:
- ChildCaret for ListItemNode
- ChildCaret for ListNode (parent of ListItemNode)
- SiblingCaret for DecoratorBlockNode (previous sibling of ListNode)
When we process each caret:
- ChildCaret for ListItemNode: state is 'initial', we do nothing
- ChildCaret for ListNode: state is 'initial', we do nothing
- SiblingCaret for DecoratorBlockNode: we process the decorator
But wait, the iteration might not be in this order. Let me look at the iterNodeCarets implementation again.
From the code:
iterNodeCarets(rootMode: RootMode = 'root'): IterableIterator<NodeCaret<D>> {
const anchor = $isTextPointCaret(this.anchor)
? this.anchor.getSiblingCaret()
: this.anchor.getLatest();
const focus = this.focus.getLatest();
const isTextFocus = $isTextPointCaret(focus);
const step = (state: NodeCaret<D>) =>
state.isSameNodeCaret(focus)
? null
: $getAdjacentChildCaret(state) || state.getParentCaret(rootMode);
return makeStepwiseIterator({
hasNext: (state: null | NodeCaret<D>): state is NodeCaret<D> =>
state !== null && !(isTextFocus && focus.isSameNodeCaret(state)),
initial: anchor.isSameNodeCaret(focus) ? null : step(anchor),
map: state => state,
step,
});
}
The step function calls $getAdjacentChildCaret(state) || state.getParentCaret(rootMode). So it first tries to get the adjacent child caret, and if that fails, it gets the parent caret.
So the iteration would be:
- Start from the anchor
- Get adjacent child caret (if available) or parent caret
- Continue until we reach the focus
If the anchor is a ChildCaret (pointing to ListItemNode), and there are no children, the first step would be to get the parent caret (ListNode).
So the iteration order might be:
- ListNode (parent of ListItemNode)
- DecoratorBlockNode (previous sibling of ListNode)
- ...
Or:
- ListItemNode (initial)
- ListNode (parent)
- DecoratorBlockNode (previous sibling)
I need to understand the iteration better, but let me focus on the fix.
The key issue is: when we're iterating and hit a DecoratorNode that's a sibling of the ListNode (parent of our anchor), we shouldn't remove it if state.type is 'initial'.
I think the fix is to add a condition to check if state.type is 'initial', and if so, break out of the loop without removing the decorator.
Let me draft the fix:
} else if ($isDecoratorNode(caret.origin)) {
if (caret.origin.isIsolated()) {
// do nothing, shouldn't delete an isolated decorator
} else if (state.type === 'initial') {
// Don't remove the decorator if we're not in a merge state.
// This can happen when the anchor is nested inside an ElementNode
// (e.g., ListItemNode inside ListNode), and the decorator is a
// sibling of that ElementNode. We should not cross that boundary.
break;
} 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;
}
Actually, wait. If we break here, we don't return from the function. We just exit the loop and continue with the code after the loop. But the code after the loop might not handle the case of "delete the empty ListItemNode."
Let me look at the code after the loop:
}
// Handle the deletion around decorators.
const focus = this.focus;
this.modify('extend', isBackward, 'character');
So after the loop, we extend the selection and try to delete. This might work correctly if the ListItemNode is selected.
But actually, I think the issue is that the return statement after processing the DecoratorNode is what prevents us from continuing with the default behavior. If we break instead, we exit the loop and continue with the code after the loop.
Let me also check: is there code that handles the case of "anchor is an empty element at the beginning of a list"?
Looking at the code after the loop, it handles "Handle the deletion around decorators." This might include the case of removing an empty element.
Actually, I think the fix might be correct. If we break when state.type is 'initial', we exit the loop and continue with the code after the loop. The code after the loop might handle the deletion correctly (by extending the selection and deleting).
But I'm not 100% sure. Let me look at the code after the loop more carefully:
// Handle the deletion around decorators.
const focus = this.focus;
this.modify('extend', isBackward, 'character');
if (!this.isCollapsed()) {
// ... handle non-collapsed selection
// This includes calls to this.removeText() which might delete the empty ListItemNode
}
So after breaking from the loop, we extend the selection by one character. If the anchor is at the beginning of the ListItemNode, extending by one character might select the ListItemNode itself. Then this.removeText() would delete it.
But actually, I'm not sure if extending by one character would select the ListItemNode. The character before the anchor (going backward) is the caret between the DecoratorBlockNode and the ListNode. So extending the selection would select this caret, not the ListItemNode.
Hmm, this is getting complicated. Let me think about a different approach.
Actually, I think the key insight is: when we're at an empty element and press backspace, we should enter the merge-block state (not just merge-next-block). This way, the code after the loop would handle the deletion correctly.
But looking at the code, the merge-block state is only set when we're descending through ChildCarets:
} 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'};
}
This only updates the state if state.type is already 'merge-next-block' or 'merge-block'. So we can never enter the merge-block state if we start with 'initial'.
I think the fix might be to also update the state when state.type is 'initial':
} else if (
state.type === 'merge-next-block' ||
state.type === 'merge-block' ||
state.type === 'initial'
) {
// Keep descending ChildCaret to find which block to merge with
state = {block: state.block, caret, type: 'merge-block'};
}
But this might change the behavior in other cases. Let me think...
Actually, no. I think the issue is specific to the DecoratorNode handling. When we hit a DecoratorNode and state.type is 'initial', we shouldn't remove the decorator. Instead, we should break and let the default behavior handle the deletion.
But I need to make sure that the default behavior actually handles the deletion correctly.
Let me trace through what happens if we break when state.type is 'initial':
- We exit the loop with state.type = 'initial'
- We skip the
if (state.type === 'merge-block') block (because state.type is 'initial')
- We call
this.modify('extend', isBackward, 'character')
- We extend the selection by one character
- If the selection is not collapsed, we enter the
if (!this.isCollapsed()) block
In step 4, extending by one character means we select one more character before the anchor. If the anchor is at the beginning of the ListItemNode (element caret with offset 0), extending by one character might select the caret before the ListItemNode (between the DecoratorBlockNode and the ListNode).
But wait, if we're at an element caret with offset 0, extending by one character might select the previous node (DecoratorBlockNode) instead of nothing.
Actually, I think the issue is more nuanced. Let me look at how modify('extend', isBackward, 'character') works.
The modify method extends the selection by one unit (character, word, or line) in the given direction. If we're at an element caret and extend by one character, we might select the previous element (DecoratorBlockNode) or the caret between them.
Hmm, this is getting too complicated to trace through mentally. Let me just look at existing tests and see if there's a pattern.
Actually, let me take a step back and think about the simplest fix.
The issue is: when deleting the first ListItemNode, the previous DecoratorBlockNode is also deleted.
The fix should be: don't delete the DecoratorBlockNode when it's not truly adjacent to the anchor.
The simplest fix is to check if the decorator is at the same level as the anchor's immediate parent. If not, don't remove it.
Actually, let me re-read the code comment:
// Empty adjacent block at the same nesting level: remove it
// instead of merging, so the current block's type (e.g.
// heading) survives. Limiting to a shared parent leaves
// structural wrappers like a ListNode containing an empty
// ListItemNode to the default cross-block merge — the
// ListNode is not considered empty just because its only
// child is.
if (
caret.origin.isEmpty() &&
!block.isEmpty() &&
caret.origin.getParent() === block.getParent()
) {
caret.origin.remove(true);
return;
}
This code is checking if the adjacent block (caret.origin) is at the same nesting level as the current block (block). If so, it removes the adjacent block.
The key condition is caret.origin.getParent() === block.getParent(). This checks if they share the same parent.
But this check is inside the if (state.type === 'merge-block') block, not inside the DecoratorNode handling.
I think the fix is to add a similar check in the DecoratorNode handling. Specifically, we should check if the decorator is at the same level as the anchor's immediate parent.
Let me think about what the check should be:
- The decorator should be at the same level as the anchor's immediate parent (i.e., sibling of the anchor's parent)
- OR the decorator should be a sibling of the anchor itself (if the anchor is a direct child of the parent)
Actually, I think the check should be simpler: the decorator should be a sibling of the anchor's immediate parent. If the decorator is a sibling of a grandparent or higher, we shouldn't remove it.
In our case:
- Anchor's immediate parent is ListNode
- Decorator's parent is Root
- ListNode's parent is Root
- So decorator is a sibling of ListNode (the anchor's parent)
So the decorator IS a sibling of the anchor's parent. This is a valid adjacency.
But the issue is that we're inside the ListNode, and we're deleting the ListItemNode. The DecoratorBlockNode is a sibling of the ListNode, not a sibling of the ListItemNode.
I think the key insight is: the deletion should only affect the ListNode and its children, not siblings of the ListNode.
So the fix should be to check if the anchor is inside a ListNode, and if so, don't remove the decorator that's a sibling of the ListNode.
Actually, let me think about this differently. The issue is that the iterNodeCarets is crossing boundaries (from ListItemNode to ListNode to DecoratorBlockNode). The DecoratorBlockNode is a sibling of the ListNode, which is the anchor's parent.
When we're iterating, we should stop at the boundary of the ListNode. In other words, we shouldn't treat the DecoratorBlockNode as a sibling of the ListItemNode.
The issue is that the code doesn't distinguish between:
- A decorator that's a sibling of the ListItemNode (true adjacency)
- A decorator that's a sibling of the ListNode (adjacency through parent)
For case 1, we might want to remove the decorator.
For case 2, we should NOT remove the decorator.
The fix should be to check if the decorator is a sibling of the anchor itself (not just a sibling of the anchor's parent).
One way to check this:
- Get the anchor's immediate parent
- If the anchor's immediate parent is the same as the decorator's parent, they're siblings (case 1)
- If the anchor's immediate parent is different from the decorator's parent, they're not direct siblings (case 2)
In our case:
- Anchor's immediate parent is ListNode
- Decorator's parent is Root
- They're different, so the decorator is NOT a sibling of the anchor
So the fix should be to check if the decorator is a sibling of the anchor (same parent). If not, don't remove it.
Let me draft the 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' &&
(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 (
caret.origin.getParent() !== initialRange.anchor.origin.getParent()
) {
// The decorator is not a sibling of the anchor, don't remove it.
// This can happen when the anchor is nested inside an ElementNode
// (e.g., ListItemNode inside ListNode), and the decorator is a
// sibling of that ElementNode.
break;
} 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 this check might not be correct either. Let me think about the caret.origin.getParent() !== initialRange.anchor.origin.getParent() condition.
caret.origin is the DecoratorBlockNode
caret.origin.getParent() is Root
initialRange.anchor.origin is the ListItemNode
initialRange.anchor.origin.getParent() is ListNode
So Root !== ListNode is true, and we would break.
But wait, initialRange.anchor.origin is the anchor node, which is the ListItemNode. And the DecoratorBlockNode's parent is Root, while the ListItemNode's parent is ListNode. So they don't share the same parent.
But the issue is: the DecoratorBlockNode IS a sibling of the ListNode (parent of the ListItemNode). So the decorator is related to the anchor, just not at the same level.
The question is: should we remove the decorator in this case?
I think the answer is NO. The reason is that we're inside the ListNode (a structural wrapper), and we shouldn't be removing the decorator that's a sibling of the ListNode.
So the fix should be to check if the decorator is at the same level as the anchor (same parent). If not, don't remove it.
But actually, I realize there might be cases where we DO want to remove a decorator that's a sibling of the anchor's parent. For example:
- If the anchor is an empty paragraph that's a child of a TableCellNode
- And the previous sibling of the TableCellNode is a DecoratorNode
- We might want to remove the DecoratorNode when we delete the empty paragraph
So the check should be more nuanced. We should only NOT remove the decorator if the anchor is inside a "structural wrapper" like a ListNode.
Actually, let me think about this differently. The issue is specific to the ListNode case. The ListNode is a structural wrapper that contains ListItemNodes. When we delete an empty ListItemNode, we shouldn't affect the siblings of the ListNode.
But for other cases (e.g., TableCellNode), we might want to affect the siblings of the TableCellNode.
I think the fix should be to check if the anchor is inside a ListNode. If so, don't remove the decorator that's a sibling of the ListNode.
But this would be a special case for ListNode. What if there are other structural wrappers in the future?
Let me think about a more general solution. The issue is that the iterNodeCarets is crossing boundaries. When we're inside a ListNode and iterate, we're crossing into the parent of the ListNode and finding siblings.
The fix might be to check if the decorator is a "direct sibling" of the anchor's immediate parent. If the decorator is a sibling of the anchor's parent, AND the anchor's parent is a "structural wrapper" (like ListNode), we should NOT remove the decorator.
But how do we identify "structural wrappers"? One way is to check if the anchor's parent canBeEmpty() returns false. If it can't be empty, it's a structural wrapper.
Actually, let me look at the ListNode:
canBeEmpty(): false {
return false;
}
So ListNode cannot be empty. This means if the ListNode becomes empty (because we removed all its children), it will be automatically removed.
But the issue is that we're removing the decorator BEFORE the ListNode is removed. So the ListNode still exists when we're checking if we should remove the decorator.
Hmm, this is getting complicated. Let me look at the code flow again:
- We're at the first ListItemNode
- We press backspace
deleteCharacter is called
- We iterate through carets and hit the DecoratorBlockNode
- We remove the DecoratorBlockNode (BUG!)
- We return
The issue is that we're removing the decorator in step 5, which is wrong.
Let me think about what SHOULD happen:
- We're at the first ListItemNode
- We press backspace
deleteCharacter is called
- We iterate through carets
- We find that there's nothing to merge with (because the decorator is not a true adjacency)
- We exit the loop and try the default behavior
- The default behavior removes the empty ListItemNode
So the fix should be to not remove the decorator and let the default behavior handle it.
The question is: what is the default behavior for removing an empty element?
Looking at the code after the loop:
// Handle the deletion around decorators.
const focus = this.focus;
this.modify('extend', isBackward, 'character');
if (!this.isCollapsed()) {
// ... handle non-collapsed selection
}
So after exiting the loop, we extend the selection by one character. This might select the caret between the DecoratorBlockNode and the ListNode.
Then, if the selection is not collapsed, we might call this.removeText(), which would remove the selected text.
But since there's no text between the DecoratorBlockNode and the ListNode, this might not do anything useful.
Actually, I think I need to trace through the actual behavior more carefully. Let me look at the code that handles the case of "empty anchor element."
Wait, I think I need to look at the code before the loop. Let me look at the full deleteCharacter method.
Actually, let me look at the code that handles the forwardDeletion first:
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;
}
So we first try forwardDeletion. If that returns true, we return. Otherwise, we continue.
Let me look at forwardDeletion:
<tool_call>
<function=bash_terminal>
<parameter=command>
grep -n "forwardDeletion" /lexical/packages/lexical/src/LexicalSelection.ts | head -20
</tool_call>