Skip to content
Merged
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
39 changes: 0 additions & 39 deletions src/librustdoc/html/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2252,42 +2252,3 @@ function preLoadCss(cssUrl) {
elem.addEventListener("click", showHideCodeExampleButtons);
});
}());


// Workaround for browser-specific bugs when copying code snippets.
//
// * In Firefox, copying text that includes elements with `user-select: none`
// inserts extra blank lines.
// - Firefox issue: https://bugzilla.mozilla.org/show_bug.cgi?id=1273836
// - Rust issue: https://github.com/rust-lang/rust/issues/141464
//
// * In Chromium-based browsers, `document.getSelection()` includes elements
// with `user-select: none`, causing unwanted line numbers to be copied.
// - Chromium issue: https://issues.chromium.org/issues/446539520
// - Rust issue: https://github.com/rust-lang/rust/issues/146816
(function() {
document.body.addEventListener("copy", event => {
let target = nonnull(event.target);
let isInsideCode = false;
while (target && target !== document.body) {
// @ts-expect-error
if (target.tagName === "CODE") {
isInsideCode = true;
break;
}
// @ts-expect-error
target = target.parentElement;
}
if (!isInsideCode) {
return;
}
const selection = nonnull(document.getSelection());
const text = Array.from({ length: selection.rangeCount }, (_, i) => {
const fragment = selection.getRangeAt(i).cloneContents();
fragment.querySelectorAll("[data-nosnippet]").forEach(el => el.remove());
return fragment.textContent;
}).join("");
nonnull(event.clipboardData).setData("text/plain", text);
event.preventDefault();
});
}());
Loading