Skip to content

Commit 4469a74

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

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/librustdoc/html/static/js/src-script.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,31 @@ const handleSrcHighlight = (function() {
206206
};
207207
}());
208208

209+
// This section is a bugfix for firefox: when copying text with `user-select: none`, it adds
210+
// extra backline characters.
211+
//
212+
// Rustdoc issue: Workaround for https://github.com/rust-lang/rust/issues/141464
213+
// Firefox issue: https://bugzilla.mozilla.org/show_bug.cgi?id=1273836
214+
(function() {
215+
document.body.addEventListener('copy', event => {
216+
let target = event.target;
217+
let isInsideCode = false;
218+
while (target !== document.body) {
219+
if (target.tagName === 'CODE') {
220+
isInsideCode = true;
221+
break;
222+
}
223+
target = target.parentElement;
224+
}
225+
if (!isInsideCode) {
226+
return;
227+
}
228+
const selection = document.getSelection();
229+
nonnull(event.clipboardData).setData('text/plain', selection.toString());
230+
event.preventDefault();
231+
});
232+
}());
233+
209234
window.addEventListener("hashchange", highlightSrcLines);
210235

211236
onEachLazy(document.querySelectorAll("a[data-nosnippet]"), el => {

0 commit comments

Comments
 (0)