* @return {string}
*/
function getElementSortKey( node ) {
- var $node = $( node ),
- // Use data-sort-value attribute.
- // Use data() instead of attr() so that live value changes
- // are processed as well (T40152).
- data = $node.data( 'sortValue' );
+ // Get data-sort-value attribute. Uses jQuery to allow live value
+ // changes from other code paths via data(), which reside only in jQuery.
+ // Must use $().data() instead of $.data(), as the latter *only*
+ // accesses the live values, without reading HTML5 attribs first (T40152).
+ var data = $( node ).data( 'sortValue' );
if ( data !== null && data !== undefined ) {
// Cast any numbers or other stuff to a string, methods
return String( data );
}
if ( node.tagName.toLowerCase() === 'img' ) {
- return $node.attr( 'alt' ) || ''; // handle undefined alt
+ return node.alt;
}
- return $.makeArray( node.childNodes ).map( function ( elem ) {
+ // Iterate the NodeList (not an array).
+ // Also uses null-return as filter in the same pass.
+ // eslint-disable-next-line no-jquery/no-map-util
+ return $.map( node.childNodes, function ( elem ) {
if ( elem.nodeType === Node.ELEMENT_NODE ) {
if ( $( elem ).hasClass( 'reference' ) ) {
return null;
- } else {
- return getElementSortKey( elem );
}
+ return getElementSortKey( elem );
+ }
+ if ( elem.nodeType === Node.TEXT_NODE ) {
+ return elem.textContent;
}
- return $.text( elem );
+ // Ignore other node types, such as HTML comments.
+ return null;
} ).join( '' );
}
}
$thead.append( this );
} );
- $table.find( ' > tbody:first' ).before( $thead );
+ $table.find( ' > tbody' ).first().before( $thead );
}
if ( !$table.get( 0 ).tFoot ) {
$tfoot = $( '<tfoot>' );
headerIndex,
exploded,
$tableHeaders = $( [] ),
- $tableRows = $( 'thead:eq(0) > tr', table );
+ $tableRows = $( table ).find( 'thead' ).eq( 0 ).find( '> tr' );
if ( $tableRows.length <= 1 ) {
$tableHeaders = $tableRows.children( 'th' );
}
function sortText( a, b ) {
- return ( ( a < b ) ? -1 : ( ( a > b ) ? 1 : 0 ) );
+ return ts.collator.compare( a, b );
}
- function sortTextDesc( a, b ) {
- return ( ( b < a ) ? -1 : ( ( b > a ) ? 1 : 0 ) );
+ function sortNumeric( a, b ) {
+ return ( ( a < b ) ? -1 : ( ( a > b ) ? 1 : 0 ) );
}
function multisort( table, sortList, cache ) {
var i,
- sortFn = [];
+ sortFn = [],
+ parsers = $( table ).data( 'tablesorter' ).config.parsers;
for ( i = 0; i < sortList.length; i++ ) {
- sortFn[ i ] = ( sortList[ i ][ 1 ] ) ? sortTextDesc : sortText;
+ // Android doesn't support Intl.Collator
+ if ( window.Intl && Intl.Collator && parsers[ sortList[ i ][ 0 ] ].type === 'text' ) {
+ sortFn[ i ] = sortText;
+ } else {
+ sortFn[ i ] = sortNumeric;
+ }
}
cache.normalized.sort( function ( array1, array2 ) {
var i, col, ret;
for ( i = 0; i < sortList.length; i++ ) {
col = sortList[ i ][ 0 ];
- ret = sortFn[ i ].call( this, array1[ col ], array2[ col ] );
+ if ( sortList[ i ][ 1 ] ) {
+ // descending
+ ret = sortFn[ i ].call( this, array2[ col ], array1[ col ] );
+ } else {
+ // ascending
+ ret = sortFn[ i ].call( this, array1[ col ], array2[ col ] );
+ }
if ( ret !== 0 ) {
return ret;
}
}
}
- function buildCollationTable() {
+ function buildCollation() {
var key, keys = [];
ts.collationTable = mw.config.get( 'tableSorterCollation' );
ts.collationRegex = null;
ts.collationRegex = new RegExp( keys.join( '|' ), 'ig' );
}
}
+ if ( window.Intl && Intl.Collator ) {
+ ts.collator = new Intl.Collator( [
+ mw.config.get( 'wgPageContentLanguage' ),
+ mw.config.get( 'wgUserLanguage' )
+ ], {
+ numeric: true
+ } );
+ }
}
function cacheRegexs() {
$table = $( table ),
firstTime = true;
+ // Don't construct twice on the same table
+ if ( $.data( table, 'tablesorter' ) ) {
+ return;
+ }
// Quit if no tbody
if ( !table.tBodies ) {
return;
// may customize tableSorterCollation but load after $.ready(), other
// scripts may call .tablesorter() before they have done the
// tableSorterCollation customizations.
- buildCollationTable();
+ buildCollation();
// Legacy fix of .sortbottoms
// Wrap them inside a tfoot (because that's what they actually want to be)
buildTransformTable();
buildDateTable();
cacheRegexs();
- buildCollationTable();
+ buildCollation();
return getParserById( id );
},
},
format: function ( s ) {
var tsc;
- s = s.toLowerCase().trim();
+ s = s.trim();
if ( ts.collationRegex ) {
tsc = ts.collationTable;
s = s.replace( ts.collationRegex, function ( match ) {
- var r = tsc[ match ] ? tsc[ match ] : tsc[ match.toUpperCase() ];
- return r.toLowerCase();
+ var r,
+ upper = match.toUpperCase(),
+ lower = match.toLowerCase();
+ if ( upper === match && !lower === match ) {
+ r = tsc[ lower ] ? tsc[ lower ] : tsc[ upper ];
+ r = r.toUpperCase();
+ } else {
+ r = tsc[ match.toLowerCase() ];
+ }
+ return r;
} );
}
return s;