@@ -2227,11 +2227,18 @@ function preLoadCss(cssUrl) {
2227
2227
} ) ;
2228
2228
} ( ) ) ;
2229
2229
2230
- // This section is a bugfix for firefox: when copying text with `user-select: none`, it adds
2231
- // extra backline characters.
2230
+
2231
+ // Workaround for browser-specific bugs when copying code snippets.
2232
+ //
2233
+ // * In Firefox, copying text that includes elements with `user-select: none`
2234
+ // inserts extra blank lines.
2235
+ // - Firefox issue: https://bugzilla.mozilla.org/show_bug.cgi?id=1273836
2236
+ // - Rust issue: https://github.com/rust-lang/rust/issues/141464
2232
2237
//
2233
- // Rustdoc issue: Workaround for https://github.com/rust-lang/rust/issues/141464
2234
- // Firefox issue: https://bugzilla.mozilla.org/show_bug.cgi?id=1273836
2238
+ // * In Chromium-based browsers, `document.getSelection()` includes elements
2239
+ // with `user-select: none`, causing unwanted line numbers to be copied.
2240
+ // - Chromium issue: https://issues.chromium.org/issues/446539520
2241
+ // - Rust issue: https://github.com/rust-lang/rust/issues/146816
2235
2242
( function ( ) {
2236
2243
document . body . addEventListener ( "copy" , event => {
2237
2244
let target = nonnull ( event . target ) ;
@@ -2248,9 +2255,13 @@ function preLoadCss(cssUrl) {
2248
2255
if ( ! isInsideCode ) {
2249
2256
return ;
2250
2257
}
2251
- const selection = document . getSelection ( ) ;
2252
- // @ts -expect-error
2253
- nonnull ( event . clipboardData ) . setData ( "text/plain" , selection . toString ( ) ) ;
2258
+ const selection = nonnull ( document . getSelection ( ) ) ;
2259
+ const text = Array . from ( { length : selection . rangeCount } , ( _ , i ) => {
2260
+ const fragment = selection . getRangeAt ( i ) . cloneContents ( ) ;
2261
+ fragment . querySelectorAll ( "[data-nosnippet]" ) . forEach ( el => el . remove ( ) ) ;
2262
+ return fragment . textContent ;
2263
+ } ) . join ( "" ) ;
2264
+ nonnull ( event . clipboardData ) . setData ( "text/plain" , text ) ;
2254
2265
event . preventDefault ( ) ;
2255
2266
} ) ;
2256
2267
} ( ) ) ;
0 commit comments