3 * https://www.mediawiki.org/wiki/JQuery_Client
5 * Copyright 2010-2015 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
) {
47 if ( nav
=== undefined ) {
48 nav
= window
.navigator
;
51 // Use the cached version if possible
52 if ( profileCache
[ nav
.userAgent
+ '|' + nav
.platform
] !== undefined ) {
53 return profileCache
[ nav
.userAgent
+ '|' + nav
.platform
];
58 key
= nav
.userAgent
+ '|' + nav
.platform
,
62 // Name of browsers or layout engines we don't recognize
64 // Generic version digit
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']
83 // Strings which precede a version number in a user agent string - combined and used as
84 // 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 version number
90 versionSuffix
= '(\\/|\\;?\\s|)([a-z0-9\\.\\+]*?)(\\;|dev|rel|\\)|\\s|$)',
91 // Names of known browsers
93 'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'konqueror', 'lynx', 'msie', 'opera',
94 'safari', 'ipod', 'iphone', 'blackberry', 'ps3', 'rekonq', 'android'
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'] ],
110 * Performs multiple replacements on a string
113 translate = function ( source
, translations
) {
115 for ( i
= 0; i
< translations
.length
; i
++ ) {
116 source
= source
.replace( translations
[i
][0], translations
[i
][1] );
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
);
135 // Everything will be in lowercase from now on
136 ua
= ua
.toLowerCase();
140 if ( match
= new RegExp( '(' + names
.join( '|' ) + ')' ).exec( ua
) ) {
141 name
= translate( match
[1], nameTranslations
);
143 if ( match
= new RegExp( '(' + layouts
.join( '|' ) + ')' ).exec( ua
) ) {
144 layout
= translate( match
[1], layoutTranslations
);
146 if ( match
= new RegExp( '(' + layoutVersions
.join( '|' ) + ')\\\/(\\d+)').exec( ua
) ) {
147 layoutversion
= parseInt( match
[2], 10 );
149 if ( match
= new RegExp( '(' + platforms
.join( '|' ) + ')' ).exec( nav
.platform
.toLowerCase() ) ) {
150 platform
= translate( match
[1], platformTranslations
);
152 if ( match
= new RegExp( '(' + versionPrefixes
.join( '|' ) + ')' + versionSuffix
).exec( ua
) ) {
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 ) {
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] ) {
171 // And Opera 15's lies about being Chrome
172 if ( name
=== 'chrome' && ( match
= ua
.match( /\bopr\/([0-9\.]*)/ ) ) ) {
178 // And IE 11's lies about being not being IE
179 if ( layout
=== 'trident' && layoutversion
>= 7 && ( match
= ua
.match( /\brv[ :\/]([0-9\.]*)/ ) ) ) {
185 // And MS Edge's lies about being Chrome
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\.]*)/ ) ) ) {
193 layoutversion
= parseInt( match
[1], 10 );
195 // And Amazon Silk's lies about being Android on mobile or Safari on desktop
196 if ( match
= ua
.match( /\bsilk\/([0-9.\-_]*)/ ) ) {
203 versionNumber
= parseFloat( version
, 10 ) || 0.0;
207 return profileCache
[ key
] = {
210 layoutVersion
: layoutversion
,
213 versionBase
: ( version
!== x
? Math
.floor( versionNumber
).toString() : x
),
214 versionNumber
: versionNumber
219 * Checks the current browser against a support map object.
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').
225 * A browser map is in the following format:
228 * // Multiple rules with configurable operators
229 * 'msie': [['>=', 7], ['!=', 9]],
230 * // Match no versions
232 * // Match any version
236 * It can optionally be split into ltr/rtl sections:
245 * // rules are not inherited from ltr
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.
255 * @return {boolean} The current browser is in the support map
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';
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
;
271 conditions
= map
[profile
.name
];
272 if ( conditions
=== false ) {
276 if ( conditions
=== null ) {
277 // Match all versions
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' );
292 while ( pieceVal
.length
< pieceVersion
.length
) {
293 pieceVal
.push( '0' );
295 // Compare components
297 for ( j
= 0; j
< pieceVersion
.length
; j
++ ) {
298 if ( Number( pieceVersion
[j
] ) < Number( pieceVal
[j
] ) ) {
301 } else if ( Number( pieceVersion
[j
] ) > Number( pieceVal
[j
] ) ) {
306 // compare will be -1, 0 or 1, depending on comparison result
307 if ( !( eval( String( compare
+ op
+ '0' ) ) ) ) {
310 } else if ( typeof val
=== 'number' ) {
311 if ( !( eval( 'profile.versionNumber' + op
+ val
) ) ) {