Merge "fix backup unit tests"
[lhc/web/wiklou.git] / resources / jquery / jquery.client.js
1 /**
2 * User-agent detection
3 */
4 ( function ( $ ) {
5
6 /* Private Members */
7
8 /**
9 * @var profileCache {Object} Keyed by userAgent string,
10 * value is the parsed $.client.profile object for that user agent.
11 */
12 var profileCache = {};
13
14 /* Public Methods */
15
16 $.client = {
17
18 /**
19 * Get an object containing information about the client.
20 *
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:
24 * {
25 * 'name': 'firefox',
26 * 'layout': 'gecko',
27 * 'layoutVersion': 20101026,
28 * 'platform': 'linux'
29 * 'version': '3.5.1',
30 * 'versionBase': '3',
31 * 'versionNumber': 3.5,
32 * }
33 */
34 profile: function ( nav ) {
35 if ( nav === undefined ) {
36 nav = window.navigator;
37 }
38 // Use the cached version if possible
39 if ( profileCache[nav.userAgent] === undefined ) {
40
41 /* Configuration */
42
43 // Name of browsers or layout engines we don't recognize
44 var uk = 'unknown';
45 // Generic version digit
46 var x = 'x';
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']
63 ];
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',
68 'lynx', 'msie', 'safari', 'ps3'
69 ];
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
73 var names = [
74 'camino', 'chrome', 'firefox', 'netscape', 'konqueror', 'lynx', 'msie', 'opera',
75 'safari', 'ipod', 'iphone', 'blackberry', 'ps3', 'rekonq'
76 ];
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']];
89
90 /* Methods */
91
92 /**
93 * Performs multiple replacements on a string
94 */
95 var translate = function ( source, translations ) {
96 var i;
97 for ( i = 0; i < translations.length; i++ ) {
98 source = source.replace( translations[i][0], translations[i][1] );
99 }
100 return source;
101 };
102
103 /* Pre-processing */
104
105 var ua = nav.userAgent,
106 match,
107 name = uk,
108 layout = uk,
109 layoutversion = uk,
110 platform = uk,
111 version = x;
112
113 if ( match = new RegExp( '(' + wildUserAgents.join( '|' ) + ')' ).exec( ua ) ) {
114 // Takes a userAgent string and translates given text into something we can more easily work with
115 ua = translate( ua, userAgentTranslations );
116 }
117 // Everything will be in lowercase from now on
118 ua = ua.toLowerCase();
119
120 /* Extraction */
121
122 if ( match = new RegExp( '(' + names.join( '|' ) + ')' ).exec( ua ) ) {
123 name = translate( match[1], nameTranslations );
124 }
125 if ( match = new RegExp( '(' + layouts.join( '|' ) + ')' ).exec( ua ) ) {
126 layout = translate( match[1], layoutTranslations );
127 }
128 if ( match = new RegExp( '(' + layoutVersions.join( '|' ) + ')\\\/(\\d+)').exec( ua ) ) {
129 layoutversion = parseInt( match[2], 10 );
130 }
131 if ( match = new RegExp( '(' + platforms.join( '|' ) + ')' ).exec( nav.platform.toLowerCase() ) ) {
132 platform = translate( match[1], platformTranslations );
133 }
134 if ( match = new RegExp( '(' + versionPrefixes.join( '|' ) + ')' + versionSuffix ).exec( ua ) ) {
135 version = match[3];
136 }
137
138 /* Edge Cases -- did I mention about how user agent string lie? */
139
140 // Decode Safari's crazy 400+ version numbers
141 if ( name.match( /safari/ ) && version > 400 ) {
142 version = '2.0';
143 }
144 // Expose Opera 10's lies about being Opera 9.8
145 if ( name === 'opera' && version >= 9.8) {
146 version = ua.match( /version\/([0-9\.]*)/i )[1] || 10;
147 }
148 var versionNumber = parseFloat( version, 10 ) || 0.0;
149
150 /* Caching */
151
152 profileCache[nav.userAgent] = {
153 name: name,
154 layout: layout,
155 layoutVersion: layoutversion,
156 platform: platform,
157 version: version,
158 versionBase: ( version !== x ? Math.floor( versionNumber ).toString() : x ),
159 versionNumber: versionNumber
160 };
161 }
162 return profileCache[nav.userAgent];
163 },
164
165 /**
166 * Checks the current browser against a support map object to determine if the browser has been black-listed or
167 * not. If the browser was not configured specifically it is assumed to work. It is assumed that the body
168 * element is classified as either "ltr" or "rtl". If neither is set, "ltr" is assumed.
169 *
170 * A browser map is in the following format:
171 * {
172 * 'ltr': {
173 * // Multiple rules with configurable operators
174 * 'msie': [['>=', 7], ['!=', 9]],
175 * // Blocked entirely
176 * 'iphone': false
177 * },
178 * 'rtl': {
179 * // Test against a string
180 * 'msie': [['!==', '8.1.2.3']],
181 * // RTL rules do not fall through to LTR rules, you must explicity set each of them
182 * 'iphone': false
183 * }
184 * }
185 *
186 * @param map {Object} Browser support map
187 * @param profile {Object} (optional) a client-profile object.
188 *
189 * @return Boolean true if browser known or assumed to be supported, false if blacklisted
190 */
191 test: function ( map, profile ) {
192 var conditions, dir, i, op, val;
193 profile = $.isPlainObject( profile ) ? profile : $.client.profile();
194
195 dir = $( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
196 // Check over each browser condition to determine if we are running in a compatible client
197 if ( typeof map[dir] !== 'object' || map[dir][profile.name] === undefined ) {
198 // Unknown, so we assume it's working
199 return true;
200 }
201 conditions = map[dir][profile.name];
202 for ( i = 0; i < conditions.length; i++ ) {
203 op = conditions[i][0];
204 val = conditions[i][1];
205 if ( val === false ) {
206 return false;
207 }
208 if ( typeof val === 'string' ) {
209 if ( !( eval( 'profile.version' + op + '"' + val + '"' ) ) ) {
210 return false;
211 }
212 } else if ( typeof val === 'number' ) {
213 if ( !( eval( 'profile.versionNumber' + op + val ) ) ) {
214 return false;
215 }
216 }
217 }
218 return true;
219 }
220 };
221 }( jQuery ) );