Merge "Change Special:Mute link on email footer to be in plain text"
[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:eq(0) th:eq(1), tr:eq(0) th:eq(2)' ).remove();
567 $table.find( 'tr:eq(0) th:eq(0)' ).attr( 'colspan', '3' );
568
569 $table.tablesorter();
570 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
571 }
572 );
573 tableTest( 'Sorting with colspanned headers: sort spanned column twice',
574 header4,
575 colspanInitial,
576 [ caa4, bbc2, abc3, aab5, aaa1 ],
577 function ( $table ) {
578 // Make colspanned header for test
579 $table.find( 'tr:eq(0) th:eq(1), tr:eq(0) th:eq(2)' ).remove();
580 $table.find( 'tr:eq(0) th:eq(0)' ).attr( 'colspan', '3' );
581
582 $table.tablesorter();
583 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
584 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
585 }
586 );
587 tableTest( 'Sorting with colspanned headers: subsequent column',
588 header4,
589 colspanInitial,
590 [ aaa1, bbc2, abc3, caa4, aab5 ],
591 function ( $table ) {
592 // Make colspanned header for test
593 $table.find( 'tr:eq(0) th:eq(1), tr:eq(0) th:eq(2)' ).remove();
594 $table.find( 'tr:eq(0) th:eq(0)' ).attr( 'colspan', '3' );
595
596 $table.tablesorter();
597 $table.find( '.headerSort:eq(1)' ).trigger( 'click' );
598 }
599 );
600 tableTest( 'Sorting with colspanned headers: sort subsequent column twice',
601 header4,
602 colspanInitial,
603 [ aab5, caa4, abc3, bbc2, aaa1 ],
604 function ( $table ) {
605 // Make colspanned header for test
606 $table.find( 'tr:eq(0) th:eq(1), tr:eq(0) th:eq(2)' ).remove();
607 $table.find( 'tr:eq(0) th:eq(0)' ).attr( 'colspan', '3' );
608
609 $table.tablesorter();
610 $table.find( '.headerSort:eq(1)' ).trigger( 'click' );
611 $table.find( '.headerSort:eq(1)' ).trigger( 'click' );
612 }
613 );
614
615 QUnit.test( 'Basic planet table: one unsortable column', function ( assert ) {
616 var $table = tableCreate( header, planets ),
617 $cell;
618 $table.find( 'tr:eq(0) > th:eq(0)' ).addClass( 'unsortable' );
619
620 $table.tablesorter();
621 $table.find( 'tr:eq(0) > th:eq(0)' ).trigger( 'click' );
622
623 assert.deepEqual(
624 tableExtract( $table ),
625 planets,
626 'table not sorted'
627 );
628
629 $cell = $table.find( 'tr:eq(0) > th:eq(0)' );
630 $table.find( 'tr:eq(0) > th:eq(1)' ).trigger( 'click' );
631
632 assert.strictEqual(
633 $cell.hasClass( 'headerSortUp' ) || $cell.hasClass( 'headerSortDown' ),
634 false,
635 'after sort: no class headerSortUp or headerSortDown'
636 );
637
638 assert.strictEqual(
639 $cell.attr( 'title' ),
640 undefined,
641 'after sort: no title tag added'
642 );
643
644 } );
645
646 // Regression tests!
647 tableTest(
648 'T30775: German-style (dmy) short numeric dates',
649 [ 'Date' ],
650 [
651 // German-style dates are day-month-year
652 [ '11.11.2011' ],
653 [ '01.11.2011' ],
654 [ '02.10.2011' ],
655 [ '03.08.2011' ],
656 [ '09.11.2011' ]
657 ],
658 [
659 // Sorted by ascending date
660 [ '03.08.2011' ],
661 [ '02.10.2011' ],
662 [ '01.11.2011' ],
663 [ '09.11.2011' ],
664 [ '11.11.2011' ]
665 ],
666 function ( $table ) {
667 mw.config.set( 'wgDefaultDateFormat', 'dmy' );
668 mw.config.set( 'wgPageContentLanguage', 'de' );
669
670 $table.tablesorter();
671 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
672 }
673 );
674
675 tableTest(
676 'T30775: American-style (mdy) short numeric dates',
677 [ 'Date' ],
678 [
679 // American-style dates are month-day-year
680 [ '11.11.2011' ],
681 [ '01.11.2011' ],
682 [ '02.10.2011' ],
683 [ '03.08.2011' ],
684 [ '09.11.2011' ]
685 ],
686 [
687 // Sorted by ascending date
688 [ '01.11.2011' ],
689 [ '02.10.2011' ],
690 [ '03.08.2011' ],
691 [ '09.11.2011' ],
692 [ '11.11.2011' ]
693 ],
694 function ( $table ) {
695 mw.config.set( 'wgDefaultDateFormat', 'mdy' );
696
697 $table.tablesorter();
698 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
699 }
700 );
701
702 tableTest(
703 'T19141: IPv4 address sorting',
704 [ 'IP' ],
705 ipv4,
706 ipv4Sorted,
707 function ( $table ) {
708 $table.tablesorter();
709 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
710 }
711 );
712 tableTest(
713 'T19141: IPv4 address sorting (reverse)',
714 [ 'IP' ],
715 ipv4,
716 reversed( ipv4Sorted ),
717 function ( $table ) {
718 $table.tablesorter();
719 $table.find( '.headerSort:eq(0)' ).trigger( 'click' ).trigger( 'click' );
720 }
721 );
722
723 tableTest(
724 'Accented Characters with custom collation',
725 [ 'Name' ],
726 umlautWords,
727 umlautWordsSortedEn,
728 function ( $table ) {
729 mw.config.set( 'tableSorterCollation', {
730 ä: 'ae',
731 ö: 'oe',
732 ß: 'ss',
733 ü: 'ue'
734 } );
735
736 $table.tablesorter();
737 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
738 }
739 );
740
741 tableTest(
742 'Accented Characters Swedish locale',
743 [ 'Name' ],
744 umlautWords,
745 umlautWordsSortedSv,
746 function ( $table ) {
747 mw.config.set( 'wgPageContentLanguage', 'sv' );
748
749 $table.tablesorter();
750 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
751 }
752 );
753
754 tableTest(
755 'Digraphs with custom collation',
756 [ 'City' ],
757 digraphWords,
758 digraphWordsSorted,
759 function ( $table ) {
760 mw.config.set( 'tableSorterCollation', {
761 lj: 'lzzzz',
762 nj: 'nzzzz'
763 } );
764
765 $table.tablesorter();
766 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
767 }
768 );
769
770 QUnit.test( 'Rowspan not exploded on init', function ( assert ) {
771 var $table = tableCreate( header, planets );
772
773 // Modify the table to have a multiple-row-spanning cell:
774 // - Remove 2nd cell of 4th row, and, 2nd cell or 5th row.
775 $table.find( 'tr:eq(3) td:eq(1), tr:eq(4) td:eq(1)' ).remove();
776 // - Set rowspan for 2nd cell of 3rd row to 3.
777 // This covers the removed cell in the 4th and 5th row.
778 $table.find( 'tr:eq(2) td:eq(1)' ).attr( 'rowspan', '3' );
779
780 $table.tablesorter();
781
782 assert.strictEqual(
783 $table.find( 'tr:eq(2) td:eq(1)' ).prop( 'rowSpan' ),
784 3,
785 'Rowspan not exploded'
786 );
787 } );
788
789 planetsRowspan = [
790 [ 'Earth', '6051.8' ],
791 jupiter,
792 [ 'Mars', '6051.8' ],
793 mercury,
794 saturn,
795 venus
796 ];
797 planetsRowspanII = [ jupiter, mercury, saturn, venus, [ 'Venus', '6371.0' ], [ 'Venus', '3390.0' ] ];
798
799 tableTest(
800 'Basic planet table: same value for multiple rows via rowspan',
801 header,
802 planets,
803 planetsRowspan,
804 function ( $table ) {
805 // Modify the table to have a multiple-row-spanning cell:
806 // - Remove 2nd cell of 4th row, and, 2nd cell or 5th row.
807 $table.find( 'tr:eq(3) td:eq(1), tr:eq(4) td:eq(1)' ).remove();
808 // - Set rowspan for 2nd cell of 3rd row to 3.
809 // This covers the removed cell in the 4th and 5th row.
810 $table.find( 'tr:eq(2) td:eq(1)' ).attr( 'rowspan', '3' );
811
812 $table.tablesorter();
813 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
814 }
815 );
816 tableTest(
817 'Basic planet table: same value for multiple rows via rowspan (sorting initially)',
818 header,
819 planets,
820 planetsRowspan,
821 function ( $table ) {
822 // Modify the table to have a multiple-row-spanning cell:
823 // - Remove 2nd cell of 4th row, and, 2nd cell or 5th row.
824 $table.find( 'tr:eq(3) td:eq(1), tr:eq(4) td:eq(1)' ).remove();
825 // - Set rowspan for 2nd cell of 3rd row to 3.
826 // This covers the removed cell in the 4th and 5th row.
827 $table.find( 'tr:eq(2) td:eq(1)' ).attr( 'rowspan', '3' );
828
829 $table.tablesorter( { sortList: [
830 { 0: 'asc' }
831 ] } );
832 }
833 );
834 tableTest(
835 'Basic planet table: Same value for multiple rows via rowspan II',
836 header,
837 planets,
838 planetsRowspanII,
839 function ( $table ) {
840 // Modify the table to have a multiple-row-spanning cell:
841 // - Remove 1st cell of 4th row, and, 1st cell or 5th row.
842 $table.find( 'tr:eq(3) td:eq(0), tr:eq(4) td:eq(0)' ).remove();
843 // - Set rowspan for 1st cell of 3rd row to 3.
844 // This covers the removed cell in the 4th and 5th row.
845 $table.find( 'tr:eq(2) td:eq(0)' ).attr( 'rowspan', '3' );
846
847 $table.tablesorter();
848 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
849 }
850 );
851
852 tableTest(
853 'Complex date parsing I',
854 [ 'date' ],
855 complexMDYDates,
856 complexMDYSorted,
857 function ( $table ) {
858 mw.config.set( 'wgDefaultDateFormat', 'mdy' );
859
860 $table.tablesorter();
861 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
862 }
863 );
864
865 tableTest(
866 'Currency parsing I',
867 [ 'currency' ],
868 currencyUnsorted,
869 currencySorted,
870 function ( $table ) {
871 $table.tablesorter();
872 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
873 }
874 );
875
876 planetsAscNameLegacy = planetsAscName.slice( 0 );
877 planetsAscNameLegacy[ 4 ] = planetsAscNameLegacy[ 5 ];
878 planetsAscNameLegacy.pop();
879
880 tableTest(
881 'Legacy compat with .sortbottom',
882 header,
883 planets,
884 planetsAscNameLegacy,
885 function ( $table ) {
886 $table.find( 'tr:last' ).addClass( 'sortbottom' );
887 $table.tablesorter();
888 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
889 }
890 );
891
892 QUnit.test( 'Test detection routine', function ( assert ) {
893 var $table;
894 $table = $(
895 '<table class="sortable">' +
896 '<caption>CAPTION</caption>' +
897 '<tr><th>THEAD</th></tr>' +
898 '<tr><td>1</td></tr>' +
899 '<tr class="sortbottom"><td>text</td></tr>' +
900 '</table>'
901 );
902 $table.tablesorter();
903 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
904
905 assert.strictEqual(
906 $table.data( 'tablesorter' ).config.parsers[ 0 ].id,
907 'number',
908 'Correctly detected column content skipping sortbottom'
909 );
910 } );
911
912 /** FIXME: the diff output is not very readeable. */
913 QUnit.test( 'T34047 - caption must be before thead', function ( assert ) {
914 var $table;
915 $table = $(
916 '<table class="sortable">' +
917 '<caption>CAPTION</caption>' +
918 '<tr><th>THEAD</th></tr>' +
919 '<tr><td>A</td></tr>' +
920 '<tr><td>B</td></tr>' +
921 '<tr class="sortbottom"><td>TFOOT</td></tr>' +
922 '</table>'
923 );
924 $table.tablesorter();
925
926 assert.strictEqual(
927 $table.children().get( 0 ).nodeName,
928 'CAPTION',
929 'First element after <thead> must be <caption> (T34047)'
930 );
931 } );
932
933 QUnit.test( 'data-sort-value attribute, when available, should override sorting position', function ( assert ) {
934 var $table, data;
935
936 // Example 1: All cells except one cell without data-sort-value,
937 // which should be sorted at it's text content value.
938 $table = $(
939 '<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
940 '<tbody>' +
941 '<tr><td>Cheetah</td></tr>' +
942 '<tr><td data-sort-value="Apple">Bird</td></tr>' +
943 '<tr><td data-sort-value="Bananna">Ferret</td></tr>' +
944 '<tr><td data-sort-value="Drupe">Elephant</td></tr>' +
945 '<tr><td data-sort-value="Cherry">Dolphin</td></tr>' +
946 '</tbody></table>'
947 );
948 $table.tablesorter().find( '.headerSort:eq(0)' ).trigger( 'click' );
949
950 data = [];
951 $table.find( 'tbody > tr' ).each( function ( i, tr ) {
952 $( tr ).find( 'td' ).each( function ( i, td ) {
953 data.push( {
954 data: $( td ).data( 'sortValue' ),
955 text: $( td ).text()
956 } );
957 } );
958 } );
959
960 assert.deepEqual( data, [
961 {
962 data: 'Apple',
963 text: 'Bird'
964 },
965 {
966 data: 'Bananna',
967 text: 'Ferret'
968 },
969 {
970 data: undefined,
971 text: 'Cheetah'
972 },
973 {
974 data: 'Cherry',
975 text: 'Dolphin'
976 },
977 {
978 data: 'Drupe',
979 text: 'Elephant'
980 }
981 ], 'Order matches expected order (based on data-sort-value attribute values)' );
982
983 // Example 2
984 $table = $(
985 '<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
986 '<tbody>' +
987 '<tr><td>D</td></tr>' +
988 '<tr><td data-sort-value="E">A</td></tr>' +
989 '<tr><td>B</td></tr>' +
990 '<tr><td>G</td></tr>' +
991 '<tr><td data-sort-value="F">C</td></tr>' +
992 '<tr><td><span data-sort-value="D">H</span></td></tr>' +
993 '</tbody></table>'
994 );
995 $table.tablesorter().find( '.headerSort:eq(0)' ).trigger( 'click' );
996
997 data = [];
998 $table.find( 'tbody > tr' ).each( function ( i, tr ) {
999 $( tr ).find( 'td' ).each( function ( i, td ) {
1000 data.push( {
1001 data: $( td ).data( 'sortValue' ),
1002 text: $( td ).text()
1003 } );
1004 } );
1005 } );
1006
1007 assert.deepEqual( data, [
1008 {
1009 data: undefined,
1010 text: 'B'
1011 },
1012 {
1013 data: undefined,
1014 text: 'D'
1015 },
1016 {
1017 data: undefined,
1018 text: 'H'
1019 },
1020 {
1021 data: 'E',
1022 text: 'A'
1023 },
1024 {
1025 data: 'F',
1026 text: 'C'
1027 },
1028 {
1029 data: undefined,
1030 text: 'G'
1031 }
1032 ], 'Order matches expected order (based on data-sort-value attribute values)' );
1033
1034 // Example 3: Test that live changes are used from data-sort-value,
1035 // even if they change after the tablesorter is constructed (T40152).
1036 $table = $(
1037 '<table class="sortable"><thead><tr><th>Data</th></tr></thead>' +
1038 '<tbody>' +
1039 '<tr><td>D</td></tr>' +
1040 '<tr><td data-sort-value="1">A</td></tr>' +
1041 '<tr><td>B</td></tr>' +
1042 '<tr><td data-sort-value="2">G</td></tr>' +
1043 '<tr><td>C</td></tr>' +
1044 '</tbody></table>'
1045 );
1046 // initialize table sorter and sort once
1047 $table
1048 .tablesorter()
1049 .find( '.headerSort:eq(0)' ).trigger( 'click' );
1050
1051 // Change the sortValue data properties (T40152)
1052 // - change data
1053 $table.find( 'td:contains(A)' ).data( 'sortValue', 3 );
1054 // - add data
1055 $table.find( 'td:contains(B)' ).data( 'sortValue', 1 );
1056 // - remove data, bring back attribute: 2
1057 $table.find( 'td:contains(G)' ).removeData( 'sortValue' );
1058
1059 // Now sort again (twice, so it is back at Ascending)
1060 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
1061 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
1062
1063 data = [];
1064 $table.find( 'tbody > tr' ).each( function ( i, tr ) {
1065 $( tr ).find( 'td' ).each( function ( i, td ) {
1066 data.push( {
1067 data: $( td ).data( 'sortValue' ),
1068 text: $( td ).text()
1069 } );
1070 } );
1071 } );
1072
1073 assert.deepEqual( data, [
1074 {
1075 data: 1,
1076 text: 'B'
1077 },
1078 {
1079 data: 2,
1080 text: 'G'
1081 },
1082 {
1083 data: 3,
1084 text: 'A'
1085 },
1086 {
1087 data: undefined,
1088 text: 'C'
1089 },
1090 {
1091 data: undefined,
1092 text: 'D'
1093 }
1094 ], 'Order matches expected order, using the current sortValue in $.data()' );
1095
1096 } );
1097
1098 tableTest( 'T10115: sort numbers with commas (ascending)',
1099 [ 'Numbers' ], numbers, numbersAsc,
1100 function ( $table ) {
1101 $table.tablesorter();
1102 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
1103 }
1104 );
1105
1106 tableTest( 'T10115: sort numbers with commas (descending)',
1107 [ 'Numbers' ], numbers, reversed( numbersAsc ),
1108 function ( $table ) {
1109 $table.tablesorter();
1110 $table.find( '.headerSort:eq(0)' ).trigger( 'click' ).trigger( 'click' );
1111 }
1112 );
1113 // TODO add numbers sorting tests for T10115 with a different language
1114
1115 QUnit.test( 'T34888 - Tables inside a tableheader cell', function ( assert ) {
1116 var $table;
1117 $table = $(
1118 '<table class="sortable" id="mw-bug-32888">' +
1119 '<tr><th>header<table id="mw-bug-32888-2">' +
1120 '<tr><th>1</th><th>2</th></tr>' +
1121 '</table></th></tr>' +
1122 '<tr><td>A</td></tr>' +
1123 '<tr><td>B</td></tr>' +
1124 '</table>'
1125 );
1126 $table.tablesorter();
1127
1128 assert.strictEqual(
1129 $table.find( '> thead:eq(0) > tr > th.headerSort' ).length,
1130 1,
1131 'Child tables inside a headercell should not interfere with sortable headers (T34888)'
1132 );
1133 assert.strictEqual(
1134 $( '#mw-bug-32888-2' ).find( 'th.headerSort' ).length,
1135 0,
1136 'The headers of child tables inside a headercell should not be sortable themselves (T34888)'
1137 );
1138 } );
1139
1140 tableTest(
1141 'Correct date sorting I',
1142 [ 'date' ],
1143 correctDateSorting1,
1144 correctDateSortingSorted1,
1145 function ( $table ) {
1146 mw.config.set( 'wgDefaultDateFormat', 'mdy' );
1147
1148 $table.tablesorter();
1149 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
1150 }
1151 );
1152
1153 tableTest(
1154 'Correct date sorting II',
1155 [ 'date' ],
1156 correctDateSorting2,
1157 correctDateSortingSorted2,
1158 function ( $table ) {
1159 mw.config.set( 'wgDefaultDateFormat', 'dmy' );
1160
1161 $table.tablesorter();
1162 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
1163 }
1164 );
1165
1166 tableTest(
1167 'ISO date sorting',
1168 [ 'isoDate' ],
1169 isoDateSorting,
1170 isoDateSortingSorted,
1171 function ( $table ) {
1172 mw.config.set( 'wgDefaultDateFormat', 'dmy' );
1173
1174 $table.tablesorter();
1175 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
1176 }
1177 );
1178
1179 QUnit.test( 'Sorting images using alt text', function ( assert ) {
1180 var $table = $(
1181 '<table class="sortable">' +
1182 '<tr><th>THEAD</th></tr>' +
1183 '<tr><td><img alt="2"/></td></tr>' +
1184 '<tr><td>1</td></tr>' +
1185 '</table>'
1186 );
1187 $table.tablesorter().find( '.headerSort:eq(0)' ).trigger( 'click' );
1188
1189 assert.strictEqual(
1190 $table.find( 'td' ).first().text(),
1191 '1',
1192 'Applied correct sorting order'
1193 );
1194 } );
1195
1196 QUnit.test( 'Sorting images using alt text (complex)', function ( assert ) {
1197 var $table = $(
1198 '<table class="sortable">' +
1199 '<tr><th>THEAD</th></tr>' +
1200 '<tr><td><img alt="D" />A</td></tr>' +
1201 '<tr><td>CC</td></tr>' +
1202 '<tr><td><a><img alt="A" /></a>F</tr>' +
1203 '<tr><td><img alt="A" /><strong>E</strong></tr>' +
1204 '<tr><td><strong><img alt="A" />D</strong></tr>' +
1205 '<tr><td><img alt="A" />C</tr>' +
1206 '</table>'
1207 );
1208 $table.tablesorter().find( '.headerSort:eq(0)' ).trigger( 'click' );
1209
1210 assert.strictEqual(
1211 $table.find( 'td' ).text(),
1212 'CDEFCCA',
1213 'Applied correct sorting order'
1214 );
1215 } );
1216
1217 QUnit.test( 'Sorting images using alt text (with format autodetection)', function ( assert ) {
1218 var $table = $(
1219 '<table class="sortable">' +
1220 '<tr><th>THEAD</th></tr>' +
1221 '<tr><td><img alt="1" />7</td></tr>' +
1222 '<tr><td>1<img alt="6" /></td></tr>' +
1223 '<tr><td>5</td></tr>' +
1224 '<tr><td>4</td></tr>' +
1225 '</table>'
1226 );
1227 $table.tablesorter().find( '.headerSort:eq(0)' ).trigger( 'click' );
1228
1229 assert.strictEqual(
1230 $table.find( 'td' ).text(),
1231 '4517',
1232 'Applied correct sorting order'
1233 );
1234 } );
1235
1236 QUnit.test( 'T40911 - The row with the largest amount of columns should receive the sort indicators', function ( assert ) {
1237 var $table = $(
1238 '<table class="sortable">' +
1239 '<thead>' +
1240 '<tr><th rowspan="2" id="A1">A1</th><th colspan="2">B2a</th></tr>' +
1241 '<tr><th id="B2b">B2b</th><th id="C2b">C2b</th></tr>' +
1242 '</thead>' +
1243 '<tr><td>A</td><td>Aa</td><td>Ab</td></tr>' +
1244 '<tr><td>B</td><td>Ba</td><td>Bb</td></tr>' +
1245 '</table>'
1246 );
1247 $table.tablesorter();
1248
1249 assert.strictEqual(
1250 $table.find( '#A1' ).attr( 'class' ),
1251 'headerSort',
1252 'The first column of the first row should be sortable'
1253 );
1254 assert.strictEqual(
1255 $table.find( '#B2b' ).attr( 'class' ),
1256 'headerSort',
1257 'The th element of the 2nd row of the 2nd column should be sortable'
1258 );
1259 assert.strictEqual(
1260 $table.find( '#C2b' ).attr( 'class' ),
1261 'headerSort',
1262 'The th element of the 2nd row of the 3rd column should be sortable'
1263 );
1264 } );
1265
1266 QUnit.test( 'rowspans in table headers should prefer the last row when rows are equal in length', function ( assert ) {
1267 var $table = $(
1268 '<table class="sortable">' +
1269 '<thead>' +
1270 '<tr><th rowspan="2" id="A1">A1</th><th>B2a</th></tr>' +
1271 '<tr><th id="B2b">B2b</th></tr>' +
1272 '</thead>' +
1273 '<tr><td>A</td><td>Aa</td></tr>' +
1274 '<tr><td>B</td><td>Ba</td></tr>' +
1275 '</table>'
1276 );
1277 $table.tablesorter();
1278
1279 assert.strictEqual(
1280 $table.find( '#A1' ).attr( 'class' ),
1281 'headerSort',
1282 'The first column of the first row should be sortable'
1283 );
1284 assert.strictEqual(
1285 $table.find( '#B2b' ).attr( 'class' ),
1286 'headerSort',
1287 'The th element of the 2nd row of the 2nd column should be sortable'
1288 );
1289 } );
1290
1291 QUnit.test( 'holes in the table headers should not throw JS errors', function ( assert ) {
1292 var $table = $(
1293 '<table class="sortable">' +
1294 '<thead>' +
1295 '<tr><th id="A1">A1</th><th>B1</th><th id="C1" rowspan="2">C1</th></tr>' +
1296 '<tr><th id="A2">A2</th></tr>' +
1297 '</thead>' +
1298 '<tr><td>A</td><td>Aa</td><td>Aaa</td></tr>' +
1299 '<tr><td>B</td><td>Ba</td><td>Bbb</td></tr>' +
1300 '</table>'
1301 );
1302 $table.tablesorter();
1303 assert.strictEqual( $table.find( '#A2' ).data( 'headerIndex' ),
1304 undefined,
1305 'A2 should not be a sort header'
1306 );
1307 assert.strictEqual( $table.find( '#C1' ).data( 'headerIndex' ),
1308 2,
1309 'C1 should be a sort header'
1310 );
1311 } );
1312
1313 // T55527
1314 QUnit.test( 'td cells in thead should not be taken into account for longest row calculation', function ( assert ) {
1315 var $table = $(
1316 '<table class="sortable">' +
1317 '<thead>' +
1318 '<tr><th id="A1">A1</th><th>B1</th><td id="C1">C1</td></tr>' +
1319 '<tr><th id="A2">A2</th><th>B2</th><th id="C2">C2</th></tr>' +
1320 '</thead>' +
1321 '</table>'
1322 );
1323 $table.tablesorter();
1324 assert.strictEqual( $table.find( '#C2' ).data( 'headerIndex' ),
1325 2,
1326 'C2 should be a sort header'
1327 );
1328 assert.strictEqual( $table.find( '#C1' ).data( 'headerIndex' ),
1329 undefined,
1330 'C1 should not be a sort header'
1331 );
1332 } );
1333
1334 // T43889 - exploding rowspans in more complex cases
1335 tableTestHTML(
1336 'Rowspan exploding with row headers',
1337 '<table class="sortable">' +
1338 '<thead><tr><th id="sortme">n</th><th>foo</th><th>bar</th><th>baz</th></tr></thead>' +
1339 '<tbody>' +
1340 '<tr><td>1</td><th rowspan="2">foo</th><td rowspan="2">bar</td><td>baz</td></tr>' +
1341 '<tr><td>2</td><td>baz</td></tr>' +
1342 '</tbody></table>',
1343 [
1344 [ '1', 'foo', 'bar', 'baz' ],
1345 [ '2', 'foo', 'bar', 'baz' ]
1346 ]
1347 );
1348
1349 // T55211 - exploding rowspans in more complex cases
1350 QUnit.test(
1351 'Rowspan exploding with row headers and colspans', function ( assert ) {
1352 var $table = $( '<table class="sortable">' +
1353 '<thead><tr><th rowspan="2">n</th><th colspan="2">foo</th><th rowspan="2">baz</th></tr>' +
1354 '<tr><th>foo</th><th>bar</th></tr></thead>' +
1355 '<tbody>' +
1356 '<tr><td>1</td><td>foo</td><td>bar</td><td>baz</td></tr>' +
1357 '<tr><td>2</td><td>foo</td><td>bar</td><td>baz</td></tr>' +
1358 '</tbody></table>' );
1359
1360 $table.tablesorter();
1361 assert.strictEqual( $table.find( 'tr:eq(1) th:eq(1)' ).data( 'headerIndex' ),
1362 2,
1363 'Incorrect index of sort header'
1364 );
1365 }
1366 );
1367
1368 tableTestHTML(
1369 'Rowspan exploding with colspanned cells',
1370 '<table class="sortable">' +
1371 '<thead><tr><th id="sortme">n</th><th>foo</th><th>bar</th><th>baz</th></tr></thead>' +
1372 '<tbody>' +
1373 '<tr><td>1</td><td>foo</td><td>bar</td><td rowspan="2">baz</td></tr>' +
1374 '<tr><td>2</td><td colspan="2">foobar</td></tr>' +
1375 '</tbody></table>',
1376 [
1377 [ '1', 'foo', 'bar', 'baz' ],
1378 [ '2', 'foobar', 'baz' ]
1379 ]
1380 );
1381
1382 tableTestHTML(
1383 'Rowspan exploding with colspanned cells (2)',
1384 '<table class="sortable">' +
1385 '<thead><tr><th>n</th><th>foo</th><th>bar</th><th>baz</th><th id="sortme">n2</th></tr></thead>' +
1386 '<tbody>' +
1387 '<tr><td>1</td><td>foo</td><td>bar</td><td rowspan="2">baz</td><td>2</td></tr>' +
1388 '<tr><td>2</td><td colspan="2">foobar</td><td>1</td></tr>' +
1389 '</tbody></table>',
1390 [
1391 [ '2', 'foobar', 'baz', '1' ],
1392 [ '1', 'foo', 'bar', 'baz', '2' ]
1393 ]
1394 );
1395
1396 tableTestHTML(
1397 'Rowspan exploding with rightmost rows spanning most',
1398 '<table class="sortable">' +
1399 '<thead><tr><th id="sortme">n</th><th>foo</th><th>bar</th></tr></thead>' +
1400 '<tbody>' +
1401 '<tr><td>1</td><td rowspan="2">foo</td><td rowspan="4">bar</td></tr>' +
1402 '<tr><td>2</td></tr>' +
1403 '<tr><td>3</td><td rowspan="2">foo</td></tr>' +
1404 '<tr><td>4</td></tr>' +
1405 '</tbody></table>',
1406 [
1407 [ '1', 'foo', 'bar' ],
1408 [ '2', 'foo', 'bar' ],
1409 [ '3', 'foo', 'bar' ],
1410 [ '4', 'foo', 'bar' ]
1411 ]
1412 );
1413
1414 tableTestHTML(
1415 'Rowspan exploding with rightmost rows spanning most (2)',
1416 '<table class="sortable">' +
1417 '<thead><tr><th id="sortme">n</th><th>foo</th><th>bar</th><th>baz</th></tr></thead>' +
1418 '<tbody>' +
1419 '<tr><td>1</td><td rowspan="2">foo</td><td rowspan="4">bar</td><td>baz</td></tr>' +
1420 '<tr><td>2</td><td>baz</td></tr>' +
1421 '<tr><td>3</td><td rowspan="2">foo</td><td>baz</td></tr>' +
1422 '<tr><td>4</td><td>baz</td></tr>' +
1423 '</tbody></table>',
1424 [
1425 [ '1', 'foo', 'bar', 'baz' ],
1426 [ '2', 'foo', 'bar', 'baz' ],
1427 [ '3', 'foo', 'bar', 'baz' ],
1428 [ '4', 'foo', 'bar', 'baz' ]
1429 ]
1430 );
1431
1432 tableTestHTML(
1433 'Rowspan exploding with row-and-colspanned cells',
1434 '<table class="sortable">' +
1435 '<thead><tr><th id="sortme">n</th><th>foo1</th><th>foo2</th><th>bar</th><th>baz</th></tr></thead>' +
1436 '<tbody>' +
1437 '<tr><td>1</td><td rowspan="2">foo1</td><td rowspan="2">foo2</td><td rowspan="4">bar</td><td>baz</td></tr>' +
1438 '<tr><td>2</td><td>baz</td></tr>' +
1439 '<tr><td>3</td><td colspan="2" rowspan="2">foo</td><td>baz</td></tr>' +
1440 '<tr><td>4</td><td>baz</td></tr>' +
1441 '</tbody></table>',
1442 [
1443 [ '1', 'foo1', 'foo2', 'bar', 'baz' ],
1444 [ '2', 'foo1', 'foo2', 'bar', 'baz' ],
1445 [ '3', 'foo', 'bar', 'baz' ],
1446 [ '4', 'foo', 'bar', 'baz' ]
1447 ]
1448 );
1449
1450 tableTestHTML(
1451 'Rowspan exploding with uneven rowspan layout',
1452 '<table class="sortable">' +
1453 '<thead><tr><th id="sortme">n</th><th>foo1</th><th>foo2</th><th>foo3</th><th>bar</th><th>baz</th></tr></thead>' +
1454 '<tbody>' +
1455 '<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>' +
1456 '<tr><td>2</td><td rowspan="3">bar</td><td>baz</td></tr>' +
1457 '<tr><td>3</td><td rowspan="2">foo1</td><td rowspan="2">foo2</td><td rowspan="2">foo3</td><td>baz</td></tr>' +
1458 '<tr><td>4</td><td>baz</td></tr>' +
1459 '</tbody></table>',
1460 [
1461 [ '1', 'foo1', 'foo2', 'foo3', 'bar', 'baz' ],
1462 [ '2', 'foo1', 'foo2', 'foo3', 'bar', 'baz' ],
1463 [ '3', 'foo1', 'foo2', 'foo3', 'bar', 'baz' ],
1464 [ '4', 'foo1', 'foo2', 'foo3', 'bar', 'baz' ]
1465 ]
1466 );
1467
1468 QUnit.test( 'T105731 - incomplete rows in table body', function ( assert ) {
1469 var $table, parsers;
1470 $table = $(
1471 '<table class="sortable">' +
1472 '<tr><th>A</th><th>B</th></tr>' +
1473 '<tr><td>3</td></tr>' +
1474 '<tr><td>1</td><td>2</td></tr>' +
1475 '</table>'
1476 );
1477 $table.tablesorter();
1478 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
1479 // now the first row have 2 columns
1480 $table.find( '.headerSort:eq(1)' ).trigger( 'click' );
1481
1482 parsers = $table.data( 'tablesorter' ).config.parsers;
1483
1484 assert.strictEqual(
1485 parsers.length,
1486 2,
1487 'detectParserForColumn() detect 2 parsers'
1488 );
1489
1490 assert.strictEqual(
1491 parsers[ 1 ].id,
1492 'number',
1493 'detectParserForColumn() detect parser.id "number" for second column'
1494 );
1495
1496 assert.strictEqual(
1497 parsers[ 1 ].format( $table.find( 'tbody > tr > td:eq(1)' ).text() ),
1498 -Infinity,
1499 'empty cell is sorted as number -Infinity'
1500 );
1501 } );
1502
1503 QUnit.test( 'bug T114721 - use of expand-child class', function ( assert ) {
1504 var $table, parsers;
1505 $table = $(
1506 '<table class="sortable">' +
1507 '<tr><th>A</th><th>B</th></tr>' +
1508 '<tr><td>b</td><td>4</td></tr>' +
1509 '<tr class="expand-child"><td colspan="2">some text follow b</td></tr>' +
1510 '<tr><td>a</td><td>2</td></tr>' +
1511 '<tr class="expand-child"><td colspan="2">some text follow a</td></tr>' +
1512 '<tr class="expand-child"><td colspan="2">more text</td></tr>' +
1513 '</table>'
1514 );
1515 $table.tablesorter();
1516 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
1517
1518 assert.deepEqual(
1519 tableExtract( $table ),
1520 [
1521 [ 'a', '2' ],
1522 [ 'some text follow a' ],
1523 [ 'more text' ],
1524 [ 'b', '4' ],
1525 [ 'some text follow b' ]
1526 ],
1527 'row with expand-child class follow above row'
1528 );
1529
1530 parsers = $table.data( 'tablesorter' ).config.parsers;
1531 assert.strictEqual(
1532 parsers[ 1 ].id,
1533 'number',
1534 'detectParserForColumn() detect parser.id "number" for second column'
1535 );
1536 } );
1537 QUnit.test( 'T29745 - References ignored in sortkey', function ( assert ) {
1538 var $table, parsers;
1539 $table = $(
1540 '<table class="sortable">' +
1541 '<tr><th>A</th></tr>' +
1542 '<tr><td>10</td></tr>' +
1543 '<tr><td>2<sup class="reference"><a href="#cite_note-1">[1]</a></sup></td></tr>' +
1544 '</table>'
1545 );
1546 $table.tablesorter();
1547 $table.find( '.headerSort:eq(0)' ).trigger( 'click' );
1548
1549 assert.deepEqual(
1550 tableExtract( $table ),
1551 [
1552 [ '2[1]' ],
1553 [ '10' ]
1554 ],
1555 'References ignored in sortkey'
1556 );
1557
1558 parsers = $table.data( 'tablesorter' ).config.parsers;
1559 assert.strictEqual(
1560 parsers[ 0 ].id,
1561 'number',
1562 'detectParserForColumn() detect parser.id "number"'
1563 );
1564 } );
1565
1566 }() );