Skip to content

Potential bug in clipboard.ts onCaptureCopy calling e.preventDefault() prematurely #4683

@anisabboud

Description

@anisabboud

Quill.js adds a 'copy' event listener on the editor, which calls onCaptureCopy().

this.quill.root.addEventListener('copy', (e) =>
this.onCaptureCopy(e, false),
);

onCaptureCopy() calls e.preventDefault() to handle the copy event by quill:

onCaptureCopy(e: ClipboardEvent, isCut = false) {
if (e.defaultPrevented) return;
e.preventDefault();
const [range] = this.quill.selection.getRange();
if (range == null) return;
const { html, text } = this.onCopy(range, isCut);
e.clipboardData?.setData('text/plain', text);
e.clipboardData?.setData('text/html', html);
if (isCut) {
deleteRange({ range, quill: this.quill });
}
}

But there is a case, where quill.selection.getRange() returns null, and the function returns without copying anything but after it has already called e.preventDefault(), so the browser doesn't copy anything either.
I think e.preventDefault(); should move two lines down, after the if (range == null) return;.
I.e., onCaptureCopy() should preventDefault() only when it handles the copy event itself.

Move preventDefault two lines down

This issue causes an issue in read-only quill viewer where you cannot copy the entire text when you select it via mouse triple-click.

You can observe the issue on the ngx-quill official examples page:

  1. Go to https://killercodemonkey.github.io/ngx-quill-example/
  2. Scroll to the View format: object example.
  3. Triple-click the "Hello World!" text.
  4. Ctrl+C, Ctrl+V.
  5. Hello World wasn't copied...

ngx-quill-example

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions