Repository navigation
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
There was a problem hiding this comment.
Thanks for picking up #3775. The root-cause analysis is right, and keeping the part of a paste that fits, with grapheme-safe cuts, is what the issue asks for. The new unit tests fail on main (9 of 10) and pass on this branch, and the issue's steps now keep lorem ipsum dolor sit amet, co in the playground. One problem needs fixing before this can merge.
Blocking: a cut inside the first grapheme of an earlier text node throws
packages/lexical-selection/src/lexical-node.ts:187 rounds offset down to a grapheme start. When the node being cut is not the anchor's node and the cut falls inside its first grapheme, offset becomes 0, so line 207 calls currentNode.splitText(0, 0). splitText returns [self] when there is only one part, so excessNode is undefined and line 208 throws TypeError: Cannot read properties of undefined (reading 'remove').
This is the ordinary path after a multi-paragraph paste: once the caret's paragraph has been removed, the anchor sits on an element point of the previous paragraph, so anchor.key never matches the text node being cut. To reproduce in the playground with ?isMaxLength=true, paste this plain text into an empty editor:
lorem ipsum dolor sit amet,
😀 next
ab
On main the result is 30 characters ending in half of the emoji. On this branch the transform throws and nothing is pasted, and because the playground's onError rethrows, later typing in that editor fails too.
A minimal fix is to send that case through the whole-node branch at line 173, which also removes the emptied parent:
if (
!$isTextNode(currentNode) ||
remaining >= currentNodeSize ||
(anchor.key !== currentNode.getKey() &&
getGraphemeStart(text, currentNodeSize - remaining) === 0)
) {With that change the paste above keeps lorem ipsum dolor sit amet, and your ten tests still pass. A regression test with your existing helper: pasteWithMaxLength(editor, ['ab', '💏c', 'x'], [2, 1], 'yz', 5) should give text: 'ab'; today it throws.
Optional, non-blocking
- The new JSDoc (
lexical-node.ts:105-110) says a grapheme is never cut in half. WithoutIntl.Segmenterthe fallback only keeps surrogate pairs whole, so "whereIntl.Segmenteris available" would be more accurate. - In the fallback test,
vi.stubGlobal('Intl', {...Intl, Segmenter: undefined})replacesIntlwith an object whose only key isSegmenter, becauseIntl's members are non-enumerable and the spread copies none of them.Object.assign(Object.create(Intl), {Segmenter: undefined})keeps the rest. - When an IME composition crosses the limit, the behaviour changes from "revert the node" to "trim before the caret". AGENTS.md asks the Test plan to say explicitly when part of the matrix could not be exercised, so a short note (or a manual check with a CJK IME near the limit) would help reviewers.
…it exceeds the max length $trimTextContentFromAnchor reverted the anchor's text node to its previous content when the node already existed, discarding the whole paste instead of trimming it. Trim the excess just before the anchor instead, and never split a grapheme in half. Closes meta#3775 Signed-off-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com> Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…eme of an earlier node A cut inside the first grapheme of a text node before the anchor's node rounded the split offset down to 0, so splitText returned only the node itself and the excess node was undefined. Remove such a node whole. Also stub only Intl.Segmenter in the fallback test, and say in the JSDoc that whole graphemes need Intl.Segmenter. Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
af187ce to
099ac56
Compare
Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Thanks for the review. Fixed in the latest push:
Also rebased onto — Ved Thakar |
There was a problem hiding this comment.
Thanks @Vedthakar, this looks good. Pasting past the limit after existing text now keeps the part that fits, as #3775 asks; emoji, flags and combining marks stay whole at the cut; and a cut that lands in the first grapheme of an earlier node now removes that node whole instead of leaving half an emoji behind. The new unit tests fail on main and pass here, and the e2e case reproduces the issue as reported.
A few optional, non-blocking suggestions:
-
Add a
Verify:paragraph to the changeset (.changeset/max-length-paste-keeps-fit.md). CONTRIBUTING.md ("Changesets") and AGENTS.md ask a fix that changes observable behavior to carry one, so it is listed under "Behavior changes". For example: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.
-
Text that is already over the limit (
packages/lexical-selection/src/lexical-node.ts:199-201). With the revert branch gone, the next edit in content that is already longer than the limit (loaded withsetEditorState, or after a caller lowers its limit) cuts existing text down to the limit. WithHello worldand a limit of 5 in the playground'sMaxLengthExtension, typing at the end or pressing Backspace leavesHello, and typing at the start leavesxHell, because this fallback moves the cut to the end of the node and deletes the text after the caret. Onmainsuch edits were ignored inside an existing node (though an edit that created a new node was already trimmed), and Undo brings the text back, so this is not a blocker. It would help to either name it in theVerify:paragraph or have the example extension reject growth when the previous size is already at or over the limit (textContentSize > prevTextContentSize && prevTextContentSize >= maxLength). -
Pin the other two behaviors this changes. A unit case for text already over the limit, and one for a multi-character input that crosses the limit (
Hell+abwith a limit of 5 now givesHella; it used to be dropped), would lock in whichever behavior you settle on. -
Test plan output. AGENTS.md ("Commits and Pull Requests") asks for the real After output to be pasted, the way the Before section is.
| '@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 |
There was a problem hiding this comment.
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:
| [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. |
| if (anchorOffset < remaining) { | ||
| anchorOffset = currentNodeSize; | ||
| } |
There was a problem hiding this comment.
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.
Description
This solves issue #3775: with
MaxLengthPlugin, pasting text that goes over the limit into a paragraph that already has text throws the whole paste away instead of keeping the part that fits.Current behavior.
MaxLengthPlugincalls$trimTextContentFromAnchor(editor, anchor, size - maxLength)from a root transform. When the excess fits in the anchor's text node and that node already existed in the previous editor state, the function does not trim. It goes into a "revert it to what was in the last editor state" branch, which reads the node ineditor.read('latest'), callssetTextContent(prevTextContent)and moves the selection back to its previous point. The paste is dropped entirely. Pasting into an empty editor works only because the text node is new, so the function falls through to the split path.Why the branch was there.
git blametraces it to the original MaxLengthPlugin (#2254, with tweaks in #2258/#2265, moved to@lexical/selectionin #3145). It existed to drop a character typed at the limit.MaxLengthPluginnow handles that case itself by calling$restoreEditorStatewhen the previous size already equalsmaxLength, so the branch is no longer needed for typing. For a paste, though, it is wrong.What this changes.
$trimTextContentFromAnchorinpackages/lexical-selection/src/lexical-node.tsdrops the revert branch and always uses the existing split path. That path removes the excess characters just before the anchor, so the text ends exactly at the limit and the caret sits at the trim point.Intl.Segmenter, the pattern already inuseCharacterLimit, and falls back to a surrogate-pair check whenIntl.Segmenteris unavailable. The trim therefore never leaves half an emoji, flag or accented letter. That was the other thing the revert branch prevented, and it keeps the "can restrict emojis on boundaries" e2e test passing. When a grapheme is dropped whole, the text can end a little under the limit.editorparameter is now unused but kept, since it is public API. The JSDoc is rewritten to match: the function always trims, it may remove a few extra characters to keep a grapheme whole, and rejecting a keystroke at the limit is left to the caller (MaxLengthPluginrestores the previous editor state).Closes #3775
Test plan
New tests:
packages/lexical-selection/src/__tests__/unit/$trimTextContentFromAnchor.test.ts: pastes at the caret and trims the wayMaxLengthPlugindoes. Each case checks the text, the size and the caret.e\u0301).Intl.Segmenterstubbed out.packages/lexical-playground/__tests__/e2e/MaxLength.spec.mjs› "keeps the start of an over-limit paste after existing text": the repro from the issue, which typeslorem ipsumand then pastes the rest.Before
On
mainwith the tests applied and the source change reverted:After
On current
main:$trimTextContentFromAnchor.test.ts: 10 passed (9 fail onmain). Chromium e2e:MaxLength.spec.mjspasses in full, including the new over-limit paste test (fails onmain) and the existing emoji-boundary test.pnpm run test-unit: all 9,580 tests passtsc,pnpm run flow(0 errors), and eslint/prettier on the changed files: cleanAuthor: Ved Thakar (@Vedthakar)
🤖 Generated with Claude Code