Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClipboardEvent.clipboardData's value is a DataTransfer, but DataTransfer.files isn't cross browser #18140

Open
tomayac opened this issue Nov 4, 2022 · 3 comments
Labels
data:api Compat data for Web APIs. https://developer.mozilla.org/docs/Web/API

Comments

@tomayac
Copy link
Contributor

tomayac commented Nov 4, 2022

By looking at compatibility data for DataTransfer.files and ClipboardEvent.clipboardData, developers might think copying a file in their operating system's file explorer and pasting it into a web application would work universally, since the support tables are all green. In practice, the code below is Chrome-only, though:

document.addEventListener("paste", async e => {
  e.preventDefault();
  if (!e.clipboardData.files.length) {
    return;
  }
  const file = e.clipboardData.files[0];
  // Read the file's contents, assuming it's a text file.
  // There is no way to write back to it.
  console.log(await file.text());
});
  1. Go to your file explorer and copy an image file.
  2. Go to SVGco.de.
  3. Try pasting the image via the browser's native Edit menu.

On Safari, with an image file on the clipboard, the Paste action is grayed out:

Screenshot 2022-11-04 at 11 26 51

On Chrome, with an image file on the clipboard, the Paste action is enabled:

Screenshot 2022-11-04 at 11 27 33

It would be great if the support situation for ClipboardEvent.clipboardData.files could be reflected more accurately.

@tomayac
Copy link
Contributor Author

tomayac commented Nov 4, 2022

CC: @chrisdavidmills.

@tomayac
Copy link
Contributor Author

tomayac commented Nov 4, 2022

There's another testing tool that may help understand better what's going on: https://madebyevan.com/clipboard-test/.

When copying a JPG file from the file explorer and pasting it, this is what happens when pasting in the input field via cmd + v:

  • Chrome: pastes the JPG file as a JPG image
  • Safari: pastes the JPG file as a JPG image
  • Firefox: converts the JPG file on the fly to PNG and pastes the PNG image

Interestingly the Edit > Paste menu, unlike in my previous test, was not grayed out on Safari this time.

@tomayac
Copy link
Contributor Author

tomayac commented Nov 4, 2022

The difference between the two implementations is document.addEventListener('paste', () => {}) in https://svgco.de/ vs. input.addEventListener('paste', () => {}) in https://madebyevan.com/clipboard-test/, so the latter listens on an element on the page, whereas the former on the document, so it's Document: paste event vs. Element: paste event. Both are ClipboardEvents.

@queengooborg queengooborg added data:api Compat data for Web APIs. https://developer.mozilla.org/docs/Web/API needs triage 🔎 labels Nov 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
data:api Compat data for Web APIs. https://developer.mozilla.org/docs/Web/API
Projects
None yet
Development

No branches or pull requests

2 participants