Page MenuHomePhabricator

ts_resortTable inconsistent trimming makes date sorting fragile
Closed, ResolvedPublic

Description

Author: henry.ayoola

Description:
In file http://images.wikia.com/common/releases_200901.4/skins/monobook/allinone.js?5134 ts_resortTable does a trim:

itm=itm.replace(/^[\s\xa0]+/,"").replace(/[\s\xa0]+$/,"");

before using itm to work out which comparison function to use. However, it doesn't then trim the keyText when building the array to sort, and nor does ts_dateToSortKey.

Consider the simple table

{| class="wikitable sortable"
! Date || Event

-
01 Apr 2000Something happened
-
02 Apr 2000Something else happened
}

This will result in table data cells with spaces:

<table class="wikitable sortable">

<tr>
<th> Date </th><th> Event
</th></tr>
<tr>
<td> 01 Apr 2000 </td><td> Something happened
</td></tr>
<tr>
<td> 02 Apr 2000 </td><td> Something else happened

</td></tr></table>

Because of the trim, ts_resortTable will detect that the first column contains a date, but when ts_datetoSortKey is passed ' 01 Apr 2000 ', of length 13 characters, it will fail to match any of the three known formats and so the sort key will be "00000000". As a result attempting to sort by the first column won't change the order at all (stable sort and every row generates the same sort key).

The result is that sorting is very fragile. The simple fix is to append after the line

var keyText=ts_getInnerText(row.cells[column]);

a further line

keyText=keyText.replace(/^[\s\xa0]+/,"").replace(/[\s\xa0]+$/,"");


Version: unspecified
Severity: normal

Details

Reference
bz17139

Event Timeline

bzimport raised the priority of this task from to Medium.Nov 21 2014, 10:25 PM
bzimport set Reference to bz17139.

david.griffiths wrote:

Was just about to open this same issue when I came across this. BTW my fix was to add this as the first line of ts_dateToSortKey:

date = date.replace(/^\s+|\s+$/g,""); // trim white space

Oh, just noticed that this is reported against a different file. My issue was in skins/common/wikibits.js.

Confirmed with $wgUseTidy off. Peeking...

Done in r53495 -- added the trim on the sort key preprocessor call as well as the selection.