about summary refs log tree commit diff
path: root/src/librustdoc/html
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2025-09-23 18:14:16 +0000
committerbors <bors@rust-lang.org>2025-09-23 18:14:16 +0000
commit975e6c8fec280816d24fbde6b8dfe19620f2efe6 (patch)
treed8bce208a81f7be58cea0cb192fcc1fd7a19a237 /src/librustdoc/html
parent40560823602064f4c726aea3e15e104449e1a392 (diff)
parent8e21b8913533764ed19f13598130cc236b3c238a (diff)
Auto merge of #146938 - matthiaskrgr:rollup-y06ggfz, r=matthiaskrgr
Rollup of 10 pull requests

Successful merges:

 - rust-lang/rust#146632 (Fix uses of "adaptor")
 - rust-lang/rust#146731 (test: Use SVG for terminal url test)
 - rust-lang/rust#146775 (fixes for numerous clippy warnings)
 - rust-lang/rust#146784 ([win] Use find-msvc-tools instead of cc to find the linker and rc on Windows)
 - rust-lang/rust#146799 (Fix a dangling reference in `rustc_thread_pool`)
 - rust-lang/rust#146802 (mbe: Simplifications and refactoring)
 - rust-lang/rust#146806 (add private module override re-export test)
 - rust-lang/rust#146827 (Linker-plugin-based LTO: update list of good combinations (inc. beta + nightly))
 - rust-lang/rust#146875 (tests/run-make/crate-loading: Rename source files for clarity)
 - rust-lang/rust#146877 (prevent line number from being copied in chrome)

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'src/librustdoc/html')
-rw-r--r--src/librustdoc/html/static/js/main.js25
1 files changed, 18 insertions, 7 deletions
diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js
index 75febd6f737..3ea9de381ec 100644
--- a/src/librustdoc/html/static/js/main.js
+++ b/src/librustdoc/html/static/js/main.js
@@ -2227,11 +2227,18 @@ function preLoadCss(cssUrl) {
     });
 }());
 
-// This section is a bugfix for firefox: when copying text with `user-select: none`, it adds
-// extra backline characters.
+
+// 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
 //
-// Rustdoc issue: Workaround for https://github.com/rust-lang/rust/issues/141464
-// Firefox issue: https://bugzilla.mozilla.org/show_bug.cgi?id=1273836
+// * 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);
@@ -2248,9 +2255,13 @@ function preLoadCss(cssUrl) {
         if (!isInsideCode) {
             return;
         }
-        const selection = document.getSelection();
-         // @ts-expect-error
-        nonnull(event.clipboardData).setData("text/plain", selection.toString());
+        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();
     });
 }());