11 * @property {Object} profileCache Keyed by userAgent string,
12 * value is the parsed $.client.profile object for that user agent.
14 var profileCache
= {};
19 * Get an object containing information about the client.
21 * @param {Object} [nav] An object with a 'userAgent' and 'platform' property.
22 * Defaults to the global `navigator` object.
23 * @return {Object} The resulting client object will be in the following format:
28 * 'layoutVersion': 20101026,
32 * 'versionNumber': 3.5,
35 profile: function ( nav
) {
36 /*jshint boss: true */
38 if ( nav
=== undefined ) {
39 nav
= window
.navigator
;
42 // Use the cached version if possible
43 if ( profileCache
[ nav
.userAgent
+ '|' + nav
.platform
] !== undefined ) {
44 return profileCache
[ nav
.userAgent
+ '|' + nav
.platform
];
49 key
= nav
.userAgent
+ '|' + nav
.platform
,
53 // Name of browsers or layout engines we don't recognize
55 // Generic version digit
57 // Strings found in user agent strings that need to be conformed
58 wildUserAgents
= ['Opera', 'Navigator', 'Minefield', 'KHTML', 'Chrome', 'PLAYSTATION 3', 'Iceweasel'],
59 // Translations for conforming user agent strings
60 userAgentTranslations
= [
61 // Tons of browsers lie about being something they are not
62 [/(Firefox|MSIE|KHTML,?\slike\sGecko|Konqueror)/, ''],
63 // Chrome lives in the shadow of Safari still
64 ['Chrome Safari', 'Chrome'],
65 // KHTML is the layout engine not the browser - LIES!
66 ['KHTML', 'Konqueror'],
67 // Firefox nightly builds
68 ['Minefield', 'Firefox'],
69 // This helps keep different versions consistent
70 ['Navigator', 'Netscape'],
71 // This prevents version extraction issues, otherwise translation would happen later
72 ['PLAYSTATION 3', 'PS3']
74 // Strings which precede a version number in a user agent string - combined and used as
75 // match 1 in version detection
77 'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'netscape6', 'opera', 'version', 'konqueror',
78 'lynx', 'msie', 'safari', 'ps3', 'android'
80 // Used as matches 2, 3 and 4 in version extraction - 3 is used as actual version number
81 versionSuffix
= '(\\/|\\;?\\s|)([a-z0-9\\.\\+]*?)(\\;|dev|rel|\\)|\\s|$)',
82 // Names of known browsers
84 'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'konqueror', 'lynx', 'msie', 'opera',
85 'safari', 'ipod', 'iphone', 'blackberry', 'ps3', 'rekonq', 'android'
87 // Tanslations for conforming browser names
88 nameTranslations
= [],
89 // Names of known layout engines
90 layouts
= ['gecko', 'konqueror', 'msie', 'trident', 'opera', 'webkit'],
91 // Translations for conforming layout names
92 layoutTranslations
= [ ['konqueror', 'khtml'], ['msie', 'trident'], ['opera', 'presto'] ],
93 // Names of supported layout engines for version number
94 layoutVersions
= ['applewebkit', 'gecko', 'trident'],
95 // Names of known operating systems
96 platforms
= ['win', 'wow64', 'mac', 'linux', 'sunos', 'solaris', 'iphone'],
97 // Translations for conforming operating system names
98 platformTranslations
= [ ['sunos', 'solaris'], ['wow64', 'win'] ],
101 * 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:
209 * // Multiple rules with configurable operators
210 * 'msie': [['>=', 7], ['!=', 9]],
211 * // Match no versions
213 * // Match any version
217 * It can optionally be split into ltr/rtl sections:
226 * // rules are not inherited from ltr
231 * @param {Object} map Browser support map
232 * @param {Object} [profile] A client-profile object
233 * @param {boolean} [exactMatchOnly=false] Only return true if the browser is matched, otherwise
234 * returns true if the browser is not found.
236 * @return {boolean} The current browser is in the support map
238 test: function ( map
, profile
, exactMatchOnly
) {
239 /*jshint evil: true */
241 var conditions
, dir
, i
, op
, val
, j
, pieceVersion
, pieceVal
, compare
;
242 profile
= $.isPlainObject( profile
) ? profile
: $.client
.profile();
243 if ( map
.ltr
&& map
.rtl
) {
244 dir
= $( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
247 // Check over each browser condition to determine if we are running in a compatible client
248 if ( typeof map
!== 'object' || map
[profile
.name
] === undefined ) {
249 // Not found, return true if exactMatchOnly not set, false otherwise
250 return !exactMatchOnly
;
252 conditions
= map
[profile
.name
];
253 if ( conditions
=== false ) {
257 if ( conditions
=== null ) {
258 // Match all versions
261 for ( i
= 0; i
< conditions
.length
; i
++ ) {
262 op
= conditions
[i
][0];
263 val
= conditions
[i
][1];
264 if ( typeof val
=== 'string' ) {
265 // Perform a component-wise comparison of versions, similar to PHP's version_compare
266 // but simpler. '1.11' is larger than '1.2'.
267 pieceVersion
= profile
.version
.toString().split( '.' );
268 pieceVal
= val
.split( '.' );
269 // Extend with zeroes to equal length
270 while ( pieceVersion
.length
< pieceVal
.length
) {
271 pieceVersion
.push( '0' );
273 while ( pieceVal
.length
< pieceVersion
.length
) {
274 pieceVal
.push( '0' );
276 // Compare components
278 for ( j
= 0; j
< pieceVersion
.length
; j
++ ) {
279 if ( Number( pieceVersion
[j
] ) < Number( pieceVal
[j
] ) ) {
282 } else if ( Number( pieceVersion
[j
] ) > Number( pieceVal
[j
] ) ) {
287 // compare will be -1, 0 or 1, depending on comparison result
288 if ( !( eval( '' + compare
+ op
+ '0' ) ) ) {
291 } else if ( typeof val
=== 'number' ) {
292 if ( !( eval( 'profile.versionNumber' + op
+ val
) ) ) {