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