Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
LiuRhoRamen opened this issue Nov 4, 2022 · 3 comments
Open

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

LiuRhoRamen opened this issue Nov 4, 2022 · 3 comments

Comments

@LiuRhoRamen
Copy link

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
Copy link
Owner

sudodoki commented Nov 4, 2022

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

@LiuRhoRamen
Copy link
Author

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
Copy link

A-ZHANG1 commented Dec 6, 2022

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 <textarea>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants