RESOLVED CONFIGURATION CHANGED 177715
clipboardData.setData() fails on iOS with certain kind of selections.
https://bugs.webkit.org/show_bug.cgi?id=177715
Summary clipboardData.setData() fails on iOS with certain kind of selections.
Lucas Garron
Reported 2017-09-30 23:28:32 PDT
As documented in https://bugs.webkit.org/show_bug.cgi?id=156529 , Safari doesn't allow document.execCommand("copy") when there is no selection. It is possible work around this but by creating a temporary selection: https://bugs.webkit.org/show_bug.cgi?id=156529#c4 However, this workaround is broken on iOS. After the temporary selection is selected, document.queryCommandEnabled("copy") returns true and a function registered using document.addEventListener("copy", ...) fires. *However*, `event.clipboardData.setData()` has no effect. Consider the following sample: <script> document.addEventListener("copy", function(e) { e.clipboardData.setData("text/plain", "This is plain text."); console.log(e.clipboardData.getData("text/plain")); e.preventDefault(); }); function copy() { var sel = document.getSelection(); var range = document.createRange(); range.selectNodeContents(document.body); sel.removeAllRanges(); sel.addRange(range); document.execCommand("copy"); window.getSelection().removeAllRanges(); } </script> <button onclick='copy()'>Copy text (plain)</button> The `console.log(...)` line prints "This is plain text." on desktop Safari, but a blank string on iOS. Consequently, the clipboard is emptied (even if it contained something before).
Attachments
Wenson Hsieh
Comment 1 2017-09-30 23:35:27 PDT
(In reply to Lucas Garron from comment #0) > As documented in https://bugs.webkit.org/show_bug.cgi?id=156529 , Safari > doesn't allow document.execCommand("copy") when there is no selection. > It is possible work around this but by creating a temporary selection: > https://bugs.webkit.org/show_bug.cgi?id=156529#c4 > > However, this workaround is broken on iOS. After the temporary selection is > selected, document.queryCommandEnabled("copy") returns true and a function > registered using document.addEventListener("copy", ...) fires. *However*, > `event.clipboardData.setData()` has no effect. > > Consider the following sample: > > <script> > document.addEventListener("copy", function(e) { > e.clipboardData.setData("text/plain", "This is plain text."); > console.log(e.clipboardData.getData("text/plain")); > e.preventDefault(); > }); > > function copy() { > var sel = document.getSelection(); > var range = document.createRange(); > range.selectNodeContents(document.body); > sel.removeAllRanges(); > sel.addRange(range); > document.execCommand("copy"); > window.getSelection().removeAllRanges(); > } > </script> > <button onclick='copy()'>Copy text (plain)</button> > > The `console.log(...)` line prints "This is plain text." on desktop Safari, > but a blank string on iOS. > Consequently, the clipboard is emptied (even if it contained something > before). This is working for me using ToT WebKit, on iOS 11. I'm pretty sure Ryosuke and I fixed this with recent work around enhanced pasteboard/DataTransfer support in WebKit.
Wenson Hsieh
Comment 2 2017-09-30 23:39:01 PDT
> This is working for me using ToT WebKit, on iOS 11. > > I'm pretty sure Ryosuke and I fixed this with recent work around enhanced > pasteboard/DataTransfer support in WebKit. ...more specifically: <https://trac.webkit.org/changeset/222228/webkit>.
Alexey Proskuryakov
Comment 3 2017-10-02 10:20:18 PDT
Marking resolved per the above comment.
Radar WebKit Bug Importer
Comment 4 2017-10-02 10:20:55 PDT
Lucas Garron
Comment 5 2017-10-24 12:33:26 PDT
Thanks for the response! Is there a way for me to test the latest behaviour on a (non-beta) iOS device? Thanks, »Lucas
Ryosuke Niwa
Comment 6 2017-10-31 01:14:46 PDT
(In reply to Lucas Garron from comment #5) > Thanks for the response! > > Is there a way for me to test the latest behaviour on a (non-beta) iOS > device? You'd have to build WebKit's iOS port on iOS simulator. Follow instructions on https://github.com/WebKit/webkit
Note You need to log in before you can comment on or make changes to this bug.