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