Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/max-length-paste-keeps-fit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lexical/selection': patch
---

[fix] `$trimTextContentFromAnchor` keeps the part of a paste that fits under the limit and no longer cuts an emoji or other grapheme in half

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: CONTRIBUTING.md ("Changesets") and AGENTS.md ask a fix that changes observable behavior to carry a Verify: paragraph, so it is listed under "Behavior changes". For example:

Suggested change
[fix] `$trimTextContentFromAnchor` keeps the part of a paste that fits under the limit and no longer cuts an emoji or other grapheme in half
[fix] `$trimTextContentFromAnchor` keeps the part of a paste that fits under the limit and no longer cuts an emoji or other grapheme in half
Verify: with a max-length transform that calls `$trimTextContentFromAnchor`, paste text that crosses the limit after existing text: the start of the paste is kept up to the limit and the caret ends after it. An emoji or other grapheme that crosses the limit is dropped whole, so the text can end a few characters short of the limit. Text that is already over the limit is cut to it by the next edit instead of the edit being ignored.

25 changes: 25 additions & 0 deletions packages/lexical-playground/__tests__/e2e/MaxLength.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@ test.describe('MaxLength', () => {
);
});

test(`keeps the start of an over-limit paste after existing text`, async ({
page,
}) => {
await focusEditor(page);
await page.keyboard.type('lorem ipsum ');
await pasteFromClipboard(page, {
'text/plain': 'dolor sit amet, consectetuer adipiscing elit',
});

await assertHTML(
page,
html`
<p class="PlaygroundEditorTheme__paragraph" dir="auto">
<span data-lexical-text="true">lorem ipsum dolor sit amet, co</span>
</p>
`,
);
await assertSelection(page, {
anchorOffset: 30,
anchorPath: [0, 0, 0],
focusOffset: 30,
focusPath: [0, 0, 0],
});
});

