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 module( 'jquery.tablesorter', QUnit
.newMwEnvironment( config
) );
12 test( '-- Initial check', function() {
14 ok( $.tablesorter
, '$.tablesorter defined' );
18 * Create an HTML table from an array of row arrays containing text strings.
19 * First row will be header row. No fancy rowspan/colspan stuff.
21 * @param {String[]} header
22 * @param {String[][]} data
25 var tableCreate = function( header
, data
) {
26 var $table
= $( '<table class="sortable"><thead></thead><tbody></tbody></table>' ),
27 $thead
= $table
.find( 'thead' ),
28 $tbody
= $table
.find( 'tbody' ),
31 $.each( header
, function( i
, str
) {
32 var $th
= $( '<th>' );
33 $th
.text( str
).appendTo( $tr
);
35 $tr
.appendTo( $thead
);
37 for (var i
= 0; i
< data
.length
; i
++) {
39 $.each( data
[i
], function( j
, str
) {
40 var $td
= $( '<td>' );
41 $td
.text( str
).appendTo( $tr
);
43 $tr
.appendTo( $tbody
);
49 * Extract text from table.
51 * @param {jQuery} $table
54 var tableExtract = function( $table
) {
57 $table
.find( 'tbody' ).find( 'tr' ).each( function( i
, tr
) {
59 $( tr
).find( 'td,th' ).each( function( i
, td
) {
60 row
.push( $( td
).text() );
68 * Run a table test by building a table with the given data,
69 * running some callback on it, then checking the results.
71 * @param {String} msg text to pass on to qunit for the comparison
72 * @param {String[]} header cols to make the table
73 * @param {String[][]} data rows/cols to make the table
74 * @param {String[][]} expected rows/cols to compare against at end
75 * @param {function($table)} callback something to do with the table before we compare
77 var tableTest = function( msg
, header
, data
, expected
, callback
) {
78 test( msg
, function() {
81 var $table
= tableCreate( header
, data
);
82 //$( 'body' ).append($table);
84 // Give caller a chance to set up sorting and manipulate the table.
87 // Table sorting is done synchronously; if it ever needs to change back
88 // to asynchronous, we'll need a timeout or a callback here.
89 var extracted
= tableExtract( $table
);
90 deepEqual( extracted
, expected
, msg
);
94 var reversed = function(arr
) {
95 var arr2
= arr
.slice(0);
100 // Sample data set using planets named and their radius
101 var header
= [ 'Planet' , 'Radius (km)'],
102 mercury
= [ 'Mercury', '2439.7' ],
103 venus
= [ 'Venus' , '6051.8' ],
104 earth
= [ 'Earth' , '6371.0' ],
105 mars
= [ 'Mars' , '3390.0' ],
106 jupiter
= [ 'Jupiter', '69911' ],
107 saturn
= [ 'Saturn' , '58232' ];
110 var planets
= [mercury
, venus
, earth
, mars
, jupiter
, saturn
];
111 var ascendingName
= [earth
, jupiter
, mars
, mercury
, saturn
, venus
];
112 var ascendingRadius
= [mercury
, mars
, venus
, earth
, saturn
, jupiter
];
115 'Basic planet table: ascending by name',
120 $table
.tablesorter();
121 $table
.find( '.headerSort:eq(0)' ).click();
125 'Basic planet table: ascending by name a second time',
130 $table
.tablesorter();
131 $table
.find( '.headerSort:eq(0)' ).click();
135 'Basic planet table: descending by name',
138 reversed(ascendingName
),
140 $table
.tablesorter();
141 $table
.find( '.headerSort:eq(0)' ).click().click();
145 'Basic planet table: ascending radius',
150 $table
.tablesorter();
151 $table
.find( '.headerSort:eq(1)' ).click();
155 'Basic planet table: descending radius',
158 reversed(ascendingRadius
),
160 $table
.tablesorter();
161 $table
.find( '.headerSort:eq(1)' ).click().click();
168 'Bug 28775: German-style (dmy) short numeric dates',
170 [ // German-style dates are day-month-year
177 [ // Sorted by ascending date
185 mw
.config
.set( 'wgDefaultDateFormat', 'dmy' );
186 mw
.config
.set( 'wgContentLanguage', 'de' );
188 $table
.tablesorter();
189 $table
.find( '.headerSort:eq(0)' ).click();
194 'Bug 28775: American-style (mdy) short numeric dates',
196 [ // American-style dates are month-day-year
203 [ // Sorted by ascending date
211 mw
.config
.set( 'wgDefaultDateFormat', 'mdy' );
213 $table
.tablesorter();
214 $table
.find( '.headerSort:eq(0)' ).click();
219 // Some randomly generated fake IPs
230 // Sort order should go octet by octet
242 'Bug 17141: IPv4 address sorting',
247 $table
.tablesorter();
248 $table
.find( '.headerSort:eq(0)' ).click();
252 'Bug 17141: IPv4 address sorting (reverse)',
255 reversed(ipv4Sorted
),
257 $table
.tablesorter();
258 $table
.find( '.headerSort:eq(0)' ).click().click();
263 // Some words with Umlauts
274 var umlautWordsSorted
= [
275 // Some words with Umlauts
287 'Accented Characters with custom collation',
292 mw
.config
.set( 'tableSorterCollation', {
299 $table
.tablesorter();
300 $table
.find( '.headerSort:eq(0)' ).click();
304 var planetsRowspan
= [["Earth","6051.8"], jupiter
, ["Mars","6051.8"], mercury
, saturn
, venus
];
305 var planetsRowspanII
= [jupiter
, mercury
, saturn
, ['Venus', '6371.0'], venus
, ['Venus', '3390.0']];
308 'Basic planet table: same value for multiple rows via rowspan',
313 // Modify the table to have a multiuple-row-spanning cell:
314 // - Remove 2nd cell of 4th row, and, 2nd cell or 5th row.
315 $table
.find( 'tr:eq(3) td:eq(1), tr:eq(4) td:eq(1)' ).remove();
316 // - Set rowspan for 2nd cell of 3rd row to 3.
317 // This covers the removed cell in the 4th and 5th row.
318 $table
.find( 'tr:eq(2) td:eq(1)' ).prop( 'rowspan', '3' );
320 $table
.tablesorter();
321 $table
.find( '.headerSort:eq(0)' ).click();
325 'Basic planet table: Same value for multiple rows via rowspan II',
330 // Modify the table to have a multiuple-row-spanning cell:
331 // - Remove 1st cell of 4th row, and, 1st cell or 5th row.
332 $table
.find( 'tr:eq(3) td:eq(0), tr:eq(4) td:eq(0)' ).remove();
333 // - Set rowspan for 1st cell of 3rd row to 3.
334 // This covers the removed cell in the 4th and 5th row.
335 $table
.find( 'tr:eq(2) td:eq(0)' ).prop( 'rowspan', '3' );
337 $table
.tablesorter();
338 $table
.find( '.headerSort:eq(0)' ).click();
342 var complexMDYDates
= [
343 // Some words with Umlauts
344 ['January, 19 2010'],
351 var complexMDYSorted
= [
355 ["January, 19 2010"],
360 'Complex date parsing I',
365 mw
.config
.set( 'wgDefaultDateFormat', 'mdy' );
367 $table
.tablesorter();
368 $table
.find( '.headerSort:eq(0)' ).click();
372 var ascendingNameLegacy
= ascendingName
.slice(0);
373 ascendingNameLegacy
[4] = ascendingNameLegacy
[5];
374 ascendingNameLegacy
.pop();
377 'Legacy compat with .sortbottom',
382 $table
.find( 'tr:last' ).addClass( 'sortbottom' );
383 $table
.tablesorter();
384 $table
.find( '.headerSort:eq(0)' ).click();
388 /** FIXME: the diff output is not very readeable. */
389 test( 'bug 32047 - caption must be before thead', function() {
392 '<table class="sortable">' +
393 '<caption>CAPTION</caption>' +
394 '<tr><th>THEAD</th></tr>' +
395 '<tr><td>A</td></tr>' +
396 '<tr><td>B</td></tr>' +
397 '<tr class="sortbottom"><td>TFOOT</td></tr>' +
400 $table
.tablesorter();
403 $table
.children( ).get( 0 ).nodeName
,
405 'First element after <thead> must be <caption> (bug 32047)'
409 test( 'data-sort-value attribute, when available, should override sorting position', function() {
412 // Simple example, one without data-sort-value which should be sorted at it's text.
414 '<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
416 '<tr><td>Cheetah</td></tr>' +
417 '<tr><td data-sort-value="Apple">Bird</td></tr>' +
418 '<tr><td data-sort-value="Bananna">Ferret</td></tr>' +
419 '<tr><td data-sort-value="Drupe">Elephant</td></tr>' +
420 '<tr><td data-sort-value="Cherry">Dolphin</td></tr>' +
423 $table
.tablesorter().find( '.headerSort:eq(0)' ).click();
426 $table
.find( 'tbody > tr' ).each( function( i
, tr
) {
427 $( tr
).find( 'td' ).each( function( i
, td
) {
428 data
.push( { data
: $( td
).data( 'sort-value' ), text
: $( td
).text() } );
453 '<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
455 '<tr><td>D</td></tr>' +
456 '<tr><td data-sort-value="E">A</td></tr>' +
457 '<tr><td>B</td></tr>' +
458 '<tr><td>G</td></tr>' +
459 '<tr><td data-sort-value="F">C</td></tr>' +
462 $table
.tablesorter().find( '.headerSort:eq(0)' ).click();
465 $table
.find( 'tbody > tr' ).each( function( i
, tr
) {
466 $( tr
).find( 'td' ).each( function( i
, td
) {
467 data
.push( { data
: $( td
).data( 'sort-value' ), text
: $( td
).text() } );
509 tableTest( 'bug 8115: sort numbers with commas (ascending)',
510 ['Numbers'], numbers
, numbersAsc
,
512 $table
.tablesorter();
513 $table
.find( '.headerSort:eq(0)' ).click();
517 tableTest( 'bug 8115: sort numbers with commas (descending)',
518 ['Numbers'], numbers
, reversed(numbersAsc
),
520 $table
.tablesorter();
521 $table
.find( '.headerSort:eq(0)' ).click().click();
524 // TODO add numbers sorting tests for bug 8115 with a different language
526 test( 'bug 32888 - Tables inside a tableheader cell', function() {
531 '<table class="sortable" id="32888">' +
532 '<tr><th>header<table id="32888-2">'+
533 '<tr><th>1</th><th>2</th></tr>' +
534 '</table></th></tr>' +
535 '<tr><td>A</td></tr>' +
536 '<tr><td>B</td></tr>' +
539 $table
.tablesorter();
542 $table
.find('> thead:eq(0) > tr > th.headerSort').length
,
544 'Child tables inside a headercell should not interfere with sortable headers (bug 32888)'
547 $('#32888-2').find('th.headerSort').length
,
549 'The headers of child tables inside a headercell should not be sortable themselves (bug 32888)'