4 wgMonthNames
: ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
5 wgMonthNamesShort
: ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
6 wgDefaultDateFormat
: 'dmy',
7 wgContentLanguage
: 'en'
10 QUnit
.module( 'jquery.tablesorter', QUnit
.newMwEnvironment({ config
: config
}) );
13 * Create an HTML table from an array of row arrays containing text strings.
14 * First row will be header row. No fancy rowspan/colspan stuff.
16 * @param {String[]} header
17 * @param {String[][]} data
20 function tableCreate( header
, data
) {
22 $table
= $( '<table class="sortable"><thead></thead><tbody></tbody></table>' ),
23 $thead
= $table
.find( 'thead' ),
24 $tbody
= $table
.find( 'tbody' ),
27 $.each( header
, function ( i
, str
) {
28 var $th
= $( '<th>' );
29 $th
.text( str
).appendTo( $tr
);
31 $tr
.appendTo( $thead
);
33 for ( i
= 0; i
< data
.length
; i
++ ) {
35 $.each( data
[i
], function ( j
, str
) {
36 var $td
= $( '<td>' );
37 $td
.text( str
).appendTo( $tr
);
39 $tr
.appendTo( $tbody
);
45 * Extract text from table.
47 * @param {jQuery} $table
50 function tableExtract( $table
) {
53 $table
.find( 'tbody' ).find( 'tr' ).each( function( i
, tr
) {
55 $( tr
).find( 'td,th' ).each( function( i
, td
) {
56 row
.push( $( td
).text() );
64 * Run a table test by building a table with the given data,
65 * running some callback on it, then checking the results.
67 * @param {String} msg text to pass on to qunit for the comparison
68 * @param {String[]} header cols to make the table
69 * @param {String[][]} data rows/cols to make the table
70 * @param {String[][]} expected rows/cols to compare against at end
71 * @param {function($table)} callback something to do with the table before we compare
73 function tableTest( msg
, header
, data
, expected
, callback
) {
74 QUnit
.test( msg
, 1, function ( assert
) {
75 var $table
= tableCreate( header
, data
);
77 // Give caller a chance to set up sorting and manipulate the table.
80 // Table sorting is done synchronously; if it ever needs to change back
81 // to asynchronous, we'll need a timeout or a callback here.
82 var extracted
= tableExtract( $table
);
83 assert
.deepEqual( extracted
, expected
, msg
);
87 function reversed(arr
) {
89 var arr2
= arr
.slice(0);
96 // Sample data set using planets named and their radius
97 var header
= [ 'Planet' , 'Radius (km)'],
98 mercury
= [ 'Mercury', '2439.7' ],
99 venus
= [ 'Venus' , '6051.8' ],
100 earth
= [ 'Earth' , '6371.0' ],
101 mars
= [ 'Mars' , '3390.0' ],
102 jupiter
= [ 'Jupiter', '69911' ],
103 saturn
= [ 'Saturn' , '58232' ];
106 var planets
= [mercury
, venus
, earth
, mars
, jupiter
, saturn
];
107 var ascendingName
= [earth
, jupiter
, mars
, mercury
, saturn
, venus
];
108 var ascendingRadius
= [mercury
, mars
, venus
, earth
, saturn
, jupiter
];
111 'Basic planet table: sorting initially - ascending by name',
115 function ( $table
) {
116 $table
.tablesorter( { sortList
: [ { 0: 'asc' } ] } );
120 'Basic planet table: sorting initially - descending by radius',
123 reversed(ascendingRadius
),
124 function ( $table
) {
125 $table
.tablesorter( { sortList
: [ { 1: 'desc' } ] } );
129 'Basic planet table: ascending by name',
133 function ( $table
) {
134 $table
.tablesorter();
135 $table
.find( '.headerSort:eq(0)' ).click();
139 'Basic planet table: ascending by name a second time',
143 function ( $table
) {
144 $table
.tablesorter();
145 $table
.find( '.headerSort:eq(0)' ).click();
149 'Basic planet table: descending by name',
152 reversed(ascendingName
),
153 function ( $table
) {
154 $table
.tablesorter();
155 $table
.find( '.headerSort:eq(0)' ).click().click();
159 'Basic planet table: ascending radius',
163 function ( $table
) {
164 $table
.tablesorter();
165 $table
.find( '.headerSort:eq(1)' ).click();
169 'Basic planet table: descending radius',
172 reversed(ascendingRadius
),
173 function ( $table
) {
174 $table
.tablesorter();
175 $table
.find( '.headerSort:eq(1)' ).click().click();
179 // Sample data set to test multiple column sorting
180 var header
= [ 'column1' , 'column2'],
187 var initial
= [a2
, b3
, a1
, a3
, b2
, b1
];
188 var asc
= [a1
, a2
, a3
, b1
, b2
, b3
];
189 var descasc
= [b1
, b2
, b3
, a1
, a2
, a3
];
192 'Sorting multiple columns by passing sort list',
196 function ( $table
) {
198 { sortList
: [ { 0: 'asc' }, { 1: 'asc' } ] }
203 'Sorting multiple columns by programmatically triggering sort()',
207 function ( $table
) {
208 $table
.tablesorter();
209 $table
.data( 'tablesorter' ).sort(
210 [ { 0: 'desc' }, { 1: 'asc' } ]
215 'Reset to initial sorting by triggering sort() without any parameters',
219 function ( $table
) {
221 { sortList
: [ { 0: 'asc' }, { 1: 'asc' } ] }
223 $table
.data( 'tablesorter' ).sort(
224 [ { 0: 'desc' }, { 1: 'asc' } ]
226 $table
.data( 'tablesorter' ).sort();
229 QUnit
.test( 'Reset sorting making table appear unsorted', 3, function ( assert
) {
230 var $table
= tableCreate( header
, initial
);
232 { sortList
: [ { 0: 'desc' }, { 1: 'asc' } ] }
234 $table
.data( 'tablesorter' ).sort( [] );
237 $table
.find( 'th.headerSortUp' ).length
+ $table
.find( 'th.headerSortDown' ).length
,
239 'No sort specific sort classes addign to header cells'
243 $table
.find( 'th' ).first().attr( 'title' ),
244 mw
.msg( 'sort-ascending' ),
245 'First header cell has default title'
249 $table
.find( 'th' ).first().attr( 'title' ),
250 $table
.find( 'th' ).last().attr( 'title' ),
251 'Both header cells\' titles match'
257 'Bug 28775: German-style (dmy) short numeric dates',
259 [ // German-style dates are day-month-year
266 [ // Sorted by ascending date
273 function ( $table
) {
274 mw
.config
.set( 'wgDefaultDateFormat', 'dmy' );
275 mw
.config
.set( 'wgContentLanguage', 'de' );
277 $table
.tablesorter();
278 $table
.find( '.headerSort:eq(0)' ).click();
283 'Bug 28775: American-style (mdy) short numeric dates',
285 [ // American-style dates are month-day-year
292 [ // Sorted by ascending date
299 function ( $table
) {
300 mw
.config
.set( 'wgDefaultDateFormat', 'mdy' );
302 $table
.tablesorter();
303 $table
.find( '.headerSort:eq(0)' ).click();
308 // Some randomly generated fake IPs
319 // Sort order should go octet by octet
331 'Bug 17141: IPv4 address sorting',
335 function ( $table
) {
336 $table
.tablesorter();
337 $table
.find( '.headerSort:eq(0)' ).click();
341 'Bug 17141: IPv4 address sorting (reverse)',
344 reversed(ipv4Sorted
),
345 function ( $table
) {
346 $table
.tablesorter();
347 $table
.find( '.headerSort:eq(0)' ).click().click();
352 // Some words with Umlauts
363 var umlautWordsSorted
= [
364 // Some words with Umlauts
376 'Accented Characters with custom collation',
380 function ( $table
) {
381 mw
.config
.set( 'tableSorterCollation', {
388 $table
.tablesorter();
389 $table
.find( '.headerSort:eq(0)' ).click();
393 var planetsRowspan
= [["Earth","6051.8"], jupiter
, ["Mars","6051.8"], mercury
, saturn
, venus
];
394 var planetsRowspanII
= [jupiter
, mercury
, saturn
, venus
, ['Venus', '6371.0'], ['Venus', '3390.0']];
397 'Basic planet table: same value for multiple rows via rowspan',
401 function ( $table
) {
402 // Modify the table to have a multiple-row-spanning cell:
403 // - Remove 2nd cell of 4th row, and, 2nd cell or 5th row.
404 $table
.find( 'tr:eq(3) td:eq(1), tr:eq(4) td:eq(1)' ).remove();
405 // - Set rowspan for 2nd cell of 3rd row to 3.
406 // This covers the removed cell in the 4th and 5th row.
407 $table
.find( 'tr:eq(2) td:eq(1)' ).prop( 'rowspan', '3' );
409 $table
.tablesorter();
410 $table
.find( '.headerSort:eq(0)' ).click();
414 'Basic planet table: same value for multiple rows via rowspan (sorting initially)',
418 function ( $table
) {
419 // Modify the table to have a multiple-row-spanning cell:
420 // - Remove 2nd cell of 4th row, and, 2nd cell or 5th row.
421 $table
.find( 'tr:eq(3) td:eq(1), tr:eq(4) td:eq(1)' ).remove();
422 // - Set rowspan for 2nd cell of 3rd row to 3.
423 // This covers the removed cell in the 4th and 5th row.
424 $table
.find( 'tr:eq(2) td:eq(1)' ).prop( 'rowspan', '3' );
426 $table
.tablesorter( { sortList
: [ { 0: 'asc' } ] } );
430 'Basic planet table: Same value for multiple rows via rowspan II',
434 function ( $table
) {
435 // Modify the table to have a multiple-row-spanning cell:
436 // - Remove 1st cell of 4th row, and, 1st cell or 5th row.
437 $table
.find( 'tr:eq(3) td:eq(0), tr:eq(4) td:eq(0)' ).remove();
438 // - Set rowspan for 1st cell of 3rd row to 3.
439 // This covers the removed cell in the 4th and 5th row.
440 $table
.find( 'tr:eq(2) td:eq(0)' ).prop( 'rowspan', '3' );
442 $table
.tablesorter();
443 $table
.find( '.headerSort:eq(0)' ).click();
447 var complexMDYDates
= [
448 // Some words with Umlauts
449 ['January, 19 2010'],
456 var complexMDYSorted
= [
460 ['January, 19 2010'],
465 'Complex date parsing I',
469 function ( $table
) {
470 mw
.config
.set( 'wgDefaultDateFormat', 'mdy' );
472 $table
.tablesorter();
473 $table
.find( '.headerSort:eq(0)' ).click();
477 var currencyUnsorted
= [
487 var currencySorted
= [
494 // Comma's sort after dots
495 // Not intentional but test to detect changes
500 'Currency parsing I',
504 function ( $table
) {
505 $table
.tablesorter();
506 $table
.find( '.headerSort:eq(0)' ).click();
510 var ascendingNameLegacy
= ascendingName
.slice(0);
511 ascendingNameLegacy
[4] = ascendingNameLegacy
[5];
512 ascendingNameLegacy
.pop();
515 'Legacy compat with .sortbottom',
520 $table
.find( 'tr:last' ).addClass( 'sortbottom' );
521 $table
.tablesorter();
522 $table
.find( '.headerSort:eq(0)' ).click();
526 QUnit
.test( 'Test detection routine', function ( assert
) {
529 '<table class="sortable">' +
530 '<caption>CAPTION</caption>' +
531 '<tr><th>THEAD</th></tr>' +
532 '<tr><td>1</td></tr>' +
533 '<tr class="sortbottom"><td>text</td></tr>' +
536 $table
.tablesorter();
539 $table
.data( 'tablesorter' ).config
.parsers
[0].id
,
541 'Correctly detected column content skipping sortbottom'
545 /** FIXME: the diff output is not very readeable. */
546 QUnit
.test( 'bug 32047 - caption must be before thead', function ( assert
) {
549 '<table class="sortable">' +
550 '<caption>CAPTION</caption>' +
551 '<tr><th>THEAD</th></tr>' +
552 '<tr><td>A</td></tr>' +
553 '<tr><td>B</td></tr>' +
554 '<tr class="sortbottom"><td>TFOOT</td></tr>' +
557 $table
.tablesorter();
560 $table
.children( ).get( 0 ).nodeName
,
562 'First element after <thead> must be <caption> (bug 32047)'
566 QUnit
.test( 'data-sort-value attribute, when available, should override sorting position', function ( assert
) {
569 // Example 1: All cells except one cell without data-sort-value,
570 // which should be sorted at it's text content value.
572 '<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
574 '<tr><td>Cheetah</td></tr>' +
575 '<tr><td data-sort-value="Apple">Bird</td></tr>' +
576 '<tr><td data-sort-value="Bananna">Ferret</td></tr>' +
577 '<tr><td data-sort-value="Drupe">Elephant</td></tr>' +
578 '<tr><td data-sort-value="Cherry">Dolphin</td></tr>' +
581 $table
.tablesorter().find( '.headerSort:eq(0)' ).click();
584 $table
.find( 'tbody > tr' ).each( function( i
, tr
) {
585 $( tr
).find( 'td' ).each( function( i
, td
) {
587 data
: $( td
).data( 'sortValue' ),
593 assert
.deepEqual( data
, [
610 ], 'Order matches expected order (based on data-sort-value attribute values)' );
614 '<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
616 '<tr><td>D</td></tr>' +
617 '<tr><td data-sort-value="E">A</td></tr>' +
618 '<tr><td>B</td></tr>' +
619 '<tr><td>G</td></tr>' +
620 '<tr><td data-sort-value="F">C</td></tr>' +
623 $table
.tablesorter().find( '.headerSort:eq(0)' ).click();
626 $table
.find( 'tbody > tr' ).each( function ( i
, tr
) {
627 $( tr
).find( 'td' ).each( function ( i
, td
) {
629 data
: $( td
).data( 'sortValue' ),
635 assert
.deepEqual( data
, [
652 ], 'Order matches expected order (based on data-sort-value attribute values)' );
654 // Example 3: Test that live changes are used from data-sort-value,
655 // even if they change after the tablesorter is constructed (bug 38152).
657 '<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
659 '<tr><td>D</td></tr>' +
660 '<tr><td data-sort-value="1">A</td></tr>' +
661 '<tr><td>B</td></tr>' +
662 '<tr><td data-sort-value="2">G</td></tr>' +
663 '<tr><td>C</td></tr>' +
666 // initialize table sorter and sort once
669 .find( '.headerSort:eq(0)' ).click();
671 // Change the sortValue data properties (bug 38152)
673 $table
.find( 'td:contains(A)' ).data( 'sortValue', 3 );
675 $table
.find( 'td:contains(B)' ).data( 'sortValue', 1 );
676 // - remove data, bring back attribute: 2
677 $table
.find( 'td:contains(G)' ).removeData( 'sortValue' );
679 // Now sort again (twice, so it is back at Ascending)
680 $table
.find( '.headerSort:eq(0)' ).click();
681 $table
.find( '.headerSort:eq(0)' ).click();
684 $table
.find( 'tbody > tr' ).each( function( i
, tr
) {
685 $( tr
).find( 'td' ).each( function( i
, td
) {
687 data
: $( td
).data( 'sortValue' ),
693 assert
.deepEqual( data
, [
710 ], 'Order matches expected order, using the current sortValue in $.data()' );
731 tableTest( 'bug 8115: sort numbers with commas (ascending)',
732 ['Numbers'], numbers
, numbersAsc
,
734 $table
.tablesorter();
735 $table
.find( '.headerSort:eq(0)' ).click();
739 tableTest( 'bug 8115: sort numbers with commas (descending)',
740 ['Numbers'], numbers
, reversed(numbersAsc
),
742 $table
.tablesorter();
743 $table
.find( '.headerSort:eq(0)' ).click().click();
746 // TODO add numbers sorting tests for bug 8115 with a different language
748 QUnit
.test( 'bug 32888 - Tables inside a tableheader cell', 2, function ( assert
) {
751 '<table class="sortable" id="mw-bug-32888">' +
752 '<tr><th>header<table id="mw-bug-32888-2">'+
753 '<tr><th>1</th><th>2</th></tr>' +
754 '</table></th></tr>' +
755 '<tr><td>A</td></tr>' +
756 '<tr><td>B</td></tr>' +
759 $table
.tablesorter();
762 $table
.find('> thead:eq(0) > tr > th.headerSort').length
,
764 'Child tables inside a headercell should not interfere with sortable headers (bug 32888)'
767 $( '#mw-bug-32888-2' ).find('th.headerSort').length
,
769 'The headers of child tables inside a headercell should not be sortable themselves (bug 32888)'
774 var correctDateSorting1
= [
776 ['05 February 2010'],
780 var correctDateSortingSorted1
= [
787 'Correct date sorting I',
790 correctDateSortingSorted1
,
791 function ( $table
) {
792 mw
.config
.set( 'wgDefaultDateFormat', 'mdy' );
794 $table
.tablesorter();
795 $table
.find( '.headerSort:eq(0)' ).click();
799 var correctDateSorting2
= [
801 ['February 05 2010'],
805 var correctDateSortingSorted2
= [
812 'Correct date sorting II',
815 correctDateSortingSorted2
,
816 function ( $table
) {
817 mw
.config
.set( 'wgDefaultDateFormat', 'dmy' );
819 $table
.tablesorter();
820 $table
.find( '.headerSort:eq(0)' ).click();
824 }( jQuery
, mediaWiki
) );