ae74a324118bb4e8afeda045ac87631d69589fa5
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', 'lynx',
68 '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', 'safari', 'ipod',
75 'iphone', 'blackberry', 'ps3'
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']];
92 // Performs multiple replacements on a string
93 var translate = function( source
, translations
) {
94 for ( var i
= 0; i
< translations
.length
; i
++ ) {
95 source
= source
.replace( translations
[i
][0], translations
[i
][1] );
102 var ua
= nav
.userAgent
,
110 if ( match
= new RegExp( '(' + wildUserAgents
.join( '|' ) + ')' ).exec( ua
) ) {
111 // Takes a userAgent string and translates given text into something we can more easily work with
112 ua
= translate( ua
, userAgentTranslations
);
114 // Everything will be in lowercase from now on
115 ua
= ua
.toLowerCase();
119 if ( match
= new RegExp( '(' + names
.join( '|' ) + ')' ).exec( ua
) ) {
120 name
= translate( match
[1], nameTranslations
);
122 if ( match
= new RegExp( '(' + layouts
.join( '|' ) + ')' ).exec( ua
) ) {
123 layout
= translate( match
[1], layoutTranslations
);
125 if ( match
= new RegExp( '(' + layoutVersions
.join( '|' ) + ')\\\/(\\d+)').exec( ua
) ) {
126 layoutversion
= parseInt( match
[2], 10 );
128 if ( match
= new RegExp( '(' + platforms
.join( '|' ) + ')' ).exec( nav
.platform
.toLowerCase() ) ) {
129 platform
= translate( match
[1], platformTranslations
);
131 if ( match
= new RegExp( '(' + versionPrefixes
.join( '|' ) + ')' + versionSuffix
).exec( ua
) ) {
135 /* Edge Cases -- did I mention about how user agent string lie? */
137 // Decode Safari's crazy 400+ version numbers
138 if ( name
.match( /safari/ ) && version
> 400 ) {
141 // Expose Opera 10's lies about being Opera 9.8
142 if ( name
=== 'opera' && version
>= 9.8) {
143 version
= ua
.match( /version\/([0-9\.]*)/i )[1] || 10;
145 var versionNumber
= parseFloat( version
, 10 ) || 0.0;
149 profileCache
[nav
.userAgent
] = {
152 'layoutVersion': layoutversion
,
153 'platform': platform
,
155 'versionBase': ( version
!== x
? Math
.floor( versionNumber
).toString() : x
),
156 'versionNumber': versionNumber
159 return profileCache
[nav
.userAgent
];
163 * Checks the current browser against a support map object to determine if the browser has been black-listed or
164 * not. If the browser was not configured specifically it is assumed to work. It is assumed that the body
165 * element is classified as either "ltr" or "rtl". If neither is set, "ltr" is assumed.
167 * A browser map is in the following format:
170 * // Multiple rules with configurable operators
171 * 'msie': [['>=', 7], ['!=', 9]],
172 * // Blocked entirely
176 * // Test against a string
177 * 'msie': [['!==', '8.1.2.3']],
178 * // RTL rules do not fall through to LTR rules, you must explicity set each of them
183 * @param map {Object} Browser support map
184 * @param profile {Object} (optional) a client-profile object.
186 * @return Boolean true if browser known or assumed to be supported, false if blacklisted
188 test: function( map
, profile
) {
189 profile
= $.isPlainObject( profile
) ? profile
: $.client
.profile();
191 var dir
= $( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
192 // Check over each browser condition to determine if we are running in a compatible client
193 if ( typeof map
[dir
] !== 'object' || typeof map
[dir
][profile
.name
] === 'undefined' ) {
194 // Unknown, so we assume it's working
197 var conditions
= map
[dir
][profile
.name
];
198 for ( var i
= 0; i
< conditions
.length
; i
++ ) {
199 var op
= conditions
[i
][0];
200 var val
= conditions
[i
][1];
201 if ( val
=== false ) {
203 } else if ( typeof val
== 'string' ) {
204 if ( !( eval( 'profile.version' + op
+ '"' + val
+ '"' ) ) ) {
207 } else if ( typeof val
== 'number' ) {
208 if ( !( eval( 'profile.versionNumber' + op
+ val
) ) ) {