Repository navigation
[lexical] Bug Fix: Clamp committed selection points when a text node is shrunk #9793
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
Open
Rohan-Dedhia
wants to merge
4
commits into
meta:main
Choose a base branch
from
Rohan-Dedhia:fix/selection-clamp-on-text-shrink
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
58b9e69
[lexical] Bug Fix: Clamp committed selection points when a text node …
Rohan-Dedhia 9b66bfd
fix: address review feedback, clamp text points in beginUpdate only
Rohan-Dedhia 4a4af34
test(lexical): assert anchor and focus keys in Issue9767Repro
Rohan-Dedhia 1aaa1a7
fix(lexical): clamp text selection points before node transforms and …
Rohan-Dedhia 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
This file contains hidden or 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,7 @@ | ||
| --- | ||
| 'lexical': patch | ||
| --- | ||
|
|
||
| [fix] Clamp committed selection points when a text node is shrunk | ||
|
|
||
| Verify: after `spliceText`/`setTextContent` shortens a selected text node, the committed selection sits at the node's new end and a following `splitText` no longer throws in dev. |
This file contains hidden or 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
144 changes: 144 additions & 0 deletions
144
packages/lexical/src/__tests__/unit/Issue9767Repro.test.ts
This file contains hidden or 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,144 @@ | ||
| /** | ||
| * 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. | ||
| * | ||
| */ | ||
|
|
||
| // @vitest-environment node | ||
| import {buildEditorFromExtensions, defineExtension} from '@lexical/extension'; | ||
| import {RichTextExtension} from '@lexical/rich-text'; | ||
| import { | ||
| $createParagraphNode, | ||
| $createTextNode, | ||
| $getRoot, | ||
| $getSelection, | ||
| $isRangeSelection, | ||
| $isTextNode, | ||
| type LexicalEditor, | ||
| type TextNode, | ||
| } from 'lexical'; | ||
| import {describe, expect, test} from 'vitest'; | ||
|
|
||
| // A programmatic shrink of a text node (spliceText without | ||
| // moveSelection, setTextContent) leaves a RangeSelection point past the end | ||
| // of the node; the update commits it, and a later splitText on that node | ||
| // throws (dev) from $getTextNodeOffset. | ||
|
|
||
| const ext = defineExtension({ | ||
| dependencies: [RichTextExtension], | ||
| name: '[selection-past-end]', | ||
| }); | ||
|
|
||
| function setup(): LexicalEditor { | ||
| const editor = buildEditorFromExtensions(ext); | ||
| editor.update( | ||
| () => { | ||
| const a = $createTextNode('ab').toggleFormat('bold'); | ||
| const t = $createTextNode('cde'); | ||
| $getRoot().clear().append($createParagraphNode().append(a, t)); | ||
| const s = t.select(3, 3); | ||
| s.anchor.set(a.getKey(), 1, 'text'); | ||
| }, | ||
| {discrete: true}, | ||
| ); | ||
| return editor; | ||
| } | ||
|
|
||
| const $first = (): TextNode => { | ||
| const t = $getRoot().getFirstDescendant(); | ||
| if (!$isTextNode(t)) { | ||
| throw new Error('no text'); | ||
| } | ||
| return t; | ||
| }; | ||
|
|
||
| const $text = (): TextNode => { | ||
| const t = $getRoot().getLastDescendant(); | ||
| if (!$isTextNode(t)) { | ||
| throw new Error('no text'); | ||
| } | ||
| return t; | ||
| }; | ||
|
|
||
| const shrink = { | ||
| setTextContent: () => $text().setTextContent('a'), | ||
| spliceText: () => $text().spliceText(1, 2, ''), | ||
| 'spliceText, moveSelection (control)': () => | ||
| $text().spliceText(1, 2, '', true), | ||
| } as const; | ||
|
|
||
| describe.each(Object.entries(shrink))('%s', (name, $shrink) => { | ||
| test('the committed selection stays inside the node', () => { | ||
| const editor = setup(); | ||
| editor.update($shrink, {discrete: true}); | ||
| const out = editor.getEditorState().read(() => { | ||
| const s = $getSelection(); | ||
| const a = $first(); | ||
| const t = $text(); | ||
| return s && $isRangeSelection(s) | ||
| ? { | ||
| anchorKey: s.anchor.key, | ||
| anchorOffset: s.anchor.offset, | ||
| firstKey: a.getKey(), | ||
| focusKey: s.focus.key, | ||
| focusOffset: s.focus.offset, | ||
| size: t.getTextContentSize(), | ||
| textKey: t.getKey(), | ||
| } | ||
| : null; | ||
| }); | ||
| expect(out).not.toBeNull(); | ||
| if (name.includes('moveSelection')) { | ||
| expect(out!.anchorKey).toBe(out!.textKey); | ||
| } else { | ||
| expect(out!.anchorKey).toBe(out!.firstKey); | ||
| } | ||
| expect(out!.anchorOffset).toBe(1); | ||
| expect(out!.focusKey).toBe(out!.textKey); | ||
| expect(out!.focusOffset).toBe(out!.size); | ||
| }); | ||
|
|
||
| test('a later splitText on the node does not throw', () => { | ||
| const editor = setup(); | ||
| editor.update($shrink, {discrete: true}); | ||
| let error: unknown = null; | ||
| editor.update( | ||
| () => { | ||
| try { | ||
| $first().splitText(1); | ||
| } catch (e) { | ||
| error = e; | ||
| } | ||
| }, | ||
| {discrete: true}, | ||
| ); | ||
| expect(String(error)).toBe('null'); | ||
| void name; | ||
| }); | ||
| }); | ||
|
|
||
| test('control: an in-bounds selection and the same splitText', () => { | ||
| const editor = setup(); | ||
| editor.update( | ||
| () => { | ||
| const s = $text().select(1, 1); | ||
| s.anchor.set($first().getKey(), 1, 'text'); | ||
| }, | ||
| {discrete: true}, | ||
| ); | ||
| editor.update(() => $text().spliceText(1, 2, ''), {discrete: true}); | ||
| let error: unknown = null; | ||
| editor.update( | ||
| () => { | ||
| try { | ||
| $first().splitText(1); | ||
| } catch (e) { | ||
| error = e; | ||
| } | ||
| }, | ||
| {discrete: true}, | ||
| ); | ||
| expect(String(error)).toBe('null'); | ||
| }); |
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.
Optional: nothing fails today if this block is removed, because the commit-time call at line 1451 repairs the selection for every case in
Issue9767Repro.test.ts. A case where aTextNodetransform callssplitTexton the shrunk node (orHashtagExtensionwithsetTextContent('a #x')on a selectedcdefghij) would pin it; without this block that case throws$getTextNodeOffset: invalid offset 8 for size 4.