2 var header
= [ 'Planet', 'Radius (km)' ],
5 mercury
= [ 'Mercury', '2439.7' ],
6 venus
= [ 'Venus', '6051.8' ],
7 earth
= [ 'Earth', '6371.0' ],
8 mars
= [ 'Mars', '3390.0' ],
9 jupiter
= [ 'Jupiter', '69911' ],
10 saturn
= [ 'Saturn', '58232' ],
11 planets
= [ mercury
, venus
, earth
, mars
, jupiter
, saturn
],
12 planetsAscName
= [ earth
, jupiter
, mars
, mercury
, saturn
, venus
],
13 planetsAscRadius
= [ mercury
, mars
, venus
, earth
, saturn
, jupiter
],
25 simple
= [ a2
, b3
, a1
, a3
, b2
, b1
],
26 simpleAsc
= [ a1
, a2
, a3
, b1
, b2
, b3
],
27 simpleDescasc
= [ b1
, b2
, b3
, a1
, a2
, a3
],
30 header4
= [ 'column1a', 'column1b', 'column1c', 'column2' ],
31 aaa1
= [ 'A', 'A', 'A', '1' ],
32 aab5
= [ 'A', 'A', 'B', '5' ],
33 abc3
= [ 'A', 'B', 'C', '3' ],
34 bbc2
= [ 'B', 'B', 'C', '2' ],
35 caa4
= [ 'C', 'A', 'A', '4' ],
36 colspanInitial
= [ aab5
, aaa1
, abc3
, bbc2
, caa4
],
40 // Some randomly generated fake IPs
44 [ '204.204.132.158' ],
51 // Sort order should go octet by octet
58 [ '204.204.132.158' ],
77 umlautWordsSortedEn
= [
91 umlautWordsSortedSv
= [
102 [ 'ä' ], // ä sorts after z in Swedish
106 // Data set "digraph"
115 digraphWordsSorted
= [
125 [ 'January, 19 2010' ],
129 [ 'December 12 \'10' ]
135 [ 'January, 19 2010' ],
136 [ 'December 12 \'10' ]
155 // Commas sort after dots
156 // Not intentional but test to detect changes
177 correctDateSorting1
= [
178 [ '01 January 2010' ],
179 [ '05 February 2010' ],
180 [ '16 January 2010' ]
182 correctDateSortingSorted1
= [
183 [ '01 January 2010' ],
184 [ '16 January 2010' ],
185 [ '05 February 2010' ]
188 correctDateSorting2
= [
189 [ 'January 01 2010' ],
190 [ 'February 05 2010' ],
191 [ 'January 16 2010' ]
193 correctDateSortingSorted2
= [
194 [ 'January 01 2010' ],
195 [ 'January 16 2010' ],
196 [ 'February 05 2010' ]
200 [ '2009-12-25T12:30:45.001Z' ],
203 [ '2009-12-25T12:30:45' ],
204 [ '2009-12-25T12:30:45.111' ],
205 [ '2009-12-25T12:30:45+01:00' ]
207 isoDateSortingSorted
= [
209 [ '2009-12-25T12:30:45+01:00' ],
210 [ '2009-12-25T12:30:45' ],
211 [ '2009-12-25T12:30:45.001Z' ],
212 [ '2009-12-25T12:30:45.111' ],
217 QUnit
.module( 'jquery.tablesorter', QUnit
.newMwEnvironment( {
219 this.liveMonths
= mw
.language
.months
;
220 mw
.language
.months
= {
222 names
: [ 'january', 'february', 'march', 'april', 'may_long', 'june',
223 'july', 'august', 'september', 'october', 'november', 'december' ],
224 genitive
: [ 'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
225 'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen', 'december-gen' ],
226 abbrev
: [ 'jan', 'feb', 'mar', 'apr', 'may', 'jun',
227 'jul', 'aug', 'sep', 'oct', 'nov', 'dec' ]
229 names
: [ 'January', 'February', 'March', 'April', 'May', 'June',
230 'July', 'August', 'September', 'October', 'November', 'December' ],
231 genitive
: [ 'January', 'February', 'March', 'April', 'May', 'June',
232 'July', 'August', 'September', 'October', 'November', 'December' ],
233 abbrev
: [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
234 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
237 teardown: function () {
238 mw
.language
.months
= this.liveMonths
;
241 wgDefaultDateFormat
: 'dmy',
242 wgSeparatorTransformTable
: [ '', '' ],
243 wgDigitTransformTable
: [ '', '' ],
244 wgPageContentLanguage
: 'en'
249 * Create an HTML table from an array of row arrays containing text strings.
250 * First row will be header row. No fancy rowspan/colspan stuff.
252 * @param {string[]} header
253 * @param {string[][]} data
256 function tableCreate( header
, data
) {
258 $table
= $( '<table class="sortable"><thead></thead><tbody></tbody></table>' ),
259 $thead
= $table
.find( 'thead' ),
260 $tbody
= $table
.find( 'tbody' ),
263 header
.forEach( function ( str
) {
264 var $th
= $( '<th>' );
265 $th
.text( str
).appendTo( $tr
);
267 $tr
.appendTo( $thead
);
269 for ( i
= 0; i
< data
.length
; i
++ ) {
271 // eslint-disable-next-line no-loop-func
272 data
[ i
].forEach( function ( str
) {
273 var $td
= $( '<td>' );
274 $td
.text( str
).appendTo( $tr
);
276 $tr
.appendTo( $tbody
);
282 * Extract text from table.
284 * @param {jQuery} $table
285 * @return {string[][]}
287 function tableExtract( $table
) {
290 $table
.find( 'tbody' ).find( 'tr' ).each( function ( i
, tr
) {
292 $( tr
).find( 'td,th' ).each( function ( i
, td
) {
293 row
.push( $( td
).text() );
301 * Run a table test by building a table with the given data,
302 * running some callback on it, then checking the results.
304 * @param {string} msg text to pass on to qunit for the comparison
305 * @param {string[]} header cols to make the table
306 * @param {string[][]} data rows/cols to make the table
307 * @param {string[][]} expected rows/cols to compare against at end
308 * @param {function($table)} callback something to do with the table before we compare
310 function tableTest( msg
, header
, data
, expected
, callback
) {
311 QUnit
.test( msg
, function ( assert
) {
313 $table
= tableCreate( header
, data
);
315 // Give caller a chance to set up sorting and manipulate the table.
318 // Table sorting is done synchronously; if it ever needs to change back
319 // to asynchronous, we'll need a timeout or a callback here.
320 extracted
= tableExtract( $table
);
321 assert
.deepEqual( extracted
, expected
, msg
);
326 * Run a table test by building a table with the given HTML,
327 * running some callback on it, then checking the results.
329 * @param {string} msg text to pass on to qunit for the comparison
330 * @param {string} html HTML to make the table
331 * @param {string[][]} expected Rows/cols to compare against at end
332 * @param {function($table)} callback Something to do with the table before we compare
334 function tableTestHTML( msg
, html
, expected
, callback
) {
335 QUnit
.test( msg
, function ( assert
) {
339 // Give caller a chance to set up sorting and manipulate the table.
343 $table
.tablesorter();
344 $table
.find( '#sortme' ).trigger( 'click' );
347 // Table sorting is done synchronously; if it ever needs to change back
348 // to asynchronous, we'll need a timeout or a callback here.
349 extracted
= tableExtract( $table
);
350 assert
.deepEqual( extracted
, expected
, msg
);
354 function reversed( arr
) {
356 var arr2
= arr
.slice( 0 );
363 // Sample data set using planets named and their radius
366 'Basic planet table: sorting initially - ascending by name',
370 function ( $table
) {
371 $table
.tablesorter( { sortList
: [
377 'Basic planet table: sorting initially - descending by radius',
380 reversed( planetsAscRadius
),
381 function ( $table
) {
382 $table
.tablesorter( { sortList
: [
388 'Basic planet table: ascending by name',
392 function ( $table
) {
393 $table
.tablesorter();
394 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
398 'Basic planet table: ascending by name a second time',
402 function ( $table
) {
403 $table
.tablesorter();
404 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
408 'Basic planet table: ascending by name (multiple clicks)',
412 function ( $table
) {
413 $table
.tablesorter();
414 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
415 $table
.find( '.headerSort' ).eq( 1 ).trigger( 'click' );
416 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
420 'Basic planet table: descending by name',
423 reversed( planetsAscName
),
424 function ( $table
) {
425 $table
.tablesorter();
426 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' ).trigger( 'click' );
430 'Basic planet table: ascending radius',
434 function ( $table
) {
435 $table
.tablesorter();
436 $table
.find( '.headerSort' ).eq( 1 ).trigger( 'click' );
440 'Basic planet table: descending radius',
443 reversed( planetsAscRadius
),
444 function ( $table
) {
445 $table
.tablesorter();
446 $table
.find( '.headerSort' ).eq( 1 ).trigger( 'click' ).trigger( 'click' );
450 'Sorting multiple columns by passing sort list',
454 function ( $table
) {
464 'Sorting multiple columns by programmatically triggering sort()',
468 function ( $table
) {
469 $table
.tablesorter();
470 $table
.data( 'tablesorter' ).sort(
479 'Reset to initial sorting by triggering sort() without any parameters',
483 function ( $table
) {
490 $table
.data( 'tablesorter' ).sort(
496 $table
.data( 'tablesorter' ).sort();
500 'Sort via click event after having initialized the tablesorter with initial sorting',
504 function ( $table
) {
506 { sortList
: [ { 0: 'asc' }, { 1: 'asc' } ] }
508 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
512 'Multi-sort via click event after having initialized the tablesorter with initial sorting',
516 function ( $table
) {
519 { sortList
: [ { 0: 'desc' }, { 1: 'desc' } ] }
521 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
523 // Pretend to click while pressing the multi-sort key
524 event
= $.Event( 'click' );
525 event
[ $table
.data( 'tablesorter' ).config
.sortMultiSortKey
] = true;
526 $table
.find( '.headerSort' ).eq( 1 ).trigger( event
);
529 QUnit
.test( 'Reset sorting making table appear unsorted', function ( assert
) {
530 var $table
= tableCreate( header
, simple
);
537 $table
.data( 'tablesorter' ).sort( [] );
540 $table
.find( 'th.headerSortUp' ).length
+ $table
.find( 'th.headerSortDown' ).length
,
542 'No sort specific sort classes addign to header cells'
546 $table
.find( 'th' ).first().attr( 'title' ),
547 mw
.msg( 'sort-ascending' ),
548 'First header cell has default title'
552 $table
.find( 'th' ).first().attr( 'title' ),
553 $table
.find( 'th' ).last().attr( 'title' ),
554 'Both header cells\' titles match'
558 // Sorting with colspans
560 tableTest( 'Sorting with colspanned headers: spanned column',
563 [ aaa1
, aab5
, abc3
, bbc2
, caa4
],
564 function ( $table
) {
565 // Make colspanned header for test
566 $table
.find( 'tr th' ).eq( 1 ).remove();
567 $table
.find( 'tr th' ).eq( 1 ).remove();
568 $table
.find( 'tr th' ).eq( 0 ).attr( 'colspan', '3' );
570 $table
.tablesorter();
571 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
574 tableTest( 'Sorting with colspanned headers: sort spanned column twice',
577 [ caa4
, bbc2
, abc3
, aab5
, aaa1
],
578 function ( $table
) {
579 // Make colspanned header for test
580 $table
.find( 'tr th' ).eq( 1 ).remove();
581 $table
.find( 'tr th' ).eq( 1 ).remove();
582 $table
.find( 'tr' ).eq( 0 ).find( 'th' ).eq( 0 ).attr( 'colspan', '3' );
584 $table
.tablesorter();
585 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
586 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
589 tableTest( 'Sorting with colspanned headers: subsequent column',
592 [ aaa1
, bbc2
, abc3
, caa4
, aab5
],
593 function ( $table
) {
594 // Make colspanned header for test
595 $table
.find( 'tr th' ).eq( 1 ).remove();
596 $table
.find( 'tr th' ).eq( 1 ).remove();
597 $table
.find( 'tr th' ).eq( 0 ).attr( 'colspan', '3' );
599 $table
.tablesorter();
600 $table
.find( '.headerSort' ).eq( 1 ).trigger( 'click' );
603 tableTest( 'Sorting with colspanned headers: sort subsequent column twice',
606 [ aab5
, caa4
, abc3
, bbc2
, aaa1
],
607 function ( $table
) {
608 // Make colspanned header for test
609 $table
.find( 'tr th' ).eq( 1 ).remove();
610 $table
.find( 'tr th' ).eq( 1 ).remove();
611 $table
.find( 'tr th' ).eq( 0 ).attr( 'colspan', '3' );
613 $table
.tablesorter();
614 $table
.find( '.headerSort' ).eq( 1 ).trigger( 'click' );
615 $table
.find( '.headerSort' ).eq( 1 ).trigger( 'click' );
619 QUnit
.test( 'Basic planet table: one unsortable column', function ( assert
) {
620 var $table
= tableCreate( header
, planets
),
622 $table
.find( 'tr > th' ).eq( 0 ).addClass( 'unsortable' );
624 $table
.tablesorter();
625 $table
.find( 'tr > th' ).eq( 0 ).trigger( 'click' );
628 tableExtract( $table
),
633 $cell
= $table
.find( 'tr > th' ).eq( 0 );
634 $table
.find( 'tr > th' ).eq( 1 ).trigger( 'click' );
637 $cell
.hasClass( 'headerSortUp' ) || $cell
.hasClass( 'headerSortDown' ),
639 'after sort: no class headerSortUp or headerSortDown'
643 $cell
.attr( 'title' ),
645 'after sort: no title tag added'
652 'T30775: German-style (dmy) short numeric dates',
655 // German-style dates are day-month-year
663 // Sorted by ascending date
670 function ( $table
) {
671 mw
.config
.set( 'wgDefaultDateFormat', 'dmy' );
672 mw
.config
.set( 'wgPageContentLanguage', 'de' );
674 $table
.tablesorter();
675 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
680 'T30775: American-style (mdy) short numeric dates',
683 // American-style dates are month-day-year
691 // Sorted by ascending date
698 function ( $table
) {
699 mw
.config
.set( 'wgDefaultDateFormat', 'mdy' );
701 $table
.tablesorter();
702 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
707 'T19141: IPv4 address sorting',
711 function ( $table
) {
712 $table
.tablesorter();
713 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
717 'T19141: IPv4 address sorting (reverse)',
720 reversed( ipv4Sorted
),
721 function ( $table
) {
722 $table
.tablesorter();
723 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' ).trigger( 'click' );
728 'Accented Characters with custom collation',
732 function ( $table
) {
733 mw
.config
.set( 'tableSorterCollation', {
740 $table
.tablesorter();
741 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
746 'Accented Characters Swedish locale',
750 function ( $table
) {
751 mw
.config
.set( 'wgPageContentLanguage', 'sv' );
753 $table
.tablesorter();
754 $table
.find( '.headerSort:eq(0)' ).trigger( 'click' );
759 'Digraphs with custom collation',
763 function ( $table
) {
764 mw
.config
.set( 'tableSorterCollation', {
769 $table
.tablesorter();
770 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
774 QUnit
.test( 'Rowspan not exploded on init', function ( assert
) {
775 var $table
= tableCreate( header
, planets
);
777 // Modify the table to have a multiple-row-spanning cell:
778 // - Remove 2nd cell of 4th row, and, 2nd cell or 5th row.
779 $table
.find( 'tr' ).eq( 3 ).find( 'td' ).eq( 1 ).remove();
780 $table
.find( 'tr' ).eq( 4 ).find( 'td' ).eq( 1 ).remove();
781 // - Set rowspan for 2nd cell of 3rd row to 3.
782 // This covers the removed cell in the 4th and 5th row.
783 $table
.find( 'tr' ).eq( 2 ).find( 'td' ).eq( 1 ).attr( 'rowspan', '3' );
785 $table
.tablesorter();
788 $table
.find( 'tr' ).eq( 2 ).find( 'td' ).eq( 1 ).prop( 'rowSpan' ),
790 'Rowspan not exploded'
795 [ 'Earth', '6051.8' ],
797 [ 'Mars', '6051.8' ],
802 planetsRowspanII
= [ jupiter
, mercury
, saturn
, venus
, [ 'Venus', '6371.0' ], [ 'Venus', '3390.0' ] ];
805 'Basic planet table: same value for multiple rows via rowspan',
809 function ( $table
) {
810 // Modify the table to have a multiple-row-spanning cell:
811 // - Remove 2nd cell of 4th row, and, 2nd cell or 5th row.
812 $table
.find( 'tr' ).eq( 3 ).find( 'td' ).eq( 1 ).remove();
813 $table
.find( 'tr' ).eq( 4 ).find( 'td' ).eq( 1 ).remove();
814 // - Set rowspan for 2nd cell of 3rd row to 3.
815 // This covers the removed cell in the 4th and 5th row.
816 $table
.find( 'tr' ).eq( 2 ).find( 'td' ).eq( 1 ).attr( 'rowspan', '3' );
818 $table
.tablesorter();
819 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
823 'Basic planet table: same value for multiple rows via rowspan (sorting initially)',
827 function ( $table
) {
828 // Modify the table to have a multiple-row-spanning cell:
829 // - Remove 2nd cell of 4th row, and, 2nd cell or 5th row.
830 $table
.find( 'tr' ).eq( 3 ).find( 'td' ).eq( 1 ).remove();
831 $table
.find( 'tr' ).eq( 4 ).find( 'td' ).eq( 1 ).remove();
832 // - Set rowspan for 2nd cell of 3rd row to 3.
833 // This covers the removed cell in the 4th and 5th row.
834 $table
.find( 'tr' ).eq( 2 ).find( 'td' ).eq( 1 ).attr( 'rowspan', '3' );
836 $table
.tablesorter( { sortList
: [
842 'Basic planet table: Same value for multiple rows via rowspan II',
846 function ( $table
) {
847 // Modify the table to have a multiple-row-spanning cell:
848 // - Remove 1st cell of 4th row, and, 1st cell or 5th row.
849 $table
.find( 'tr' ).eq( 3 ).find( 'td' ).eq( 0 ).remove();
850 $table
.find( 'tr' ).eq( 4 ).find( 'td' ).eq( 0 ).remove();
851 // - Set rowspan for 1st cell of 3rd row to 3.
852 // This covers the removed cell in the 4th and 5th row.
853 $table
.find( 'tr' ).eq( 2 ).find( 'td' ).eq( 0 ).attr( 'rowspan', '3' );
855 $table
.tablesorter();
856 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
861 'Complex date parsing I',
865 function ( $table
) {
866 mw
.config
.set( 'wgDefaultDateFormat', 'mdy' );
868 $table
.tablesorter();
869 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
874 'Currency parsing I',
878 function ( $table
) {
879 $table
.tablesorter();
880 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
884 planetsAscNameLegacy
= planetsAscName
.slice( 0 );
885 planetsAscNameLegacy
[ 4 ] = planetsAscNameLegacy
[ 5 ];
886 planetsAscNameLegacy
.pop();
889 'Legacy compat with .sortbottom',
892 planetsAscNameLegacy
,
893 function ( $table
) {
894 $table
.find( 'tr' ).last().addClass( 'sortbottom' );
895 $table
.tablesorter();
896 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
900 QUnit
.test( 'Test detection routine', function ( assert
) {
903 '<table class="sortable">' +
904 '<caption>CAPTION</caption>' +
905 '<tr><th>THEAD</th></tr>' +
906 '<tr><td>1</td></tr>' +
907 '<tr class="sortbottom"><td>text</td></tr>' +
910 $table
.tablesorter();
911 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
914 $table
.data( 'tablesorter' ).config
.parsers
[ 0 ].id
,
916 'Correctly detected column content skipping sortbottom'
920 /** FIXME: the diff output is not very readeable. */
921 QUnit
.test( 'T34047 - caption must be before thead', function ( assert
) {
924 '<table class="sortable">' +
925 '<caption>CAPTION</caption>' +
926 '<tr><th>THEAD</th></tr>' +
927 '<tr><td>A</td></tr>' +
928 '<tr><td>B</td></tr>' +
929 '<tr class="sortbottom"><td>TFOOT</td></tr>' +
932 $table
.tablesorter();
935 $table
.children().get( 0 ).nodeName
,
937 'First element after <thead> must be <caption> (T34047)'
941 QUnit
.test( 'data-sort-value attribute, when available, should override sorting position', function ( assert
) {
944 // Example 1: All cells except one cell without data-sort-value,
945 // which should be sorted at it's text content value.
947 '<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
949 '<tr><td>Cheetah</td></tr>' +
950 '<tr><td data-sort-value="Apple">Bird</td></tr>' +
951 '<tr><td data-sort-value="Bananna">Ferret</td></tr>' +
952 '<tr><td data-sort-value="Drupe">Elephant</td></tr>' +
953 '<tr><td data-sort-value="Cherry">Dolphin</td></tr>' +
956 $table
.tablesorter().find( '.headerSort' ).eq( 0 ).trigger( 'click' );
959 $table
.find( 'tbody > tr' ).each( function ( i
, tr
) {
960 $( tr
).find( 'td' ).each( function ( i
, td
) {
962 data
: $( td
).data( 'sortValue' ),
968 assert
.deepEqual( data
, [
989 ], 'Order matches expected order (based on data-sort-value attribute values)' );
993 '<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
995 '<tr><td>D</td></tr>' +
996 '<tr><td data-sort-value="E">A</td></tr>' +
997 '<tr><td>B</td></tr>' +
998 '<tr><td>G</td></tr>' +
999 '<tr><td data-sort-value="F">C</td></tr>' +
1000 '<tr><td><span data-sort-value="D">H</span></td></tr>' +
1003 $table
.tablesorter().find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1006 $table
.find( 'tbody > tr' ).each( function ( i
, tr
) {
1007 $( tr
).find( 'td' ).each( function ( i
, td
) {
1009 data
: $( td
).data( 'sortValue' ),
1010 text
: $( td
).text()
1015 assert
.deepEqual( data
, [
1040 ], 'Order matches expected order (based on data-sort-value attribute values)' );
1042 // Example 3: Test that live changes are used from data-sort-value,
1043 // even if they change after the tablesorter is constructed (T40152).
1045 '<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
1047 '<tr><td>D</td></tr>' +
1048 '<tr><td data-sort-value="1">A</td></tr>' +
1049 '<tr><td>B</td></tr>' +
1050 '<tr><td data-sort-value="2">G</td></tr>' +
1051 '<tr><td>C</td></tr>' +
1054 // initialize table sorter and sort once
1055 $table
.tablesorter().find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1057 // Change the sortValue data properties (T40152)
1059 $table
.find( 'td:contains(A)' ).data( 'sortValue', 3 );
1061 $table
.find( 'td:contains(B)' ).data( 'sortValue', 1 );
1062 // - remove data, bring back attribute: 2
1063 $table
.find( 'td:contains(G)' ).removeData( 'sortValue' );
1065 // Now sort again (twice, so it is back at Ascending)
1066 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1067 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1070 $table
.find( 'tbody > tr' ).each( function ( i
, tr
) {
1071 $( tr
).find( 'td' ).each( function ( i
, td
) {
1073 data
: $( td
).data( 'sortValue' ),
1074 text
: $( td
).text()
1079 assert
.deepEqual( data
, [
1100 ], 'Order matches expected order, using the current sortValue in $.data()' );
1104 tableTest( 'T10115: sort numbers with commas (ascending)',
1105 [ 'Numbers' ], numbers
, numbersAsc
,
1106 function ( $table
) {
1107 $table
.tablesorter();
1108 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1112 tableTest( 'T10115: sort numbers with commas (descending)',
1113 [ 'Numbers' ], numbers
, reversed( numbersAsc
),
1114 function ( $table
) {
1115 $table
.tablesorter();
1116 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' ).trigger( 'click' );
1119 // TODO add numbers sorting tests for T10115 with a different language
1121 QUnit
.test( 'T34888 - Tables inside a tableheader cell', function ( assert
) {
1124 '<table class="sortable" id="mw-bug-32888">' +
1125 '<tr><th>header<table id="mw-bug-32888-2">' +
1126 '<tr><th>1</th><th>2</th></tr>' +
1127 '</table></th></tr>' +
1128 '<tr><td>A</td></tr>' +
1129 '<tr><td>B</td></tr>' +
1132 $table
.tablesorter();
1135 $table
.find( '> thead > tr > th.headerSort' ).length
,
1137 'Child tables inside a headercell should not interfere with sortable headers (T34888)'
1140 $( '#mw-bug-32888-2' ).find( 'th.headerSort' ).length
,
1142 'The headers of child tables inside a headercell should not be sortable themselves (T34888)'
1147 'Correct date sorting I',
1149 correctDateSorting1
,
1150 correctDateSortingSorted1
,
1151 function ( $table
) {
1152 mw
.config
.set( 'wgDefaultDateFormat', 'mdy' );
1154 $table
.tablesorter();
1155 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1160 'Correct date sorting II',
1162 correctDateSorting2
,
1163 correctDateSortingSorted2
,
1164 function ( $table
) {
1165 mw
.config
.set( 'wgDefaultDateFormat', 'dmy' );
1167 $table
.tablesorter();
1168 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1176 isoDateSortingSorted
,
1177 function ( $table
) {
1178 mw
.config
.set( 'wgDefaultDateFormat', 'dmy' );
1180 $table
.tablesorter();
1181 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1185 QUnit
.test( 'Sorting images using alt text', function ( assert
) {
1187 '<table class="sortable">' +
1188 '<tr><th>THEAD</th></tr>' +
1189 '<tr><td><img alt="2"/></td></tr>' +
1190 '<tr><td>1</td></tr>' +
1193 $table
.tablesorter().find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1196 $table
.find( 'td' ).first().text(),
1198 'Applied correct sorting order'
1202 QUnit
.test( 'Sorting images using alt text (complex)', function ( assert
) {
1204 '<table class="sortable">' +
1205 '<tr><th>THEAD</th></tr>' +
1206 '<tr><td><img alt="D" />A</td></tr>' +
1207 '<tr><td>CC</td></tr>' +
1208 '<tr><td><a><img alt="A" /></a>F</tr>' +
1209 '<tr><td><img alt="A" /><strong>E</strong></tr>' +
1210 '<tr><td><strong><img alt="A" />D</strong></tr>' +
1211 '<tr><td><img alt="A" />C</tr>' +
1214 $table
.tablesorter().find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1217 $table
.find( 'td' ).text(),
1219 'Applied correct sorting order'
1223 QUnit
.test( 'Sorting images using alt text (with format autodetection)', function ( assert
) {
1225 '<table class="sortable">' +
1226 '<tr><th>THEAD</th></tr>' +
1227 '<tr><td><img alt="1" />7</td></tr>' +
1228 '<tr><td>1<img alt="6" /></td></tr>' +
1229 '<tr><td>5</td></tr>' +
1230 '<tr><td>4</td></tr>' +
1233 $table
.tablesorter().find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1236 $table
.find( 'td' ).text(),
1238 'Applied correct sorting order'
1242 QUnit
.test( 'T40911 - The row with the largest amount of columns should receive the sort indicators', function ( assert
) {
1244 '<table class="sortable">' +
1246 '<tr><th rowspan="2" id="A1">A1</th><th colspan="2">B2a</th></tr>' +
1247 '<tr><th id="B2b">B2b</th><th id="C2b">C2b</th></tr>' +
1249 '<tr><td>A</td><td>Aa</td><td>Ab</td></tr>' +
1250 '<tr><td>B</td><td>Ba</td><td>Bb</td></tr>' +
1253 $table
.tablesorter();
1256 $table
.find( '#A1' ).attr( 'class' ),
1258 'The first column of the first row should be sortable'
1261 $table
.find( '#B2b' ).attr( 'class' ),
1263 'The th element of the 2nd row of the 2nd column should be sortable'
1266 $table
.find( '#C2b' ).attr( 'class' ),
1268 'The th element of the 2nd row of the 3rd column should be sortable'
1272 QUnit
.test( 'rowspans in table headers should prefer the last row when rows are equal in length', function ( assert
) {
1274 '<table class="sortable">' +
1276 '<tr><th rowspan="2" id="A1">A1</th><th>B2a</th></tr>' +
1277 '<tr><th id="B2b">B2b</th></tr>' +
1279 '<tr><td>A</td><td>Aa</td></tr>' +
1280 '<tr><td>B</td><td>Ba</td></tr>' +
1283 $table
.tablesorter();
1286 $table
.find( '#A1' ).attr( 'class' ),
1288 'The first column of the first row should be sortable'
1291 $table
.find( '#B2b' ).attr( 'class' ),
1293 'The th element of the 2nd row of the 2nd column should be sortable'
1297 QUnit
.test( 'holes in the table headers should not throw JS errors', function ( assert
) {
1299 '<table class="sortable">' +
1301 '<tr><th id="A1">A1</th><th>B1</th><th id="C1" rowspan="2">C1</th></tr>' +
1302 '<tr><th id="A2">A2</th></tr>' +
1304 '<tr><td>A</td><td>Aa</td><td>Aaa</td></tr>' +
1305 '<tr><td>B</td><td>Ba</td><td>Bbb</td></tr>' +
1308 $table
.tablesorter();
1309 assert
.strictEqual( $table
.find( '#A2' ).data( 'headerIndex' ),
1311 'A2 should not be a sort header'
1313 assert
.strictEqual( $table
.find( '#C1' ).data( 'headerIndex' ),
1315 'C1 should be a sort header'
1320 QUnit
.test( 'td cells in thead should not be taken into account for longest row calculation', function ( assert
) {
1322 '<table class="sortable">' +
1324 '<tr><th id="A1">A1</th><th>B1</th><td id="C1">C1</td></tr>' +
1325 '<tr><th id="A2">A2</th><th>B2</th><th id="C2">C2</th></tr>' +
1329 $table
.tablesorter();
1330 assert
.strictEqual( $table
.find( '#C2' ).data( 'headerIndex' ),
1332 'C2 should be a sort header'
1334 assert
.strictEqual( $table
.find( '#C1' ).data( 'headerIndex' ),
1336 'C1 should not be a sort header'
1340 // T43889 - exploding rowspans in more complex cases
1342 'Rowspan exploding with row headers',
1343 '<table class="sortable">' +
1344 '<thead><tr><th id="sortme">n</th><th>foo</th><th>bar</th><th>baz</th></tr></thead>' +
1346 '<tr><td>1</td><th rowspan="2">foo</th><td rowspan="2">bar</td><td>baz</td></tr>' +
1347 '<tr><td>2</td><td>baz</td></tr>' +
1350 [ '1', 'foo', 'bar', 'baz' ],
1351 [ '2', 'foo', 'bar', 'baz' ]
1355 // T55211 - exploding rowspans in more complex cases
1357 'Rowspan exploding with row headers and colspans', function ( assert
) {
1358 var $table
= $( '<table class="sortable">' +
1359 '<thead><tr><th rowspan="2">n</th><th colspan="2">foo</th><th rowspan="2">baz</th></tr>' +
1360 '<tr><th>foo</th><th>bar</th></tr></thead>' +
1362 '<tr><td>1</td><td>foo</td><td>bar</td><td>baz</td></tr>' +
1363 '<tr><td>2</td><td>foo</td><td>bar</td><td>baz</td></tr>' +
1364 '</tbody></table>' );
1366 $table
.tablesorter();
1367 assert
.strictEqual( $table
.find( 'tr' ).eq( 1 ).find( 'th' ).eq( 1 ).data( 'headerIndex' ),
1369 'Incorrect index of sort header'
1375 'Rowspan exploding with colspanned cells',
1376 '<table class="sortable">' +
1377 '<thead><tr><th id="sortme">n</th><th>foo</th><th>bar</th><th>baz</th></tr></thead>' +
1379 '<tr><td>1</td><td>foo</td><td>bar</td><td rowspan="2">baz</td></tr>' +
1380 '<tr><td>2</td><td colspan="2">foobar</td></tr>' +
1383 [ '1', 'foo', 'bar', 'baz' ],
1384 [ '2', 'foobar', 'baz' ]
1389 'Rowspan exploding with colspanned cells (2)',
1390 '<table class="sortable">' +
1391 '<thead><tr><th>n</th><th>foo</th><th>bar</th><th>baz</th><th id="sortme">n2</th></tr></thead>' +
1393 '<tr><td>1</td><td>foo</td><td>bar</td><td rowspan="2">baz</td><td>2</td></tr>' +
1394 '<tr><td>2</td><td colspan="2">foobar</td><td>1</td></tr>' +
1397 [ '2', 'foobar', 'baz', '1' ],
1398 [ '1', 'foo', 'bar', 'baz', '2' ]
1403 'Rowspan exploding with rightmost rows spanning most',
1404 '<table class="sortable">' +
1405 '<thead><tr><th id="sortme">n</th><th>foo</th><th>bar</th></tr></thead>' +
1407 '<tr><td>1</td><td rowspan="2">foo</td><td rowspan="4">bar</td></tr>' +
1408 '<tr><td>2</td></tr>' +
1409 '<tr><td>3</td><td rowspan="2">foo</td></tr>' +
1410 '<tr><td>4</td></tr>' +
1413 [ '1', 'foo', 'bar' ],
1414 [ '2', 'foo', 'bar' ],
1415 [ '3', 'foo', 'bar' ],
1416 [ '4', 'foo', 'bar' ]
1421 'Rowspan exploding with rightmost rows spanning most (2)',
1422 '<table class="sortable">' +
1423 '<thead><tr><th id="sortme">n</th><th>foo</th><th>bar</th><th>baz</th></tr></thead>' +
1425 '<tr><td>1</td><td rowspan="2">foo</td><td rowspan="4">bar</td><td>baz</td></tr>' +
1426 '<tr><td>2</td><td>baz</td></tr>' +
1427 '<tr><td>3</td><td rowspan="2">foo</td><td>baz</td></tr>' +
1428 '<tr><td>4</td><td>baz</td></tr>' +
1431 [ '1', 'foo', 'bar', 'baz' ],
1432 [ '2', 'foo', 'bar', 'baz' ],
1433 [ '3', 'foo', 'bar', 'baz' ],
1434 [ '4', 'foo', 'bar', 'baz' ]
1439 'Rowspan exploding with row-and-colspanned cells',
1440 '<table class="sortable">' +
1441 '<thead><tr><th id="sortme">n</th><th>foo1</th><th>foo2</th><th>bar</th><th>baz</th></tr></thead>' +
1443 '<tr><td>1</td><td rowspan="2">foo1</td><td rowspan="2">foo2</td><td rowspan="4">bar</td><td>baz</td></tr>' +
1444 '<tr><td>2</td><td>baz</td></tr>' +
1445 '<tr><td>3</td><td colspan="2" rowspan="2">foo</td><td>baz</td></tr>' +
1446 '<tr><td>4</td><td>baz</td></tr>' +
1449 [ '1', 'foo1', 'foo2', 'bar', 'baz' ],
1450 [ '2', 'foo1', 'foo2', 'bar', 'baz' ],
1451 [ '3', 'foo', 'bar', 'baz' ],
1452 [ '4', 'foo', 'bar', 'baz' ]
1457 'Rowspan exploding with uneven rowspan layout',
1458 '<table class="sortable">' +
1459 '<thead><tr><th id="sortme">n</th><th>foo1</th><th>foo2</th><th>foo3</th><th>bar</th><th>baz</th></tr></thead>' +
1461 '<tr><td>1</td><td rowspan="2">foo1</td><td rowspan="2">foo2</td><td rowspan="2">foo3</td><td>bar</td><td>baz</td></tr>' +
1462 '<tr><td>2</td><td rowspan="3">bar</td><td>baz</td></tr>' +
1463 '<tr><td>3</td><td rowspan="2">foo1</td><td rowspan="2">foo2</td><td rowspan="2">foo3</td><td>baz</td></tr>' +
1464 '<tr><td>4</td><td>baz</td></tr>' +
1467 [ '1', 'foo1', 'foo2', 'foo3', 'bar', 'baz' ],
1468 [ '2', 'foo1', 'foo2', 'foo3', 'bar', 'baz' ],
1469 [ '3', 'foo1', 'foo2', 'foo3', 'bar', 'baz' ],
1470 [ '4', 'foo1', 'foo2', 'foo3', 'bar', 'baz' ]
1474 QUnit
.test( 'T105731 - incomplete rows in table body', function ( assert
) {
1475 var $table
, parsers
;
1477 '<table class="sortable">' +
1478 '<tr><th>A</th><th>B</th></tr>' +
1479 '<tr><td>3</td></tr>' +
1480 '<tr><td>1</td><td>2</td></tr>' +
1483 $table
.tablesorter();
1484 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1485 // now the first row have 2 columns
1486 $table
.find( '.headerSort' ).eq( 1 ).trigger( 'click' );
1488 parsers
= $table
.data( 'tablesorter' ).config
.parsers
;
1493 'detectParserForColumn() detect 2 parsers'
1499 'detectParserForColumn() detect parser.id "number" for second column'
1503 parsers
[ 1 ].format( $table
.find( 'tbody > tr > td' ).eq( 1 ).text() ),
1505 'empty cell is sorted as number -Infinity'
1509 QUnit
.test( 'bug T114721 - use of expand-child class', function ( assert
) {
1510 var $table
, parsers
;
1512 '<table class="sortable">' +
1513 '<tr><th>A</th><th>B</th></tr>' +
1514 '<tr><td>b</td><td>4</td></tr>' +
1515 '<tr class="expand-child"><td colspan="2">some text follow b</td></tr>' +
1516 '<tr><td>a</td><td>2</td></tr>' +
1517 '<tr class="expand-child"><td colspan="2">some text follow a</td></tr>' +
1518 '<tr class="expand-child"><td colspan="2">more text</td></tr>' +
1521 $table
.tablesorter();
1522 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1525 tableExtract( $table
),
1528 [ 'some text follow a' ],
1531 [ 'some text follow b' ]
1533 'row with expand-child class follow above row'
1536 parsers
= $table
.data( 'tablesorter' ).config
.parsers
;
1540 'detectParserForColumn() detect parser.id "number" for second column'
1543 QUnit
.test( 'T29745 - References ignored in sortkey', function ( assert
) {
1544 var $table
, parsers
;
1546 '<table class="sortable">' +
1547 '<tr><th>A</th></tr>' +
1548 '<tr><td>10</td></tr>' +
1549 '<tr><td>2<sup class="reference"><a href="#cite_note-1">[1]</a></sup></td></tr>' +
1552 $table
.tablesorter();
1553 $table
.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1556 tableExtract( $table
),
1561 'References ignored in sortkey'
1564 parsers
= $table
.data( 'tablesorter' ).config
.parsers
;
1568 'detectParserForColumn() detect parser.id "number"'