copy/paste: Try to fix Casper flake and add comments.

I think this will fix a Casper flake where there was a race
window with multiple temp DOM elements holding copied text.

I also add a comment to the code I think causes this race
for the tests.
This commit is contained in:
Steve Howell
2018-11-28 01:23:42 +00:00
committed by Tim Abbott
parent 65a9ee476c
commit b79d63e9c3
2 changed files with 22 additions and 2 deletions

View File

@@ -63,6 +63,11 @@ function copy_messages(start_message, end_message) {
window.getSelection().removeAllRanges(); window.getSelection().removeAllRanges();
window.getSelection().addRange(selectedRange); window.getSelection().addRange(selectedRange);
// Remove existing copy/paste divs, which may linger from the previous
// example. (The code clears these out with a zero-second timeout, which
// is probably sufficient for human users, but which causes problems here.)
$('#copytempdiv').remove();
// emulate copy event // emulate copy event
var event = document.createEvent('Event'); var event = document.createEvent('Event');
event.initEvent('copy', true, true); event.initEvent('copy', true, true);

View File

@@ -141,8 +141,23 @@ function copy_handler() {
$('body').append(div); $('body').append(div);
selection.selectAllChildren(div[0]); selection.selectAllChildren(div[0]);
// After the copy has happened, delete the div and /*
// change the selection back to the original selection The techniques we use in this code date back to
2013 and may be obsolete today (and may not have
been even the best workaround back then).
https://github.com/zulip/zulip/commit/fc0b7c00f16316a554349f0ad58c6517ebdd7ac4
The idea is that we build a temp div, return from
this function, let jQuery process the selection,
then restore the selection on a zero-second timer
back to the original selection.
Do not be afraid to change this code if you understand
how modern browsers deal with copy/paste. Just test
your changes carefully.
*/
window.setTimeout(function () { window.setTimeout(function () {
selection = window.getSelection(); selection = window.getSelection();
selection.removeAllRanges(); selection.removeAllRanges();