9 * @var {Object} profileCache Keyed by userAgent string,
10 * value is the parsed $.client.profile object for that user agent.
12 var profileCache
= {};
19 * Get an object containing information about the client.
21 * @param {Object} nav An object with atleast a 'userAgent' and 'platform' key.
22 * Defaults to the global Navigator object.
23 * @return {Object} The resulting client object will be in the following format:
27 * 'layoutVersion': 20101026,
31 * 'versionNumber': 3.5,
34 profile: function ( nav
) {
35 /*jshint boss: true */
37 if ( nav
=== undefined ) {
38 nav
= window
.navigator
;
41 // Use the cached version if possible
42 if ( profileCache
[ nav
.userAgent
+ '|' + nav
.platform
] !== undefined ) {
43 return profileCache
[ nav
.userAgent
+ '|' + nav
.platform
];
48 key
= nav
.userAgent
+ '|' + nav
.platform
,
52 // Name of browsers or layout engines we don't recognize
54 // Generic version digit
56 // Strings found in user agent strings that need to be conformed
57 wildUserAgents
= ['Opera', 'Navigator', 'Minefield', 'KHTML', 'Chrome', 'PLAYSTATION 3', 'Iceweasel'],
58 // Translations for conforming user agent strings
59 userAgentTranslations
= [
60 // Tons of browsers lie about being something they are not
61 [/(Firefox|MSIE|KHTML,?\slike\sGecko|Konqueror)/, ''],
62 // Chrome lives in the shadow of Safari still
63 ['Chrome Safari', 'Chrome'],
64 // KHTML is the layout engine not the browser - LIES!
65 ['KHTML', 'Konqueror'],
66 // Firefox nightly builds
67 ['Minefield', 'Firefox'],
68 // This helps keep different versions consistent
69 ['Navigator', 'Netscape'],
70 // This prevents version extraction issues, otherwise translation would happen later
71 ['PLAYSTATION 3', 'PS3']
73 // Strings which precede a version number in a user agent string - combined and used as
74 // match 1 in version detection
76 'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'netscape6', 'opera', 'version', 'konqueror',
77 'lynx', 'msie', 'safari', 'ps3', 'android'
79 // Used as matches 2, 3 and 4 in version extraction - 3 is used as actual version number
80 versionSuffix
= '(\\/|\\;?\\s|)([a-z0-9\\.\\+]*?)(\\;|dev|rel|\\)|\\s|$)',
81 // Names of known browsers
83 'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'konqueror', 'lynx', 'msie', 'opera',
84 'safari', 'ipod', 'iphone', 'blackberry', 'ps3', 'rekonq', 'android'
86 // Tanslations for conforming browser names
87 nameTranslations
= [],
88 // Names of known layout engines
89 layouts
= ['gecko', 'konqueror', 'msie', 'trident', 'opera', 'webkit'],
90 // Translations for conforming layout names
91 layoutTranslations
= [ ['konqueror', 'khtml'], ['msie', 'trident'], ['opera', 'presto'] ],
92 // Names of supported layout engines for version number
93 layoutVersions
= ['applewebkit', 'gecko', 'trident'],
94 // Names of known operating systems
95 platforms
= ['win', 'wow64', 'mac', 'linux', 'sunos', 'solaris', 'iphone'],
96 // Translations for conforming operating system names
97 platformTranslations
= [ ['sunos', 'solaris'], ['wow64', 'win'] ],
102 * Performs multiple replacements on a string
104 translate = function ( source
, translations
) {
106 for ( i
= 0; i
< translations
.length
; i
++ ) {
107 source
= source
.replace( translations
[i
][0], translations
[i
][1] );
122 if ( match
= new RegExp( '(' + wildUserAgents
.join( '|' ) + ')' ).exec( ua
) ) {
123 // Takes a userAgent string and translates given text into something we can more easily work with
124 ua
= translate( ua
, userAgentTranslations
);
126 // Everything will be in lowercase from now on
127 ua
= ua
.toLowerCase();
131 if ( match
= new RegExp( '(' + names
.join( '|' ) + ')' ).exec( ua
) ) {
132 name
= translate( match
[1], nameTranslations
);
134 if ( match
= new RegExp( '(' + layouts
.join( '|' ) + ')' ).exec( ua
) ) {
135 layout
= translate( match
[1], layoutTranslations
);
137 if ( match
= new RegExp( '(' + layoutVersions
.join( '|' ) + ')\\\/(\\d+)').exec( ua
) ) {
138 layoutversion
= parseInt( match
[2], 10 );
140 if ( match
= new RegExp( '(' + platforms
.join( '|' ) + ')' ).exec( nav
.platform
.toLowerCase() ) ) {
141 platform
= translate( match
[1], platformTranslations
);
143 if ( match
= new RegExp( '(' + versionPrefixes
.join( '|' ) + ')' + versionSuffix
).exec( ua
) ) {
147 /* Edge Cases -- did I mention about how user agent string lie? */
149 // Decode Safari's crazy 400+ version numbers
150 if ( name
=== 'safari' && version
> 400 ) {
153 // Expose Opera 10's lies about being Opera 9.8
154 if ( name
=== 'opera' && version
>= 9.8 ) {
155 match
= ua
.match( /\bversion\/([0-9\.]*)/ );
156 if ( match
&& match
[1] ) {
162 // And Opera 15's lies about being Chrome
163 if ( name
=== 'chrome' && ( match
= ua
.match( /\bopr\/([0-9\.]*)/ ) ) ) {
169 // And IE 11's lies about being not being IE
170 if ( layout
=== 'trident' && layoutversion
>= 7 && ( match
= ua
.match( /\brv[ :\/]([0-9\.]*)/ ) ) ) {
176 // And Amazon Silk's lies about being Android on mobile or Safari on desktop
177 if ( match
= ua
.match( /\bsilk\/([0-9.\-_]*)/ ) ) {
184 versionNumber
= parseFloat( version
, 10 ) || 0.0;
188 return profileCache
[ key
] = {
191 layoutVersion
: layoutversion
,
194 versionBase
: ( version
!== x
? Math
.floor( versionNumber
).toString() : x
),
195 versionNumber
: versionNumber
200 * Checks the current browser against a support map object.
202 * Version numbers passed as numeric values will be compared like numbers (1.2 > 1.11).
203 * Version numbers passed as string values will be compared using a simple component-wise
204 * algorithm, similar to PHP's version_compare ('1.2' < '1.11').
206 * A browser map is in the following format:
208 * // Multiple rules with configurable operators
209 * 'msie': [['>=', 7], ['!=', 9]],
210 * // Match no versions
212 * // Match any version
216 * It can optionally be split into ltr/rtl sections:
224 * // rules are not inherited from ltr
229 * @param {Object} map Browser support map
230 * @param {Object} [profile] A client-profile object
231 * @param {boolean} [exactMatchOnly=false] Only return true if the browser is matched, otherwise
232 * returns true if the browser is not found.
234 * @return {boolean} The current browser is in the support map
236 test: function ( map
, profile
, exactMatchOnly
) {
237 /*jshint evil: true */
239 var conditions
, dir
, i
, op
, val
, j
, pieceVersion
, pieceVal
, compare
;
240 profile
= $.isPlainObject( profile
) ? profile
: $.client
.profile();
241 if ( map
.ltr
&& map
.rtl
) {
242 dir
= $( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
245 // Check over each browser condition to determine if we are running in a compatible client
246 if ( typeof map
!== 'object' || map
[profile
.name
] === undefined ) {
247 // Not found, return true if exactMatchOnly not set, false otherwise
248 return !exactMatchOnly
;
250 conditions
= map
[profile
.name
];
251 if ( conditions
=== false ) {
255 if ( conditions
=== null ) {
256 // Match all versions
259 for ( i
= 0; i
< conditions
.length
; i
++ ) {
260 op
= conditions
[i
][0];
261 val
= conditions
[i
][1];
262 if ( typeof val
=== 'string' ) {
263 // Perform a component-wise comparison of versions, similar to PHP's version_compare
264 // but simpler. '1.11' is larger than '1.2'.
265 pieceVersion
= profile
.version
.toString().split( '.' );
266 pieceVal
= val
.split( '.' );
267 // Extend with zeroes to equal length
268 while ( pieceVersion
.length
< pieceVal
.length
) {
269 pieceVersion
.push( '0' );
271 while ( pieceVal
.length
< pieceVersion
.length
) {
272 pieceVal
.push( '0' );
274 // Compare components
276 for ( j
= 0; j
< pieceVersion
.length
; j
++ ) {
277 if ( Number( pieceVersion
[j
] ) < Number( pieceVal
[j
] ) ) {
280 } else if ( Number( pieceVersion
[j
] ) > Number( pieceVal
[j
] ) ) {
285 // compare will be -1, 0 or 1, depending on comparison result
286 if ( !( eval( '' + compare
+ op
+ '0' ) ) ) {
289 } else if ( typeof val
=== 'number' ) {
290 if ( !( eval( 'profile.versionNumber' + op
+ val
) ) ) {