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 * @returns {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;
40 // Use the cached version if possible
41 if ( profileCache[nav.userAgent] === undefined ) {
48 // Name of browsers or layout engines we don't recognize
50 // Generic version digit
52 // Strings found in user agent strings that need to be conformed
53 wildUserAgents = ['Opera', 'Navigator', 'Minefield', 'KHTML', 'Chrome', 'PLAYSTATION 3', 'Iceweasel'],
54 // Translations for conforming user agent strings
55 userAgentTranslations = [
56 // Tons of browsers lie about being something they are not
57 [/(Firefox|MSIE|KHTML,?\slike\sGecko|Konqueror)/, ''],
58 // Chrome lives in the shadow of Safari still
59 ['Chrome Safari', 'Chrome'],
60 // KHTML is the layout engine not the browser - LIES!
61 ['KHTML', 'Konqueror'],
62 // Firefox nightly builds
63 ['Minefield', 'Firefox'],
64 // This helps keep differnt versions consistent
65 ['Navigator', 'Netscape'],
66 // This prevents version extraction issues, otherwise translation would happen later
67 ['PLAYSTATION 3', 'PS3']
69 // Strings which precede a version number in a user agent string - combined and used as match 1 in
70 // version detectection
72 'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'netscape6', 'opera', 'version', 'konqueror',
73 'lynx', 'msie', 'safari', 'ps3', 'android'
75 // Used as matches 2, 3 and 4 in version extraction - 3 is used as actual version number
76 versionSuffix = '(\\/|\\;?\\s|)([a-z0-9\\.\\+]*?)(\\;|dev|rel|\\)|\\s|$)',
77 // Names of known browsers
79 'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'konqueror', 'lynx', 'msie', 'opera',
80 'safari', 'ipod', 'iphone', 'blackberry', 'ps3', 'rekonq', 'android'
82 // Tanslations for conforming browser names
83 nameTranslations = [],
84 // Names of known layout engines
85 layouts = ['gecko', 'konqueror', 'msie', 'opera', 'webkit'],
86 // Translations for conforming layout names
87 layoutTranslations = [ ['konqueror', 'khtml'], ['msie', 'trident'], ['opera', 'presto'] ],
88 // Names of supported layout engines for version number
89 layoutVersions = ['applewebkit', 'gecko'],
90 // Names of known operating systems
91 platforms = ['win', 'mac', 'linux', 'sunos', 'solaris', 'iphone'],
92 // Translations for conforming operating system names
93 platformTranslations = [ ['sunos', 'solaris'] ],
98 * Performs multiple replacements on a string
100 translate = function ( source, translations ) {
102 for ( i = 0; i < translations.length; i++ ) {
103 source = source.replace( translations[i][0], translations[i][1] );
118 if ( match = new RegExp( '(' + wildUserAgents.join( '|' ) + ')' ).exec( ua ) ) {
119 // Takes a userAgent string and translates given text into something we can more easily work with
120 ua = translate( ua, userAgentTranslations );
122 // Everything will be in lowercase from now on
123 ua = ua.toLowerCase();
127 if ( match = new RegExp( '(' + names.join( '|' ) + ')' ).exec( ua ) ) {
128 name = translate( match[1], nameTranslations );
130 if ( match = new RegExp( '(' + layouts.join( '|' ) + ')' ).exec( ua ) ) {
131 layout = translate( match[1], layoutTranslations );
133 if ( match = new RegExp( '(' + layoutVersions.join( '|' ) + ')\\\/(\\d+)').exec( ua ) ) {
134 layoutversion = parseInt( match[2], 10 );
136 if ( match = new RegExp( '(' + platforms.join( '|' ) + ')' ).exec( nav.platform.toLowerCase() ) ) {
137 platform = translate( match[1], platformTranslations );
139 if ( match = new RegExp( '(' + versionPrefixes.join( '|' ) + ')' + versionSuffix ).exec( ua ) ) {
143 /* Edge Cases -- did I mention about how user agent string lie? */
145 // Decode Safari's crazy 400+ version numbers
146 if ( name.match( /safari/ ) && version > 400 ) {
149 // Expose Opera 10's lies about being Opera 9.8
150 if ( name === 'opera' && version >= 9.8) {
151 match = ua.match( /version\/([0-9\.]*)/i );
152 if ( match && match[1] ) {
158 versionNumber = parseFloat( version, 10 ) || 0.0;
162 profileCache[nav.userAgent] = {
165 layoutVersion: layoutversion,
168 versionBase: ( version !== x ? Math.floor( versionNumber ).toString() : x ),
169 versionNumber: versionNumber
172 return profileCache[nav.userAgent];
176 * Checks the current browser against a support map object.
178 * A browser map is in the following format:
180 * // Multiple rules with configurable operators
181 * 'msie': [['>=', 7], ['!=', 9]],
182 * // Match no versions
184 * // Match any version
188 * It can optionally be split into ltr/rtl sections:
196 * // rules are not inherited from ltr
201 * @param {Object} map Browser support map
202 * @param {Object} [profile] A client-profile object
203 * @param {boolean} [exactMatchOnly=false] Only return true if the browser is matched, otherwise
204 * returns true if the browser is not found.
206 * @returns {boolean} The current browser is in the support map
208 test: function ( map, profile, exactMatchOnly ) {
209 /*jshint evil: true */
211 var conditions, dir, i, op, val;
212 profile = $.isPlainObject( profile ) ? profile : $.client.profile();
213 if ( map.ltr && map.rtl ) {
214 dir = $( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
217 // Check over each browser condition to determine if we are running in a compatible client
218 if ( typeof map !== 'object' || map[profile.name] === undefined ) {
219 // Not found, return true if exactMatchOnly not set, false otherwise
220 return !exactMatchOnly;
222 conditions = map[profile.name];
223 if ( conditions === false ) {
227 if ( conditions === null ) {
228 // Match all versions
231 for ( i = 0; i < conditions.length; i++ ) {
232 op = conditions[i][0];
233 val = conditions[i][1];
234 if ( typeof val === 'string' ) {
235 if ( !( eval( 'profile.version' + op + '"' + val + '"' ) ) ) {
238 } else if ( typeof val === 'number' ) {
239 if ( !( eval( 'profile.versionNumber' + op + val ) ) ) {