Skip to content

Commit

Permalink
highlight: remove additional newlines from code if copied from cursor…
Browse files Browse the repository at this point in the history
… selection #925
  • Loading branch information
McShelby committed Oct 11, 2024
1 parent d5a0ee0 commit b896794
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions static/js/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,44 @@ function initCodeClipboard(){
return actionMsg;
}

document.addEventListener( 'copy', function( ev ){
// shabby FF generates empty lines on cursor selection that we need to filter out; see #925
var selection = document.getSelection();
var node = selection.anchorNode;

// in case of GC, it works without this handler;
// instead GC fails if this handler is active, because it still contains
// the line number nodes with class 'ln' in the selection, although
// they are flagged with 'user-select: none;' see https://issues.chromium.org/issues/41393366;
// so in case of GC we don't want to do anything and bail out early in below code
function selectionContainsLnClass( selection ) {
for (var i = 0; i < selection.rangeCount; i++) {
var range = selection.getRangeAt(i);
var fragment = range.cloneContents();
if (fragment.querySelector('.ln')) {
return true;
}
}
return false;
}

if( !selectionContainsLnClass( selection ) ){
while( node ){
// selection could start in a text node, so account for this as it
// obviously does not support `classList`
if( node.nodeType === Node.ELEMENT_NODE && node.classList.contains( 'highlight' ) ){
// only do this if we are inside of a code highlight node;
// now fix FFs selection by calculating the text ourself
var text = selection.toString();
ev.clipboardData.setData( 'text/plain', text );
ev.preventDefault();
break;
}
node = node.parentNode;
}
}
});

var codeElements = document.querySelectorAll( 'code' );
for( var i = 0; i < codeElements.length; i++ ){
var code = codeElements[i];
Expand Down

0 comments on commit b896794

Please sign in to comment.