clipboard-apis icon indicating copy to clipboard operation
clipboard-apis copied to clipboard

Specify how to handle unsupported data types in `write()`.

Open lgarron opened this issue 5 years ago • 0 comments

Right now, the spec is not clear about how unsupported data types should be handled in write().

I think there are a few reasonable options:

  • Return which data types were copied, e.g. give write() a return type of Promise<Array<{[type: string]: boolean}>>.
  • Specify that the browser should error.
  • Specify that the browser should ignore it.

It would also be useful if there was a way to query for supported types (#67), especially in the last case.

Conflicting browser implementations

Consider the following snippet:

document.body.addEventListener("click", async function() {
  console.log("copying!");
  const item = new ClipboardItem({
    "food/falafel": new Blob(["\uD83E\uDDC6"], { type: "food/falafel" })
  });
  await navigator.clipboard.write([item]);
  console.log("copied!");
});

Chrome throws an error. It seems to be DOMException for unknown data types and NotAllowedException for known but unsupported data types. Screen Shot 2020-06-26 at 16 05 17 Screen Shot 2020-06-26 at 16 26 09

Safari resolves the Promise without an error. Screen Shot 2020-06-26 at 16 05 57

Note that since the return type is Promise<void>, there is no other way (in general) to check that the copy operation succeeded as expected.

lgarron avatar Jun 26 '20 23:06 lgarron