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