Skip to content

Commit

Permalink
Run clipboard matchers against plain text pastes
Browse files Browse the repository at this point in the history
At the moment, if the clipboard pastes `text/plain` content and no
`text/html` content, the `Clipboard.convert()` function will completely
skip the matching logic.

This is surprising when registering text node clipboard matchers.

This change updates the `convert()` function to change the plain text
into basic HTML, which is passed through the matchers.

The conversion interprets newlines as paragraph `<p>` elements,
consistent with Quill's existing behaviour.
  • Loading branch information
alecgibson committed Jun 2, 2023
1 parent b3d1532 commit 870c18c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion modules/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ class Clipboard extends Module<ClipboardOptions> {
});
}
if (!html) {
return new Delta().insert(text || '');
html = (text || '')
.split('\n')
.map(line => `<p>${line}</p>`)
.join('');
}
const delta = this.convertHTML(html);
// Remove trailing newline
Expand Down
24 changes: 24 additions & 0 deletions test/unit/modules/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,30 @@ describe('Clipboard', function () {
expect(delta).toEqual(expected);
});

it('runs a text node matcher on a plain text paste', function () {
this.clipboard.addMatcher(Node.TEXT_NODE, function (node, delta) {
let index = 0;
const regex = /https?:\/\/[^\s]+/g;
let match = null;
const composer = new Delta();
// eslint-disable-next-line no-cond-assign
while ((match = regex.exec(node.data))) {
composer.retain(match.index - index);
index = regex.lastIndex;
composer.retain(match[0].length, { link: match[0] });
}
return delta.compose(composer);
});
const delta = this.clipboard.convert({
text: 'http://github.com https://quilljs.com',
});
const expected = new Delta()
.insert('http://github.com', { link: 'http://github.com' })
.insert(' ')
.insert('https://quilljs.com', { link: 'https://quilljs.com' });
expect(delta).toEqual(expected);
});

it('does not execute javascript', function () {
window.unsafeFunction = jasmine.createSpy('unsafeFunction');
const html =
Expand Down

0 comments on commit 870c18c

Please sign in to comment.