From: MatmaRex Date: Fri, 7 Sep 2012 19:04:25 +0000 (+0200) Subject: (bug 40044) make $.tablesorter treat alt attribute like text X-Git-Tag: 1.31.0-rc.0~20807^2 X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=commitdiff_plain;h=39d1f3967be9d888c91d93c9df73a40359cef2f6;p=lhc%2Fweb%2Fwiklou.git (bug 40044) make $.tablesorter treat alt attribute like text Now images in cells will not be ignored when sorting; instead, their alt attribute will be used for the sort key. Internal interface changes: * renamed getElementText() to getElementSortKey() with changes Change-Id: I3e9898f3424a995507095384e80c8b570da118f0 --- diff --git a/resources/jquery/jquery.tablesorter.js b/resources/jquery/jquery.tablesorter.js index 6297f9a745..8320304ff1 100644 --- a/resources/jquery/jquery.tablesorter.js +++ b/resources/jquery/jquery.tablesorter.js @@ -86,7 +86,7 @@ return false; } - function getElementText( node ) { + function getElementSortKey( node ) { var $node = $( node ), // Use data-sort-value attribute. // Use data() instead of attr() so that live value changes @@ -98,13 +98,24 @@ // like charAt, toLowerCase and split are expected. return String( data ); } else { - return $node.text(); + if ( node.tagName.toLowerCase() === 'img' ) { + return $node.attr( 'alt' ) || ''; // handle undefined alt + } else { + return $.map( $.makeArray( node.childNodes ), function( elem ) { + // 1 is for document.ELEMENT_NODE (the constant is undefined on old browsers) + if ( elem.nodeType === 1 ) { + return getElementSortKey( elem ); + } else { + return $.text( elem ); + } + } ).join( '' ); + } } } function getTextFromRowAndCellIndex( rows, rowIndex, cellIndex ) { if ( rows[rowIndex] && rows[rowIndex].cells[cellIndex] ) { - return $.trim( getElementText( rows[rowIndex].cells[cellIndex] ) ); + return $.trim( getElementSortKey( rows[rowIndex].cells[cellIndex] ) ); } else { return ''; } @@ -205,7 +216,7 @@ cache.row.push( $row ); for ( var j = 0; j < totalCells; ++j ) { - cols.push( parsers[j].format( getElementText( $row[0].cells[j] ), table, $row[0].cells[j] ) ); + cols.push( parsers[j].format( getElementSortKey( $row[0].cells[j] ), table, $row[0].cells[j] ) ); } cols.push( cache.normalized.length ); // add position for rowCache diff --git a/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js b/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js index 0000f0c34f..b8d816e981 100644 --- a/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js @@ -881,4 +881,61 @@ } ); +QUnit.test( 'Sorting images using alt text', function ( assert ) { + var $table = $( + '' + + '' + + '' + + '' + + '
THEAD
2
1
' + ); + $table.tablesorter().find( '.headerSort:eq(0)' ).click(); + + assert.equal( + $table.find( 'td' ).first().text(), + '1', + 'Applied correct sorting order' + ); +} ); + +QUnit.test( 'Sorting images using alt text (complex)', function ( assert ) { + var $table = $( + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '
THEAD
DA
CC
AF
AE
AD
AC
' + ); + $table.tablesorter().find( '.headerSort:eq(0)' ).click(); + + assert.equal( + $table.find( 'td' ).text(), + 'CDEFCCA', + 'Applied correct sorting order' + ); +} ); + +QUnit.test( 'Sorting images using alt text (with format autodetection)', function ( assert ) { + var $table = $( + '' + + '' + + '' + + '' + + '' + + '' + + '
THEAD
17
16
5
4
' + ); + $table.tablesorter().find( '.headerSort:eq(0)' ).click(); + + assert.equal( + $table.find( 'td' ).text(), + '4517', + 'Applied correct sorting order' + ); +} ); + }( jQuery, mediaWiki ) );