2711ddfe4e5bb549312b554c7accb258f35c2722
[lhc/web/wiklou.git] / tests / qunit / suites / resources / jquery / jquery.tablesorter.test.js
1 ( function () {
2 var header = [ 'Planet', 'Radius (km)' ],
3
4 // Data set "planets"
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 ],
14 planetsRowspan,
15 planetsRowspanII,
16 planetsAscNameLegacy,
17
18 // Data set "simple"
19 a1 = [ 'A', '1' ],
20 a2 = [ 'A', '2' ],
21 a3 = [ 'A', '3' ],
22 b1 = [ 'B', '1' ],
23 b2 = [ 'B', '2' ],
24 b3 = [ 'B', '3' ],
25 simple = [ a2, b3, a1, a3, b2, b1 ],
26 simpleAsc = [ a1, a2, a3, b1, b2, b3 ],
27 simpleDescasc = [ b1, b2, b3, a1, a2, a3 ],
28
29 // Data set "colspan"
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 ],
37
38 // Data set "ipv4"
39 ipv4 = [
40 // Some randomly generated fake IPs
41 [ '45.238.27.109' ],
42 [ '44.172.9.22' ],
43 [ '247.240.82.209' ],
44 [ '204.204.132.158' ],
45 [ '170.38.91.162' ],
46 [ '197.219.164.9' ],
47 [ '45.68.154.72' ],
48 [ '182.195.149.80' ]
49 ],
50 ipv4Sorted = [
51 // Sort order should go octet by octet
52 [ '44.172.9.22' ],
53 [ '45.68.154.72' ],
54 [ '45.238.27.109' ],
55 [ '170.38.91.162' ],
56 [ '182.195.149.80' ],
57 [ '197.219.164.9' ],
58 [ '204.204.132.158' ],
59 [ '247.240.82.209' ]
60 ],
61
62 // Data set "umlaut"
63 umlautWords = [
64 [ 'Günther' ],
65 [ 'Peter' ],
66 [ 'Björn' ],
67 [ 'ä' ],
68 [ 'z' ],
69 [ 'Bjorn' ],
70 [ 'BjÖrn' ],
71 [ 'apfel' ],
72 [ 'Apfel' ],
73 [ 'Äpfel' ],
74 [ 'Strasse' ],
75 [ 'Sträßschen' ]
76 ],
77 umlautWordsSortedEn = [
78 [ 'ä' ],
79 [ 'Äpfel' ],
80 [ 'apfel' ],
81 [ 'Apfel' ],
82 [ 'Björn' ],
83 [ 'BjÖrn' ],
84 [ 'Bjorn' ],
85 [ 'Günther' ],
86 [ 'Peter' ],
87 [ 'Sträßschen' ],
88 [ 'Strasse' ],
89 [ 'z' ]
90 ],
91 umlautWordsSortedSv = [
92 [ 'apfel' ],
93 [ 'Apfel' ],
94 [ 'Bjorn' ],
95 [ 'Björn' ],
96 [ 'BjÖrn' ],
97 [ 'Günther' ],
98 [ 'Peter' ],
99 [ 'Strasse' ],
100 [ 'Sträßschen' ],
101 [ 'z' ],
102 [ 'ä' ], // ä sorts after z in Swedish
103 [ 'Äpfel' ]
104 ],
105
106 // Data set "digraph"
107 digraphWords = [
108 [ 'London' ],
109 [ 'Ljubljana' ],
110 [ 'Luxembourg' ],
111 [ 'Njivice' ],
112 [ 'Norwich' ],
113 [ 'New York' ]
114 ],
115 digraphWordsSorted = [
116 [ 'London' ],
117 [ 'Luxembourg' ],
118 [ 'Ljubljana' ],
119 [ 'New York' ],
120 [ 'Norwich' ],
121 [ 'Njivice' ]
122 ],
123
124 complexMDYDates = [
125 [ 'January, 19 2010' ],
126 [ 'April 21 1991' ],
127 [ '04 22 1991' ],
128 [ '5.12.1990' ],
129 [ 'December 12 \'10' ]
130 ],
131 complexMDYSorted = [
132 [ '5.12.1990' ],
133 [ 'April 21 1991' ],
134 [ '04 22 1991' ],
135 [ 'January, 19 2010' ],
136 [ 'December 12 \'10' ]
137 ],
138
139 currencyUnsorted = [
140 [ '1.02 $' ],
141 [ '$ 3.00' ],
142 [ '€ 2,99' ],
143 [ '$ 1.00' ],
144 [ '$3.50' ],
145 [ '$ 1.50' ],
146 [ '€ 0.99' ]
147 ],
148 currencySorted = [
149 [ '€ 0.99' ],
150 [ '$ 1.00' ],
151 [ '1.02 $' ],
152 [ '$ 1.50' ],
153 [ '$ 3.00' ],
154 [ '$3.50' ],
155 // Commas sort after dots
156 // Not intentional but test to detect changes
157 [ '€ 2,99' ]
158 ],
159
160 numbers = [
161 [ '12' ],
162 [ '7' ],
163 [ '13,000' ],
164 [ '9' ],
165 [ '14' ],
166 [ '8.0' ]
167 ],
168 numbersAsc = [
169 [ '7' ],
170 [ '8.0' ],
171 [ '9' ],
172 [ '12' ],
173 [ '14' ],
174 [ '13,000' ]
175 ],
176
177 correctDateSorting1 = [
178 [ '01 January 2010' ],
179 [ '05 February 2010' ],
180 [ '16 January 2010' ]
181 ],
182 correctDateSortingSorted1 = [
183 [ '01 January 2010' ],
184 [ '16 January 2010' ],
185 [ '05 February 2010' ]
186 ],
187
188 correctDateSorting2 = [
189 [ 'January 01 2010' ],
190 [ 'February 05 2010' ],
191 [ 'January 16 2010' ]
192 ],
193 correctDateSortingSorted2 = [
194 [ 'January 01 2010' ],
195 [ 'January 16 2010' ],
196 [ 'February 05 2010' ]
197 ],
198 isoDateSorting = [
199 [ '2010-02-01' ],
200 [ '2009-12-25T12:30:45.001Z' ],
201 [ '2010-01-31' ],
202 [ '2009' ],
203 [ '2009-12-25T12:30:45' ],
204 [ '2009-12-25T12:30:45.111' ],
205 [ '2009-12-25T12:30:45+01:00' ]
206 ],
207 isoDateSortingSorted = [
208 [ '2009' ],
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' ],
213 [ '2010-01-31' ],
214 [ '2010-02-01' ]
215 ];
216
217 QUnit.module( 'jquery.tablesorter', QUnit.newMwEnvironment( {
218 setup: function () {
219 this.liveMonths = mw.language.months;
220 mw.language.months = {
221 keys: {
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' ]
228 },
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' ]
235 };
236 },
237 teardown: function () {
238 mw.language.months = this.liveMonths;
239 },
240 config: {
241 wgDefaultDateFormat: 'dmy',
242 wgSeparatorTransformTable: [ '', '' ],
243 wgDigitTransformTable: [ '', '' ],
244 wgPageContentLanguage: 'en'
245 }
246 } ) );
247
248 /**
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.
251 *
252 * @param {string[]} header
253 * @param {string[][]} data
254 * @return {jQuery}
255 */
256 function tableCreate( header, data ) {
257 var i,
258 $table = $( '<table class="sortable"><thead></thead><tbody></tbody></table>' ),
259 $thead = $table.find( 'thead' ),
260 $tbody = $table.find( 'tbody' ),
261 $tr = $( '<tr>' );
262
263 header.forEach( function ( str ) {
264 var $th = $( '<th>' );
265 $th.text( str ).appendTo( $tr );
266 } );
267 $tr.appendTo( $thead );
268
269 for ( i = 0; i < data.length; i++ ) {
270 $tr = $( '<tr>' );
271 // eslint-disable-next-line no-loop-func
272 data[ i ].forEach( function ( str ) {
273 var $td = $( '<td>' );
274 $td.text( str ).appendTo( $tr );
275 } );
276 $tr.appendTo( $tbody );
277 }
278 return $table;
279 }
280
281 /**
282 * Extract text from table.
283 *
284 * @param {jQuery} $table
285 * @return {string[][]}
286 */
287 function tableExtract( $table ) {
288 var data = [];
289
290 $table.find( 'tbody' ).find( 'tr' ).each( function ( i, tr ) {
291 var row = [];
292 $( tr ).find( 'td,th' ).each( function ( i, td ) {
293 row.push( $( td ).text() );
294 } );
295 data.push( row );
296 } );
297 return data;
298 }
299
300 /**
301 * Run a table test by building a table with the given data,
302 * running some callback on it, then checking the results.
303 *
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
309 */
310 function tableTest( msg, header, data, expected, callback ) {
311 QUnit.test( msg, function ( assert ) {
312 var extracted,
313 $table = tableCreate( header, data );
314
315 // Give caller a chance to set up sorting and manipulate the table.
316 callback( $table );
317
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 );
322 } );
323 }
324
325 /**
326 * Run a table test by building a table with the given HTML,
327 * running some callback on it, then checking the results.
328 *
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
333 */
334 function tableTestHTML( msg, html, expected, callback ) {
335 QUnit.test( msg, function ( assert ) {
336 var extracted,
337 $table = $( html );
338
339 // Give caller a chance to set up sorting and manipulate the table.
340 if ( callback ) {
341 callback( $table );
342 } else {
343 $table.tablesorter();
344 $table.find( '#sortme' ).trigger( 'click' );
345 }
346
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 );
351 } );
352 }
353
354 function reversed( arr ) {
355 // Clone array
356 var arr2 = arr.slice( 0 );
357
358 arr2.reverse();
359
360 return arr2;
361 }
362
363 // Sample data set using planets named and their radius
364
365 tableTest(
366 'Basic planet table: sorting initially - ascending by name',
367 header,
368 planets,
369 planetsAscName,
370 function ( $table ) {
371 $table.tablesorter( { sortList: [
372 { 0: 'asc' }
373 ] } );
374 }
375 );
376 tableTest(
377 'Basic planet table: sorting initially - descending by radius',
378 header,
379 planets,
380 reversed( planetsAscRadius ),
381 function ( $table ) {
382 $table.tablesorter( { sortList: [
383 { 1: 'desc' }
384 ] } );
385 }
386 );
387 tableTest(
388 'Basic planet table: ascending by name',
389 header,
390 planets,
391 planetsAscName,
392 function ( $table ) {
393 $table.tablesorter();
394 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
395 }
396 );
397 tableTest(
398 'Basic planet table: ascending by name a second time',
399 header,
400 planets,
401 planetsAscName,
402 function ( $table ) {
403 $table.tablesorter();
404 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
405 }
406 );
407 tableTest(
408 'Basic planet table: ascending by name (multiple clicks)',
409 header,
410 planets,
411 planetsAscName,
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' );
417 }
418 );
419 tableTest(
420 'Basic planet table: descending by name',
421 header,
422 planets,
423 reversed( planetsAscName ),
424 function ( $table ) {
425 $table.tablesorter();
426 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' ).trigger( 'click' );
427 }
428 );
429 tableTest(
430 'Basic planet table: ascending radius',
431 header,
432 planets,
433 planetsAscRadius,
434 function ( $table ) {
435 $table.tablesorter();
436 $table.find( '.headerSort' ).eq( 1 ).trigger( 'click' );
437 }
438 );
439 tableTest(
440 'Basic planet table: descending radius',
441 header,
442 planets,
443 reversed( planetsAscRadius ),
444 function ( $table ) {
445 $table.tablesorter();
446 $table.find( '.headerSort' ).eq( 1 ).trigger( 'click' ).trigger( 'click' );
447 }
448 );
449 tableTest(
450 'Sorting multiple columns by passing sort list',
451 header,
452 simple,
453 simpleAsc,
454 function ( $table ) {
455 $table.tablesorter(
456 { sortList: [
457 { 0: 'asc' },
458 { 1: 'asc' }
459 ] }
460 );
461 }
462 );
463 tableTest(
464 'Sorting multiple columns by programmatically triggering sort()',
465 header,
466 simple,
467 simpleDescasc,
468 function ( $table ) {
469 $table.tablesorter();
470 $table.data( 'tablesorter' ).sort(
471 [
472 { 0: 'desc' },
473 { 1: 'asc' }
474 ]
475 );
476 }
477 );
478 tableTest(
479 'Reset to initial sorting by triggering sort() without any parameters',
480 header,
481 simple,
482 simpleAsc,
483 function ( $table ) {
484 $table.tablesorter(
485 { sortList: [
486 { 0: 'asc' },
487 { 1: 'asc' }
488 ] }
489 );
490 $table.data( 'tablesorter' ).sort(
491 [
492 { 0: 'desc' },
493 { 1: 'asc' }
494 ]
495 );
496 $table.data( 'tablesorter' ).sort();
497 }
498 );
499 tableTest(
500 'Sort via click event after having initialized the tablesorter with initial sorting',
501 header,
502 simple,
503 simpleDescasc,
504 function ( $table ) {
505 $table.tablesorter(
506 { sortList: [ { 0: 'asc' }, { 1: 'asc' } ] }
507 );
508 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
509 }
510 );
511 tableTest(
512 'Multi-sort via click event after having initialized the tablesorter with initial sorting',
513 header,
514 simple,
515 simpleAsc,
516 function ( $table ) {
517 var event;
518 $table.tablesorter(
519 { sortList: [ { 0: 'desc' }, { 1: 'desc' } ] }
520 );
521 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
522
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 );
527 }
528 );
529 QUnit.test( 'Reset sorting making table appear unsorted', function ( assert ) {
530 var $table = tableCreate( header, simple );
531 $table.tablesorter(
532 { sortList: [
533 { 0: 'desc' },
534 { 1: 'asc' }
535 ] }
536 );
537 $table.data( 'tablesorter' ).sort( [] );
538
539 assert.strictEqual(
540 $table.find( 'th.headerSortUp' ).length + $table.find( 'th.headerSortDown' ).length,
541 0,
542 'No sort specific sort classes addign to header cells'
543 );
544
545 assert.strictEqual(
546 $table.find( 'th' ).first().attr( 'title' ),
547 mw.msg( 'sort-ascending' ),
548 'First header cell has default title'
549 );
550
551 assert.strictEqual(
552 $table.find( 'th' ).first().attr( 'title' ),
553 $table.find( 'th' ).last().attr( 'title' ),
554 'Both header cells\' titles match'
555 );
556 } );
557
558 // Sorting with colspans
559
560 tableTest( 'Sorting with colspanned headers: spanned column',
561 header4,
562 colspanInitial,
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' );
569
570 $table.tablesorter();
571 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
572 }
573 );
574 tableTest( 'Sorting with colspanned headers: sort spanned column twice',
575 header4,
576 colspanInitial,
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' );
583
584 $table.tablesorter();
585 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
586 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
587 }
588 );
589 tableTest( 'Sorting with colspanned headers: subsequent column',
590 header4,
591 colspanInitial,
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' );
598
599 $table.tablesorter();
600 $table.find( '.headerSort' ).eq( 1 ).trigger( 'click' );
601 }
602 );
603 tableTest( 'Sorting with colspanned headers: sort subsequent column twice',
604 header4,
605 colspanInitial,
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' );
612
613 $table.tablesorter();
614 $table.find( '.headerSort' ).eq( 1 ).trigger( 'click' );
615 $table.find( '.headerSort' ).eq( 1 ).trigger( 'click' );
616 }
617 );
618
619 QUnit.test( 'Basic planet table: one unsortable column', function ( assert ) {
620 var $table = tableCreate( header, planets ),
621 $cell;
622 $table.find( 'tr > th' ).eq( 0 ).addClass( 'unsortable' );
623
624 $table.tablesorter();
625 $table.find( 'tr > th' ).eq( 0 ).trigger( 'click' );
626
627 assert.deepEqual(
628 tableExtract( $table ),
629 planets,
630 'table not sorted'
631 );
632
633 $cell = $table.find( 'tr > th' ).eq( 0 );
634 $table.find( 'tr > th' ).eq( 1 ).trigger( 'click' );
635
636 assert.strictEqual(
637 $cell.hasClass( 'headerSortUp' ) || $cell.hasClass( 'headerSortDown' ),
638 false,
639 'after sort: no class headerSortUp or headerSortDown'
640 );
641
642 assert.strictEqual(
643 $cell.attr( 'title' ),
644 undefined,
645 'after sort: no title tag added'
646 );
647
648 } );
649
650 // Regression tests!
651 tableTest(
652 'T30775: German-style (dmy) short numeric dates',
653 [ 'Date' ],
654 [
655 // German-style dates are day-month-year
656 [ '11.11.2011' ],
657 [ '01.11.2011' ],
658 [ '02.10.2011' ],
659 [ '03.08.2011' ],
660 [ '09.11.2011' ]
661 ],
662 [
663 // Sorted by ascending date
664 [ '03.08.2011' ],
665 [ '02.10.2011' ],
666 [ '01.11.2011' ],
667 [ '09.11.2011' ],
668 [ '11.11.2011' ]
669 ],
670 function ( $table ) {
671 mw.config.set( 'wgDefaultDateFormat', 'dmy' );
672 mw.config.set( 'wgPageContentLanguage', 'de' );
673
674 $table.tablesorter();
675 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
676 }
677 );
678
679 tableTest(
680 'T30775: American-style (mdy) short numeric dates',
681 [ 'Date' ],
682 [
683 // American-style dates are month-day-year
684 [ '11.11.2011' ],
685 [ '01.11.2011' ],
686 [ '02.10.2011' ],
687 [ '03.08.2011' ],
688 [ '09.11.2011' ]
689 ],
690 [
691 // Sorted by ascending date
692 [ '01.11.2011' ],
693 [ '02.10.2011' ],
694 [ '03.08.2011' ],
695 [ '09.11.2011' ],
696 [ '11.11.2011' ]
697 ],
698 function ( $table ) {
699 mw.config.set( 'wgDefaultDateFormat', 'mdy' );
700
701 $table.tablesorter();
702 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
703 }
704 );
705
706 tableTest(
707 'T19141: IPv4 address sorting',
708 [ 'IP' ],
709 ipv4,
710 ipv4Sorted,
711 function ( $table ) {
712 $table.tablesorter();
713 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
714 }
715 );
716 tableTest(
717 'T19141: IPv4 address sorting (reverse)',
718 [ 'IP' ],
719 ipv4,
720 reversed( ipv4Sorted ),
721 function ( $table ) {
722 $table.tablesorter();
723 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' ).trigger( 'click' );
724 }
725 );
726
727 tableTest(
728 'Accented Characters with custom collation',
729 [ 'Name' ],
730 umlautWords,
731 umlautWordsSortedEn,
732 function ( $table ) {
733 mw.config.set( 'tableSorterCollation', {
734 ä: 'ae',
735 ö: 'oe',
736 ß: 'ss',
737 ü: 'ue'
738 } );
739
740 $table.tablesorter();
741 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
742 }
743 );
744
745 tableTest(
746 'Accented Characters Swedish locale',
747 [ 'Name' ],
748 umlautWords,
749 umlautWordsSortedSv,
750 function ( $table ) {
751 mw.config.set( 'wgPageContentLanguage', 'sv' );
752
753 $table.tablesorter();
754 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
755 }
756 );
757
758 tableTest(
759 'Digraphs with custom collation',
760 [ 'City' ],
761 digraphWords,
762 digraphWordsSorted,
763 function ( $table ) {
764 mw.config.set( 'tableSorterCollation', {
765 lj: 'lzzzz',
766 nj: 'nzzzz'
767 } );
768
769 $table.tablesorter();
770 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
771 }
772 );
773
774 QUnit.test( 'Rowspan not exploded on init', function ( assert ) {
775 var $table = tableCreate( header, planets );
776
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' );
784
785 $table.tablesorter();
786
787 assert.strictEqual(
788 $table.find( 'tr' ).eq( 2 ).find( 'td' ).eq( 1 ).prop( 'rowSpan' ),
789 3,
790 'Rowspan not exploded'
791 );
792 } );
793
794 planetsRowspan = [
795 [ 'Earth', '6051.8' ],
796 jupiter,
797 [ 'Mars', '6051.8' ],
798 mercury,
799 saturn,
800 venus
801 ];
802 planetsRowspanII = [ jupiter, mercury, saturn, venus, [ 'Venus', '6371.0' ], [ 'Venus', '3390.0' ] ];
803
804 tableTest(
805 'Basic planet table: same value for multiple rows via rowspan',
806 header,
807 planets,
808 planetsRowspan,
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' );
817
818 $table.tablesorter();
819 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
820 }
821 );
822 tableTest(
823 'Basic planet table: same value for multiple rows via rowspan (sorting initially)',
824 header,
825 planets,
826 planetsRowspan,
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' );
835
836 $table.tablesorter( { sortList: [
837 { 0: 'asc' }
838 ] } );
839 }
840 );
841 tableTest(
842 'Basic planet table: Same value for multiple rows via rowspan II',
843 header,
844 planets,
845 planetsRowspanII,
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' );
854
855 $table.tablesorter();
856 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
857 }
858 );
859
860 tableTest(
861 'Complex date parsing I',
862 [ 'date' ],
863 complexMDYDates,
864 complexMDYSorted,
865 function ( $table ) {
866 mw.config.set( 'wgDefaultDateFormat', 'mdy' );
867
868 $table.tablesorter();
869 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
870 }
871 );
872
873 tableTest(
874 'Currency parsing I',
875 [ 'currency' ],
876 currencyUnsorted,
877 currencySorted,
878 function ( $table ) {
879 $table.tablesorter();
880 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
881 }
882 );
883
884 planetsAscNameLegacy = planetsAscName.slice( 0 );
885 planetsAscNameLegacy[ 4 ] = planetsAscNameLegacy[ 5 ];
886 planetsAscNameLegacy.pop();
887
888 tableTest(
889 'Legacy compat with .sortbottom',
890 header,
891 planets,
892 planetsAscNameLegacy,
893 function ( $table ) {
894 $table.find( 'tr' ).last().addClass( 'sortbottom' );
895 $table.tablesorter();
896 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
897 }
898 );
899
900 QUnit.test( 'Test detection routine', function ( assert ) {
901 var $table;
902 $table = $(
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>' +
908 '</table>'
909 );
910 $table.tablesorter();
911 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
912
913 assert.strictEqual(
914 $table.data( 'tablesorter' ).config.parsers[ 0 ].id,
915 'number',
916 'Correctly detected column content skipping sortbottom'
917 );
918 } );
919
920 /** FIXME: the diff output is not very readeable. */
921 QUnit.test( 'T34047 - caption must be before thead', function ( assert ) {
922 var $table;
923 $table = $(
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>' +
930 '</table>'
931 );
932 $table.tablesorter();
933
934 assert.strictEqual(
935 $table.children().get( 0 ).nodeName,
936 'CAPTION',
937 'First element after <thead> must be <caption> (T34047)'
938 );
939 } );
940
941 QUnit.test( 'data-sort-value attribute, when available, should override sorting position', function ( assert ) {
942 var $table, data;
943
944 // Example 1: All cells except one cell without data-sort-value,
945 // which should be sorted at it's text content value.
946 $table = $(
947 '<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
948 '<tbody>' +
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>' +
954 '</tbody></table>'
955 );
956 $table.tablesorter().find( '.headerSort' ).eq( 0 ).trigger( 'click' );
957
958 data = [];
959 $table.find( 'tbody > tr' ).each( function ( i, tr ) {
960 $( tr ).find( 'td' ).each( function ( i, td ) {
961 data.push( {
962 data: $( td ).data( 'sortValue' ),
963 text: $( td ).text()
964 } );
965 } );
966 } );
967
968 assert.deepEqual( data, [
969 {
970 data: 'Apple',
971 text: 'Bird'
972 },
973 {
974 data: 'Bananna',
975 text: 'Ferret'
976 },
977 {
978 data: undefined,
979 text: 'Cheetah'
980 },
981 {
982 data: 'Cherry',
983 text: 'Dolphin'
984 },
985 {
986 data: 'Drupe',
987 text: 'Elephant'
988 }
989 ], 'Order matches expected order (based on data-sort-value attribute values)' );
990
991 // Example 2
992 $table = $(
993 '<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
994 '<tbody>' +
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>' +
1001 '</tbody></table>'
1002 );
1003 $table.tablesorter().find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1004
1005 data = [];
1006 $table.find( 'tbody > tr' ).each( function ( i, tr ) {
1007 $( tr ).find( 'td' ).each( function ( i, td ) {
1008 data.push( {
1009 data: $( td ).data( 'sortValue' ),
1010 text: $( td ).text()
1011 } );
1012 } );
1013 } );
1014
1015 assert.deepEqual( data, [
1016 {
1017 data: undefined,
1018 text: 'B'
1019 },
1020 {
1021 data: undefined,
1022 text: 'D'
1023 },
1024 {
1025 data: undefined,
1026 text: 'H'
1027 },
1028 {
1029 data: 'E',
1030 text: 'A'
1031 },
1032 {
1033 data: 'F',
1034 text: 'C'
1035 },
1036 {
1037 data: undefined,
1038 text: 'G'
1039 }
1040 ], 'Order matches expected order (based on data-sort-value attribute values)' );
1041
1042 // Example 3: Test that live changes are used from data-sort-value,
1043 // even if they change after the tablesorter is constructed (T40152).
1044 $table = $(
1045 '<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
1046 '<tbody>' +
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>' +
1052 '</tbody></table>'
1053 );
1054 // initialize table sorter and sort once
1055 $table.tablesorter().find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1056
1057 // Change the sortValue data properties (T40152)
1058 // - change data
1059 $table.find( 'td:contains(A)' ).data( 'sortValue', 3 );
1060 // - add data
1061 $table.find( 'td:contains(B)' ).data( 'sortValue', 1 );
1062 // - remove data, bring back attribute: 2
1063 $table.find( 'td:contains(G)' ).removeData( 'sortValue' );
1064
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' );
1068
1069 data = [];
1070 $table.find( 'tbody > tr' ).each( function ( i, tr ) {
1071 $( tr ).find( 'td' ).each( function ( i, td ) {
1072 data.push( {
1073 data: $( td ).data( 'sortValue' ),
1074 text: $( td ).text()
1075 } );
1076 } );
1077 } );
1078
1079 assert.deepEqual( data, [
1080 {
1081 data: 1,
1082 text: 'B'
1083 },
1084 {
1085 data: 2,
1086 text: 'G'
1087 },
1088 {
1089 data: 3,
1090 text: 'A'
1091 },
1092 {
1093 data: undefined,
1094 text: 'C'
1095 },
1096 {
1097 data: undefined,
1098 text: 'D'
1099 }
1100 ], 'Order matches expected order, using the current sortValue in $.data()' );
1101
1102 } );
1103
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' );
1109 }
1110 );
1111
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' );
1117 }
1118 );
1119 // TODO add numbers sorting tests for T10115 with a different language
1120
1121 QUnit.test( 'T34888 - Tables inside a tableheader cell', function ( assert ) {
1122 var $table;
1123 $table = $(
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>' +
1130 '</table>'
1131 );
1132 $table.tablesorter();
1133
1134 assert.strictEqual(
1135 $table.find( '> thead > tr > th.headerSort' ).length,
1136 1,
1137 'Child tables inside a headercell should not interfere with sortable headers (T34888)'
1138 );
1139 assert.strictEqual(
1140 $( '#mw-bug-32888-2' ).find( 'th.headerSort' ).length,
1141 0,
1142 'The headers of child tables inside a headercell should not be sortable themselves (T34888)'
1143 );
1144 } );
1145
1146 tableTest(
1147 'Correct date sorting I',
1148 [ 'date' ],
1149 correctDateSorting1,
1150 correctDateSortingSorted1,
1151 function ( $table ) {
1152 mw.config.set( 'wgDefaultDateFormat', 'mdy' );
1153
1154 $table.tablesorter();
1155 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1156 }
1157 );
1158
1159 tableTest(
1160 'Correct date sorting II',
1161 [ 'date' ],
1162 correctDateSorting2,
1163 correctDateSortingSorted2,
1164 function ( $table ) {
1165 mw.config.set( 'wgDefaultDateFormat', 'dmy' );
1166
1167 $table.tablesorter();
1168 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1169 }
1170 );
1171
1172 tableTest(
1173 'ISO date sorting',
1174 [ 'isoDate' ],
1175 isoDateSorting,
1176 isoDateSortingSorted,
1177 function ( $table ) {
1178 mw.config.set( 'wgDefaultDateFormat', 'dmy' );
1179
1180 $table.tablesorter();
1181 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1182 }
1183 );
1184
1185 QUnit.test( 'Sorting images using alt text', function ( assert ) {
1186 var $table = $(
1187 '<table class="sortable">' +
1188 '<tr><th>THEAD</th></tr>' +
1189 '<tr><td><img alt="2"/></td></tr>' +
1190 '<tr><td>1</td></tr>' +
1191 '</table>'
1192 );
1193 $table.tablesorter().find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1194
1195 assert.strictEqual(
1196 $table.find( 'td' ).first().text(),
1197 '1',
1198 'Applied correct sorting order'
1199 );
1200 } );
1201
1202 QUnit.test( 'Sorting images using alt text (complex)', function ( assert ) {
1203 var $table = $(
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>' +
1212 '</table>'
1213 );
1214 $table.tablesorter().find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1215
1216 assert.strictEqual(
1217 $table.find( 'td' ).text(),
1218 'CDEFCCA',
1219 'Applied correct sorting order'
1220 );
1221 } );
1222
1223 QUnit.test( 'Sorting images using alt text (with format autodetection)', function ( assert ) {
1224 var $table = $(
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>' +
1231 '</table>'
1232 );
1233 $table.tablesorter().find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1234
1235 assert.strictEqual(
1236 $table.find( 'td' ).text(),
1237 '4517',
1238 'Applied correct sorting order'
1239 );
1240 } );
1241
1242 QUnit.test( 'T40911 - The row with the largest amount of columns should receive the sort indicators', function ( assert ) {
1243 var $table = $(
1244 '<table class="sortable">' +
1245 '<thead>' +
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>' +
1248 '</thead>' +
1249 '<tr><td>A</td><td>Aa</td><td>Ab</td></tr>' +
1250 '<tr><td>B</td><td>Ba</td><td>Bb</td></tr>' +
1251 '</table>'
1252 );
1253 $table.tablesorter();
1254
1255 assert.strictEqual(
1256 $table.find( '#A1' ).attr( 'class' ),
1257 'headerSort',
1258 'The first column of the first row should be sortable'
1259 );
1260 assert.strictEqual(
1261 $table.find( '#B2b' ).attr( 'class' ),
1262 'headerSort',
1263 'The th element of the 2nd row of the 2nd column should be sortable'
1264 );
1265 assert.strictEqual(
1266 $table.find( '#C2b' ).attr( 'class' ),
1267 'headerSort',
1268 'The th element of the 2nd row of the 3rd column should be sortable'
1269 );
1270 } );
1271
1272 QUnit.test( 'rowspans in table headers should prefer the last row when rows are equal in length', function ( assert ) {
1273 var $table = $(
1274 '<table class="sortable">' +
1275 '<thead>' +
1276 '<tr><th rowspan="2" id="A1">A1</th><th>B2a</th></tr>' +
1277 '<tr><th id="B2b">B2b</th></tr>' +
1278 '</thead>' +
1279 '<tr><td>A</td><td>Aa</td></tr>' +
1280 '<tr><td>B</td><td>Ba</td></tr>' +
1281 '</table>'
1282 );
1283 $table.tablesorter();
1284
1285 assert.strictEqual(
1286 $table.find( '#A1' ).attr( 'class' ),
1287 'headerSort',
1288 'The first column of the first row should be sortable'
1289 );
1290 assert.strictEqual(
1291 $table.find( '#B2b' ).attr( 'class' ),
1292 'headerSort',
1293 'The th element of the 2nd row of the 2nd column should be sortable'
1294 );
1295 } );
1296
1297 QUnit.test( 'holes in the table headers should not throw JS errors', function ( assert ) {
1298 var $table = $(
1299 '<table class="sortable">' +
1300 '<thead>' +
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>' +
1303 '</thead>' +
1304 '<tr><td>A</td><td>Aa</td><td>Aaa</td></tr>' +
1305 '<tr><td>B</td><td>Ba</td><td>Bbb</td></tr>' +
1306 '</table>'
1307 );
1308 $table.tablesorter();
1309 assert.strictEqual( $table.find( '#A2' ).data( 'headerIndex' ),
1310 undefined,
1311 'A2 should not be a sort header'
1312 );
1313 assert.strictEqual( $table.find( '#C1' ).data( 'headerIndex' ),
1314 2,
1315 'C1 should be a sort header'
1316 );
1317 } );
1318
1319 // T55527
1320 QUnit.test( 'td cells in thead should not be taken into account for longest row calculation', function ( assert ) {
1321 var $table = $(
1322 '<table class="sortable">' +
1323 '<thead>' +
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>' +
1326 '</thead>' +
1327 '</table>'
1328 );
1329 $table.tablesorter();
1330 assert.strictEqual( $table.find( '#C2' ).data( 'headerIndex' ),
1331 2,
1332 'C2 should be a sort header'
1333 );
1334 assert.strictEqual( $table.find( '#C1' ).data( 'headerIndex' ),
1335 undefined,
1336 'C1 should not be a sort header'
1337 );
1338 } );
1339
1340 // T43889 - exploding rowspans in more complex cases
1341 tableTestHTML(
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>' +
1345 '<tbody>' +
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>' +
1348 '</tbody></table>',
1349 [
1350 [ '1', 'foo', 'bar', 'baz' ],
1351 [ '2', 'foo', 'bar', 'baz' ]
1352 ]
1353 );
1354
1355 // T55211 - exploding rowspans in more complex cases
1356 QUnit.test(
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>' +
1361 '<tbody>' +
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>' );
1365
1366 $table.tablesorter();
1367 assert.strictEqual( $table.find( 'tr' ).eq( 1 ).find( 'th' ).eq( 1 ).data( 'headerIndex' ),
1368 2,
1369 'Incorrect index of sort header'
1370 );
1371 }
1372 );
1373
1374 tableTestHTML(
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>' +
1378 '<tbody>' +
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>' +
1381 '</tbody></table>',
1382 [
1383 [ '1', 'foo', 'bar', 'baz' ],
1384 [ '2', 'foobar', 'baz' ]
1385 ]
1386 );
1387
1388 tableTestHTML(
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>' +
1392 '<tbody>' +
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>' +
1395 '</tbody></table>',
1396 [
1397 [ '2', 'foobar', 'baz', '1' ],
1398 [ '1', 'foo', 'bar', 'baz', '2' ]
1399 ]
1400 );
1401
1402 tableTestHTML(
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>' +
1406 '<tbody>' +
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>' +
1411 '</tbody></table>',
1412 [
1413 [ '1', 'foo', 'bar' ],
1414 [ '2', 'foo', 'bar' ],
1415 [ '3', 'foo', 'bar' ],
1416 [ '4', 'foo', 'bar' ]
1417 ]
1418 );
1419
1420 tableTestHTML(
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>' +
1424 '<tbody>' +
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>' +
1429 '</tbody></table>',
1430 [
1431 [ '1', 'foo', 'bar', 'baz' ],
1432 [ '2', 'foo', 'bar', 'baz' ],
1433 [ '3', 'foo', 'bar', 'baz' ],
1434 [ '4', 'foo', 'bar', 'baz' ]
1435 ]
1436 );
1437
1438 tableTestHTML(
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>' +
1442 '<tbody>' +
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>' +
1447 '</tbody></table>',
1448 [
1449 [ '1', 'foo1', 'foo2', 'bar', 'baz' ],
1450 [ '2', 'foo1', 'foo2', 'bar', 'baz' ],
1451 [ '3', 'foo', 'bar', 'baz' ],
1452 [ '4', 'foo', 'bar', 'baz' ]
1453 ]
1454 );
1455
1456 tableTestHTML(
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>' +
1460 '<tbody>' +
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>' +
1465 '</tbody></table>',
1466 [
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' ]
1471 ]
1472 );
1473
1474 QUnit.test( 'T105731 - incomplete rows in table body', function ( assert ) {
1475 var $table, parsers;
1476 $table = $(
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>' +
1481 '</table>'
1482 );
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' );
1487
1488 parsers = $table.data( 'tablesorter' ).config.parsers;
1489
1490 assert.strictEqual(
1491 parsers.length,
1492 2,
1493 'detectParserForColumn() detect 2 parsers'
1494 );
1495
1496 assert.strictEqual(
1497 parsers[ 1 ].id,
1498 'number',
1499 'detectParserForColumn() detect parser.id "number" for second column'
1500 );
1501
1502 assert.strictEqual(
1503 parsers[ 1 ].format( $table.find( 'tbody > tr > td' ).eq( 1 ).text() ),
1504 -Infinity,
1505 'empty cell is sorted as number -Infinity'
1506 );
1507 } );
1508
1509 QUnit.test( 'bug T114721 - use of expand-child class', function ( assert ) {
1510 var $table, parsers;
1511 $table = $(
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>' +
1519 '</table>'
1520 );
1521 $table.tablesorter();
1522 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1523
1524 assert.deepEqual(
1525 tableExtract( $table ),
1526 [
1527 [ 'a', '2' ],
1528 [ 'some text follow a' ],
1529 [ 'more text' ],
1530 [ 'b', '4' ],
1531 [ 'some text follow b' ]
1532 ],
1533 'row with expand-child class follow above row'
1534 );
1535
1536 parsers = $table.data( 'tablesorter' ).config.parsers;
1537 assert.strictEqual(
1538 parsers[ 1 ].id,
1539 'number',
1540 'detectParserForColumn() detect parser.id "number" for second column'
1541 );
1542 } );
1543 QUnit.test( 'T29745 - References ignored in sortkey', function ( assert ) {
1544 var $table, parsers;
1545 $table = $(
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>' +
1550 '</table>'
1551 );
1552 $table.tablesorter();
1553 $table.find( '.headerSort' ).eq( 0 ).trigger( 'click' );
1554
1555 assert.deepEqual(
1556 tableExtract( $table ),
1557 [
1558 [ '2[1]' ],
1559 [ '10' ]
1560 ],
1561 'References ignored in sortkey'
1562 );
1563
1564 parsers = $table.data( 'tablesorter' ).config.parsers;
1565 assert.strictEqual(
1566 parsers[ 0 ].id,
1567 'number',
1568 'detectParserForColumn() detect parser.id "number"'
1569 );
1570 } );
1571
1572 }() );