Skip to content
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

Merged

Conversation

etrepum
Copy link
Collaborator

@etrepum etrepum commented Dec 30, 2024

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 for getNodes() 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 a DecoratorNode 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.

@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Dec 30, 2024
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 30, 2024
Copy link

vercel bot commented Dec 30, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 30, 2024 10:09pm
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 30, 2024 10:09pm

Copy link

size-limit report 📦

Path Size
lexical - cjs 31.21 KB (0%)
lexical - esm 31.1 KB (0%)
@lexical/rich-text - cjs 40.31 KB (0%)
@lexical/rich-text - esm 33.01 KB (0%)
@lexical/plain-text - cjs 38.85 KB (0%)
@lexical/plain-text - esm 30.22 KB (0%)
@lexical/react - cjs 42.13 KB (0%)
@lexical/react - esm 34.26 KB (0%)

@etrepum etrepum changed the title [lexical] Bug Fix: Fix over eager delete character command [lexical] Bug Fix: Fix getNodes over-selection Dec 30, 2024
@@ -1129,7 +1148,7 @@ export class RangeSelection implements BaseSelection {
lastPoint.offset = lastNode.getTextContentSize();
}

selectedNodes.forEach((node) => {
for (const node of selectedNodes) {
Copy link
Collaborator Author

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);
Copy link
Collaborator Author

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.

@etrepum etrepum added this pull request to the merge queue Jan 1, 2025
Merged via the queue into facebook:main with commit aaa9009 Jan 1, 2025
45 of 73 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR
Projects
None yet
3 participants