r87243 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r87242‎ | r87243 | r87244 >
Date:11:31, 2 May 2011
Author:diebuche
Status:resolved (Comments)
Tags:
Comment:
Make jquery.tablesorter more resilient by checking multiple cells before assuming a type. Fixes Bug 28775
Modified paths:
  • /trunk/phase3/resources/jquery/jquery.tablesorter.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/jquery/jquery.tablesorter.js
@@ -108,7 +108,7 @@
109109 }
110110
111111 if (p === false) {
112 - p = detectParserForColumn( table, rows, -1, i );
 112+ p = detectParserForColumn( table, rows, i );
113113 }
114114 // if ( table.config.debug ) {
115115 // console.log( "column:" + i + " parser:" + p.id + "\n" );
@@ -119,40 +119,51 @@
120120 return list;
121121 }
122122
123 - function detectParserForColumn( table, rows, rowIndex, cellIndex ) {
 123+ function detectParserForColumn( table, rows, cellIndex ) {
124124 var l = parsers.length,
125 - node = false,
126 - nodeValue = false,
127 - keepLooking = true;
128 - while ( nodeValue == '' && keepLooking ) {
129 - rowIndex++;
130 - if ( rows[rowIndex] ) {
131 - node = getNodeFromRowAndCellIndex( rows, rowIndex, cellIndex );
132 - nodeValue = trimAndGetNodeText( node );
133 - // if ( table.config.debug ) {
134 - // console.log( 'Checking if value was empty on row:' + rowIndex );
135 - // }
 125+ nodeValue,
 126+ // Start with 1 because 0 is the fallback parser
 127+ i = 1,
 128+ rowIndex = 0,
 129+ concurrent = 0,
 130+ needed = (rows.length > 4 ) ? 5 : rows.length;
 131+ while( i<l ) {
 132+ nodeValue = getTextFromRowAndCellIndex( rows, rowIndex, cellIndex );
 133+ if ( nodeValue != '') {
 134+ if ( parsers[i].is( nodeValue, table ) ) {
 135+ concurrent++;
 136+ rowIndex++;
 137+ if (concurrent >= needed ) {
 138+ // Confirmed the parser for multiple cells, let's return it
 139+ return parsers[i];
 140+ }
 141+ } else {
 142+ // Check next parser, reset rows
 143+ i++;
 144+ rowIndex = 0;
 145+ }
136146 } else {
137 - keepLooking = false;
 147+ // Empty cell
 148+ rowIndex++;
 149+ if ( rowIndex > rows.length ) {
 150+ rowIndex = 0;
 151+ i++;
 152+ }
138153 }
139154 }
140 - for ( var i = 1; i < l; i++ ) {
141 - if ( parsers[i].is( nodeValue, table, node ) ) {
142 - return parsers[i];
143 - }
144 - }
 155+
145156 // 0 is always the generic parser ( text )
146157 return parsers[0];
147158 }
148 -
149 - function getNodeFromRowAndCellIndex( rows, rowIndex, cellIndex ) {
150 - return rows[rowIndex].cells[cellIndex];
 159+
 160+ function getTextFromRowAndCellIndex( rows, rowIndex, cellIndex ) {
 161+ if ( rows[rowIndex] && rows[rowIndex].cells[cellIndex] ) {
 162+ return $.trim( getElementText( rows[rowIndex].cells[cellIndex] ) );
 163+ } else {
 164+ return '';
 165+ }
151166 }
152 -
153 - function trimAndGetNodeText( node ) {
154 - return $.trim( getElementText( node ) );
155 - }
156 -
 167+
157168 function getParserById( name ) {
158169 var l = parsers.length;
159170 for ( var i = 0; i < l; i++ ) {
@@ -726,17 +737,6 @@
727738 } );
728739
729740 ts.addParser( {
730 - id: "number",
731 - is: function ( s, table ) {
732 - return $.tablesorter.numberRegex.test( $.trim(s ));
733 - },
734 - format: function (s) {
735 - return $.tablesorter.formatDigit(s);
736 - },
737 - type: "numeric"
738 - } );
739 -
740 - ts.addParser( {
741741 id: "currency",
742742 is: function (s) {
743743 return ts.rgx.currency[0].test(s);
@@ -842,4 +842,15 @@
843843 },
844844 type: "numeric"
845845 } );
 846+ ts.addParser( {
 847+ id: "number",
 848+ is: function ( s, table ) {
 849+ return $.tablesorter.numberRegex.test( $.trim(s ));
 850+ },
 851+ format: function (s) {
 852+ return $.tablesorter.formatDigit(s);
 853+ },
 854+ type: "numeric"
 855+ } );
 856+
846857 } )( jQuery );
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r90619Followup r87243: qunit test cases for bug 28775 (German-style date sorting)brion22:24, 22 June 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r86088Completely rewritten table sorting script....diebuche21:47, 14 April 2011

Comments

#Comment by Brion VIBBER (talk | contribs)   22:45, 7 June 2011

Looks ok offhand, but especially since this is fixing a known regression, it should really come with some test cases.

Reopened bug 28775 w/ needs-unittests keyword & added some notes on https://bugzilla.wikimedia.org/show_bug.cgi?id=28775#c2

#Comment by Brion VIBBER (talk | contribs)   23:18, 7 June 2011

Going ahead and marking this a fixme since regression testing is a) very wise and b) it shouldn't be hard for one of us to stuff it in sometime in the next few days. :)

#Comment by Brion VIBBER (talk | contribs)   20:13, 22 June 2011

Added needs-js-test keyword on this rev; also note that the parent r86088 and friends seem to have a lot of serious problems, so the whole lot may be reverted.

#Comment by Brion VIBBER (talk | contribs)   22:09, 22 June 2011

The biggest problems from the patch set have been cleared up, so just needs test cases; I'll whip this up from the example on the bug.

#Comment by TheDJ (talk | contribs)   22:16, 22 June 2011

Some useful testcases would be

  • colspan/rowspan
  • forced data-sort-type on a column

I don't think our version supports initial sorting, otherwise that might be nice too. http://tablesorter.com/docs/example-meta-sort-list.html

#Comment by Brion VIBBER (talk | contribs)   22:26, 22 June 2011

....and done in r90619.

Status & tagging log