3 * https://www.mediawiki.org/wiki/JQuery_Client
5 * Copyright 2010-2019 jquery-client maintainers and other contributors.
6 * Released under the MIT license
7 * http://jquery-client.mit-license.org
11 * User-agent detection
13 * @class jQuery.client
20 * @property {Object} profileCache Keyed by userAgent string,
21 * value is the parsed $.client.profile object for that user agent.
23 var profileCache
= {};
28 * Get an object containing information about the client.
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:
37 * 'layoutVersion': 20101026,
41 * 'versionNumber': 3.5,
44 profile: function ( nav
) {
45 if ( nav
=== undefined ) {
46 nav
= window
.navigator
;
49 // Use the cached version if possible
50 if ( profileCache
[ nav
.userAgent
+ '|' + nav
.platform
] !== undefined ) {
51 return profileCache
[ nav
.userAgent
+ '|' + nav
.platform
];
54 // eslint-disable-next-line vars-on-top
57 key
= nav
.userAgent
+ '|' + nav
.platform
,
61 // Name of browsers or layout engines we don't recognize
63 // Generic version digit
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' ]
83 // Strings which precede a version number in a user agent string - combined and
84 // used as match 1 in version detection
86 'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'netscape6', 'opera', 'version', 'konqueror',
87 'lynx', 'msie', 'safari', 'ps3', 'android'
89 // Used as matches 2, 3 and 4 in version extraction - 3 is used as actual
91 versionSuffix
= '(\\/|\\;?\\s|)([a-z0-9\\.\\+]*?)(\\;|dev|rel|\\)|\\s|$)',
92 // Names of known browsers
94 'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'konqueror', 'lynx', 'msie', 'opera',
95 'safari', 'ipod', 'iphone', 'blackberry', 'ps3', 'rekonq', 'android'
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' ] ],
110 // Performs multiple replacements on a string
111 translate = function ( source
, translations
) {
113 for ( i
= 0; i
< translations
.length
; i
++ ) {
114 source
= source
.replace( translations
[ i
][ 0 ], translations
[ i
][ 1 ] );
129 if ( ( match
= new RegExp( '(' + wildUserAgents
.join( '|' ) + ')' ).exec( ua
) ) ) {
130 // Takes a userAgent string and translates given text into something we can more
132 ua
= translate( ua
, userAgentTranslations
);
134 // Everything will be in lowercase from now on
135 ua
= ua
.toLowerCase();
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
), '' );
144 if ( ( match
= new RegExp( '(' + names
.join( '|' ) + ')' ).exec( ua
) ) ) {
145 name
= translate( match
[ 1 ], nameTranslations
);
147 if ( ( match
= new RegExp( '(' + layouts
.join( '|' ) + ')' ).exec( ua
) ) ) {
148 layout
= translate( match
[ 1 ], layoutTranslations
);
150 if ( ( match
= new RegExp( '(' + layoutVersions
.join( '|' ) + ')\\/(\\d+)' ).exec( ua
) ) ) {
151 layoutversion
= parseInt( match
[ 2 ], 10 );
153 if ( ( match
= new RegExp( '(' + platforms
.join( '|' ) + ')' ).exec( nav
.platform
.toLowerCase() ) ) ) {
154 platform
= translate( match
[ 1 ], platformTranslations
);
156 if ( ( match
= new RegExp( '(' + versionPrefixes
.join( '|' ) + ')' + versionSuffix
).exec( ua
) ) ) {
157 version
= match
[ 3 ];
160 // Edge Cases -- did I mention about how user agent string lie?
162 // Decode Safari's crazy 400+ version numbers
163 if ( name
=== 'safari' && version
> 400 ) {
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 ];
175 // And Opera 15's lies about being Chrome
176 if ( name
=== 'chrome' && ( match
= ua
.match( /\bopr\/([0-9.]*)/ ) ) ) {
179 version
= match
[ 1 ];
182 // And IE 11's lies about being not being IE
183 if ( layout
=== 'trident' && layoutversion
>= 7 && ( match
= ua
.match( /\brv[ :/]([0-9.]*)/ ) ) ) {
186 version
= match
[ 1 ];
189 // And MS Edge's lies about being Chrome
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.]*)/ ) ) ) {
195 version
= match
[ 1 ];
197 layoutversion
= parseInt( match
[ 1 ], 10 );
199 // And Amazon Silk's lies about being Android on mobile or Safari on desktop
200 if ( ( match
= ua
.match( /\bsilk\/([0-9.\-_]*)/ ) ) ) {
203 version
= match
[ 1 ];
207 versionNumber
= parseFloat( version
, 10 ) || 0.0;
210 profileCache
[ key
] = {
213 layoutVersion
: layoutversion
,
216 versionBase
: ( version
!== x
? Math
.floor( versionNumber
).toString() : x
),
217 versionNumber
: versionNumber
220 return profileCache
[ key
];
224 * Checks the current browser against a support map object.
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').
230 * A browser map is in the following format:
233 * // Multiple rules with configurable operators
234 * 'msie': [['>=', 7], ['!=', 9]],
235 * // Match no versions
237 * // Match any version
241 * It can optionally be split into ltr/rtl sections:
250 * // rules are not inherited from ltr
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.
260 * @return {boolean} The current browser is in the support map
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';
269 // Check over each browser condition to determine if we are running in a
271 if ( typeof map
!== 'object' || map
[ profile
.name
] === undefined ) {
272 // Not found, return true if exactMatchOnly not set, false otherwise
273 return !exactMatchOnly
;
275 conditions
= map
[ profile
.name
];
276 if ( conditions
=== false ) {
280 if ( conditions
=== null ) {
281 // Match all versions
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' );
296 while ( pieceVal
.length
< pieceVersion
.length
) {
297 pieceVal
.push( '0' );
299 // Compare components
301 for ( j
= 0; j
< pieceVersion
.length
; j
++ ) {
302 if ( Number( pieceVersion
[ j
] ) < Number( pieceVal
[ j
] ) ) {
305 } else if ( Number( pieceVersion
[ j
] ) > Number( pieceVal
[ j
] ) ) {
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' ) ) ) ) {
315 } else if ( typeof val
=== 'number' ) {
316 // eslint-disable-next-line no-eval
317 if ( !( eval( 'profile.versionNumber' + op
+ val
) ) ) {