w3c / clipboard-apis

Clipboard API and events
https://w3c.github.io/clipboard-apis/
Other
143 stars 41 forks source link

Undefined behaviour when handling data promise resolved value #185

Closed EdgarChen closed 1 year ago

EdgarChen commented 1 year ago

Consider following example:

let object = {
  toString() {
    return "foo";
  }
};
let promise = Promise.resolve(object);
let item = new ClipboardItem({'text/plain': promise});
navigator.clipboard.write([item]);

And per steps 3.3.4.1.2 of https://w3c.github.io/clipboard-apis/#dom-clipboard-write

  1. If representationDataPromise was fulfilled with value v, then:
    1. If v is a DOMString, then follow the below steps: ....
    2. If v is a Blob, then add v to itemList.

The result seems undefined, because object isn't a DOMString or Blob.

But ClipboardItemData is defined as Promise<(DOMString or Blob)> in idl, see https://w3c.github.io/clipboard-apis/#clipboard-item-interface and object actually could be converted to a DOMString or Blob per https://webidl.spec.whatwg.org/#dfn-convert-ecmascript-to-idl-value.

Should we convert the value v to DOMString or Blob idl type first while handling the data promise resolved value, like https://encoding.spec.whatwg.org/#decode-and-enqueue-a-chunk, which should cover above example.

annevk commented 1 year ago

How I've always imagined this to work is that Web IDL essentially does a .then() on the passed in promise and in its .then() it does the type conversion and then resolves or rejects as appropriate. I.e., this is not something each specification should have to handle individually.

cc @domenic

EdgarChen commented 1 year ago

React to representationDataPromise:

Ah, I think you are right, the steps in react do the type conversion. (how did I not noticed it!) I think we can close this one.