9 * @var profileCache {Object} 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 nav {Object} 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
) {
37 if ( nav
=== undefined ) {
38 nav
= window
.navigator
;
40 // Use the cached version if possible
41 if ( profileCache
[nav
.userAgent
] === undefined ) {
45 // Name of browsers or layout engines we don't recognize
47 // Generic version digit
49 // Strings found in user agent strings that need to be conformed
50 var wildUserAgents
= ['Opera', 'Navigator', 'Minefield', 'KHTML', 'Chrome', 'PLAYSTATION 3'];
51 // Translations for conforming user agent strings
52 var userAgentTranslations
= [
53 // Tons of browsers lie about being something they are not
54 [/(Firefox|MSIE|KHTML,\slike\sGecko|Konqueror)/, ''],
55 // Chrome lives in the shadow of Safari still
56 ['Chrome Safari', 'Chrome'],
57 // KHTML is the layout engine not the browser - LIES!
58 ['KHTML', 'Konqueror'],
59 // Firefox nightly builds
60 ['Minefield', 'Firefox'],
61 // This helps keep differnt versions consistent
62 ['Navigator', 'Netscape'],
63 // This prevents version extraction issues, otherwise translation would happen later
64 ['PLAYSTATION 3', 'PS3']
66 // Strings which precede a version number in a user agent string - combined and used as match 1 in
67 // version detectection
68 var versionPrefixes
= [
69 'camino', 'chrome', 'firefox', 'netscape', 'netscape6', 'opera', 'version', 'konqueror',
70 'lynx', 'msie', 'safari', 'ps3'
72 // Used as matches 2, 3 and 4 in version extraction - 3 is used as actual version number
73 var versionSuffix
= '(\\/|\\;?\\s|)([a-z0-9\\.\\+]*?)(\\;|dev|rel|\\)|\\s|$)';
74 // Names of known browsers
76 'camino', 'chrome', 'firefox', 'netscape', 'konqueror', 'lynx', 'msie', 'opera',
77 'safari', 'ipod', 'iphone', 'blackberry', 'ps3', 'rekonq'
79 // Tanslations for conforming browser names
80 var nameTranslations
= [];
81 // Names of known layout engines
82 var layouts
= ['gecko', 'konqueror', 'msie', 'opera', 'webkit'];
83 // Translations for conforming layout names
84 var layoutTranslations
= [['konqueror', 'khtml'], ['msie', 'trident'], ['opera', 'presto']];
85 // Names of supported layout engines for version number
86 var layoutVersions
= ['applewebkit', 'gecko'];
87 // Names of known operating systems
88 var platforms
= ['win', 'mac', 'linux', 'sunos', 'solaris', 'iphone'];
89 // Translations for conforming operating system names
90 var platformTranslations
= [['sunos', 'solaris']];
95 * Performs multiple replacements on a string
97 var translate = function ( source
, translations
) {
99 for ( i
= 0; i
< translations
.length
; i
++ ) {
100 source
= source
.replace( translations
[i
][0], translations
[i
][1] );
107 var ua
= nav
.userAgent
,
115 if ( match
= new RegExp( '(' + wildUserAgents
.join( '|' ) + ')' ).exec( ua
) ) {
116 // Takes a userAgent string and translates given text into something we can more easily work with
117 ua
= translate( ua
, userAgentTranslations
);
119 // Everything will be in lowercase from now on
120 ua
= ua
.toLowerCase();
124 if ( match
= new RegExp( '(' + names
.join( '|' ) + ')' ).exec( ua
) ) {
125 name
= translate( match
[1], nameTranslations
);
127 if ( match
= new RegExp( '(' + layouts
.join( '|' ) + ')' ).exec( ua
) ) {
128 layout
= translate( match
[1], layoutTranslations
);
130 if ( match
= new RegExp( '(' + layoutVersions
.join( '|' ) + ')\\\/(\\d+)').exec( ua
) ) {
131 layoutversion
= parseInt( match
[2], 10 );
133 if ( match
= new RegExp( '(' + platforms
.join( '|' ) + ')' ).exec( nav
.platform
.toLowerCase() ) ) {
134 platform
= translate( match
[1], platformTranslations
);
136 if ( match
= new RegExp( '(' + versionPrefixes
.join( '|' ) + ')' + versionSuffix
).exec( ua
) ) {
140 /* Edge Cases -- did I mention about how user agent string lie? */
142 // Decode Safari's crazy 400+ version numbers
143 if ( name
.match( /safari/ ) && version
> 400 ) {
146 // Expose Opera 10's lies about being Opera 9.8
147 if ( name
=== 'opera' && version
>= 9.8) {
148 version
= ua
.match( /version\/([0-9\.]*)/i )[1] || 10;
150 var versionNumber
= parseFloat( version
, 10 ) || 0.0;
154 profileCache
[nav
.userAgent
] = {
157 layoutVersion
: layoutversion
,
160 versionBase
: ( version
!== x
? Math
.floor( versionNumber
).toString() : x
),
161 versionNumber
: versionNumber
164 return profileCache
[nav
.userAgent
];
168 * Checks the current browser against a support map object to determine if the browser has been black-listed or
169 * not. If the browser was not configured specifically it is assumed to work. It is assumed that the body
170 * element is classified as either "ltr" or "rtl". If neither is set, "ltr" is assumed.
172 * A browser map is in the following format:
175 * // Multiple rules with configurable operators
176 * 'msie': [['>=', 7], ['!=', 9]],
177 * // Blocked entirely
181 * // Test against a string
182 * 'msie': [['!==', '8.1.2.3']],
183 * // RTL rules do not fall through to LTR rules, you must explicity set each of them
188 * @param map {Object} Browser support map
189 * @param profile {Object} (optional) a client-profile object.
191 * @return Boolean true if browser known or assumed to be supported, false if blacklisted
193 test: function ( map
, profile
) {
194 /*jshint evil:true */
196 var conditions
, dir
, i
, op
, val
;
197 profile
= $.isPlainObject( profile
) ? profile
: $.client
.profile();
199 dir
= $( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
200 // Check over each browser condition to determine if we are running in a compatible client
201 if ( typeof map
[dir
] !== 'object' || map
[dir
][profile
.name
] === undefined ) {
202 // Unknown, so we assume it's working
205 conditions
= map
[dir
][profile
.name
];
206 for ( i
= 0; i
< conditions
.length
; i
++ ) {
207 op
= conditions
[i
][0];
208 val
= conditions
[i
][1];
209 if ( val
=== false ) {
212 if ( typeof val
=== 'string' ) {
213 if ( !( eval( 'profile.version' + op
+ '"' + val
+ '"' ) ) ) {
216 } else if ( typeof val
=== 'number' ) {
217 if ( !( eval( 'profile.versionNumber' + op
+ val
) ) ) {