sudodoki / copy-to-clipboard

Copy stuff into clipboard from your browser using JS
http://sudodoki.github.io/copy-to-clipboard/example/
MIT License
1.33k stars 130 forks source link

Copy incorrectly if my text contains "\r\n" when using firefox #124

Open LiuRhoRamen opened 2 years ago

LiuRhoRamen commented 2 years ago

I need to copy text which contains "\r\n". when using firefox, I found that the "\r\n" inside the copied text turned to be "\n\n". After reading the source code, I found the problem is the use of Selection. (Maybe it's a bug of Firefox)

Heres a simple example to explain the problem: https://jsfiddle.net/LiuRhoRamen/mo1zfxvb/7/

Maybe It's better to create a textarea, call .select() method and do the copy?

sudodoki commented 2 years ago

yes, that's an issue I have observed as well. Using textarea introduces other issues. Did you try using alternative copy packages?

LiuRhoRamen commented 2 years ago

Yeah, I've found sindresorhus/copy-text-to-clipboard which use textarea, but I'm not sure if it will cause some other issues that you mentioned. Also I've found there's a new api called navigator.clipboard that can be used in modern browsers.

A-ZHANG1 commented 1 year ago

For plain/text, try replace /r/n with ES6/Babel backticks.i.e. text.replace(/(\r\n)/g, ); For html, the solution is to create