Guest User

Untitled

a guest
Jul 27th, 2025
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Find all header rows in a thead-less table and put them in a <thead> tag.
  3.  * This only treats a row as a header row if it contains only <th>s (no <td>s)
  4.  * and if it is preceded entirely by header rows. The algorithm stops when
  5.  * it encounters the first non-header row.
  6.  *
  7.  * After this, it will look at all rows at the bottom for footer rows
  8.  * And place these in a tfoot using similar rules.
  9.  *
  10.  * If the footer is an exact duplicate of the header, it will be hidden.
  11.  *
  12.  * This function was copied from MediaWiki's jquery.tablesorter module
  13.  * @param {jQuery} $table object for a <table>
  14.  */
  15. function emulateTHeadAndFoot( $table ) {
  16.     var $thead, $tfoot, i, len,
  17.         $rows = $table.find( '> tbody > tr' );
  18.     if ( !$table.get( 0 ).tHead ) {
  19.         $thead = $( '<thead>' );
  20.         // T289817
  21.         $thead.addClass('mw-sticky-header-element');
  22.         $rows.each( function () {
  23.             if ( $( this ).children( 'td' ).length ) {
  24.                 // This row contains a <td>, so it's not a header row
  25.                 // Stop here
  26.                 return false;
  27.             }
  28.             $thead.append( this );
  29.         } );
  30.         $table.find( ' > tbody:first' ).before( $thead );
  31.     }
  32.     if ( !$table.get( 0 ).tFoot ) {
  33.         $tfoot = $( '<tfoot>' );
  34.         len = $rows.length;
  35.         for ( i = len - 1; i >= 0; i-- ) {
  36.             if ( $( $rows[ i ] ).children( 'td' ).length ) {
  37.                 break;
  38.             }
  39.             $tfoot.prepend( $( $rows[ i ] ) );
  40.         }
  41.         $table.append( $tfoot );
  42.        
  43.         // Check if footer is duplicate of header and hide if so
  44.         hideDuplicateFooter( $table );
  45.     }
  46. }
  47.  
  48. /**
  49.  * Hide the table footer if it's an exact duplicate of the header
  50.  * @param {jQuery} $table object for a <table>
  51.  */
  52. function hideDuplicateFooter( $table ) {
  53.     var $thead = $table.find( '> thead' );
  54.     var $tfoot = $table.find( '> tfoot' );
  55.    
  56.     // Only proceed if both thead and tfoot exist
  57.     if ( !$thead.length || !$tfoot.length ) {
  58.         return;
  59.     }
  60.    
  61.     var $headerRows = $thead.find( 'tr' );
  62.     var $footerRows = $tfoot.find( 'tr' );
  63.    
  64.     // Must have same number of rows
  65.     if ( $headerRows.length !== $footerRows.length ) {
  66.         return;
  67.     }
  68.    
  69.     // Compare each row
  70.     var isDuplicate = true;
  71.     $headerRows.each( function( index ) {
  72.         var $headerRow = $( this );
  73.         var $footerRow = $( $footerRows[ index ] );
  74.        
  75.         // Compare number of cells
  76.         var $headerCells = $headerRow.find( 'th, td' );
  77.         var $footerCells = $footerRow.find( 'th, td' );
  78.        
  79.         if ( $headerCells.length !== $footerCells.length ) {
  80.             isDuplicate = false;
  81.             return false; // Break out of each loop
  82.         }
  83.        
  84.         // Compare each cell's text content and tag name
  85.         $headerCells.each( function( cellIndex ) {
  86.             var $headerCell = $( this );
  87.             var $footerCell = $( $footerCells[ cellIndex ] );
  88.            
  89.             // Compare tag names (th vs td)
  90.             if ( $headerCell.prop( 'tagName' ) !== $footerCell.prop( 'tagName' ) ) {
  91.                 isDuplicate = false;
  92.                 return false;
  93.             }
  94.            
  95.             // Compare text content (trimmed to ignore whitespace differences)
  96.             if ( $.trim( $headerCell.text() ) !== $.trim( $footerCell.text() ) ) {
  97.                 isDuplicate = false;
  98.                 return false;
  99.             }
  100.         } );
  101.        
  102.         if ( !isDuplicate ) {
  103.             return false; // Break out of row loop
  104.         }
  105.     } );
  106.    
  107.     // Hide the footer if it's a duplicate
  108.     if ( isDuplicate ) {
  109.         $tfoot.hide();
  110.         // Optionally add a class or data attribute to indicate it was hidden
  111.         $tfoot.attr( 'data-duplicate-hidden', 'true' );
  112.     }
  113. }
  114.  
  115. mw.hook('wikipage.content').add( function( $content ) {
  116.     // Do this for wikitable, but sortable does it on it's own already
  117.     $content.find('.wikitable:not(.sortable) ').each( function ( i, table ) {
  118.         if ( table.tBodies && !table.tHead ) {
  119.             // No thead found. Look for rows with <th>s and
  120.             // move them into a <thead> tag or a <tfoot> tag
  121.             emulateTHeadAndFoot( $(table).addClass('mw-sticky-header') );
  122.         }
  123.     } );
  124. } );
Advertisement
Add Comment
Please, Sign In to add comment