Add language Doteli (dty)
[lhc/web/wiklou.git] / resources / lib / jquery.client / jquery.client.js
1 /*!
2 * jQuery Client v1.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 */
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 /*jshint boss:true */
46
47 if ( nav === undefined ) {
48 nav = window.navigator;
49 }
50
51 // Use the cached version if possible
52 if ( profileCache[ nav.userAgent + '|' + nav.platform ] !== undefined ) {
53 return profileCache[ nav.userAgent + '|' + nav.platform ];
54 }
55
56 var
57 versionNumber,
58 key = nav.userAgent + '|' + nav.platform,
59
60 // Configuration
61
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'] ],
108
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 },
120
121 // Pre-processing
122
123 ua = nav.userAgent,
124 match,
125 name = uk,
126 layout = uk,
127 layoutversion = uk,
128 platform = uk,
129 version = x;
130
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();
137
138 // Extraction
139
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 }
155
156 // Edge Cases -- did I mention about how user agent string lie?
157
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 IE 12's different lies about not being IE
186 if ( name === 'chrome' && ( match = ua.match( /\bedge\/([0-9\.]*)/ ) ) ) {
187 name = 'msie';
188 version = match[1];
189 layout = 'edge';
190 layoutversion = parseInt( match[1], 10 );
191 }
192 // And Amazon Silk's lies about being Android on mobile or Safari on desktop
193 if ( match = ua.match( /\bsilk\/([0-9.\-_]*)/ ) ) {
194 if ( match[1] ) {
195 name = 'silk';
196 version = match[1];
197 }
198 }
199
200 versionNumber = parseFloat( version, 10 ) || 0.0;
201
202 // Caching
203
204 return profileCache[ key ] = {
205 name: name,
206 layout: layout,
207 layoutVersion: layoutversion,
208 platform: platform,
209 version: version,
210 versionBase: ( version !== x ? Math.floor( versionNumber ).toString() : x ),
211 versionNumber: versionNumber
212 };
213 },
214
215 /**
216 * Checks the current browser against a support map object.
217 *
218 * Version numbers passed as numeric values will be compared like numbers (1.2 > 1.11).
219 * Version numbers passed as string values will be compared using a simple component-wise
220 * algorithm, similar to PHP's version_compare ('1.2' < '1.11').
221 *
222 * A browser map is in the following format:
223 *
224 * {
225 * // Multiple rules with configurable operators
226 * 'msie': [['>=', 7], ['!=', 9]],
227 * // Match no versions
228 * 'iphone': false,
229 * // Match any version
230 * 'android': null
231 * }
232 *
233 * It can optionally be split into ltr/rtl sections:
234 *
235 * {
236 * 'ltr': {
237 * 'android': null,
238 * 'iphone': false
239 * },
240 * 'rtl': {
241 * 'android': false,
242 * // rules are not inherited from ltr
243 * 'iphone': false
244 * }
245 * }
246 *
247 * @param {Object} map Browser support map
248 * @param {Object} [profile] A client-profile object
249 * @param {boolean} [exactMatchOnly=false] Only return true if the browser is matched, otherwise
250 * returns true if the browser is not found.
251 *
252 * @return {boolean} The current browser is in the support map
253 */
254 test: function ( map, profile, exactMatchOnly ) {
255 /*jshint evil:true */
256
257 var conditions, dir, i, op, val, j, pieceVersion, pieceVal, compare;
258 profile = $.isPlainObject( profile ) ? profile : $.client.profile();
259 if ( map.ltr && map.rtl ) {
260 dir = $( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
261 map = map[dir];
262 }
263 // Check over each browser condition to determine if we are running in a compatible client
264 if ( typeof map !== 'object' || map[profile.name] === undefined ) {
265 // Not found, return true if exactMatchOnly not set, false otherwise
266 return !exactMatchOnly;
267 }
268 conditions = map[profile.name];
269 if ( conditions === false ) {
270 // Match no versions
271 return false;
272 }
273 if ( conditions === null ) {
274 // Match all versions
275 return true;
276 }
277 for ( i = 0; i < conditions.length; i++ ) {
278 op = conditions[i][0];
279 val = conditions[i][1];
280 if ( typeof val === 'string' ) {
281 // Perform a component-wise comparison of versions, similar to PHP's version_compare
282 // but simpler. '1.11' is larger than '1.2'.
283 pieceVersion = profile.version.toString().split( '.' );
284 pieceVal = val.split( '.' );
285 // Extend with zeroes to equal length
286 while ( pieceVersion.length < pieceVal.length ) {
287 pieceVersion.push( '0' );
288 }
289 while ( pieceVal.length < pieceVersion.length ) {
290 pieceVal.push( '0' );
291 }
292 // Compare components
293 compare = 0;
294 for ( j = 0; j < pieceVersion.length; j++ ) {
295 if ( Number( pieceVersion[j] ) < Number( pieceVal[j] ) ) {
296 compare = -1;
297 break;
298 } else if ( Number( pieceVersion[j] ) > Number( pieceVal[j] ) ) {
299 compare = 1;
300 break;
301 }
302 }
303 // compare will be -1, 0 or 1, depending on comparison result
304 if ( !( eval( String( compare + op + '0' ) ) ) ) {
305 return false;
306 }
307 } else if ( typeof val === 'number' ) {
308 if ( !( eval( 'profile.versionNumber' + op + val ) ) ) {
309 return false;
310 }
311 }
312 }
313
314 return true;
315 }
316 };
317 }( jQuery ) );