Skip to content

Commit 71e24e8

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

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

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

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

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

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

0 commit comments

Comments
 (0)