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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/librustdoc/html/static/js/src-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,15 @@ const handleSrcHighlight = (function() {
};
}());

// Workaround for https://github.com/rust-lang/rust/issues/141464
if (navigator.userAgent.includes("Firefox")) {
document.addEventListener("copy", e => {
const text = nonnull(window.getSelection()).toString();
nonnull(e.clipboardData).setData("text/plain", text);
e.preventDefault();
});
}

Copy link
Member

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

window.addEventListener("hashchange", highlightSrcLines);

onEachLazy(document.querySelectorAll("a[data-nosnippet]"), el => {
Expand Down
Loading