2013-04-26 21:40:03 +00:00
|
|
|
/**
|
|
|
|
* User-agent detection
|
|
|
|
*/
|
|
|
|
( function ( $ ) {
|
|
|
|
|
|
|
|
/* Private Members */
|
|
|
|
|
|
|
|
/**
|
2013-05-26 15:23:03 +00:00
|
|
|
* @var {Object} profileCache Keyed by userAgent string,
|
2013-04-26 21:40:03 +00:00
|
|
|
* value is the parsed $.client.profile object for that user agent.
|
|
|
|
*/
|
|
|
|
var profileCache = {};
|
|
|
|
|
|
|
|
/* Public Methods */
|
|
|
|
|
|
|
|
$.client = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an object containing information about the client.
|
|
|
|
*
|
2013-05-26 15:23:03 +00:00
|
|
|
* @param {Object} nav An object with atleast a 'userAgent' and 'platform' key.
|
2013-04-26 21:40:03 +00:00
|
|
|
* Defaults to the global Navigator object.
|
2013-05-26 15:23:03 +00:00
|
|
|
* @returns {Object} The resulting client object will be in the following format:
|
2013-04-26 21:40:03 +00:00
|
|
|
* {
|
|
|
|
* 'name': 'firefox',
|
|
|
|
* 'layout': 'gecko',
|
|
|
|
* 'layoutVersion': 20101026,
|
|
|
|
* 'platform': 'linux'
|
|
|
|
* 'version': '3.5.1',
|
|
|
|
* 'versionBase': '3',
|
|
|
|
* 'versionNumber': 3.5,
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
profile: function ( nav ) {
|
|
|
|
/*jshint boss: true */
|
|
|
|
|
|
|
|
if ( nav === undefined ) {
|
|
|
|
nav = window.navigator;
|
|
|
|
}
|
|
|
|
// Use the cached version if possible
|
|
|
|
if ( profileCache[nav.userAgent] === undefined ) {
|
|
|
|
|
|
|
|
var
|
|
|
|
versionNumber,
|
|
|
|
|
|
|
|
/* Configuration */
|
|
|
|
|
|
|
|
// Name of browsers or layout engines we don't recognize
|
|
|
|
uk = 'unknown',
|
|
|
|
// Generic version digit
|
|
|
|
x = 'x',
|
|
|
|
// Strings found in user agent strings that need to be conformed
|
2013-05-26 15:23:03 +00:00
|
|
|
wildUserAgents = ['Opera', 'Navigator', 'Minefield', 'KHTML', 'Chrome', 'PLAYSTATION 3', 'Iceweasel'],
|
2013-04-26 21:40:03 +00:00
|
|
|
// Translations for conforming user agent strings
|
|
|
|
userAgentTranslations = [
|
|
|
|
// Tons of browsers lie about being something they are not
|
2013-05-26 15:23:03 +00:00
|
|
|
[/(Firefox|MSIE|KHTML,?\slike\sGecko|Konqueror)/, ''],
|
2013-04-26 21:40:03 +00:00
|
|
|
// Chrome lives in the shadow of Safari still
|
|
|
|
['Chrome Safari', 'Chrome'],
|
|
|
|
// KHTML is the layout engine not the browser - LIES!
|
|
|
|
['KHTML', 'Konqueror'],
|
|
|
|
// Firefox nightly builds
|
|
|
|
['Minefield', 'Firefox'],
|
2013-07-10 01:35:35 +00:00
|
|
|
// This helps keep different versions consistent
|
2013-04-26 21:40:03 +00:00
|
|
|
['Navigator', 'Netscape'],
|
|
|
|
// This prevents version extraction issues, otherwise translation would happen later
|
|
|
|
['PLAYSTATION 3', 'PS3']
|
|
|
|
],
|
2013-07-10 01:35:35 +00:00
|
|
|
// Strings which precede a version number in a user agent string - combined and used as
|
|
|
|
// match 1 in version detection
|
2013-04-26 21:40:03 +00:00
|
|
|
versionPrefixes = [
|
|
|
|
'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'netscape6', 'opera', 'version', 'konqueror',
|
2013-05-26 15:23:03 +00:00
|
|
|
'lynx', 'msie', 'safari', 'ps3', 'android'
|
2013-04-26 21:40:03 +00:00
|
|
|
],
|
|
|
|
// Used as matches 2, 3 and 4 in version extraction - 3 is used as actual version number
|
|
|
|
versionSuffix = '(\\/|\\;?\\s|)([a-z0-9\\.\\+]*?)(\\;|dev|rel|\\)|\\s|$)',
|
|
|
|
// Names of known browsers
|
|
|
|
names = [
|
|
|
|
'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'konqueror', 'lynx', 'msie', 'opera',
|
2013-05-26 15:23:03 +00:00
|
|
|
'safari', 'ipod', 'iphone', 'blackberry', 'ps3', 'rekonq', 'android'
|
2013-04-26 21:40:03 +00:00
|
|
|
],
|
|
|
|
// Tanslations for conforming browser names
|
|
|
|
nameTranslations = [],
|
|
|
|
// Names of known layout engines
|
2013-07-10 21:23:11 +00:00
|
|
|
layouts = ['gecko', 'konqueror', 'msie', 'trident', 'opera', 'webkit'],
|
2013-04-26 21:40:03 +00:00
|
|
|
// Translations for conforming layout names
|
|
|
|
layoutTranslations = [ ['konqueror', 'khtml'], ['msie', 'trident'], ['opera', 'presto'] ],
|
|
|
|
// Names of supported layout engines for version number
|
2013-07-10 21:23:11 +00:00
|
|
|
layoutVersions = ['applewebkit', 'gecko', 'trident'],
|
2013-04-26 21:40:03 +00:00
|
|
|
// Names of known operating systems
|
2013-07-10 21:23:11 +00:00
|
|
|
platforms = ['win', 'wow64', 'mac', 'linux', 'sunos', 'solaris', 'iphone'],
|
2013-04-26 21:40:03 +00:00
|
|
|
// Translations for conforming operating system names
|
2013-07-10 21:23:11 +00:00
|
|
|
platformTranslations = [ ['sunos', 'solaris'], ['wow64', 'win'] ],
|
2013-04-26 21:40:03 +00:00
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs multiple replacements on a string
|
|
|
|
*/
|
|
|
|
translate = function ( source, translations ) {
|
|
|
|
var i;
|
|
|
|
for ( i = 0; i < translations.length; i++ ) {
|
|
|
|
source = source.replace( translations[i][0], translations[i][1] );
|
|
|
|
}
|
|
|
|
return source;
|
|
|
|
},
|
|
|
|
|
|
|
|
/* Pre-processing */
|
|
|
|
|
|
|
|
ua = nav.userAgent,
|
|
|
|
match,
|
|
|
|
name = uk,
|
|
|
|
layout = uk,
|
|
|
|
layoutversion = uk,
|
|
|
|
platform = uk,
|
|
|
|
version = x;
|
|
|
|
|
|
|
|
if ( match = new RegExp( '(' + wildUserAgents.join( '|' ) + ')' ).exec( ua ) ) {
|
|
|
|
// Takes a userAgent string and translates given text into something we can more easily work with
|
|
|
|
ua = translate( ua, userAgentTranslations );
|
|
|
|
}
|
|
|
|
// Everything will be in lowercase from now on
|
|
|
|
ua = ua.toLowerCase();
|
|
|
|
|
|
|
|
/* Extraction */
|
|
|
|
|
|
|
|
if ( match = new RegExp( '(' + names.join( '|' ) + ')' ).exec( ua ) ) {
|
|
|
|
name = translate( match[1], nameTranslations );
|
|
|
|
}
|
|
|
|
if ( match = new RegExp( '(' + layouts.join( '|' ) + ')' ).exec( ua ) ) {
|
|
|
|
layout = translate( match[1], layoutTranslations );
|
|
|
|
}
|
|
|
|
if ( match = new RegExp( '(' + layoutVersions.join( '|' ) + ')\\\/(\\d+)').exec( ua ) ) {
|
|
|
|
layoutversion = parseInt( match[2], 10 );
|
|
|
|
}
|
|
|
|
if ( match = new RegExp( '(' + platforms.join( '|' ) + ')' ).exec( nav.platform.toLowerCase() ) ) {
|
|
|
|
platform = translate( match[1], platformTranslations );
|
|
|
|
}
|
|
|
|
if ( match = new RegExp( '(' + versionPrefixes.join( '|' ) + ')' + versionSuffix ).exec( ua ) ) {
|
|
|
|
version = match[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Edge Cases -- did I mention about how user agent string lie? */
|
|
|
|
|
|
|
|
// Decode Safari's crazy 400+ version numbers
|
2013-07-10 01:35:35 +00:00
|
|
|
if ( name === 'safari' && version > 400 ) {
|
2013-04-26 21:40:03 +00:00
|
|
|
version = '2.0';
|
|
|
|
}
|
|
|
|
// Expose Opera 10's lies about being Opera 9.8
|
2013-07-10 01:35:35 +00:00
|
|
|
if ( name === 'opera' && version >= 9.8 ) {
|
|
|
|
match = ua.match( /\bversion\/([0-9\.]*)/ );
|
2013-04-26 21:40:03 +00:00
|
|
|
if ( match && match[1] ) {
|
|
|
|
version = match[1];
|
|
|
|
} else {
|
|
|
|
version = '10';
|
|
|
|
}
|
|
|
|
}
|
2013-07-10 01:35:35 +00:00
|
|
|
// And Opera 15's lies about being Chrome
|
|
|
|
if ( name === 'chrome' && ( match = ua.match( /\bopr\/([0-9\.]*)/ ) ) ) {
|
|
|
|
if ( match[1] ) {
|
|
|
|
name = 'opera';
|
|
|
|
version = match[1];
|
|
|
|
}
|
|
|
|
}
|
2013-07-10 21:23:11 +00:00
|
|
|
// And IE 11's lies about being not being IE
|
|
|
|
if ( layout === 'trident' && layoutversion >= 7 && ( match = ua.match( /\brv[ :\/]([0-9\.]*)/ ) ) ) {
|
|
|
|
if ( match[1] ) {
|
|
|
|
name = 'msie';
|
|
|
|
version = match[1];
|
|
|
|
}
|
|
|
|
}
|
2013-07-10 01:35:35 +00:00
|
|
|
|
2013-04-26 21:40:03 +00:00
|
|
|
versionNumber = parseFloat( version, 10 ) || 0.0;
|
|
|
|
|
|
|
|
/* Caching */
|
|
|
|
|
|
|
|
profileCache[nav.userAgent] = {
|
|
|
|
name: name,
|
|
|
|
layout: layout,
|
|
|
|
layoutVersion: layoutversion,
|
|
|
|
platform: platform,
|
|
|
|
version: version,
|
|
|
|
versionBase: ( version !== x ? Math.floor( versionNumber ).toString() : x ),
|
|
|
|
versionNumber: versionNumber
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return profileCache[nav.userAgent];
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-05-26 15:23:03 +00:00
|
|
|
* Checks the current browser against a support map object.
|
2013-04-26 21:40:03 +00:00
|
|
|
*
|
|
|
|
* A browser map is in the following format:
|
|
|
|
* {
|
2013-05-26 15:23:03 +00:00
|
|
|
* // Multiple rules with configurable operators
|
|
|
|
* 'msie': [['>=', 7], ['!=', 9]],
|
|
|
|
* // Match no versions
|
|
|
|
* 'iphone': false,
|
|
|
|
* // Match any version
|
|
|
|
* 'android': null
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* It can optionally be split into ltr/rtl sections:
|
|
|
|
* {
|
2013-04-26 21:40:03 +00:00
|
|
|
* 'ltr': {
|
2013-05-26 15:23:03 +00:00
|
|
|
* 'android': null,
|
2013-04-26 21:40:03 +00:00
|
|
|
* 'iphone': false
|
|
|
|
* },
|
|
|
|
* 'rtl': {
|
2013-05-26 15:23:03 +00:00
|
|
|
* 'android': false,
|
|
|
|
* // rules are not inherited from ltr
|
2013-04-26 21:40:03 +00:00
|
|
|
* 'iphone': false
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2013-05-26 15:23:03 +00:00
|
|
|
* @param {Object} map Browser support map
|
|
|
|
* @param {Object} [profile] A client-profile object
|
|
|
|
* @param {boolean} [exactMatchOnly=false] Only return true if the browser is matched, otherwise
|
|
|
|
* returns true if the browser is not found.
|
2013-04-26 21:40:03 +00:00
|
|
|
*
|
2013-05-26 15:23:03 +00:00
|
|
|
* @returns {boolean} The current browser is in the support map
|
2013-04-26 21:40:03 +00:00
|
|
|
*/
|
2013-05-26 15:23:03 +00:00
|
|
|
test: function ( map, profile, exactMatchOnly ) {
|
2013-04-26 21:40:03 +00:00
|
|
|
/*jshint evil: true */
|
|
|
|
|
|
|
|
var conditions, dir, i, op, val;
|
|
|
|
profile = $.isPlainObject( profile ) ? profile : $.client.profile();
|
2013-05-26 15:23:03 +00:00
|
|
|
if ( map.ltr && map.rtl ) {
|
|
|
|
dir = $( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
|
|
|
|
map = map[dir];
|
|
|
|
}
|
2013-04-26 21:40:03 +00:00
|
|
|
// Check over each browser condition to determine if we are running in a compatible client
|
2013-05-26 15:23:03 +00:00
|
|
|
if ( typeof map !== 'object' || map[profile.name] === undefined ) {
|
|
|
|
// Not found, return true if exactMatchOnly not set, false otherwise
|
|
|
|
return !exactMatchOnly;
|
2013-04-26 21:40:03 +00:00
|
|
|
}
|
2013-05-26 15:23:03 +00:00
|
|
|
conditions = map[profile.name];
|
2013-04-26 21:40:03 +00:00
|
|
|
if ( conditions === false ) {
|
2013-05-26 15:23:03 +00:00
|
|
|
// Match no versions
|
2013-04-26 21:40:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-05-26 15:23:03 +00:00
|
|
|
if ( conditions === null ) {
|
|
|
|
// Match all versions
|
|
|
|
return true;
|
|
|
|
}
|
2013-04-26 21:40:03 +00:00
|
|
|
for ( i = 0; i < conditions.length; i++ ) {
|
|
|
|
op = conditions[i][0];
|
|
|
|
val = conditions[i][1];
|
|
|
|
if ( typeof val === 'string' ) {
|
|
|
|
if ( !( eval( 'profile.version' + op + '"' + val + '"' ) ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if ( typeof val === 'number' ) {
|
|
|
|
if ( !( eval( 'profile.versionNumber' + op + val ) ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}( jQuery ) );
|