Repository navigation
[lexical] Bug Fix: Preserve pasted blocks after consecutive line breaks - #8890
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Hi @wanxiankai! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
etrepum
left a comment
There was a problem hiding this comment.
The cursor ends up in the wrong location after pasting
## Description Treat consecutive soft line breaks as a block boundary when inserting blocks so pasted blocks do not merge into either side of the surrounding paragraph. This preserves block formatting and leaves the cursor at the end of the pasted content. Closes meta#4815 ## Test plan ### Before Issue4815Repro.test.ts fails because the heading is merged into the surrounding paragraph or the final pasted paragraph is merged with the original trailing text, moving the cursor to the wrong location. ### After Issue4815Repro.test.ts passes, preserving both pasted block boundaries, the pasted-content selection, and the existing single-line-break merge behavior.
7367e5f to
1edace8
Compare
|
Thanks for catching this. I reproduced it with the playground state from #4815. The previous version only prevented the first pasted block from merging into the left side; the final pasted paragraph could still merge with the original trailing content, so the block boundary and cursor result were incomplete. Updated in
The regression test now asserts the four resulting blocks ( |
Resolves the one conflict in `RangeSelection.insertNodes`: `main` split the `insertedParagraph` merge into a nested `if`/`else if` in meta#9099, so the `!shouldPreserveInsertedBlocks` guard this branch added moves onto the inner `if` and `main`'s new `else if (insertedParagraph.isEmpty())` fallback is kept.
…ailing text when pasting blocks after it ## Description The previous commit makes two consecutive soft line breaks act as a block boundary in `RangeSelection.insertNodes`, but the guard it adds has four problems. Pasting at the *end* of a paragraph that ends in two line breaks — pressing Shift+Enter twice and pasting, the literal repro of meta#4815 — left a stray empty paragraph behind, because skipping the `insertedParagraph` merge orphaned the paragraph the split had just created. Restoring that merge also restores the ordinary paste semantics for the text *after* the insertion point: it joins the last pasted block, exactly as it does for any other mid-paragraph paste, rather than depending on what precedes the caret. The caret was already correct without that change — it lands at the end of the pasted content either way. With nothing merging into the block being split, the pre-existing cleanup at the end of `insertNodes` (which removes a dangling line break only when `lastInsertedBlock !== firstBlock`) started firing on the run of line breaks the user typed, so `text<br><br>` became `text<br>` and one blank line was silently deleted. That cleanup is now skipped when the empty line was deliberately treated as a block boundary, since it is content, not a leftover of the split. The guard also only matched an element point, or a text point at offset 0 whose own previous sibling was a line break, and only for a collapsed selection. So the same paste behaved differently when the point sat one inline element deep (`<p>a<br><br><b>|x</b></p>`), and differently again when it replaced a selection instead of sitting at a collapsed caret. The check is now a `$getNodeBeforePoint` helper that ascends out of inline elements up to the block, and it runs after `removeText()` in CASE 3, next to the two places that read it, instead of at the top of the method where every other case paid for it and none used it. Test plan below covers all four, plus an end-to-end paste in the playground, which is what decides whether the browser hands Lexical an element point or a text point. Closes meta#4815 ## Test plan ### Before `packages/lexical/src/__tests__/unit/Issue4815Repro.test.ts`, run against the previous commit: ``` $ npx vitest run packages/lexical/src/__tests__/unit/Issue4815Repro.test.ts × preserves a pasted block at the end of a paragraph after consecutive line breaks × preserves a pasted block before text that follows consecutive line breaks × preserves a pasted block for a text point at the start of the line × preserves a pasted block inside an inline element at the start of the line × preserves a pasted block when replacing a selection that starts after the empty line × preserves every line break of a longer run Tests 6 failed | 2 passed (8) ``` The first failure is the stray paragraph and the eaten line break: ``` - 'paragraph("Line of text\n\n")' - 'test-heading("Heading 3")' - 'paragraph("Some paragraph")' + 'paragraph("Line of text\n")' + 'test-heading("Heading 3")' + 'paragraph("Some paragraph")' + 'paragraph("")' ``` The new end-to-end test against `main`, where the pasted `<h3>` is flattened into the paragraph above it: ``` $ npx playwright test --project=chromium HTMLCopyAndPaste.spec.mjs -g "blocks after two line breaks" Error: innerHTML of contenteditable in page did not match <p class="PlaygroundEditorTheme__paragraph" dir="auto"> <span data-lexical-text="true">Line of text</span> <br /> <br /> - <br data-lexical-managed-linebreak="true" /> - </p> - <h3 class="PlaygroundEditorTheme__h3" dir="auto"> <span data-lexical-text="true">Heading 3</span> - </h3> + </p> <p class="PlaygroundEditorTheme__paragraph" dir="auto"> <span data-lexical-text="true">Some paragraph</span> </p> ``` ### After ``` $ npx vitest run --project unit Test Files 306 passed (306) Tests 4706 passed | 1 skipped (4707) $ npx playwright test --project=chromium 16 failed 2 flaky 33 skipped 818 passed (25.9m) $ npx tsc --noEmit && npm run flow && npx eslint <changed files> && npx prettier --check <changed files> No errors! All matched files use Prettier code style! ``` The 16 chromium failures are all in `Composition.spec.mjs` (IME) and `Share.spec.mjs`, and they fail identically with `LexicalSelection.ts` reverted to `main`: this environment substitutes its own Chromium build, which does not drive the CDP IME input those tests need, and blocks the outbound requests Share makes. The 2 flaky tests passed on retry, and `Can adjust triple click selection linebreak` passes 3/3 when run on its own. Firefox and WebKit were not exercised: only Chromium is available in this environment. Co-Authored-By: Claude Opus 5 <[email protected]>
luantaraschi
left a comment
There was a problem hiding this comment.
Heads up that this does not merge any more, and the reason is worth knowing before you rebase: the conflict sits in the same block you are guarding.
#9099 landed on the 27th, "insertNodes no longer leaves an empty paragraph after a block inserted at the end of a paragraph", and it restructured the if (insertedParagraph ...) in LexicalSelection.ts that this PR prefixes with !shouldPreserveInsertedBlocks &&. It added an else if (insertedParagraph.isEmpty()) { insertedParagraph.remove(); } arm.
So the rebase is a decision rather than a mechanical fix. Guarding the inner condition keeps that cleanup; guarding the outer one suppresses it, and then a paste with the caret at the end of a block after two breaks leaves an empty paragraph behind. Both resolutions compile and both keep the suite green, including the regression tests #9099 brought with it, so nothing in the repo will tell you which one you picked.
Worth saying too that the green CI here is from July 31 against a base that has moved a long way since, so it is not evidence about today.
Separately, a question about the fix itself rather than the rebase. The guard looks at the node before the anchor. Does that mean a paste with the caret just before a blank line still merges leftward, or is that covered somewhere I missed?
…890-review-vlx4fb
… an empty line, as the paste boundary ## Description Two corrections to the previous commit's handling of pasted blocks after soft line breaks, both found by comparing the reconciled DOM against the paragraph-structured document that renders the same visual lines. Skipping the trailing-line-break cleanup was wrong, and reintroduced a blank line rather than preserving one. A paragraph whose last child is a line break gets a managed trailing `<br>` from the reconciler, so `a<br><br>` renders as three lines, not two. When the caret sits on the last of those lines, the paste consumes that line, and the break that terminated it is the split leftover the pre-existing cleanup removes; the user's own empty line is the break before it and survives either way. Keeping both left `<p>a<br><br>|b</p>` plus a pasted heading and paragraph rendering as "a" / blank / blank / heading, where the equivalent `<p>a</p><p></p><p>|b</p>` gives "a" / blank / heading. That cleanup is now back to what `main` has, so this file's only remaining change is the merge guard. The guard itself keyed off the wrong property: it asked whether the previous line was blank rather than whether the point is at the start of a line, so one Shift+Enter and a pasted `<h3>` still flattened the heading into the line above while two kept it -- the same meta#4815 symptom one keystroke away. It now takes a single line break, which makes a line-broken line behave like the start of a block, where `firstBlock` is empty after the split and the first pasted block already keeps its own identity. Every case probed now matches its paragraph-structured reference exactly. That does mean pasting a lone paragraph at the start of a line-broken line gives it its own block instead of continuing the line (`<p>a<br>|b</p>` plus `<p>X</p>` becomes `<p>a</p><p>Xb</p>`), which is what the same paste at the start of a paragraph already does, and what the previous commit already did after two line breaks. There is a new unit test pinning it, since it is the one place where reference equivalence costs a soft break. ## Test plan ### Before ``` $ npx vitest run packages/lexical/src/__tests__/unit/Issue4815Repro.test.ts preserves a pasted block at the end of a paragraph after consecutive line breaks -> paragraph("Line of text\n\n") | test-heading | paragraph (one blank line too many) keeps the existing merge behavior after a single line break -> paragraph("Line of text\nHeading 3") | paragraph (heading flattened) ``` Rendered, for `<p>a<br><br>|b</p>` pasting `<h3>H</h3><p>P</p>`: ``` soft breaks: p["a" / "" / "" / ""] h3["H"] p["Pb"] reference: p["a"] p["" / ""] h3["H"] p["Pb"] ``` and for `<p>a<br>|b</p>`: ``` soft breaks: p["a" / "H"] p["Pb"] reference: p["a"] h3["H"] p["Pb"] ``` ### After Both render identically to their references, and the E2E expectation no longer carries the extra `<br>`: ``` $ npx vitest run --project unit Test Files 311 passed (311) Tests 4752 passed | 1 skipped (4753) $ npx playwright test --project=chromium 16 failed 1 flaky 33 skipped 821 passed (17.6m) ``` The 16 chromium failures are the same `Composition.spec.mjs` (IME) and `Share.spec.mjs` ones as before, which fail identically with `LexicalSelection.ts` reverted to `main`: this environment substitutes its own Chromium build, which does not drive the CDP IME input those tests need, and blocks the requests Share makes. `TextEntry > Can insert a paragraph between two text nodes` passed on retry and passes 3/3 on its own. ``` $ npx tsc --noEmit && npx eslint <changed> && npx prettier --check <changed> All matched files use Prettier code style! ``` Firefox and WebKit were not exercised: only Chromium is available in this environment. Co-Authored-By: Claude Opus 5 <[email protected]>
|
The rewrite sidesteps the conflict rather than resolving it, which is better. Putting the decision in Adding the e2e spec is worth having too. The unit test could not reach the clipboard path the issue is actually reported against. |
… line break, for the paste boundary ## Description Narrows the previous commit back to two consecutive soft line breaks. Keying the boundary off a single break is the more consistent rule -- the point is just as much at the start of a line either way -- but it also decides that a lone pasted paragraph stops continuing the line and becomes its own block (`<p>a<br>|b</p>` plus `<p>X</p>` giving `<p>a</p><p>Xb</p>`), and a single Shift+Enter is common enough that this is a wider change to paste than a fix for meta#4815 should make. Nothing in the repo enforced either answer, so the choice was ours to make and this takes the narrower one. The tradeoff does not disappear, it just applies only after an empty line, so the test that pins it moves there rather than being dropped. Both the reasoning and the rejected alternative are recorded on `$isPointAfterEmptyLine` so the next reader does not have to rediscover them. The previous commit's other correction stands: the trailing-line-break cleanup stays exactly as it is on `main`, so `insertNodes` still has a single line-break-related change, the `!isAfterEmptyLine` guard on `isMergeable`. The E2E case for a single line break goes away with the behavior it covered. The two-line-break case stays, and remains the only test that exercises the real clipboard path a browser paste takes. ## Test plan ### Before With the single-break rule, pasting `<h3>` after one Shift+Enter split the paragraph, and so did pasting a lone paragraph: ``` $ npx vitest run packages/lexical/src/__tests__/unit/Issue4815Repro.test.ts preserves a pasted block after a single line break -> paragraph("Line of text") | test-heading | paragraph("Some paragraphParagraph 1") gives a lone pasted paragraph its own block at the start of a line -> paragraph("Line of text") | paragraph("Some paragraphParagraph 1") ``` ### After A single line break keeps the pre-existing merge, and the lone-paragraph tradeoff is pinned after an empty line instead: ``` $ npx vitest run packages/lexical/src/__tests__/unit/Issue4815Repro.test.ts keeps the existing merge behavior after a single line break -> paragraph("Line of text\nHeading 3") | paragraph("Some paragraphParagraph 1") gives a lone pasted paragraph its own block after an empty line -> paragraph("Line of text\n") | paragraph("Some paragraphParagraph 1") Tests 10 passed (10) ``` ``` $ npx vitest run --project unit Test Files 311 passed (311) Tests 4752 passed | 1 skipped (4753) $ npx playwright test --project=chromium 16 failed 33 skipped 821 passed (15.5m) $ npx tsc --noEmit && npx eslint <changed> && npx prettier --check <changed> All matched files use Prettier code style! ``` The 16 chromium failures are the same `Composition.spec.mjs` (IME) and `Share.spec.mjs` ones as in the previous commits, which fail identically with `LexicalSelection.ts` reverted to `main`: this environment substitutes its own Chromium build, which does not drive the CDP IME input those tests need, and blocks the requests Share makes. Nothing else failed and nothing was flaky. Firefox and WebKit were not exercised: only Chromium is available in this environment. Co-Authored-By: Claude Opus 5 <[email protected]>
|
There's still more work to do on this one, the selection doesn't consistently land in the correct place after the paste. |
This comment was marked as spam.
This comment was marked as spam.
…n pasted content ends in an empty block ## Description `RangeSelection.insertNodes` marks where the pasted content ends with `nodeToSelect.selectEnd()`, but ran it after the split-off content had already been appended into `lastInsertedBlock`. That is harmless while `nodeToSelect` is a text node -- the appended children land beside it -- but when the pasted content ends in an empty block, `blocksParent.getLastDescendant()` is that empty block, and it is also the block the split-off content is appended into. Selecting its end afterwards put the caret past the text that had just moved in, at the end of the document. Copying several blocks out of an editor is exactly the case that carries a trailing empty paragraph, so with the playground's own default content on the clipboard and the caret before the "a" of "asdf", pasting left the caret after the "f". Marking the caret before the append fixes it: an element `nodeToSelect` keeps its element point at the start, so the appended children land after the caret, and a text `nodeToSelect` is unaffected either way. This predates meta#8890 -- the reported case does not involve line breaks, so it reproduces the same way with this file reverted to `main`. ## Test plan ### Before With `LexicalSelection.ts` at `main`, pasting the playground's default content (three blocks, the last of them empty) before the "a" of "asdf": ``` doc: "Welcome to the playgroundThe playground is a demo environment.asdf" sel: path=[2,0,0] offset=4 in "asdf" <- after the "f" ``` and the new unit test: ``` $ npx vitest run packages/lexical/src/__tests__/unit/LexicalSelection.test.ts \ -t "leaves the caret where pasted content" × leaves the caret where pasted content ending in an empty block joins Expected: ""asdf"@0" Received: ""asdf"@4" Tests 1 failed | 1 passed | 98 skipped (100) ``` ### After ``` doc: "Welcome to the playgroundThe playground is a demo environment.asdf" sel: path=[2] offset=0 in "asdf" <- at the join ``` A sweep of eleven paste payloads at the same caret position (`h3+p`, `p+p`, `h1+h2+p`, `ul`, `p+hr`, `hr+p`, `pre+p`, `quote+p`, `p+p+empty`, `p+ul`, `table`) shows only `p+p+empty` changing; every other caret position is identical before and after. ``` $ npx vitest run --project unit Test Files 311 passed (311) Tests 4754 passed | 1 skipped (4755) $ npx playwright test --project=chromium 16 failed 33 skipped 822 passed (16.9m) $ npx tsc --noEmit && npx eslint <changed> TSC clean LINT clean ``` The 16 chromium failures are the same `Composition.spec.mjs` (IME) and `Share.spec.mjs` ones as in the previous commits, which fail identically with `LexicalSelection.ts` reverted to `main`: this environment substitutes its own Chromium build, which does not drive the CDP IME input those tests need, and blocks the requests Share makes. Nothing else failed and nothing was flaky. Firefox and WebKit were not exercised: only Chromium is available in this environment. Co-Authored-By: Claude Opus 5 <[email protected]>
…s leaves onto the adjacent text
## Description
The previous commit marks the end of the pasted content before the split-off
content is appended, which fixed the caret landing at the end of the document
but left an element point behind whenever `nodeToSelect` was an element with
nothing after the caret: an empty block that has since received that content,
or a block-level decorator. It described the right position, and the browser
put the caret in the right place, but it read differently from the text point
every other insertion in this branch produces, and only settled into one on the
next DOM-driven selection read.
`$normalizeCaret` resolves it onto the text beside it, so the caret is the same
shape whatever the pasted content ended with. It is a no-op on a point that is
already a text point, and on an element with nothing to resolve onto.
## Test plan
### Before
Pasting at the start of "Hello", the two payloads whose last node is an element
left an element point (`path` stopping at the block rather than reaching its
text node):
```
p+hr sel: path=[2] offset=0
p+p+empty sel: path=[2] offset=0
```
### After
Both resolve onto the text, at the same position:
```
p+hr sel: path=[2,0,0] offset=0 in "Hello"
p+p+empty sel: path=[2,0,0] offset=0 in "Hello"
```
A sweep of thirteen paste payloads at that caret position (the eleven from the
previous commit plus `<p></p>` and `<hr>` alone) now leaves a text point for
every one, and no position moved.
The unit tests assert the point type as well as the position, since the two
spellings describe the same place and the previous assertion could not tell
them apart.
```
$ npx vitest run --project unit
Test Files 311 passed (311)
Tests 4754 passed | 1 skipped (4755)
$ npx playwright test --project=chromium
16 failed
33 skipped
822 passed (17.8m)
$ npx tsc --noEmit && npx eslint <changed>
TSC clean
LINT clean
```
The 16 chromium failures are the same `Composition.spec.mjs` (IME) and
`Share.spec.mjs` ones as in the previous commits, which fail identically with
`LexicalSelection.ts` reverted to `main`: this environment substitutes its own
Chromium build, which does not drive the CDP IME input those tests need, and
blocks the requests Share makes. Nothing else failed and nothing was flaky.
Firefox and WebKit were not exercised: only Chromium is available in this
environment.
Co-Authored-By: Claude Opus 5 <[email protected]>
Description
Pasting multiple blocks after two consecutive soft line breaks currently merges the first pasted block into the surrounding paragraph. This drops block semantics, such as a heading's tag.
Treat consecutive soft line breaks as a block boundary so the first pasted block retains its type. The selection remains at the end of the pasted content, before the original trailing text.
Closes #4815
Test plan
Before
Issue4815Repro.test.tsfails because the heading is merged into the surrounding paragraph.After
Issue4815Repro.test.tspasses, preserving the heading block and selection. A second assertion confirms the existing single-line-break merge behavior is unchanged.