copy-to-clipboard icon indicating copy to clipboard operation
copy-to-clipboard copied to clipboard

addRange fails in Chrome in some context

Open FlavienBusseuil opened this issue 8 years ago • 3 comments

I have a very specific issue in Chrome.

The first attempt to copy the content of an input content fails because somehow when document.body.appendChild(mark) is called in copy() the selection is not null. Instead the type in selection.type is set to Caret and so the rangeCountis equal to 1. Which, I suppose, made addRange fails due, I suppose again, to this error in my console:

[Deprecation] The behavior that Selection.addRange() merges existing Range and the specified Range was removed. See https://www.chromestatus.com/features/6680566019653632 for more details.

If I click somewhere in my input before the copy, it works. Don't know what's the mess with the caret?!

I'm on a mac and it works in safari. Also I'm using copy-to-clipboard through react-copy-to-clipboard.

Thanks

EDIT: As a work around I put a autofocus on the input field and it works.

FlavienBusseuil avatar Sep 29 '17 16:09 FlavienBusseuil

Hey, sorry to hear that. Can you, please, link to a reprocase in smth like jsbin/etc and provide 1) steps to reproduce, 2) chrome & OS version?

sudodoki avatar Sep 29 '17 17:09 sudodoki

I'm not sure how to reproduce this... I'm in a pretty big project/environment. I will try to find out a simple reprocase I can share when possible.

I use Chrome 61.0.3163.91 and Mac Sierra 10.12.6

FlavienBusseuil avatar Sep 29 '17 20:09 FlavienBusseuil

I have the similar problem. It occurs very rarely. In most cases - after login in and using the copy() function for the first time. Chrome gives me a message: "[Deprecation] The behavior that Selection.addRange() merges existing Range...". After that I can successfully copy text to clipboard. I've fixed it by adding before the copy('Text') next lines: const select = window.getSelection(); select.removeAllRanges();

yataras avatar Oct 08 '17 15:10 yataras