Skip to content

Commit 4f6eaa9

Browse files
committed
prevent line number from being copied in chrome
1 parent 1d23da6 commit 4f6eaa9

File tree

1 file changed

+11
-3
lines changed
  • src/librustdoc/html/static/js

1 file changed

+11
-3
lines changed

src/librustdoc/html/static/js/main.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2248,9 +2248,17 @@ function preLoadCss(cssUrl) {
22482248
if (!isInsideCode) {
22492249
return;
22502250
}
2251-
const selection = document.getSelection();
2252-
// @ts-expect-error
2253-
nonnull(event.clipboardData).setData("text/plain", selection.toString());
2251+
// On Chrome, the line numbers are copied when copying from a code block.
2252+
// Remove them from the selection.
2253+
//
2254+
// Issue: https://github.com/rust-lang/rust/issues/146816
2255+
const selection = nonnull(document.getSelection());
2256+
const text = Array.from({ length: selection.rangeCount }, (_, i) => {
2257+
const fragment = selection.getRangeAt(i).cloneContents();
2258+
fragment.querySelectorAll("[data-nosnippet]").forEach(el => el.remove());
2259+
return fragment.textContent;
2260+
}).join("");
2261+
nonnull(event.clipboardData).setData("text/plain", text);
22542262
event.preventDefault();
22552263
});
22562264
}());

0 commit comments

Comments
 (0)