Bug 177715
Summary: | clipboardData.setData() fails on iOS with certain kind of selections. | ||
---|---|---|---|
Product: | WebKit | Reporter: | Lucas Garron <lgarron> |
Component: | HTML Editing | Assignee: | Nobody <webkit-unassigned> |
Status: | RESOLVED CONFIGURATION CHANGED | ||
Severity: | Normal | CC: | rniwa, webkit-bug-importer, wenson_hsieh |
Priority: | P2 | Keywords: | InRadar |
Version: | Safari 11 | ||
Hardware: | iPhone / iPad | ||
OS: | iOS 11 |
Lucas Garron
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 | ||
---|---|---|
Add attachment proposed patch, testcase, etc. |
Wenson Hsieh
(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
> 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
Marking resolved per the above comment.
Radar WebKit Bug Importer
<rdar://problem/34769566>
Lucas Garron
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
(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