Skip to content

Commit b0a8219

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

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,32 @@ 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+
234+
209235
window.addEventListener("hashchange", highlightSrcLines);
210236

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

0 commit comments

Comments
 (0)