From 71fada2f7f039e67bc2d33ac2e62b7c3bb017f5c Mon Sep 17 00:00:00 2001 From: Zihua Li Date: Thu, 14 Mar 2024 17:20:16 +0800 Subject: [PATCH] Fix redundant newlines when pasting from external sources (#4050) --- CHANGELOG.md | 1 + packages/quill/src/modules/clipboard.ts | 5 ++++- packages/quill/test/unit/modules/clipboard.spec.ts | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21315b3dd7..2b321c7c79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Fix `Quill#getSemanticHTML()` for list items - Remove unnecessary Firefox workaround +- **Clipboard** Fix redundant newlines when pasting from external sources # 2.0.0-rc.2 diff --git a/packages/quill/src/modules/clipboard.ts b/packages/quill/src/modules/clipboard.ts index ead9758d29..df1c0c250d 100644 --- a/packages/quill/src/modules/clipboard.ts +++ b/packages/quill/src/modules/clipboard.ts @@ -529,7 +529,10 @@ function matchList(node: Node, delta: Delta, scroll: ScrollBlot) { function matchNewline(node: Node, delta: Delta, scroll: ScrollBlot) { if (!deltaEndsWith(delta, '\n')) { - if (isLine(node, scroll)) { + if ( + isLine(node, scroll) && + (node.childNodes.length > 0 || node instanceof HTMLParagraphElement) + ) { return delta.insert('\n'); } if (delta.length() > 0 && node.nextSibling) { diff --git a/packages/quill/test/unit/modules/clipboard.spec.ts b/packages/quill/test/unit/modules/clipboard.spec.ts index a7e929dd44..eaa98a169d 100644 --- a/packages/quill/test/unit/modules/clipboard.spec.ts +++ b/packages/quill/test/unit/modules/clipboard.spec.ts @@ -563,5 +563,11 @@ describe('Clipboard', () => { .insert('\n'), ); }); + + test('ignore empty elements except paragraphs', () => { + const html = '
hello
my

world
'; + const delta = createClipboard().convert({ html }); + expect(delta).toEqual(new Delta().insert('hello\nmy\n\nworld')); + }); }); });