Make AuthManager::getAuthenticationProvider() public
[mediawiki.git] / resources / lib / jquery.client / jquery.client.js
blob8257d85bc75b9c6e96b1307d051f9294a69ea937
1 /*!
2  * jQuery Client v2.0.0
3  * https://www.mediawiki.org/wiki/JQuery_Client
4  *
5  * Copyright 2010-2015 jquery-client maintainers and other contributors.
6  * Released under the MIT license
7  * http://jquery-client.mit-license.org
8  */
10 /**
11  * User-agent detection
12  *
13  * @class jQuery.client
14  * @singleton
15  */
16 ( function ( $ ) {
18         /**
19          * @private
20          * @property {Object} profileCache Keyed by userAgent string,
21          * value is the parsed $.client.profile object for that user agent.
22          */
23         var profileCache = {};
25         $.client = {
27                 /**
28                  * Get an object containing information about the client.
29                  *
30                  * @param {Object} [nav] An object with a 'userAgent' and 'platform' property.
31                  *  Defaults to the global `navigator` object.
32                  * @return {Object} The resulting client object will be in the following format:
33                  *
34                  *     {
35                  *         'name': 'firefox',
36                  *         'layout': 'gecko',
37                  *         'layoutVersion': 20101026,
38                  *         'platform': 'linux'
39                  *         'version': '3.5.1',
40                  *         'versionBase': '3',
41                  *         'versionNumber': 3.5,
42                  *     }
43                  */
44                 profile: function ( nav ) {
45                         /*jshint boss:true */
47                         if ( nav === undefined ) {
48                                 nav = window.navigator;
49                         }
51                         // Use the cached version if possible
52                         if ( profileCache[ nav.userAgent + '|' + nav.platform ] !== undefined ) {
53                                 return profileCache[ nav.userAgent + '|' + nav.platform ];
54                         }
56                         var
57                                 versionNumber,
58                                 key = nav.userAgent + '|' + nav.platform,
60                                 // Configuration
62                                 // Name of browsers or layout engines we don't recognize
63                                 uk = 'unknown',
64                                 // Generic version digit
65                                 x = 'x',
66                                 // Strings found in user agent strings that need to be conformed
67                                 wildUserAgents = ['Opera', 'Navigator', 'Minefield', 'KHTML', 'Chrome', 'PLAYSTATION 3', 'Iceweasel'],
68                                 // Translations for conforming user agent strings
69                                 userAgentTranslations = [
70                                         // Tons of browsers lie about being something they are not
71                                         [/(Firefox|MSIE|KHTML,?\slike\sGecko|Konqueror)/, ''],
72                                         // Chrome lives in the shadow of Safari still
73                                         ['Chrome Safari', 'Chrome'],
74                                         // KHTML is the layout engine not the browser - LIES!
75                                         ['KHTML', 'Konqueror'],
76                                         // Firefox nightly builds
77                                         ['Minefield', 'Firefox'],
78                                         // This helps keep different versions consistent
79                                         ['Navigator', 'Netscape'],
80                                         // This prevents version extraction issues, otherwise translation would happen later
81                                         ['PLAYSTATION 3', 'PS3']
82                                 ],
83                                 // Strings which precede a version number in a user agent string - combined and used as
84                                 // match 1 in version detection
85                                 versionPrefixes = [
86                                         'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'netscape6', 'opera', 'version', 'konqueror',
87                                         'lynx', 'msie', 'safari', 'ps3', 'android'
88                                 ],
89                                 // Used as matches 2, 3 and 4 in version extraction - 3 is used as actual version number
90                                 versionSuffix = '(\\/|\\;?\\s|)([a-z0-9\\.\\+]*?)(\\;|dev|rel|\\)|\\s|$)',
91                                 // Names of known browsers
92                                 names = [
93                                         'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'konqueror', 'lynx', 'msie', 'opera',
94                                         'safari', 'ipod', 'iphone', 'blackberry', 'ps3', 'rekonq', 'android'
95                                 ],
96                                 // Tanslations for conforming browser names
97                                 nameTranslations = [],
98                                 // Names of known layout engines
99                                 layouts = ['gecko', 'konqueror', 'msie', 'trident', 'edge', 'opera', 'webkit'],
100                                 // Translations for conforming layout names
101                                 layoutTranslations = [ ['konqueror', 'khtml'], ['msie', 'trident'], ['opera', 'presto'] ],
102                                 // Names of supported layout engines for version number
103                                 layoutVersions = ['applewebkit', 'gecko', 'trident', 'edge'],
104                                 // Names of known operating systems
105                                 platforms = ['win', 'wow64', 'mac', 'linux', 'sunos', 'solaris', 'iphone'],
106                                 // Translations for conforming operating system names
107                                 platformTranslations = [ ['sunos', 'solaris'], ['wow64', 'win'] ],
109                                 /**
110                                  * Performs multiple replacements on a string
111                                  * @ignore
112                                  */
113                                 translate = function ( source, translations ) {
114                                         var i;
115                                         for ( i = 0; i < translations.length; i++ ) {
116                                                 source = source.replace( translations[i][0], translations[i][1] );
117                                         }
118                                         return source;
119                                 },
121                                 // Pre-processing
123                                 ua = nav.userAgent,
124                                 match,
125                                 name = uk,
126                                 layout = uk,
127                                 layoutversion = uk,
128                                 platform = uk,
129                                 version = x;
131                         if ( match = new RegExp( '(' + wildUserAgents.join( '|' ) + ')' ).exec( ua ) ) {
132                                 // Takes a userAgent string and translates given text into something we can more easily work with
133                                 ua = translate( ua, userAgentTranslations );
134                         }
135                         // Everything will be in lowercase from now on
136                         ua = ua.toLowerCase();
138                         // Extraction
140                         if ( match = new RegExp( '(' + names.join( '|' ) + ')' ).exec( ua ) ) {
141                                 name = translate( match[1], nameTranslations );
142                         }
143                         if ( match = new RegExp( '(' + layouts.join( '|' ) + ')' ).exec( ua ) ) {
144                                 layout = translate( match[1], layoutTranslations );
145                         }
146                         if ( match = new RegExp( '(' + layoutVersions.join( '|' ) + ')\\\/(\\d+)').exec( ua ) ) {
147                                 layoutversion = parseInt( match[2], 10 );
148                         }
149                         if ( match = new RegExp( '(' + platforms.join( '|' ) + ')' ).exec( nav.platform.toLowerCase() ) ) {
150                                 platform = translate( match[1], platformTranslations );
151                         }
152                         if ( match = new RegExp( '(' + versionPrefixes.join( '|' ) + ')' + versionSuffix ).exec( ua ) ) {
153                                 version = match[3];
154                         }
156                         // Edge Cases -- did I mention about how user agent string lie?
158                         // Decode Safari's crazy 400+ version numbers
159                         if ( name === 'safari' && version > 400 ) {
160                                 version = '2.0';
161                         }
162                         // Expose Opera 10's lies about being Opera 9.8
163                         if ( name === 'opera' && version >= 9.8 ) {
164                                 match = ua.match( /\bversion\/([0-9\.]*)/ );
165                                 if ( match && match[1] ) {
166                                         version = match[1];
167                                 } else {
168                                         version = '10';
169                                 }
170                         }
171                         // And Opera 15's lies about being Chrome
172                         if ( name === 'chrome' && ( match = ua.match( /\bopr\/([0-9\.]*)/ ) ) ) {
173                                 if ( match[1] ) {
174                                         name = 'opera';
175                                         version = match[1];
176                                 }
177                         }
178                         // And IE 11's lies about being not being IE
179                         if ( layout === 'trident' && layoutversion >= 7 && ( match = ua.match( /\brv[ :\/]([0-9\.]*)/ ) ) ) {
180                                 if ( match[1] ) {
181                                         name = 'msie';
182                                         version = match[1];
183                                 }
184                         }
185                         // And MS Edge's lies about being Chrome
186                         //
187                         // It's different enough from classic IE Trident engine that they do this
188                         // to avoid getting caught by MSIE-specific browser sniffing.
189                         if ( name === 'chrome' && ( match = ua.match( /\bedge\/([0-9\.]*)/ ) ) ) {
190                                 name = 'edge';
191                                 version = match[1];
192                                 layout = 'edge';
193                                 layoutversion = parseInt( match[1], 10 );
194                         }
195                         // And Amazon Silk's lies about being Android on mobile or Safari on desktop
196                         if ( match = ua.match( /\bsilk\/([0-9.\-_]*)/ ) ) {
197                                 if ( match[1] ) {
198                                         name = 'silk';
199                                         version = match[1];
200                                 }
201                         }
203                         versionNumber = parseFloat( version, 10 ) || 0.0;
205                         // Caching
207                         return profileCache[ key  ] = {
208                                 name: name,
209                                 layout: layout,
210                                 layoutVersion: layoutversion,
211                                 platform: platform,
212                                 version: version,
213                                 versionBase: ( version !== x ? Math.floor( versionNumber ).toString() : x ),
214                                 versionNumber: versionNumber
215                         };
216                 },
218                 /**
219                  * Checks the current browser against a support map object.
220                  *
221                  * Version numbers passed as numeric values will be compared like numbers (1.2 > 1.11).
222                  * Version numbers passed as string values will be compared using a simple component-wise
223                  * algorithm, similar to PHP's version_compare ('1.2' < '1.11').
224                  *
225                  * A browser map is in the following format:
226                  *
227                  *     {
228                  *         // Multiple rules with configurable operators
229                  *         'msie': [['>=', 7], ['!=', 9]],
230                  *         // Match no versions
231                  *         'iphone': false,
232                  *         // Match any version
233                  *         'android': null
234                  *     }
235                  *
236                  * It can optionally be split into ltr/rtl sections:
237                  *
238                  *     {
239                  *         'ltr': {
240                  *             'android': null,
241                  *             'iphone': false
242                  *         },
243                  *         'rtl': {
244                  *             'android': false,
245                  *             // rules are not inherited from ltr
246                  *             'iphone': false
247                  *         }
248                  *     }
249                  *
250                  * @param {Object} map Browser support map
251                  * @param {Object} [profile] A client-profile object
252                  * @param {boolean} [exactMatchOnly=false] Only return true if the browser is matched, otherwise
253                  * returns true if the browser is not found.
254                  *
255                  * @return {boolean} The current browser is in the support map
256                  */
257                 test: function ( map, profile, exactMatchOnly ) {
258                         /*jshint evil:true */
260                         var conditions, dir, i, op, val, j, pieceVersion, pieceVal, compare;
261                         profile = $.isPlainObject( profile ) ? profile : $.client.profile();
262                         if ( map.ltr && map.rtl ) {
263                                 dir = $( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
264                                 map = map[dir];
265                         }
266                         // Check over each browser condition to determine if we are running in a compatible client
267                         if ( typeof map !== 'object' || map[profile.name] === undefined ) {
268                                 // Not found, return true if exactMatchOnly not set, false otherwise
269                                 return !exactMatchOnly;
270                         }
271                         conditions = map[profile.name];
272                         if ( conditions === false ) {
273                                 // Match no versions
274                                 return false;
275                         }
276                         if ( conditions === null ) {
277                                 // Match all versions
278                                 return true;
279                         }
280                         for ( i = 0; i < conditions.length; i++ ) {
281                                 op = conditions[i][0];
282                                 val = conditions[i][1];
283                                 if ( typeof val === 'string' ) {
284                                         // Perform a component-wise comparison of versions, similar to PHP's version_compare
285                                         // but simpler. '1.11' is larger than '1.2'.
286                                         pieceVersion = profile.version.toString().split( '.' );
287                                         pieceVal = val.split( '.' );
288                                         // Extend with zeroes to equal length
289                                         while ( pieceVersion.length < pieceVal.length ) {
290                                                 pieceVersion.push( '0' );
291                                         }
292                                         while ( pieceVal.length < pieceVersion.length ) {
293                                                 pieceVal.push( '0' );
294                                         }
295                                         // Compare components
296                                         compare = 0;
297                                         for ( j = 0; j < pieceVersion.length; j++ ) {
298                                                 if ( Number( pieceVersion[j] ) < Number( pieceVal[j] ) ) {
299                                                         compare = -1;
300                                                         break;
301                                                 } else if ( Number( pieceVersion[j] ) > Number( pieceVal[j] ) ) {
302                                                         compare = 1;
303                                                         break;
304                                                 }
305                                         }
306                                         // compare will be -1, 0 or 1, depending on comparison result
307                                         if ( !( eval( String( compare + op + '0' ) ) ) ) {
308                                                 return false;
309                                         }
310                                 } else if ( typeof val === 'number' ) {
311                                         if ( !( eval( 'profile.versionNumber' + op + val ) ) ) {
312                                                 return false;
313                                         }
314                                 }
315                         }
317                         return true;
318                 }
319         };
320 }( jQuery ) );