Skip to content

Commit a3ede10

Browse files
committed
Fixes firefox copy paste issue
1 parent 2b0274c commit a3ede10

File tree

1 file changed

+29
-1
lines changed
  • src/librustdoc/html/static/js

1 file changed

+29
-1
lines changed

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Local js definitions:
22
/* global addClass, getSettingValue, hasClass, updateLocalStorage */
3-
/* global onEachLazy, removeClass, getVar */
3+
/* global onEachLazy, removeClass, getVar, nonnull */
44

55
"use strict";
66

@@ -2138,3 +2138,31 @@ function preLoadCss(cssUrl) {
21382138
elem.addEventListener("click", showHideCodeExampleButtons);
21392139
});
21402140
}());
2141+
2142+
// This section is a bugfix for firefox: when copying text with `user-select: none`, it adds
2143+
// extra backline characters.
2144+
//
2145+
// Rustdoc issue: Workaround for https://github.com/rust-lang/rust/issues/141464
2146+
// Firefox issue: https://bugzilla.mozilla.org/show_bug.cgi?id=1273836
2147+
(function() {
2148+
document.body.addEventListener("copy", event => {
2149+
let target = nonnull(event.target);
2150+
let isInsideCode = false;
2151+
while (target && target !== document.body) {
2152+
// @ts-expect-error
2153+
if (target.tagName === "CODE") {
2154+
isInsideCode = true;
2155+
break;
2156+
}
2157+
// @ts-expect-error
2158+
target = target.parentElement;
2159+
}
2160+
if (!isInsideCode) {
2161+
return;
2162+
}
2163+
const selection = document.getSelection();
2164+
// @ts-expect-error
2165+
nonnull(event.clipboardData).setData("text/plain", selection.toString());
2166+
event.preventDefault();
2167+
});
2168+
}());

0 commit comments

Comments
 (0)