-
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
Merged
etrepum
merged 2 commits into
facebook:main
from
etrepum:fix-over-eager-delete-character-command
Jan 1, 2025
+109
−2
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
packages/lexical-playground/__tests__/regression/6974-delete-character-backward.spec.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import { | ||
deleteBackward, | ||
moveToLineBeginning, | ||
} from '../keyboardShortcuts/index.mjs'; | ||
import { | ||
assertHTML, | ||
focusEditor, | ||
html, | ||
initialize, | ||
test, | ||
} from '../utils/index.mjs'; | ||
|
||
test.describe('Regression tests for #6974', () => { | ||
test.beforeEach(({isPlainText, isCollab, page}) => | ||
initialize({isCollab, isPlainText, page}), | ||
); | ||
|
||
test(`deleteCharacter merges children from adjacent blocks even if the previous leaf is an inline decorator`, async ({ | ||
page, | ||
isCollab, | ||
isPlainText, | ||
}) => { | ||
test.skip(isCollab || isPlainText); | ||
await focusEditor(page); | ||
const testEquation = '$x$'; | ||
const testString = 'test'; | ||
await page.keyboard.type(testEquation); | ||
await page.keyboard.press('Enter'); | ||
await page.keyboard.type(testString); | ||
const beforeHtml = html` | ||
<p> | ||
<span contenteditable="false" data-lexical-decorator="true"> | ||
<img alt="" src="#" /> | ||
<span role="button" tabindex="-1"> | ||
<span> | ||
<span aria-hidden="true"> | ||
<span> | ||
<span></span> | ||
<span>x</span> | ||
</span> | ||
</span> | ||
</span> | ||
</span> | ||
<img alt="" src="#" /> | ||
</span> | ||
<br /> | ||
</p> | ||
<p dir="ltr"><span data-lexical-text="true">test</span></p> | ||
`; | ||
await assertHTML(page, beforeHtml, beforeHtml, { | ||
ignoreClasses: true, | ||
ignoreInlineStyles: true, | ||
}); | ||
await moveToLineBeginning(page); | ||
await deleteBackward(page); | ||
const afterHtml = html` | ||
<p dir="ltr"> | ||
<span contenteditable="false" data-lexical-decorator="true"> | ||
<img alt="" src="#" /> | ||
<span role="button" tabindex="-1"> | ||
<span> | ||
<span aria-hidden="true"> | ||
<span> | ||
<span></span> | ||
<span>x</span> | ||
</span> | ||
</span> | ||
</span> | ||
</span> | ||
<img alt="" src="#" /> | ||
</span> | ||
<span data-lexical-text="true">test</span> | ||
</p> | ||
`; | ||
await assertHTML(page, afterHtml, afterHtml, { | ||
ignoreClasses: true, | ||
ignoreInlineStyles: true, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,6 +96,14 @@ export class Point { | |
_selection: BaseSelection | null; | ||
|
||
constructor(key: NodeKey, offset: number, type: 'text' | 'element') { | ||
if (__DEV__) { | ||
// This prevents a circular reference error when serialized as JSON, | ||
// which happens on unit test failures | ||
Object.defineProperty(this, '_selection', { | ||
enumerable: false, | ||
writable: true, | ||
}); | ||
} | ||
this._selection = null; | ||
this.key = key; | ||
this.offset = offset; | ||
|
@@ -473,6 +481,10 @@ export class RangeSelection implements BaseSelection { | |
const lastPoint = isBefore ? focus : anchor; | ||
let firstNode = firstPoint.getNode(); | ||
let lastNode = lastPoint.getNode(); | ||
const overselectedFirstNode = | ||
$isElementNode(firstNode) && | ||
firstPoint.offset > 0 && | ||
firstPoint.offset >= firstNode.getChildrenSize(); | ||
const startOffset = firstPoint.offset; | ||
const endOffset = lastPoint.offset; | ||
|
||
|
@@ -506,6 +518,13 @@ export class RangeSelection implements BaseSelection { | |
} | ||
} else { | ||
nodes = firstNode.getNodesBetween(lastNode); | ||
// Prevent over-selection due to the edge case of getDescendantByIndex always returning something #6974 | ||
if (overselectedFirstNode) { | ||
const deleteCount = nodes.findIndex( | ||
(node) => !node.is(firstNode) && !node.isBefore(firstNode), | ||
); | ||
nodes.splice(0, deleteCount); | ||
} | ||
} | ||
if (!isCurrentlyReadOnlyMode()) { | ||
this._cachedNodes = nodes; | ||
|
@@ -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 commentThe 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 |
||
if ( | ||
!$hasAncestor(firstNode, node) && | ||
!$hasAncestor(lastNode, node) && | ||
|
@@ -1138,7 +1157,7 @@ export class RangeSelection implements BaseSelection { | |
) { | ||
node.remove(); | ||
} | ||
}); | ||
} | ||
|
||
const fixText = (node: TextNode, del: number) => { | ||
if (node.getTextContent() === '') { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.