-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[lexical] Bug Fix: Fix getNodes over-selection #7006
[lexical] Bug Fix: Fix getNodes over-selection #7006
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
size-limit report 📦
|
@@ -1129,7 +1148,7 @@ export class RangeSelection implements BaseSelection { | |||
lastPoint.offset = lastNode.getTextContentSize(); | |||
} | |||
|
|||
selectedNodes.forEach((node) => { | |||
for (const node of selectedNodes) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Loops are generally more performant and provide a better debugging experience than Array.prototype.forEach
const deleteCount = nodes.findIndex( | ||
(node) => !node.is(firstNode) && !node.isBefore(firstNode), | ||
); | ||
nodes.splice(0, deleteCount); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note that this doesn't need any sort of bounds checking because a negative deleteCount is equivalent to 0.
Description
ElementNode.getDescendantByIndex
has error-prone semantics where it will return the last descendant when the index overflows. There was already a workaround for over-selection on the lastNode side forgetNodes()
and this adds a similar workaround for the firstNode side.The most common side-effect of this (literal) edge case is that too many nodes would be deleted by
removeText
, particularly when an element ends with aDecoratorNode
and the point is at the offset after it.Closes #6974
Closes #6851
Closes #6842
Test plan
See new regression test, plus all existing suites.