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

Not working if the selected text is in disabled textarea in FF

Open josefsabl opened this issue 3 years ago • 7 comments

If user selects text in disabled textarea (and probably input as well) in Firefox (104.0.2 in my case), this function doesn't deselect it.

It is pretty nasty in combination with this https://github.com/sudodoki/copy-to-clipboard

josefsabl avatar Sep 08 '22 14:09 josefsabl

oh my, does document.execCommand works on its own in this case?

sudodoki avatar Sep 08 '22 16:09 sudodoki

Yes, this works. But if there is disabled textarea with text selected somewhere in the page, the selected text gets copied instead of the text that is sent to copy-to-clipboard.

Unfortunatelly, as it seems, enabling the textarea before trying to apply toggle-selection and then disabling again it doesn't solve the problem. Seems that selected text during 'disabled' mode doesn't make the blur method deselect it later even if it is enabled at that moment (didn't test this that much though).

josefsabl avatar Sep 09 '22 07:09 josefsabl

yeah I don't have FF either to test it out. Does it still work for your use case in other browsers?

sudodoki avatar Sep 09 '22 07:09 sudodoki

I had to install FF to work on this as well :-) In Chrome it works as expected. Didn't test other browsers.

josefsabl avatar Sep 09 '22 08:09 josefsabl

I see, well my guess would be it's either:

  • selection.rangeCount returning falsy value in this case :thinking: (https://github.com/sudodoki/toggle-selection/blob/gh-pages/index.js#L4)
  • active.blur throwing an exception (https://github.com/sudodoki/toggle-selection/blob/gh-pages/index.js#L17)

sudodoki avatar Sep 09 '22 08:09 sudodoki

  • I don't think selection.rangeCount has anything to do in case of textarea. I was debugging (console logging) your script and I believe the rangeCount was 0.
  • No, active.blur didn't throw an exception. In fact no error could be seen in console even if debug mode for you script was set to true.

It's just that active.blur doesn't deslect the selected text in textarea.

I understand you use two methods to deselect text a) normal selection by working with selection and ranges b) active.blur for inputs and textareas. It just looks that method b) just doesn't work for disabled textarea.

josefsabl avatar Sep 09 '22 09:09 josefsabl

I believe the rangeCount was 0.

Then, here's your culprit as it's the first thing that is being checked whenever we go into function

sudodoki avatar Sep 09 '22 09:09 sudodoki