fixed comment
[lhc/web/wiklou.git] / resources / jquery / jquery.client.js
1 /*
2 * User-agent detection
3 */
4 jQuery.client = new ( function() {
5
6 /* Private Members */
7
8 var profile;
9
10 /* Public Functions */
11
12 /**
13 * Returns an object containing information about the browser
14 *
15 * The resulting client object will be in the following format:
16 * {
17 * 'browser': 'firefox',
18 * 'layout': 'gecko',
19 * 'os': 'linux'
20 * 'version': '3.5.1',
21 * 'versionBase': '3',
22 * 'versionNumber': 3.5,
23 * }
24 */
25 this.profile = function() {
26 // Use the cached version if possible
27 if ( typeof profile === 'undefined' ) {
28
29 /* Configuration */
30
31 // Name of browsers or layout engines we don't recognize
32 var uk = 'unknown';
33 // Generic version digit
34 var x = 'x';
35 // Strings found in user agent strings that need to be conformed
36 var wildUserAgents = [ 'Opera', 'Navigator', 'Minefield', 'KHTML', 'Chrome', 'PLAYSTATION 3'];
37 // Translations for conforming user agent strings
38 var userAgentTranslations = [
39 // Tons of browsers lie about being something they are not
40 [/(Firefox|MSIE|KHTML,\slike\sGecko|Konqueror)/, ''],
41 // Chrome lives in the shadow of Safari still
42 ['Chrome Safari', 'Chrome'],
43 // KHTML is the layout engine not the browser - LIES!
44 ['KHTML', 'Konqueror'],
45 // Firefox nightly builds
46 ['Minefield', 'Firefox'],
47 // This helps keep differnt versions consistent
48 ['Navigator', 'Netscape'],
49 // This prevents version extraction issues, otherwise translation would happen later
50 ['PLAYSTATION 3', 'PS3'],
51 ];
52 // Strings which precede a version number in a user agent string - combined and used as match 1 in
53 // version detectection
54 var versionPrefixes = [
55 'camino', 'chrome', 'firefox', 'netscape', 'netscape6', 'opera', 'version', 'konqueror', 'lynx',
56 'msie', 'safari', 'ps3'
57 ];
58 // Used as matches 2, 3 and 4 in version extraction - 3 is used as actual version number
59 var versionSuffix = '(\/|\;?\s|)([a-z0-9\.\+]*?)(\;|dev|rel|\\)|\s|$)';
60 // Names of known browsers
61 var browserNames = [
62 'camino', 'chrome', 'firefox', 'netscape', 'konqueror', 'lynx', 'msie', 'opera', 'safari', 'ipod',
63 'iphone', 'blackberry', 'ps3'
64 ];
65 // Tanslations for conforming browser names
66 var browserTranslations = [];
67 // Names of known layout engines
68 var layoutNames = ['gecko', 'konqueror', 'msie', 'opera', 'webkit'];
69 // Translations for conforming layout names
70 var layoutTranslations = [['konqueror', 'khtml'], ['msie', 'trident'], ['opera', 'presto']];
71 // Names of known operating systems
72 var osNames = ['win', 'mac', 'linux', 'sunos', 'solaris', 'iphone'];
73 // Translations for conforming operating system names
74 var osTranslations = [['sunos', 'solaris']];
75
76 /* Methods */
77
78 // Performs multiple replacements on a string
79 function translate( source, translations ) {
80 for ( var i = 0; i < translations.length; i++ ) {
81 source = source.replace( translations[i][0], translations[i][1] );
82 }
83 return source;
84 };
85
86 /* Pre-processing */
87
88 var userAgent = navigator.userAgent, match, browser = uk, layout = uk, os = uk, version = x;
89 if ( match = new RegExp( '(' + wildUserAgents.join( '|' ) + ')' ).exec( userAgent ) ) {
90 // Takes a userAgent string and translates given text into something we can more easily work with
91 userAgent = translate( userAgent, userAgentTranslations );
92 }
93 // Everything will be in lowercase from now on
94 userAgent = userAgent.toLowerCase();
95
96 /* Extraction */
97
98 if ( match = new RegExp( '(' + browserNames.join( '|' ) + ')' ).exec( userAgent ) ) {
99 browser = translate( match[1], browserTranslations );
100 }
101 if ( match = new RegExp( '(' + layoutNames.join( '|' ) + ')' ).exec( userAgent ) ) {
102 layout = translate( match[1], layoutTranslations );
103 }
104 if ( match = new RegExp( '(' + osNames.join( '|' ) + ')' ).exec( navigator.platform.toLowerCase() ) ) {
105 var os = translate( match[1], osTranslations );
106 }
107 if ( match = new RegExp( '(' + versionPrefixes.join( '|' ) + ')' + versionSuffix ).exec( userAgent ) ) {
108 version = match[3];
109 }
110
111 /* Edge Cases -- did I mention about how user agent string lie? */
112
113 // Decode Safari's crazy 400+ version numbers
114 if ( name.match( /safari/ ) && version > 400 ) {
115 version = '2.0';
116 }
117 // Expose Opera 10's lies about being Opera 9.8
118 if ( name === 'opera' && version >= 9.8) {
119 version = userAgent.match( /version\/([0-9\.]*)/i )[1] || 10;
120 }
121
122 /* Caching */
123
124 profile = {
125 'browser': browser,
126 'layout': layout,
127 'os': os,
128 'version': version,
129 'versionBase': ( version !== x ? new String( version ).substr( 0, 1 ) : x ),
130 'versionNumber': ( parseFloat( version, 10 ) || 0.0 )
131 };
132 }
133 return profile;
134 };
135
136 /**
137 * Checks the current browser against a support map object to determine if the browser has been black-listed or
138 * not. If the browser was not configured specifically it is assumed to work. It is assumed that the body
139 * element is classified as either "ltr" or "rtl". If neither is set, "ltr" is assumed.
140 *
141 * A browser map is in the following format:
142 * {
143 * 'ltr': {
144 * // Multiple rules with configurable operators
145 * 'msie': [['>=', 7], ['!=', 9]],
146 * // Blocked entirely
147 * 'iphone': false
148 * },
149 * 'rtl': {
150 * // Test against a string
151 * 'msie': [['!==', '8.1.2.3']],
152 * // RTL rules do not fall through to LTR rules, you must explicity set each of them
153 * 'iphone': false
154 * }
155 * }
156 *
157 * @param map Object of browser support map
158 *
159 * @return Boolean true if browser known or assumed to be supported, false if blacklisted
160 */
161 this.test = function( map ) {
162 var profile = jQuery.client.profile();
163 var dir = jQuery( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
164 // Check over each browser condition to determine if we are running in a compatible client
165 if ( typeof map[dir] !== 'object' || map[dir][profile.browser] !== 'object' ) {
166 // Unknown, so we assume it's working
167 return true;
168 }
169 var browser = map[dir][profile.browser];
170 for ( var condition in browser ) {
171 var op = browser[condition][0];
172 var val = browser[condition][1];
173 if ( val === false ) {
174 return false;
175 } else if ( typeof val == 'string' ) {
176 if ( !( eval( 'profile.version' + op + '"' + val + '"' ) ) ) {
177 return false;
178 }
179 } else if ( typeof val == 'number' ) {
180 if ( !( eval( 'profile.versionNumber' + op + val ) ) ) {
181 return false;
182 }
183 }
184 }
185 return true;
186 }
187 } )();