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 QUnit
.test( 'Rowspan not exploded on init', 1, function ( assert
) {
397 var $table
= tableCreate( header
, planets
);
399 // Modify the table to have a multiple-row-spanning cell:
400 // - Remove 2nd cell of 4th row, and, 2nd cell or 5th row.
401 $table
.find( 'tr:eq(3) td:eq(1), tr:eq(4) td:eq(1)' ).remove();
402 // - Set rowspan for 2nd cell of 3rd row to 3.
403 // This covers the removed cell in the 4th and 5th row.
404 $table
.find( 'tr:eq(2) td:eq(1)' ).prop( 'rowspan', '3' );
406 $table
.tablesorter();
409 $table
.find( 'tr:eq(2) td:eq(1)' ).prop( 'rowspan' ),
411 'Rowspan not exploded'
415 var planetsRowspan
= [ [ 'Earth', '6051.8' ], jupiter
, [ 'Mars', '6051.8' ], mercury
, saturn
, venus
];
416 var planetsRowspanII
= [ jupiter
, mercury
, saturn
, venus
, [ 'Venus', '6371.0' ], [ 'Venus', '3390.0' ] ];
419 'Basic planet table: same value for multiple rows via rowspan',
423 function ( $table
) {
424 // Modify the table to have a multiple-row-spanning cell:
425 // - Remove 2nd cell of 4th row, and, 2nd cell or 5th row.
426 $table
.find( 'tr:eq(3) td:eq(1), tr:eq(4) td:eq(1)' ).remove();
427 // - Set rowspan for 2nd cell of 3rd row to 3.
428 // This covers the removed cell in the 4th and 5th row.
429 $table
.find( 'tr:eq(2) td:eq(1)' ).prop( 'rowspan', '3' );
431 $table
.tablesorter();
432 $table
.find( '.headerSort:eq(0)' ).click();
436 'Basic planet table: same value for multiple rows via rowspan (sorting initially)',
440 function ( $table
) {
441 // Modify the table to have a multiple-row-spanning cell:
442 // - Remove 2nd cell of 4th row, and, 2nd cell or 5th row.
443 $table
.find( 'tr:eq(3) td:eq(1), tr:eq(4) td:eq(1)' ).remove();
444 // - Set rowspan for 2nd cell of 3rd row to 3.
445 // This covers the removed cell in the 4th and 5th row.
446 $table
.find( 'tr:eq(2) td:eq(1)' ).prop( 'rowspan', '3' );
448 $table
.tablesorter( { sortList
: [ { 0: 'asc' } ] } );
452 'Basic planet table: Same value for multiple rows via rowspan II',
456 function ( $table
) {
457 // Modify the table to have a multiple-row-spanning cell:
458 // - Remove 1st cell of 4th row, and, 1st cell or 5th row.
459 $table
.find( 'tr:eq(3) td:eq(0), tr:eq(4) td:eq(0)' ).remove();
460 // - Set rowspan for 1st cell of 3rd row to 3.
461 // This covers the removed cell in the 4th and 5th row.
462 $table
.find( 'tr:eq(2) td:eq(0)' ).prop( 'rowspan', '3' );
464 $table
.tablesorter();
465 $table
.find( '.headerSort:eq(0)' ).click();
469 var complexMDYDates
= [
470 // Some words with Umlauts
471 ['January, 19 2010'],
478 var complexMDYSorted
= [
482 ['January, 19 2010'],
487 'Complex date parsing I',
491 function ( $table
) {
492 mw
.config
.set( 'wgDefaultDateFormat', 'mdy' );
494 $table
.tablesorter();
495 $table
.find( '.headerSort:eq(0)' ).click();
499 var currencyUnsorted
= [
509 var currencySorted
= [
516 // Comma's sort after dots
517 // Not intentional but test to detect changes
522 'Currency parsing I',
526 function ( $table
) {
527 $table
.tablesorter();
528 $table
.find( '.headerSort:eq(0)' ).click();
532 var ascendingNameLegacy
= ascendingName
.slice(0);
533 ascendingNameLegacy
[4] = ascendingNameLegacy
[5];
534 ascendingNameLegacy
.pop();
537 'Legacy compat with .sortbottom',
542 $table
.find( 'tr:last' ).addClass( 'sortbottom' );
543 $table
.tablesorter();
544 $table
.find( '.headerSort:eq(0)' ).click();
548 QUnit
.test( 'Test detection routine', function ( assert
) {
551 '<table class="sortable">' +
552 '<caption>CAPTION</caption>' +
553 '<tr><th>THEAD</th></tr>' +
554 '<tr><td>1</td></tr>' +
555 '<tr class="sortbottom"><td>text</td></tr>' +
558 $table
.tablesorter();
559 $table
.find( '.headerSort:eq(0)' ).click();
562 $table
.data( 'tablesorter' ).config
.parsers
[0].id
,
564 'Correctly detected column content skipping sortbottom'
568 /** FIXME: the diff output is not very readeable. */
569 QUnit
.test( 'bug 32047 - caption must be before thead', function ( assert
) {
572 '<table class="sortable">' +
573 '<caption>CAPTION</caption>' +
574 '<tr><th>THEAD</th></tr>' +
575 '<tr><td>A</td></tr>' +
576 '<tr><td>B</td></tr>' +
577 '<tr class="sortbottom"><td>TFOOT</td></tr>' +
580 $table
.tablesorter();
583 $table
.children( ).get( 0 ).nodeName
,
585 'First element after <thead> must be <caption> (bug 32047)'
589 QUnit
.test( 'data-sort-value attribute, when available, should override sorting position', function ( assert
) {
592 // Example 1: All cells except one cell without data-sort-value,
593 // which should be sorted at it's text content value.
595 '<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
597 '<tr><td>Cheetah</td></tr>' +
598 '<tr><td data-sort-value="Apple">Bird</td></tr>' +
599 '<tr><td data-sort-value="Bananna">Ferret</td></tr>' +
600 '<tr><td data-sort-value="Drupe">Elephant</td></tr>' +
601 '<tr><td data-sort-value="Cherry">Dolphin</td></tr>' +
604 $table
.tablesorter().find( '.headerSort:eq(0)' ).click();
607 $table
.find( 'tbody > tr' ).each( function( i
, tr
) {
608 $( tr
).find( 'td' ).each( function( i
, td
) {
610 data
: $( td
).data( 'sortValue' ),
616 assert
.deepEqual( data
, [
633 ], 'Order matches expected order (based on data-sort-value attribute values)' );
637 '<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
639 '<tr><td>D</td></tr>' +
640 '<tr><td data-sort-value="E">A</td></tr>' +
641 '<tr><td>B</td></tr>' +
642 '<tr><td>G</td></tr>' +
643 '<tr><td data-sort-value="F">C</td></tr>' +
646 $table
.tablesorter().find( '.headerSort:eq(0)' ).click();
649 $table
.find( 'tbody > tr' ).each( function ( i
, tr
) {
650 $( tr
).find( 'td' ).each( function ( i
, td
) {
652 data
: $( td
).data( 'sortValue' ),
658 assert
.deepEqual( data
, [
675 ], 'Order matches expected order (based on data-sort-value attribute values)' );
677 // Example 3: Test that live changes are used from data-sort-value,
678 // even if they change after the tablesorter is constructed (bug 38152).
680 '<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
682 '<tr><td>D</td></tr>' +
683 '<tr><td data-sort-value="1">A</td></tr>' +
684 '<tr><td>B</td></tr>' +
685 '<tr><td data-sort-value="2">G</td></tr>' +
686 '<tr><td>C</td></tr>' +
689 // initialize table sorter and sort once
692 .find( '.headerSort:eq(0)' ).click();
694 // Change the sortValue data properties (bug 38152)
696 $table
.find( 'td:contains(A)' ).data( 'sortValue', 3 );
698 $table
.find( 'td:contains(B)' ).data( 'sortValue', 1 );
699 // - remove data, bring back attribute: 2
700 $table
.find( 'td:contains(G)' ).removeData( 'sortValue' );
702 // Now sort again (twice, so it is back at Ascending)
703 $table
.find( '.headerSort:eq(0)' ).click();
704 $table
.find( '.headerSort:eq(0)' ).click();
707 $table
.find( 'tbody > tr' ).each( function( i
, tr
) {
708 $( tr
).find( 'td' ).each( function( i
, td
) {
710 data
: $( td
).data( 'sortValue' ),
716 assert
.deepEqual( data
, [
733 ], 'Order matches expected order, using the current sortValue in $.data()' );
754 tableTest( 'bug 8115: sort numbers with commas (ascending)',
755 ['Numbers'], numbers
, numbersAsc
,
757 $table
.tablesorter();
758 $table
.find( '.headerSort:eq(0)' ).click();
762 tableTest( 'bug 8115: sort numbers with commas (descending)',
763 ['Numbers'], numbers
, reversed(numbersAsc
),
765 $table
.tablesorter();
766 $table
.find( '.headerSort:eq(0)' ).click().click();
769 // TODO add numbers sorting tests for bug 8115 with a different language
771 QUnit
.test( 'bug 32888 - Tables inside a tableheader cell', 2, function ( assert
) {
774 '<table class="sortable" id="mw-bug-32888">' +
775 '<tr><th>header<table id="mw-bug-32888-2">'+
776 '<tr><th>1</th><th>2</th></tr>' +
777 '</table></th></tr>' +
778 '<tr><td>A</td></tr>' +
779 '<tr><td>B</td></tr>' +
782 $table
.tablesorter();
785 $table
.find('> thead:eq(0) > tr > th.headerSort').length
,
787 'Child tables inside a headercell should not interfere with sortable headers (bug 32888)'
790 $( '#mw-bug-32888-2' ).find('th.headerSort').length
,
792 'The headers of child tables inside a headercell should not be sortable themselves (bug 32888)'
797 var correctDateSorting1
= [
799 ['05 February 2010'],
803 var correctDateSortingSorted1
= [
810 'Correct date sorting I',
813 correctDateSortingSorted1
,
814 function ( $table
) {
815 mw
.config
.set( 'wgDefaultDateFormat', 'mdy' );
817 $table
.tablesorter();
818 $table
.find( '.headerSort:eq(0)' ).click();
822 var correctDateSorting2
= [
824 ['February 05 2010'],
828 var correctDateSortingSorted2
= [
835 'Correct date sorting II',
838 correctDateSortingSorted2
,
839 function ( $table
) {
840 mw
.config
.set( 'wgDefaultDateFormat', 'dmy' );
842 $table
.tablesorter();
843 $table
.find( '.headerSort:eq(0)' ).click();
847 }( jQuery
, mediaWiki
) );