[clipops][editing] document.execCommand('copy', false, 'some data') ?

It's pretty obvious that the currently spec'ed code for "place 'foo' on the
clipboard when user clicks my <button>"  is not nice at all. Per  [1] the
code you need now is more or less

document.addEventListener('copy', function(e){
  e.clipboardData.setData('text/plain', 'foo');
  e.preventDefault(); // default behaviour is to copy any selected text
}
element.onclick = function(){
  document.execCommand('copy', false, ''); // only works in click handler
or other user-triggered thread
}

There is a very annoying disconnect between the code that knows what the
user clicked and the code that actually places content on the clipboard.
This requires rather poor hacks and workarounds like having global
variables (or variables in a closure both functions can access) for storing
data temporarily.

However, document.execCommand() is spec'ed as having a "value" argument.
What about actually using it here? Simplifying the above code to:

element.onclick = function(){
    document.execCommand('copy', false, 'foo');
}

If we wanted to get fancy, we could even allow

element.onclick = function(){
    document.execCommand('copy', false, {'text/plain': 'foo',
'text/html':'<p><b>foo</b></p>'});
}

Thoughts?

[1] https://bugzilla.mozilla.org/show_bug.cgi?id=1151429#c2

Received on Friday, 10 April 2015 12:13:27 UTC