diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2025-06-24 11:20:06 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-24 11:20:06 +0200 |
| commit | 78210a4163625fdc34f203c3596913df1f43fb35 (patch) | |
| tree | 36b17ea366c9ed9747ceb672a764cf447b12ef98 /src/librustdoc/html/static/js | |
| parent | 5f2dae19c4c8553ffbfad493e4e213d06782fd06 (diff) | |
| parent | a3ede1038f19262b746f21080acd90ec5f703b3f (diff) | |
Rollup merge of #142354 - gstjepan2:firefox_copy_paste_issue, r=GuillaumeGomez
Fixes firefox copy paste issue
Diffstat (limited to 'src/librustdoc/html/static/js')
| -rw-r--r-- | src/librustdoc/html/static/js/main.js | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index 7b1a61a3ffa..2de8f836da3 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -1,6 +1,6 @@ // Local js definitions: /* global addClass, getSettingValue, hasClass, updateLocalStorage */ -/* global onEachLazy, removeClass, getVar */ +/* global onEachLazy, removeClass, getVar, nonnull */ "use strict"; @@ -2138,3 +2138,31 @@ function preLoadCss(cssUrl) { elem.addEventListener("click", showHideCodeExampleButtons); }); }()); + +// 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 = 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 = document.getSelection(); + // @ts-expect-error + nonnull(event.clipboardData).setData("text/plain", selection.toString()); + event.preventDefault(); + }); +}()); |
