Skip to content

Commit 36b9d73

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

File tree

1 file changed

+25
-0
lines changed
  • src/librustdoc/html/static/js

1 file changed

+25
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,3 +2138,28 @@ 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 = event.target;
2150+
let isInsideCode = false;
2151+
while (target !== document.body) {
2152+
if (target.tagName === "CODE") {
2153+
isInsideCode = true;
2154+
break;
2155+
}
2156+
target = target.parentElement;
2157+
}
2158+
if (!isInsideCode) {
2159+
return;
2160+
}
2161+
const selection = document.getSelection();
2162+
nonnull(event.clipboardData).setData("text/plain", selection.toString());
2163+
event.preventDefault();
2164+
});
2165+
}());

0 commit comments

Comments
 (0)