@@ -484,13 +484,16 @@ function initAnchorClipboard(){
484
484
485
485
function initCodeClipboard ( ) {
486
486
function getCodeText ( node ) {
487
- var text = node . textContent ;
487
+ // if highlight shortcode is used in inline lineno mode, remove lineno nodes before generating text, otherwise it doesn't hurt
488
+ var code = node . cloneNode ( true ) ;
489
+ Array . from ( code . querySelectorAll ( '*:scope > span > span:first-child:not(:last-child)' ) ) . forEach ( function ( lineno ) {
490
+ lineno . remove ( ) ;
491
+ } ) ;
492
+ var text = code . textContent ;
488
493
// remove a trailing line break, this may most likely
489
494
// come from the browser / Hugo transformation
490
495
text = text . replace ( / \n $ / , '' ) ;
491
- // removes leading $ signs from text in an assumption
492
- // that this has to be the unix prompt marker - weird
493
- return text . replace ( / ^ \$ \s / gm, '' ) ;
496
+ return text ;
494
497
}
495
498
496
499
function fallbackMessage ( action ) {
@@ -508,114 +511,41 @@ function initCodeClipboard(){
508
511
return actionMsg ;
509
512
}
510
513
511
- if ( ! window . disableHighlightWrapFix ) {
512
- // if the highlight shortcode was used with table lineno mode, the generated DOM
513
- // is a table, containg exactly one row with two cells, the first cell for all linenos
514
- // the second with the code;
515
- // this does not look nice if the code gets wrapped around, so we reformat the table
516
- // by creating a row for each line, containing the two cells but with only the content of
517
- // one line
518
- var codeTables = Array . from ( document . querySelectorAll ( 'code' ) ) . reduce ( function ( a , code ) {
519
- // collect all tbody's without duplicates that need our treatment
520
- if ( code . parentNode . tagName . toLowerCase ( ) == 'pre' &&
521
- code . parentNode . parentNode . tagName . toLowerCase ( ) == 'td' &&
522
- code . parentNode . parentNode . parentNode . tagName . toLowerCase ( ) == 'tr' &&
523
- code . parentNode . parentNode . parentNode . parentNode . tagName . toLowerCase ( ) == 'tbody' &&
524
- code . parentNode . parentNode . parentNode . querySelector ( 'td:first-child > pre > code' ) == code &&
525
- ( ! a . length || a [ a . length - 1 ] != code . parentNode . parentNode . parentNode . parentNode ) ) {
526
- var table = code . parentNode . parentNode . parentNode . parentNode ;
527
- a . push ( table ) ;
528
- }
529
- return a ;
530
- } , [ ] ) ;
531
- for ( var i = 0 ; i < codeTables . length ; i ++ ) {
532
- // now treat the table (tbody);
533
- // first we collect some data, setting up our row template and collect the text
534
- // representation of the code for later usage with copy-to-clipboard
535
- var table = codeTables [ i ] ;
536
- var text = getCodeText ( table . querySelector ( 'td:last-child code' ) ) ;
537
- var tr = table . querySelector ( 'tr' ) . cloneNode ( ) ;
538
- tr . appendChild ( table . querySelector ( 'td:first-child' ) . cloneNode ( ) )
539
- . appendChild ( table . querySelector ( 'td:first-child pre' ) . cloneNode ( ) )
540
- . appendChild ( table . querySelector ( 'td:first-child code' ) . cloneNode ( ) )
541
- . classList . add ( 'nocode' ) ;
542
- tr . appendChild ( table . querySelector ( 'td:last-child' ) . cloneNode ( ) )
543
- . appendChild ( table . querySelector ( 'td:last-child pre' ) . cloneNode ( ) )
544
- . appendChild ( table . querySelector ( 'td:last-child code' ) . cloneNode ( ) )
545
- . classList . add ( 'nocode' ) ;
546
-
547
- // select lineno and code cell of first line that contains all the content
548
- var linenums = table . querySelectorAll ( 'td:first-child code > span' ) ;
549
- var codes = table . querySelectorAll ( 'td:last-child code > span' ) ;
550
- for ( var j = 0 ; j < linenums . length ; j ++ ) {
551
- // now create a new table row by cloning our template
552
- // and transfering the original content
553
- var clonedTr = tr . cloneNode ( true ) ;
554
- var code1 = clonedTr . querySelector ( 'td:first-child code' ) ;
555
- var code2 = clonedTr . querySelector ( 'td:last-child code' ) ;
556
- code1 . appendChild ( linenums [ j ] ) ;
557
- code2 . appendChild ( codes [ j ] ) ;
558
- table . appendChild ( clonedTr ) ;
559
- }
560
- // in the end we have an empty first row, that needs to be deleted
561
- table . querySelector ( 'tr:first-child' ) . remove ( ) ;
562
- // we delete the reformat marker of the first code cell to allow the
563
- // copy-to-clipboard functionality
564
- table . querySelector ( 'tr:first-child td:last-child code' ) . classList . remove ( 'nocode' ) ;
565
- // put the text representation into a data attribute
566
- table . querySelector ( 'tr:first-child td:last-child code' ) . dataset [ 'code' ] = text ;
567
- // finally mark our tbody to apply special CSS styling
568
- table . parentNode . parentNode . parentNode . classList . add ( 'wrapfix' ) ;
569
- }
570
- }
571
-
572
- var codeElements = document . querySelectorAll ( 'code:not(.nocode)' ) ;
514
+ var codeElements = document . querySelectorAll ( 'code' ) ;
573
515
for ( var i = 0 ; i < codeElements . length ; i ++ ) {
574
516
var code = codeElements [ i ] ;
575
- var text = code . textContent ;
517
+ var text = getCodeText ( code ) ;
576
518
var inPre = code . parentNode . tagName . toLowerCase ( ) == 'pre' ;
519
+ var inTable = inPre &&
520
+ code . parentNode . parentNode . tagName . toLowerCase ( ) == 'td' ;
577
521
// avoid copy-to-clipboard for highlight shortcode in table lineno mode
578
- var isFirstLineCell = inPre &&
579
- code . parentNode . parentNode . tagName . toLowerCase ( ) == 'td' &&
522
+ var isFirstLineCell = inTable &&
580
523
code . parentNode . parentNode . parentNode . querySelector ( 'td:first-child > pre > code' ) == code ;
581
524
582
525
if ( ! isFirstLineCell && ( inPre || text . length > 5 ) ) {
583
526
var clip = new ClipboardJS ( '.copy-to-clipboard-button' , {
584
527
text : function ( trigger ) {
585
- if ( ! ( trigger . previousElementSibling && trigger . previousElementSibling . matches ( 'code' ) ) ) {
528
+ if ( ! trigger . previousElementSibling ) {
586
529
return '' ;
587
530
}
588
- // if we already have a text representation, return it
589
- var code = trigger . previousElementSibling ;
590
- if ( code . dataset . code ) {
591
- return code . dataset . code ;
592
- }
593
- // if highlight shortcode used in inline lineno mode, remove lineno nodes before generating text
594
- code = code . cloneNode ( true ) ;
595
- Array . from ( code . querySelectorAll ( '*:scope > span > span:first-child:not(:last-child)' ) ) . forEach ( function ( lineno ) {
596
- lineno . remove ( ) ;
597
- } ) ;
598
- // generate and save generated text for repeated usage
599
- var text = getCodeText ( code ) ;
600
- trigger . previousElementSibling . dataset [ 'code' ] = text ;
601
- return text ;
531
+ return trigger . previousElementSibling . dataset . code || '' ;
602
532
}
603
533
} ) ;
604
534
605
535
clip . on ( 'success' , function ( e ) {
606
536
e . clearSelection ( ) ;
607
- var inPre = e . trigger . parentNode . tagName . toLowerCase ( ) == 'pre' ;
537
+ var doBeside = e . trigger . parentNode . tagName . toLowerCase ( ) == 'pre' || ( e . trigger . previousElementSibling && e . trigger . previousElementSibling . tagName . toLowerCase ( ) == 'table' ) ;
608
538
e . trigger . setAttribute ( 'aria-label' , window . T_Copied_to_clipboard ) ;
609
- e . trigger . classList . add ( 'tooltipped' , 'tooltipped-' + ( inPre ? 'w' : 's' + ( isRtl ?'e' :'w' ) ) ) ;
539
+ e . trigger . classList . add ( 'tooltipped' , 'tooltipped-' + ( doBeside ? 'w' : 's' + ( isRtl ?'e' :'w' ) ) ) ;
610
540
} ) ;
611
541
612
542
clip . on ( 'error' , function ( e ) {
613
- var inPre = e . trigger . parentNode . tagName . toLowerCase ( ) == 'pre' ;
543
+ var doBeside = e . trigger . parentNode . tagName . toLowerCase ( ) == 'pre' || ( e . trigger . previousElementSibling && e . trigger . previousElementSibling . tagName . toLowerCase ( ) == 'table' ) ;
614
544
e . trigger . setAttribute ( 'aria-label' , fallbackMessage ( e . action ) ) ;
615
- e . trigger . classList . add ( 'tooltipped' , 'tooltipped-' + ( inPre ? 'w' : 's' + ( isRtl ?'e' :'w' ) ) ) ;
545
+ e . trigger . classList . add ( 'tooltipped' , 'tooltipped-' + ( doBeside ? 'w' : 's' + ( isRtl ?'e' :'w' ) ) ) ;
616
546
var f = function ( ) {
617
547
e . trigger . setAttribute ( 'aria-label' , window . T_Copied_to_clipboard ) ;
618
- e . trigger . classList . add ( 'tooltipped' , 'tooltipped-' + ( inPre ? 'w' : 's' + ( isRtl ?'e' :'w' ) ) ) ;
548
+ e . trigger . classList . add ( 'tooltipped' , 'tooltipped-' + ( doBeside ? 'w' : 's' + ( isRtl ?'e' :'w' ) ) ) ;
619
549
document . removeEventListener ( 'copy' , f ) ;
620
550
} ;
621
551
document . addEventListener ( 'copy' , f ) ;
@@ -627,12 +557,11 @@ function initCodeClipboard(){
627
557
code . parentNode . classList . add ( 'pre-code' ) ;
628
558
}
629
559
else {
630
- var clone = code . cloneNode ( true ) ;
560
+ var parent = code . parentNode ;
631
561
var span = document . createElement ( 'span' ) ;
632
562
span . classList . add ( 'copy-to-clipboard' ) ;
633
- span . appendChild ( clone ) ;
634
- code . parentNode . replaceChild ( span , code ) ;
635
- code = clone ;
563
+ span . appendChild ( code ) ;
564
+ parent . appendChild ( span ) ;
636
565
}
637
566
var button = document . createElement ( 'span' ) ;
638
567
button . classList . add ( 'copy-to-clipboard-button' ) ;
@@ -642,7 +571,15 @@ function initCodeClipboard(){
642
571
this . removeAttribute ( 'aria-label' ) ;
643
572
this . classList . remove ( 'tooltipped' , 'tooltipped-w' , 'tooltipped-se' , 'tooltipped-sw' ) ;
644
573
} ) ;
645
- code . parentNode . insertBefore ( button , code . nextSibling ) ;
574
+ if ( inTable ) {
575
+ var table = code . parentNode . parentNode . parentNode . parentNode . parentNode ;
576
+ table . dataset [ 'code' ] = text ;
577
+ table . parentNode . insertBefore ( button , table . nextSibling ) ;
578
+ }
579
+ else {
580
+ code . dataset [ 'code' ] = text ;
581
+ code . parentNode . insertBefore ( button , code . nextSibling ) ;
582
+ }
646
583
}
647
584
}
648
585
}
0 commit comments