test(`can restrict emojis on boundaries`, async ({page}) => {
await focusEditor(page);
await pasteFromClipboard(page, {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
/**
* 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} from '@lexical/extension';
import {$trimTextContentFromAnchor} from '@lexical/selection';
import {
$createParagraphNode,
$createTextNode,
$getRoot,
$getSelection,
$isRangeSelection,
type LexicalEditor,
} from 'lexical';
import {describe, expect, test, vi} from 'vitest';

/**
* Commit `paragraphs` with the caret at `caret` (paragraph index, offset),
* then paste `text` at the caret and trim to `maxLength` from the anchor,
* the way MaxLengthPlugin does in a transform of the same update.
*/
function pasteWithMaxLength(
editor: LexicalEditor,
paragraphs: string[],
caret: [number, number],
text: string,
maxLength: number,
) {
editor.update(
() => {
$getRoot().clear();
paragraphs.forEach((content, index) => {
const paragraph = $createParagraphNode();
$getRoot().append(paragraph);
if (content === '') {
if (index === caret[0]) {
paragraph.select();
}
return;
}
const textNode = $createTextNode(content);
paragraph.append(textNode);
if (index === caret[0]) {
textNode.select(caret[1], caret[1]);
}
});
},
{discrete: true},
);
editor.update(
() => {
const selection = $getSelection();
if (!$isRangeSelection(selection)) {
throw new Error('Expected a range selection');
}
selection.insertText(text);
const delCount = $getRoot().getTextContentSize() - maxLength;
if (delCount > 0) {
$trimTextContentFromAnchor(editor, selection.anchor, delCount);
}
},
{discrete: true},
);
return editor.read(() => {
const selection = $getSelection();
if (!$isRangeSelection(selection)) {
throw new Error('Expected a range selection');
}
return {
anchorOffset: selection.anchor.offset,
anchorText: selection.anchor.getNode().getTextContent(),
isCollapsed: selection.isCollapsed(),
size: $getRoot().getTextContentSize(),
text: $getRoot().getTextContent(),
};
});
}

describe('$trimTextContentFromAnchor (#3775)', () => {
test('keeps the start of a paste into an empty editor', () => {
using editor = buildEditorFromExtensions({name: 'trim-test'});
expect(pasteWithMaxLength(editor, [''], [0, 0], '0123456789', 4)).toEqual({
anchorOffset: 4,
anchorText: '0123',
isCollapsed: true,
size: 4,
text: '0123',
});
});

test('keeps the start of a paste after existing text', () => {
using editor = buildEditorFromExtensions({name: 'trim-test'});
expect(pasteWithMaxLength(editor, ['a'], [0, 1], '0123456789', 5)).toEqual({
anchorOffset: 5,
anchorText: 'a0123',
isCollapsed: true,
size: 5,
text: 'a0123',
});
});

test('trims a paste in the middle of the text just before the caret', () => {
using editor = buildEditorFromExtensions({name: 'trim-test'});
expect(
pasteWithMaxLength(editor, ['Hello world'], [0, 5], 'XYZ', 13),
).toEqual({
anchorOffset: 7,
anchorText: 'HelloXY world',
isCollapsed: true,
size: 13,
text: 'HelloXY world',
});
});

test('trims a paste into the last of several paragraphs', () => {
using editor = buildEditorFromExtensions({name: 'trim-test'});
expect(
pasteWithMaxLength(editor, ['abc', 'de'], [1, 2], '123456', 10),
).toEqual({
anchorOffset: 5,
anchorText: 'de123',
isCollapsed: true,
size: 10,
text: 'abc\n\nde123',
});
});

test('drops an emoji whole instead of splitting its surrogate pair', () => {
using editor = buildEditorFromExtensions({name: 'trim-test'});
expect(pasteWithMaxLength(editor, ['abc'], [0, 3], 'd💏', 5)).toEqual({
anchorOffset: 4,
anchorText: 'abcd',
isCollapsed: true,
size: 4,
text: 'abcd',
});
});

test('drops a multi code point grapheme whole', () => {
using editor = buildEditorFromExtensions({name: 'trim-test'});
expect(pasteWithMaxLength(editor, ['ab'], [0, 2], 'c👨‍💻', 6)).toEqual({
anchorOffset: 3,
anchorText: 'abc',
isCollapsed: true,
size: 3,
text: 'abc',
});
});

test('drops a flag emoji whole', () => {
using editor = buildEditorFromExtensions({name: 'trim-test'});
expect(pasteWithMaxLength(editor, ['ab'], [0, 2], 'c🇨🇦', 5)).toEqual({
anchorOffset: 3,
anchorText: 'abc',
isCollapsed: true,
size: 3,
text: 'abc',
});
});

test('drops a letter with a combining mark whole', () => {
using editor = buildEditorFromExtensions({name: 'trim-test'});
expect(pasteWithMaxLength(editor, ['ab'], [0, 2], 'ce\u0301', 4)).toEqual({
anchorOffset: 3,
anchorText: 'abc',
isCollapsed: true,
size: 3,
text: 'abc',
});
});

test('drops an emoji whole when the cut lands in an earlier node', () => {
Comment thread
rootnode-bot[bot] marked this conversation as resolved.
Resolved
using editor = buildEditorFromExtensions({name: 'trim-test'});
expect(pasteWithMaxLength(editor, ['ab💏', 'x'], [1, 1], 'yz', 3)).toEqual({
anchorOffset: 2,
anchorText: 'ab',
isCollapsed: true,
size: 2,
text: 'ab',
});
});

test('removes an earlier node whose first grapheme the cut lands in', () => {
using editor = buildEditorFromExtensions({name: 'trim-test'});
expect(
pasteWithMaxLength(editor, ['ab', '💏c', 'x'], [2, 1], 'yz', 5),
).toMatchObject({text: 'ab'});
});

test('keeps surrogate pairs whole without Intl.Segmenter', () => {
// Intl's members are not enumerable, so a spread would drop them all
vi.stubGlobal(
'Intl',
Object.assign(Object.create(Intl), {Segmenter: undefined}),
);
try {
using editor = buildEditorFromExtensions({name: 'trim-test'});
expect(pasteWithMaxLength(editor, ['abc'], [0, 3], 'd💏', 5)).toEqual({
anchorOffset: 4,
anchorText: 'abcd',
isCollapsed: true,
size: 4,
text: 'abcd',
});
} finally {
vi.unstubAllGlobals();
}
});
});
67 changes: 35 additions & 32 deletions packages/lexical-selection/src/lexical-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {
$caretRangeFromSelection,
$cloneWithPropertiesEphemeral,
$createTextNode,
$getNodeByKey,
$getPreviousSelection,
$getSelection,
$getTextPointCaretSliceForNode,
$isElementNode,
Expand Down Expand Up @@ -108,10 +106,13 @@ export function $isAtNodeEnd(point: Point): boolean {
}

/**
* Trims text from a node in order to shorten it, eg. to enforce a text's max length. If it deletes text
* that is an ancestor of the anchor then it will leave 2 indents, otherwise, if no text content exists, it deletes
* the TextNode. It will move the focus to either the end of any left over text or beginning of a new TextNode.
* @param editor - The lexical editor.
* Trims `delCount` characters of text content ending at the anchor, eg. to enforce a max length. Text just
* before the anchor is removed first, then text from preceding nodes. Nodes left with no text are removed, and
* an empty block counts as 2 characters (its line breaks). Where `Intl.Segmenter` is available, a grapheme
* (eg. an emoji) is never cut in half (without it, only surrogate pairs are kept whole): it is removed whole,
* so slightly more than `delCount` characters may be removed. This always trims; rejecting a keystroke made at
* the limit is up to the caller (MaxLengthPlugin restores the previous editor state in that case).
* @param editor - Unused, kept for backwards compatibility.
* @param anchor - The anchor of the current selection, where the selection should be pointing.
* @param delCount - The amount of characters to delete. Useful as a dynamic variable eg. textContentSize - maxLength;
*/
Expand Down Expand Up @@ -167,7 +168,13 @@ export function $trimTextContentFromAnchor(
}
const currentNodeSize = text.length;

if (!$isTextNode(currentNode) || remaining >= currentNodeSize) {
if (
!$isTextNode(currentNode) ||
remaining >= currentNodeSize ||
// The cut is inside the first grapheme of a node before the anchor's, so nothing of it is kept
(anchor.key !== currentNode.getKey() &&
getGraphemeStart(text, currentNodeSize - remaining) === 0)
) {
const parent = currentNode.getParent();
currentNode.remove();
if (
Expand All @@ -181,31 +188,9 @@ export function $trimTextContentFromAnchor(
currentNode = nextNode;
} else {
const key = currentNode.getKey();
// See if we can just revert it to what was in the last editor state
const prevTextContent: string | null = editor.read('latest', () => {
const prevNode = $getNodeByKey(key);
if ($isTextNode(prevNode) && prevNode.isSimpleText()) {
return prevNode.getTextContent();
}
return null;
});
const offset = currentNodeSize - remaining;
const offset = getGraphemeStart(text, currentNodeSize - remaining);
Comment thread
rootnode-bot[bot] marked this conversation as resolved.
Resolved
const slicedText = text.slice(0, offset);
if (prevTextContent !== null && prevTextContent !== text) {
const prevSelection = $getPreviousSelection();
let target = currentNode;
if (!currentNode.isSimpleText()) {
const textNode = $createTextNode(prevTextContent);
currentNode.replace(textNode);
target = textNode;
} else {
currentNode.setTextContent(prevTextContent);
}
if ($isRangeSelection(prevSelection) && prevSelection.isCollapsed()) {
const prevOffset = prevSelection.anchor.offset;
target.select(prevOffset, prevOffset);
}
} else if (currentNode.isSimpleText()) {
if (currentNode.isSimpleText()) {
// Split text
const isSelected = anchor.key === key;
let anchorOffset = anchor.offset;
Expand All @@ -214,7 +199,10 @@ export function $trimTextContentFromAnchor(
if (anchorOffset < remaining) {
anchorOffset = currentNodeSize;
}
Comment on lines 199 to 201

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional, non-blocking: with the revert branch gone, this fallback is now reached for an existing node too. In text that is already over the limit (loaded with setEditorState, or after a caller lowers its limit), typing near the start moves the cut to the end of the node and deletes the text after the caret while keeping the keystroke: Hello world with a limit of 5 and x typed at offset 0 gives xHell through the playground's MaxLengthExtension (main ignored that keystroke). Undo restores it, so this isn't a blocker, but it may be worth naming in the changeset's Verify: paragraph, or having the example extension reject growth when the previous size is already at or over the limit.

const splitStart = isSelected ? anchorOffset - remaining : 0;
// Never cut a grapheme (eg. an emoji) in half; drop it whole instead.
const splitStart = isSelected
? getGraphemeStart(text, anchorOffset - remaining)
: 0;
const splitEnd = isSelected ? anchorOffset : offset;
if (isSelected && splitStart === 0) {
const [excessNode] = currentNode.splitText(splitStart, splitEnd);
Expand Down Expand Up @@ -406,3 +394,18 @@ export function $ensureForwardRangeSelection(selection: RangeSelection): void {
focus.set(key, offset, type);
}
}

function getGraphemeStart(text: string, offset: number): number {
if (typeof Intl.Segmenter === 'function') {
let start = 0;
for (const {index} of new Intl.Segmenter().segment(text)) {
if (index > offset) {
break;
}
start = index;
}
return start;
}
const code = text.charCodeAt(offset);
return offset > 0 && code >= 0xdc00 && code <= 0xdfff ? offset - 1 : offset;
}
Loading