2 /*jshint onevar: false */
5 wgMonthNames
: ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
6 wgMonthNamesShort
: ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
7 wgDefaultDateFormat
: 'dmy',
8 wgContentLanguage
: 'en'
11 QUnit
.module( 'jquery.tablesorter', QUnit
.newMwEnvironment({ config
: config
}) );
14 * Create an HTML table from an array of row arrays containing text strings.
15 * First row will be header row. No fancy rowspan/colspan stuff.
17 * @param {String[]} header
18 * @param {String[][]} data
21 function tableCreate( header
, data
) {
23 $table
= $( '<table class="sortable"><thead></thead><tbody></tbody></table>' ),
24 $thead
= $table
.find( 'thead' ),
25 $tbody
= $table
.find( 'tbody' ),
28 $.each( header
, function ( i
, str
) {
29 var $th
= $( '<th>' );
30 $th
.text( str
).appendTo( $tr
);
32 $tr
.appendTo( $thead
);
34 for ( i
= 0; i
< data
.length
; i
++ ) {
35 /*jshint loopfunc: true */
37 $.each( data
[i
], function ( j
, str
) {
38 var $td
= $( '<td>' );
39 $td
.text( str
).appendTo( $tr
);
41 $tr
.appendTo( $tbody
);
47 * Extract text from table.
49 * @param {jQuery} $table
52 function tableExtract( $table
) {
55 $table
.find( 'tbody' ).find( 'tr' ).each( function( i
, tr
) {
57 $( tr
).find( 'td,th' ).each( function( i
, td
) {
58 row
.push( $( td
).text() );
66 * Run a table test by building a table with the given data,
67 * running some callback on it, then checking the results.
69 * @param {String} msg text to pass on to qunit for the comparison
70 * @param {String[]} header cols to make the table
71 * @param {String[][]} data rows/cols to make the table
72 * @param {String[][]} expected rows/cols to compare against at end
73 * @param {function($table)} callback something to do with the table before we compare
75 function tableTest( msg
, header
, data
, expected
, callback
) {
76 QUnit
.test( msg
, 1, function ( assert
) {
77 var $table
= tableCreate( header
, data
);
79 // Give caller a chance to set up sorting and manipulate the table.
82 // Table sorting is done synchronously; if it ever needs to change back
83 // to asynchronous, we'll need a timeout or a callback here.
84 var extracted
= tableExtract( $table
);
85 assert
.deepEqual( extracted
, expected
, msg
);
89 function reversed(arr
) {
91 var arr2
= arr
.slice(0);
98 // Sample data set using planets named and their radius
99 var header
= [ 'Planet' , 'Radius (km)'],
100 mercury
= [ 'Mercury', '2439.7' ],
101 venus
= [ 'Venus' , '6051.8' ],
102 earth
= [ 'Earth' , '6371.0' ],
103 mars
= [ 'Mars' , '3390.0' ],
104 jupiter
= [ 'Jupiter', '69911' ],
105 saturn
= [ 'Saturn' , '58232' ];
108 var planets
= [mercury
, venus
, earth
, mars
, jupiter
, saturn
];
109 var ascendingName
= [earth
, jupiter
, mars
, mercury
, saturn
, venus
];
110 var ascendingRadius
= [mercury
, mars
, venus
, earth
, saturn
, jupiter
];
113 'Basic planet table: sorting initially - ascending by name',
117 function ( $table
) {
118 $table
.tablesorter( { sortList
: [ { 0: 'asc' } ] } );
122 'Basic planet table: sorting initially - descending by radius',
125 reversed(ascendingRadius
),
126 function ( $table
) {
127 $table
.tablesorter( { sortList
: [ { 1: 'desc' } ] } );
131 'Basic planet table: ascending by name',
135 function ( $table
) {
136 $table
.tablesorter();
137 $table
.find( '.headerSort:eq(0)' ).click();
141 'Basic planet table: ascending by name a second time',
145 function ( $table
) {
146 $table
.tablesorter();
147 $table
.find( '.headerSort:eq(0)' ).click();
151 'Basic planet table: descending by name',
154 reversed(ascendingName
),
155 function ( $table
) {
156 $table
.tablesorter();
157 $table
.find( '.headerSort:eq(0)' ).click().click();
161 'Basic planet table: ascending radius',
165 function ( $table
) {
166 $table
.tablesorter();
167 $table
.find( '.headerSort:eq(1)' ).click();
171 'Basic planet table: descending radius',
174 reversed(ascendingRadius
),
175 function ( $table
) {
176 $table
.tablesorter();
177 $table
.find( '.headerSort:eq(1)' ).click().click();
181 // Sample data set to test multiple column sorting
182 header
= [ 'column1' , 'column2'];
190 var initial
= [a2
, b3
, a1
, a3
, b2
, b1
];
191 var asc
= [a1
, a2
, a3
, b1
, b2
, b3
];
192 var descasc
= [b1
, b2
, b3
, a1
, a2
, a3
];
195 'Sorting multiple columns by passing sort list',
199 function ( $table
) {
201 { sortList
: [ { 0: 'asc' }, { 1: 'asc' } ] }
206 'Sorting multiple columns by programmatically triggering sort()',
210 function ( $table
) {
211 $table
.tablesorter();
212 $table
.data( 'tablesorter' ).sort(
213 [ { 0: 'desc' }, { 1: 'asc' } ]
218 'Reset to initial sorting by triggering sort() without any parameters',
222 function ( $table
) {
224 { sortList
: [ { 0: 'asc' }, { 1: 'asc' } ] }
226 $table
.data( 'tablesorter' ).sort(
227 [ { 0: 'desc' }, { 1: 'asc' } ]
229 $table
.data( 'tablesorter' ).sort();
232 QUnit
.test( 'Reset sorting making table appear unsorted', 3, function ( assert
) {
233 var $table
= tableCreate( header
, initial
);
235 { sortList
: [ { 0: 'desc' }, { 1: 'asc' } ] }
237 $table
.data( 'tablesorter' ).sort( [] );
240 $table
.find( 'th.headerSortUp' ).length
+ $table
.find( 'th.headerSortDown' ).length
,
242 'No sort specific sort classes addign to header cells'
246 $table
.find( 'th' ).first().attr( 'title' ),
247 mw
.msg( 'sort-ascending' ),
248 'First header cell has default title'
252 $table
.find( 'th' ).first().attr( 'title' ),
253 $table
.find( 'th' ).last().attr( 'title' ),
254 'Both header cells\' titles match'
260 'Bug 28775: German-style (dmy) short numeric dates',
262 [ // German-style dates are day-month-year
269 [ // Sorted by ascending date
276 function ( $table
) {
277 mw
.config
.set( 'wgDefaultDateFormat', 'dmy' );
278 mw
.config
.set( 'wgContentLanguage', 'de' );
280 $table
.tablesorter();
281 $table
.find( '.headerSort:eq(0)' ).click();
286 'Bug 28775: American-style (mdy) short numeric dates',
288 [ // American-style dates are month-day-year
295 [ // Sorted by ascending date
302 function ( $table
) {
303 mw
.config
.set( 'wgDefaultDateFormat', 'mdy' );
305 $table
.tablesorter();
306 $table
.find( '.headerSort:eq(0)' ).click();
311 // Some randomly generated fake IPs
322 // Sort order should go octet by octet
334 'Bug 17141: IPv4 address sorting',
338 function ( $table
) {
339 $table
.tablesorter();
340 $table
.find( '.headerSort:eq(0)' ).click();
344 'Bug 17141: IPv4 address sorting (reverse)',
347 reversed(ipv4Sorted
),
348 function ( $table
) {
349 $table
.tablesorter();
350 $table
.find( '.headerSort:eq(0)' ).click().click();
355 // Some words with Umlauts
366 var umlautWordsSorted
= [
367 // Some words with Umlauts
379 'Accented Characters with custom collation',
383 function ( $table
) {
384 mw
.config
.set( 'tableSorterCollation', {
391 $table
.tablesorter();
392 $table
.find( '.headerSort:eq(0)' ).click();
396 var planetsRowspan
= [ [ 'Earth', '6051.8' ], jupiter
, [ 'Mars', '6051.8' ], mercury
, saturn
, venus
];
397 var planetsRowspanII
= [ jupiter
, mercury
, saturn
, venus
, [ 'Venus', '6371.0' ], [ 'Venus', '3390.0' ] ];
400 'Basic planet table: same value for multiple rows via rowspan',
404 function ( $table
) {
405 // Modify the table to have a multiple-row-spanning cell:
406 // - Remove 2nd cell of 4th row, and, 2nd cell or 5th row.
407 $table
.find( 'tr:eq(3) td:eq(1), tr:eq(4) td:eq(1)' ).remove();
408 // - Set rowspan for 2nd cell of 3rd row to 3.
409 // This covers the removed cell in the 4th and 5th row.
410 $table
.find( 'tr:eq(2) td:eq(1)' ).prop( 'rowspan', '3' );
412 $table
.tablesorter();
413 $table
.find( '.headerSort:eq(0)' ).click();
417 'Basic planet table: same value for multiple rows via rowspan (sorting initially)',
421 function ( $table
) {
422 // Modify the table to have a multiple-row-spanning cell:
423 // - Remove 2nd cell of 4th row, and, 2nd cell or 5th row.
424 $table
.find( 'tr:eq(3) td:eq(1), tr:eq(4) td:eq(1)' ).remove();
425 // - Set rowspan for 2nd cell of 3rd row to 3.
426 // This covers the removed cell in the 4th and 5th row.
427 $table
.find( 'tr:eq(2) td:eq(1)' ).prop( 'rowspan', '3' );
429 $table
.tablesorter( { sortList
: [ { 0: 'asc' } ] } );
433 'Basic planet table: Same value for multiple rows via rowspan II',
437 function ( $table
) {
438 // Modify the table to have a multiple-row-spanning cell:
439 // - Remove 1st cell of 4th row, and, 1st cell or 5th row.
440 $table
.find( 'tr:eq(3) td:eq(0), tr:eq(4) td:eq(0)' ).remove();
441 // - Set rowspan for 1st cell of 3rd row to 3.
442 // This covers the removed cell in the 4th and 5th row.
443 $table
.find( 'tr:eq(2) td:eq(0)' ).prop( 'rowspan', '3' );
445 $table
.tablesorter();
446 $table
.find( '.headerSort:eq(0)' ).click();
450 var complexMDYDates
= [
451 // Some words with Umlauts
452 ['January, 19 2010'],
459 var complexMDYSorted
= [
463 ['January, 19 2010'],
468 'Complex date parsing I',
472 function ( $table
) {
473 mw
.config
.set( 'wgDefaultDateFormat', 'mdy' );
475 $table
.tablesorter();
476 $table
.find( '.headerSort:eq(0)' ).click();
480 var currencyUnsorted
= [
490 var currencySorted
= [
497 // Comma's sort after dots
498 // Not intentional but test to detect changes
503 'Currency parsing I',
507 function ( $table
) {
508 $table
.tablesorter();
509 $table
.find( '.headerSort:eq(0)' ).click();
513 var ascendingNameLegacy
= ascendingName
.slice(0);
514 ascendingNameLegacy
[4] = ascendingNameLegacy
[5];
515 ascendingNameLegacy
.pop();
518 'Legacy compat with .sortbottom',
523 $table
.find( 'tr:last' ).addClass( 'sortbottom' );
524 $table
.tablesorter();
525 $table
.find( '.headerSort:eq(0)' ).click();
529 QUnit
.test( 'Test detection routine', function ( assert
) {
532 '<table class="sortable">' +
533 '<caption>CAPTION</caption>' +
534 '<tr><th>THEAD</th></tr>' +
535 '<tr><td>1</td></tr>' +
536 '<tr class="sortbottom"><td>text</td></tr>' +
539 $table
.tablesorter();
542 $table
.data( 'tablesorter' ).config
.parsers
[0].id
,
544 'Correctly detected column content skipping sortbottom'
548 /** FIXME: the diff output is not very readeable. */
549 QUnit
.test( 'bug 32047 - caption must be before thead', function ( assert
) {
552 '<table class="sortable">' +
553 '<caption>CAPTION</caption>' +
554 '<tr><th>THEAD</th></tr>' +
555 '<tr><td>A</td></tr>' +
556 '<tr><td>B</td></tr>' +
557 '<tr class="sortbottom"><td>TFOOT</td></tr>' +
560 $table
.tablesorter();
563 $table
.children( ).get( 0 ).nodeName
,
565 'First element after <thead> must be <caption> (bug 32047)'
569 QUnit
.test( 'data-sort-value attribute, when available, should override sorting position', function ( assert
) {
572 // Example 1: All cells except one cell without data-sort-value,
573 // which should be sorted at it's text content value.
575 '<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
577 '<tr><td>Cheetah</td></tr>' +
578 '<tr><td data-sort-value="Apple">Bird</td></tr>' +
579 '<tr><td data-sort-value="Bananna">Ferret</td></tr>' +
580 '<tr><td data-sort-value="Drupe">Elephant</td></tr>' +
581 '<tr><td data-sort-value="Cherry">Dolphin</td></tr>' +
584 $table
.tablesorter().find( '.headerSort:eq(0)' ).click();
587 $table
.find( 'tbody > tr' ).each( function( i
, tr
) {
588 $( tr
).find( 'td' ).each( function( i
, td
) {
590 data
: $( td
).data( 'sortValue' ),
596 assert
.deepEqual( data
, [
613 ], 'Order matches expected order (based on data-sort-value attribute values)' );
617 '<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
619 '<tr><td>D</td></tr>' +
620 '<tr><td data-sort-value="E">A</td></tr>' +
621 '<tr><td>B</td></tr>' +
622 '<tr><td>G</td></tr>' +
623 '<tr><td data-sort-value="F">C</td></tr>' +
626 $table
.tablesorter().find( '.headerSort:eq(0)' ).click();
629 $table
.find( 'tbody > tr' ).each( function ( i
, tr
) {
630 $( tr
).find( 'td' ).each( function ( i
, td
) {
632 data
: $( td
).data( 'sortValue' ),
638 assert
.deepEqual( data
, [
655 ], 'Order matches expected order (based on data-sort-value attribute values)' );
657 // Example 3: Test that live changes are used from data-sort-value,
658 // even if they change after the tablesorter is constructed (bug 38152).
660 '<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
662 '<tr><td>D</td></tr>' +
663 '<tr><td data-sort-value="1">A</td></tr>' +
664 '<tr><td>B</td></tr>' +
665 '<tr><td data-sort-value="2">G</td></tr>' +
666 '<tr><td>C</td></tr>' +
669 // initialize table sorter and sort once
672 .find( '.headerSort:eq(0)' ).click();
674 // Change the sortValue data properties (bug 38152)
676 $table
.find( 'td:contains(A)' ).data( 'sortValue', 3 );
678 $table
.find( 'td:contains(B)' ).data( 'sortValue', 1 );
679 // - remove data, bring back attribute: 2
680 $table
.find( 'td:contains(G)' ).removeData( 'sortValue' );
682 // Now sort again (twice, so it is back at Ascending)
683 $table
.find( '.headerSort:eq(0)' ).click();
684 $table
.find( '.headerSort:eq(0)' ).click();
687 $table
.find( 'tbody > tr' ).each( function( i
, tr
) {
688 $( tr
).find( 'td' ).each( function( i
, td
) {
690 data
: $( td
).data( 'sortValue' ),
696 assert
.deepEqual( data
, [
713 ], 'Order matches expected order, using the current sortValue in $.data()' );
734 tableTest( 'bug 8115: sort numbers with commas (ascending)',
735 ['Numbers'], numbers
, numbersAsc
,
737 $table
.tablesorter();
738 $table
.find( '.headerSort:eq(0)' ).click();
742 tableTest( 'bug 8115: sort numbers with commas (descending)',
743 ['Numbers'], numbers
, reversed(numbersAsc
),
745 $table
.tablesorter();
746 $table
.find( '.headerSort:eq(0)' ).click().click();
749 // TODO add numbers sorting tests for bug 8115 with a different language
751 QUnit
.test( 'bug 32888 - Tables inside a tableheader cell', 2, function ( assert
) {
754 '<table class="sortable" id="mw-bug-32888">' +
755 '<tr><th>header<table id="mw-bug-32888-2">'+
756 '<tr><th>1</th><th>2</th></tr>' +
757 '</table></th></tr>' +
758 '<tr><td>A</td></tr>' +
759 '<tr><td>B</td></tr>' +
762 $table
.tablesorter();
765 $table
.find('> thead:eq(0) > tr > th.headerSort').length
,
767 'Child tables inside a headercell should not interfere with sortable headers (bug 32888)'
770 $( '#mw-bug-32888-2' ).find('th.headerSort').length
,
772 'The headers of child tables inside a headercell should not be sortable themselves (bug 32888)'
777 var correctDateSorting1
= [
779 ['05 February 2010'],
783 var correctDateSortingSorted1
= [
790 'Correct date sorting I',
793 correctDateSortingSorted1
,
794 function ( $table
) {
795 mw
.config
.set( 'wgDefaultDateFormat', 'mdy' );
797 $table
.tablesorter();
798 $table
.find( '.headerSort:eq(0)' ).click();
802 var correctDateSorting2
= [
804 ['February 05 2010'],
808 var correctDateSortingSorted2
= [
815 'Correct date sorting II',
818 correctDateSortingSorted2
,
819 function ( $table
) {
820 mw
.config
.set( 'wgDefaultDateFormat', 'dmy' );
822 $table
.tablesorter();
823 $table
.find( '.headerSort:eq(0)' ).click();
827 }( jQuery
, mediaWiki
) );