From a46ad5be83073a1c68e5f0c6bec562f6fcc09efc Mon Sep 17 00:00:00 2001 From: Derk-Jan Hartman Date: Tue, 6 Oct 2015 21:21:28 +0200 Subject: [PATCH] jquery.tablesorter: Match clobbered iso dates and fix edgecases * Support dates that have references or other 'postfixes' (T114479) * Match leapseconds, but not invalid seconds. Invalid seconds (61 or higher) will be dropped alltogether. These still are recognized as proper dates, :61 will be treated as a 'postfix'. * We cannot match just YYYY or YYYY-MM, because then many plain numbers would be recognized as dates. Bug: T114479 Change-Id: I65a2f125e6c6480b35c9efd38e1e8b1f898a74b7 --- resources/src/jquery/jquery.tablesorter.js | 9 +++++++-- .../jquery/jquery.tablesorter.parsers.test.js | 10 ++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/resources/src/jquery/jquery.tablesorter.js b/resources/src/jquery/jquery.tablesorter.js index 0eefae6c90..69f8dd8ae4 100644 --- a/resources/src/jquery/jquery.tablesorter.js +++ b/resources/src/jquery/jquery.tablesorter.js @@ -740,7 +740,7 @@ new RegExp( /(https?|ftp|file):\/\// ) ], isoDate: [ - new RegExp( /^([-+]?\d{1,4})-([01]\d)-([0-3]\d)([T\s]((([01]\d|2[0-3])(:?[0-5]\d)?|24:?00)?(:?([0-5]\d))?([.,]\d+)?)([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?/ ), + new RegExp( /^([-+]?\d{1,4})-([01]\d)-([0-3]\d)([T\s]((([01]\d|2[0-3])(:?[0-5]\d)?|24:?00)?(:?([0-5]\d|60))?([.,]\d+)?)([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?/ ), new RegExp( /^([-+]?\d{1,4})-([01]\d)-([0-3]\d)/ ) ], usLongDate: [ @@ -1165,7 +1165,12 @@ return $.tablesorter.formatFloat( 0 ); } } else { - isodate = new Date( $.trim( s ) ); + matches = s.match( ts.rgx.isoDate[ 0 ] ); + if ( matches ) { + isodate = new Date( $.trim( matches[ 0 ] ) ); + } else { + return $.tablesorter.formatFloat( 0 ); + } } return $.tablesorter.formatFloat( ( isodate !== undefined ) ? isodate.getTime() : 0 ); }, diff --git a/tests/qunit/suites/resources/jquery/jquery.tablesorter.parsers.test.js b/tests/qunit/suites/resources/jquery/jquery.tablesorter.parsers.test.js index cab408095a..11ceeea2fc 100644 --- a/tests/qunit/suites/resources/jquery/jquery.tablesorter.parsers.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.tablesorter.parsers.test.js @@ -174,8 +174,8 @@ parserTest( 'Y Dates', 'date', YDates ); ISODates = [ - [ '2000', false, 946684800000, 'Plain 4-digit year' ], - [ '2000-01', false, 946684800000, 'Year with month' ], + [ '2000', false, 0, 'Plain 4-digit year' ], + [ '2000-01', false, 0, 'Year with month' ], [ '2000-01-01', true, 946684800000, 'Year with month and day' ], [ '2000-13-01', true, 0, 'Non existant month' ], [ '2000-01-32', true, 0, 'Non existant day' ], @@ -183,7 +183,7 @@ [ '2000-01-01T12:30:30Z', true, 946729830000, 'Date with a UTC+0 time' ], [ '2000-01-01T24:30:30Z', true, 0, 'Date with invalid hours' ], [ '2000-01-01T12:60:30Z', true, 0, 'Date with invalid minutes' ], - [ '2000-01-01T12:30:61Z', true, 0, 'Date with invalid amount of seconds' ], + [ '2000-01-01T12:30:61Z', true, 946729800000, 'Date with invalid amount of seconds, drops seconds' ], [ '2000-01-01T23:59:59Z', true, 946771199000, 'Edges of time' ], [ '2000-01-01T12:30:30.111Z', true, 946729830111, 'Date with milliseconds' ], [ '2000-01-01T12:30:30.11111Z', true, 946729830111, 'Date with too high precision' ], @@ -192,7 +192,9 @@ [ '2000-01-01T12:30:30+01:30', true, 946724430000, 'Date time in UTC+1:30' ], [ '2000-01-01T12:30:30-01:00', true, 946733430000, 'Date time in UTC-1' ], [ '2000-01-01T12:30:30-01:30', true, 946735230000, 'Date time in UTC-1:30' ], - [ '2000-01-01T12:30:30.111+01:00', true, 946726230111, 'Date time and milliseconds in UTC+1 ' ] + [ '2000-01-01T12:30:30.111+01:00', true, 946726230111, 'Date time and milliseconds in UTC+1 ' ], + [ '2000-01-01Postfix', true, 946684800000, 'Date with appended postfix' ], + [ '2000-01-01 Postfix', true, 946684800000, 'Date with separate postfix' ] /* Disable testcases, because behavior is browser dependant */ /* [ '2000-11-31', true, 0, '31 days in 30 day month' ], -- 2.20.1