Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gstjepan2
Copy link
Member

@gstjepan2 gstjepan2 commented Jun 11, 2025

Fixes firefox copy paste issue where copy pasting from src adds extra newline.
Closes: #141464

@rustbot
Copy link
Collaborator

rustbot commented Jun 11, 2025

r? @GuillaumeGomez

rustbot has assigned @GuillaumeGomez.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels Jun 11, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 11, 2025

Some changes occurred in HTML/CSS/JS.

cc @GuillaumeGomez, @jsha, @lolbinarycat

@rust-log-analyzer

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');
Copy link
Member

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.

Copy link
Member Author

@gstjepan2 gstjepan2 Jun 11, 2025

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

@gstjepan2 gstjepan2 force-pushed the firefox_copy_paste_issue branch from 3624543 to 17ce882 Compare June 11, 2025 11:41
@rust-log-analyzer

This comment has been minimized.

@gstjepan2 gstjepan2 force-pushed the firefox_copy_paste_issue branch from 17ce882 to bb5aabf Compare June 11, 2025 12:14
Copy link
Contributor

@lolbinarycat lolbinarycat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. looking for the string "Firefox" is correct because safari also puts "Mozilla" in its user agent string
  2. getSelection can only return null if in a contextless environment like a detached iframe (in which case we shouldn't be getting copy events) and clipboardData can only be null for manually constructed events, so the usage of nonnull 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 "

@gstjepan2 gstjepan2 force-pushed the firefox_copy_paste_issue branch from bb5aabf to 92bc271 Compare June 12, 2025 12:36
@gstjepan2
Copy link
Member Author

Added a workaround for comment

@GuillaumeGomez
Copy link
Member

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?

@gstjepan2
Copy link
Member Author

I agree, but wouldn't that be a too big of a (breaking?) change just for copy pasting issue in firefox?

@GuillaumeGomez
Copy link
Member

I don't think so? But maybe we have something different in mind. Adding an attribute on pre.rust or on pre.rust > code to tell the web browser how to copy-paste so we don't need a JS fix should be possible, no?

@gstjepan2
Copy link
Member Author

gstjepan2 commented Jun 12, 2025

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

@lolbinarycat
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Copying and pasting from rustdoc code view leaves blank lines
5 participants