Skip to content

Commit

Permalink
Fix redundant newlines when pasting from external sources (#4050)
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Mar 14, 2024
1 parent 80f22d8 commit 71fada2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion packages/quill/src/modules/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions packages/quill/test/unit/modules/clipboard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,5 +563,11 @@ describe('Clipboard', () => {
.insert('\n'),
);
});

test('ignore empty elements except paragraphs', () => {
const html = '<div>hello<div></div>my<p></p>world</div>';
const delta = createClipboard().convert({ html });
expect(delta).toEqual(new Delta().insert('hello\nmy\n\nworld'));
});
});
});

0 comments on commit 71fada2

Please sign in to comment.