-
Notifications
You must be signed in to change notification settings - Fork 13.5k
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
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. |
There is a bug report open in firefox about it: https://bugzilla.mozilla.org/show_bug.cgi?id=1273836 It's kinda a weird corner case apparently where they don't really what's the right behaviour. |
Ah interesting: if we remove the |
e.preventDefault(); | ||
}); | ||
} | ||
|
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.
So I think JS is the only way to fix this bug. However, it can be reproduced on other pages that src
so the fix needs to be moved into main.js
. We also need to make some changes to only tamper with the clipboard copy if we're copying from a <code>
element.
So at the end of the main.js
file you can add:
// This section is a bugfix for firefox: when copying text with `user-select: none`, it adds
// extra backline characters.
//
// Rustdoc issue: Workaround for https://github.com/rust-lang/rust/issues/141464
// Firefox issue: https://bugzilla.mozilla.org/show_bug.cgi?id=1273836
(function() {
document.body.addEventListener('copy', event => {
let target = event.target;
let isInsideCode = false;
while (target !== document.body) {
if (target.tagName === 'CODE') {
isInsideCode = true;
break;
}
target = target.parentElement;
}
if (!isInsideCode) {
return;
}
const selection = document.getSelection();
nonnull(event.clipboardData).setData('text/plain', selection.toString());
event.preventDefault();
});
}());
I can also send a PR with the change directly, as you prefer. 😉
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.
I ammended the commit with that change.
This comment has been minimized.
This comment has been minimized.
ca39813
to
92bc271
Compare
71e24e8
to
b0a8219
Compare
792e55b
to
4469a74
Compare
This comment has been minimized.
This comment has been minimized.
4469a74
to
4adaf0a
Compare
This comment has been minimized.
This comment has been minimized.
// | ||
// Rustdoc issue: Workaround for https://github.com/rust-lang/rust/issues/141464 | ||
// Firefox issue: https://bugzilla.mozilla.org/show_bug.cgi?id=1273836 | ||
(function() { |
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.
It needs to be in main.js
.
c67264f
to
36b9d73
Compare
This comment has been minimized.
This comment has been minimized.
Almost there, just need to fix typescript errors now. :) If you need help, don't hesitate to ask me. |
9369751
to
5ed0173
Compare
This comment has been minimized.
This comment has been minimized.
5ed0173
to
96255ec
Compare
document.body.addEventListener("copy", event => { | ||
let target = nonnull(event.target); | ||
let isInsideCode = false; | ||
while (target !== document.body) { |
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.
Just thought that it could potentially crash here since we never check target
:
while (target !== document.body) { | |
while (target && target !== document.body) { |
CI is happy and code is almost all good. Just one change then I'll approve this PR. Thanks a lot! |
96255ec
to
a3ede10
Compare
pushed it. |
There's a few hypothetical crashes in the event handler, but since preventDefault is called last, I believe this should be fine. |
@lolbinarycat Absolutely. Although I do expect that it's not possible on any main browser to have this code fail (or maybe under special security levels? Unsure). Thanks a lot for your work @gstjepan2! @bors r+ rollup |
Rollup of 9 pull requests Successful merges: - #140005 (Set MSG_NOSIGNAL for UnixStream) - #140622 (compiletest: Improve diagnostics for line annotation mismatches) - #142354 (Fixes firefox copy paste issue) - #142695 (Port `#[rustc_skip_during_method_dispatch]` to the new attribute system) - #142779 (Add note about `str::split` handling of no matches.) - #142894 (phantom_variance_markers: fix identifier usage in macro) - #142928 (Fix hang in --print=file-names in bootstrap) - #142932 (rustdoc-json: Keep empty generic args if parenthesized) - #142933 (Simplify root goal API of solver a bit) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of #142354 - gstjepan2:firefox_copy_paste_issue, r=GuillaumeGomez Fixes firefox copy paste issue
Fixes firefox copy paste issue where copy pasting from src adds extra newline.
Closes: #141464