-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Fixes firefox copy paste issue #142354
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
base: master
Are you sure you want to change the base?
Fixes firefox copy paste issue #142354
Conversation
rustbot has assigned @GuillaumeGomez. Use |
Some changes occurred in HTML/CSS/JS. |
This comment has been minimized.
This comment has been minimized.
@@ -206,6 +206,15 @@ const handleSrcHighlight = (function() { | |||
}; | |||
}()); | |||
|
|||
if (navigator.userAgent.includes('Firefox')) { | |||
document.addEventListener('copy', function(e){ | |||
const text = nonnull(window.getSelection()).toString().replace(/\n\n/g, '\n'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this fix, this code:
fn foo() {}
fn bar() {}
would become:
fn foo() {}
fn bar() {}
Which definitely sounds like a new bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right it it also like looks I've went with the wrong approach.
Just settings the data to text/plain seems to fix the issue
if (navigator.userAgent.includes('Firefox')) {
document.addEventListener('copy', function(e) {
const text = nonnull(window.getSelection()).toString();
nonnull(e.clipboardData).setData('text/plain', text);
e.preventDefault();
});
}
I'll amend the commit with this
3624543
to
17ce882
Compare
This comment has been minimized.
This comment has been minimized.
17ce882
to
bb5aabf
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- looking for the string "Firefox" is correct because safari also puts "Mozilla" in its user agent string
getSelection
can only return null if in a contextless environment like a detached iframe (in which case we shouldn't be getting copy events) andclipboardData
can only be null for manually constructed events, so the usage ofnonnull
here looks correct.
It would be nice to fix this without user agent detection, but I don't see an obvious cause, the pre
element only contains one set of linebreaks, and there are no extraneous whitespace nodes. The issue is the same with js enabled and disabled.
If this fixes the issue, I guess that's good.
At the very least we should probably have a comment that says why we are doing this, like "workaround for "
bb5aabf
to
92bc271
Compare
Added a workaround for comment |
I'm really not convinced it's the right fix. Even more considering it's only for one specific use-case. Wouldn't adding the right tags to the code viewer element be a better alternative? |
I agree, but wouldn't that be a too big of a (breaking?) change just for copy pasting issue in firefox? |
I don't think so? But maybe we have something different in mind. Adding an attribute on |
Oh gotcha, i am not aware of any clean way of doing that though, expecially without js not sure if copy paste behaviour can be modified |
Hold on, have we verified this is only an issue on firefox? It doesn't seem to happen in chrome but someone should probably also check safari at least. |
Fixes firefox copy paste issue where copy pasting from src adds extra newline.
Closes: #141464