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
) {
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
];
56 key
= nav
.userAgent
+ '|' + nav
.platform
,
60 // Name of browsers or layout engines we don't recognize
62 // Generic version digit
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' ]
81 // Strings which precede a version number in a user agent string - combined and used as
82 // match 1 in version detection
84 'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'netscape6', 'opera', 'version', 'konqueror',
85 'lynx', 'msie', 'safari', 'ps3', 'android'
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
91 'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'konqueror', 'lynx', 'msie', 'opera',
92 'safari', 'ipod', 'iphone', 'blackberry', 'ps3', 'rekonq', 'android'
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' ] ],
108 * 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 easily work with
131 ua
= translate( ua
, userAgentTranslations
);
133 // Everything will be in lowercase from now on
134 ua
= ua
.toLowerCase();
138 if ( match
= new RegExp( '(' + names
.join( '|' ) + ')' ).exec( ua
) ) {
139 name
= translate( match
[ 1 ], nameTranslations
);
141 if ( match
= new RegExp( '(' + layouts
.join( '|' ) + ')' ).exec( ua
) ) {
142 layout
= translate( match
[ 1 ], layoutTranslations
);
144 if ( match
= new RegExp( '(' + layoutVersions
.join( '|' ) + ')\\/(\\d+)' ).exec( ua
) ) {
145 layoutversion
= parseInt( match
[ 2 ], 10 );
147 if ( match
= new RegExp( '(' + platforms
.join( '|' ) + ')' ).exec( nav
.platform
.toLowerCase() ) ) {
148 platform
= translate( match
[ 1 ], platformTranslations
);
150 if ( match
= new RegExp( '(' + versionPrefixes
.join( '|' ) + ')' + versionSuffix
).exec( ua
) ) {
151 version
= match
[ 3 ];
154 // Edge Cases -- did I mention about how user agent string lie?
156 // Decode Safari's crazy 400+ version numbers
157 if ( name
=== 'safari' && version
> 400 ) {
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 ];
169 // And Opera 15's lies about being Chrome
170 if ( name
=== 'chrome' && ( match
= ua
.match( /\bopr\/([0-9.]*)/ ) ) ) {
173 version
= match
[ 1 ];
176 // And IE 11's lies about being not being IE
177 if ( layout
=== 'trident' && layoutversion
>= 7 && ( match
= ua
.match( /\brv[ :/]([0-9.]*)/ ) ) ) {
180 version
= match
[ 1 ];
183 // And MS Edge's lies about being Chrome
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.]*)/ ) ) ) {
189 version
= match
[ 1 ];
191 layoutversion
= parseInt( match
[ 1 ], 10 );
193 // And Amazon Silk's lies about being Android on mobile or Safari on desktop
194 if ( match
= ua
.match( /\bsilk\/([0-9.\-_]*)/ ) ) {
197 version
= match
[ 1 ];
201 versionNumber
= parseFloat( version
, 10 ) || 0.0;
205 return profileCache
[ key
] = {
208 layoutVersion
: layoutversion
,
211 versionBase
: ( version
!== x
? Math
.floor( versionNumber
).toString() : x
),
212 versionNumber
: versionNumber
217 * Checks the current browser against a support map object.
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').
223 * A browser map is in the following format:
226 * // Multiple rules with configurable operators
227 * 'msie': [['>=', 7], ['!=', 9]],
228 * // Match no versions
230 * // Match any version
234 * It can optionally be split into ltr/rtl sections:
243 * // rules are not inherited from ltr
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.
253 * @return {boolean} The current browser is in the support map
255 test: function ( map
, profile
, exactMatchOnly
) {
256 /* eslint-disable no-eval */
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';
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
;
269 conditions
= map
[ profile
.name
];
270 if ( conditions
=== false ) {
274 if ( conditions
=== null ) {
275 // Match all versions
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' );
290 while ( pieceVal
.length
< pieceVersion
.length
) {
291 pieceVal
.push( '0' );
293 // Compare components
295 for ( j
= 0; j
< pieceVersion
.length
; j
++ ) {
296 if ( Number( pieceVersion
[ j
] ) < Number( pieceVal
[ j
] ) ) {
299 } else if ( Number( pieceVersion
[ j
] ) > Number( pieceVal
[ j
] ) ) {
304 // compare will be -1, 0 or 1, depending on comparison result
305 if ( !( eval( String( compare
+ op
+ '0' ) ) ) ) {
308 } else if ( typeof val
=== 'number' ) {
309 if ( !( eval( 'profile.versionNumber' + op
+ val
) ) ) {