[lexical][lexical-clipboard] Bug Fix: Preserve alignment when pasting selected text
Description
When copying selected text from an aligned paragraph inside the same Lexical namespace, the application/x-lexical-editor clipboard payload took priority over text/html. That JSON payload only contained the selected TextNode, so pasting into another paragraph reused the destination paragraph's default alignment and discarded the source paragraph alignment.
This updates clipboard JSON export to retain the non-inline parent element when the selected child content covers the full element text. That preserves block-level formatting such as paragraph alignment for full-paragraph text selections while partial text selections remain text-only.
Closes #8101
Test plan
Before
Copying all visible text from an aligned paragraph and pasting it elsewhere lost the source paragraph alignment.
After
Full-paragraph text selections preserve block-level alignment on paste, including paragraphs that contain inline elements such as links, while partial text selections remain text-only.
Automated tests:
node node_modules/vitest/vitest.mjs --project unit packages/lexical/src/__tests__/unit/HTMLCopyAndPaste.test.ts --runnode node_modules/vitest/vitest.mjs --project unit packages/lexical-clipboard/src/__tests__/unit/GetClipboardDataExtension.test.ts packages/lexical-clipboard/src/__tests__/unit/ClipboardImportExtension.test.ts --runnode node_modules/prettier/bin/prettier.cjs --check packages/lexical-clipboard/src/clipboard.ts packages/lexical/src/__tests__/unit/HTMLCopyAndPaste.test.tsnode node_modules/eslint/bin/eslint.js packages/lexical-clipboard/src/clipboard.ts packages/lexical/src/__tests__/unit/HTMLCopyAndPaste.test.ts
The latest updates on your projects. Learn more about Vercel for GitHub.
| Project | Deployment | Actions | Updated (UTC) |
|---|---|---|---|
| Preview | Aug 29, 2026 5:54pm | ||
| Preview | Aug 29, 2026 5:54pm |
Title and description doesn't match the pull request template
The approach makes sense, but $shouldIncludeElementWithSelectedChildren breaks on paragraphs containing inline elements (links, marks, etc.). selection.getNodes() includes intermediate element nodes, so their getTextContent() gets double-counted in the join — e.g. "Hello worldworld" vs "Hello world". Tested locally and confirmed the alignment isn't preserved when a link is present.
Details and a suggested fix inline. Also noted how HeadingNode handles the same problem via extractWithChild for reference.
Addressed the review feedback in 752a47ecc:
- filtered selected intermediate
ElementNodes out of$shouldIncludeElementWithSelectedChildren()so inline elements like links are not double-counted - added regression coverage for preserving centered paragraph alignment when the selected paragraph contains a link
- simplified the paste target selection to use the
RangeSelectionreturned bytarget.select()
Re-ran:
node node_modules/vitest/vitest.mjs --project unit packages/lexical/src/__tests__/unit/HTMLCopyAndPaste.test.ts --run
node node_modules/vitest/vitest.mjs --project unit packages/lexical-clipboard/src/__tests__/unit/GetClipboardDataExtension.test.ts packages/lexical-clipboard/src/__tests__/unit/ClipboardImportExtension.test.ts --run
node node_modules/prettier/bin/prettier.cjs --check packages/lexical-clipboard/src/clipboard.ts packages/lexical/src/__tests__/unit/HTMLCopyAndPaste.test.ts
node node_modules/eslint/bin/eslint.js packages/lexical-clipboard/src/clipboard.ts packages/lexical/src/__tests__/unit/HTMLCopyAndPaste.test.ts
Superseded by #9059