Bartosz Dziewoński has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/95962


Change subject: jquery.client: Component-wise version comparison in #test with 
strings
......................................................................

jquery.client: Component-wise version comparison in #test with strings

Version '1.10' is larger than '1.2'.

Change-Id: Ic6f3a848645bd05af4b2fdaa29eb58b8aa8f2571
---
M resources/jquery/jquery.client.js
M tests/qunit/suites/resources/jquery/jquery.client.test.js
2 files changed, 64 insertions(+), 18 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/62/95962/1

diff --git a/resources/jquery/jquery.client.js 
b/resources/jquery/jquery.client.js
index 5a95dc5..f35782b 100644
--- a/resources/jquery/jquery.client.js
+++ b/resources/jquery/jquery.client.js
@@ -190,6 +190,10 @@
                /**
                 * Checks the current browser against a support map object.
                 *
+                * Version numbers passed as numeric values will be compared 
like numbers (1.2 > 1.11).
+                * Version numbers passed as string values will be compared 
using a simple component-wise
+                * algorithm, similar to PHP's version_compare ('1.2' < '1.11').
+                *
                 * A browser map is in the following format:
                 * {
                 *   // Multiple rules with configurable operators
@@ -223,7 +227,7 @@
                test: function ( map, profile, exactMatchOnly ) {
                        /*jshint evil: true */
 
-                       var conditions, dir, i, op, val;
+                       var conditions, dir, i, op, val, j, pieceVersion, 
pieceVal, compare;
                        profile = $.isPlainObject( profile ) ? profile : 
$.client.profile();
                        if ( map.ltr && map.rtl ) {
                                dir = $( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
@@ -247,7 +251,30 @@
                                op = conditions[i][0];
                                val = conditions[i][1];
                                if ( typeof val === 'string' ) {
-                                       if ( !( eval( 'profile.version' + op + 
'"' + val + '"' ) ) ) {
+                                       // Perform a component-wise comparison 
of versions, similar to PHP's version_compare
+                                       // but simpler. '1.11' is larger than 
'1.2'.
+                                       pieceVersion = 
profile.version.toString().split( '.' );
+                                       pieceVal = val.split( '.' );
+                                       // Extend with zeroes to equal length
+                                       while ( pieceVersion.length < 
pieceVal.length ) {
+                                               pieceVersion.push( '0' );
+                                       }
+                                       while ( pieceVal.length < 
pieceVersion.length ) {
+                                               pieceVal.push( '0' );
+                                       }
+                                       // Compare components
+                                       compare = 0;
+                                       for ( j = 0; j < pieceVersion.length; 
j++ ) {
+                                               if ( Number( pieceVersion[j] ) 
< Number( pieceVal[j] ) ) {
+                                                       compare = -1;
+                                                       break;
+                                               } else if ( Number( 
pieceVersion[j] ) > Number( pieceVal[j] ) ) {
+                                                       compare = 1;
+                                                       break;
+                                               }
+                                       }
+                                       // compare will be -1, 0 or 1, 
depending on comparison result
+                                       if ( !( eval( '' + compare + op + '0' ) 
) ) {
                                                return false;
                                        }
                                } else if ( typeof val === 'number' ) {
diff --git a/tests/qunit/suites/resources/jquery/jquery.client.test.js 
b/tests/qunit/suites/resources/jquery/jquery.client.test.js
index 4c7c302..acd6ca3 100644
--- a/tests/qunit/suites/resources/jquery/jquery.client.test.js
+++ b/tests/qunit/suites/resources/jquery/jquery.client.test.js
@@ -396,7 +396,7 @@
                                        rtl: true
                                }
                        },
-                       // Bug #34924
+                       // Rekonq (bug 34924)
                        'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.34 
(KHTML, like Gecko) rekonq Safari/534.34': {
                                title: 'Rekonq',
                                platform: 'Linux i686',
@@ -413,30 +413,49 @@
                                        ltr: true,
                                        rtl: true
                                }
+                       },
+                       // Konqueror
+                       'Mozilla/5.0 (X11; Linux i686) KHTML/4.9.1 (like Gecko) 
Konqueror/4.9': {
+                               title: 'Konqueror',
+                               platform: 'Linux i686',
+                               profile: {
+                                       name: 'konqueror',
+                                       layout: 'khtml',
+                                       layoutVersion: 'unknown',
+                                       platform: 'linux',
+                                       version: '4.9.1',
+                                       versionBase: '4',
+                                       versionNumber: 4.9
+                               },
+                               wikiEditor: {
+                                       // '4.9' is less than '4.11'.
+                                       ltr: false,
+                                       rtl: false
+                               }
                        }
                },
                testMap = {
-                       // Example from WikiEditor
-                       // Make sure to use raw numbers, a string like "7.0" 
would fail on a
-                       // version 10 browser since in string comparaison "10" 
is before "7.0" :)
+                       // Example from WikiEditor (modified with spoof 
Konqueror addition)
                        'ltr': {
-                               'msie': [['>=', 7.0]],
-                               'firefox': [['>=', 2]],
-                               'opera': [['>=', 9.6]],
-                               'safari': [['>=', 3]],
-                               'chrome': [['>=', 3]],
-                               'netscape': [['>=', 9]],
+                               'msie': [['>=', '7.0']],
+                               'firefox': [['>=', '2']],
+                               'opera': [['>=', '9.6']],
+                               'safari': [['>=', '3']],
+                               'chrome': [['>=', '3']],
+                               'netscape': [['>=', '9']],
+                               'konqueror': [['>=', '4.11']],
                                'blackberry': false,
                                'ipod': false,
                                'iphone': false
                        },
                        'rtl': {
-                               'msie': [['>=', 8]],
-                               'firefox': [['>=', 2]],
-                               'opera': [['>=', 9.6]],
-                               'safari': [['>=', 3]],
-                               'chrome': [['>=', 3]],
-                               'netscape': [['>=', 9]],
+                               'msie': [['>=', '8']],
+                               'firefox': [['>=', '2']],
+                               'opera': [['>=', '9.6']],
+                               'safari': [['>=', '3']],
+                               'chrome': [['>=', '3']],
+                               'netscape': [['>=', '9']],
+                               'konqueror': [['>=', '4.11']],
                                'blackberry': false,
                                'ipod': false,
                                'iphone': false

-- 
To view, visit https://gerrit.wikimedia.org/r/95962
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic6f3a848645bd05af4b2fdaa29eb58b8aa8f2571
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Bartosz Dziewoński <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to