From 9427b08d08fb8896ce19ebd1db27a48f974561e4 Mon Sep 17 00:00:00 2001 From: Derk-Jan Hartman Date: Sat, 18 Feb 2012 14:35:14 +0000 Subject: [PATCH] Add support to tablesorter to handle IP/CIDR notation This fixes bug 34475 --- RELEASE-NOTES-1.20 | 1 + resources/jquery/jquery.tablesorter.js | 14 +++-- .../jquery/jquery.tablesorter.test.js | 53 +++++++++++++++++++ 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/RELEASE-NOTES-1.20 b/RELEASE-NOTES-1.20 index 5090672bdf..aa5edcb840 100644 --- a/RELEASE-NOTES-1.20 +++ b/RELEASE-NOTES-1.20 @@ -19,6 +19,7 @@ production. preference for the non-default skin to look at something using the default skin. * (bug 31417) New ID mw-content-text around the actual page text, without categories, contentSub, ... The same div often also contains the class mw-content-ltr/rtl. +* (bug 34475) Add support for IP/CIDR notation to tablesorter === Bug fixes in 1.20 === * (bug 30245) Use the correct way to construct a log page title. diff --git a/resources/jquery/jquery.tablesorter.js b/resources/jquery/jquery.tablesorter.js index ea86b64e48..ca9971b2b9 100644 --- a/resources/jquery/jquery.tablesorter.js +++ b/resources/jquery/jquery.tablesorter.js @@ -484,7 +484,7 @@ } ts.rgx = { IPAddress: [ - new RegExp( /^\d{1,3}[\.]\d{1,3}[\.]\d{1,3}[\.]\d{1,3}$/) + new RegExp( /^\d{1,3}[\.]\d{1,3}[\.]\d{1,3}[\.]\d{1,3}(\/\d{1,3})?$/) ], currency: [ new RegExp( /^[£$€?.]/), @@ -767,9 +767,15 @@ }, format: function( s ) { var a = s.split( '.' ), - r = '', - l = a.length; - for ( var i = 0; i < l; i++ ) { + r = ''; + if( a.length == 4 ) { + var cidr = a[3].split('/'); + if (cidr.length > 1 ) { + a[3] = cidr[0]; + a[4] = cidr[1]; + } else a[4] = '000'; + } + for ( var i = 0; i < a.length; i++ ) { var item = a[i]; if ( item.length == 1 ) { r += '00' + item; diff --git a/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js b/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js index 95284c7d72..ec34102749 100644 --- a/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js @@ -236,6 +236,38 @@ var ipv4Sorted = [ ['204.204.132.158'], ['247.240.82.209'] ]; +var ipv4CIDR = [ + // Some randomly generated fake IPs + ['45.238.27.109/36'], + ['170.38.91.162/36'], + ['247.240.82.209/36'], + ['204.204.132.158/24'], + ['170.38.91.162/24'] +]; +var ipv4CIDRSorted = [ + // Sort order should go octet by octet + ['45.238.27.109/36'], + ['170.38.91.162/24'], + ['170.38.91.162/36'], + ['204.204.132.158/24'], + ['247.240.82.209/36'] +]; +var ipv4Mixed = [ + // Some randomly generated fake IPs + ['45.238.27.109'], + ['170.38.91.162'], + ['247.240.82.209'], + ['204.204.132.158/24'], + ['170.38.91.162/24'] +]; +var ipv4MixedSorted = [ + // Sort order should go octet by octet + ['45.238.27.109'], + ['170.38.91.162'], + ['170.38.91.162/24'], + ['204.204.132.158/24'], + ['247.240.82.209'] +]; tableTest( 'Bug 17141: IPv4 address sorting', @@ -257,6 +289,27 @@ tableTest( $table.find( '.headerSort:eq(0)' ).click().click(); } ); +tableTest( + 'Bug 34475: IPv4/CIDR address sorting', + ['IP'], + ipv4CIDR, + ipv4CIDRSorted, + function( $table ) { + $table.tablesorter(); + $table.find( '.headerSort:eq(0)' ).click(); + } +); + +tableTest( + 'Bug 34475: Mixed IPv4 and IP/CIDR address sorting', + ['IP'], + ipv4Mixed, + ipv4MixedSorted, + function( $table ) { + $table.tablesorter(); + $table.find( '.headerSort:eq(0)' ).click(); + } +); var umlautWords = [ // Some words with Umlauts -- 2.20.1