Merge "Fix issue with link in history line for revs hidden from sysops"
[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 /*jshint boss: true */
36
37 if ( nav === undefined ) {
38 nav = window.navigator;
39 }
40 // Use the cached version if possible
41 if ( profileCache[nav.userAgent] === undefined ) {
42
43 /* Configuration */
44
45 // Name of browsers or layout engines we don't recognize
46 var uk = 'unknown';
47 // Generic version digit
48 var x = 'x';
49 // Strings found in user agent strings that need to be conformed
50 var wildUserAgents = ['Opera', 'Navigator', 'Minefield', 'KHTML', 'Chrome', 'PLAYSTATION 3'];
51 // Translations for conforming user agent strings
52 var userAgentTranslations = [
53 // Tons of browsers lie about being something they are not
54 [/(Firefox|MSIE|KHTML,\slike\sGecko|Konqueror)/, ''],
55 // Chrome lives in the shadow of Safari still
56 ['Chrome Safari', 'Chrome'],
57 // KHTML is the layout engine not the browser - LIES!
58 ['KHTML', 'Konqueror'],
59 // Firefox nightly builds
60 ['Minefield', 'Firefox'],
61 // This helps keep differnt versions consistent
62 ['Navigator', 'Netscape'],
63 // This prevents version extraction issues, otherwise translation would happen later
64 ['PLAYSTATION 3', 'PS3']
65 ];
66 // Strings which precede a version number in a user agent string - combined and used as match 1 in
67 // version detectection
68 var versionPrefixes = [
69 'camino', 'chrome', 'firefox', 'netscape', 'netscape6', 'opera', 'version', 'konqueror',
70 'lynx', 'msie', 'safari', 'ps3'
71 ];
72 // Used as matches 2, 3 and 4 in version extraction - 3 is used as actual version number
73 var versionSuffix = '(\\/|\\;?\\s|)([a-z0-9\\.\\+]*?)(\\;|dev|rel|\\)|\\s|$)';
74 // Names of known browsers
75 var names = [
76 'camino', 'chrome', 'firefox', 'netscape', 'konqueror', 'lynx', 'msie', 'opera',
77 'safari', 'ipod', 'iphone', 'blackberry', 'ps3', 'rekonq'
78 ];
79 // Tanslations for conforming browser names
80 var nameTranslations = [];
81 // Names of known layout engines
82 var layouts = ['gecko', 'konqueror', 'msie', 'opera', 'webkit'];
83 // Translations for conforming layout names
84 var layoutTranslations = [['konqueror', 'khtml'], ['msie', 'trident'], ['opera', 'presto']];
85 // Names of supported layout engines for version number
86 var layoutVersions = ['applewebkit', 'gecko'];
87 // Names of known operating systems
88 var platforms = ['win', 'mac', 'linux', 'sunos', 'solaris', 'iphone'];
89 // Translations for conforming operating system names
90 var platformTranslations = [['sunos', 'solaris']];
91
92 /* Methods */
93
94 /**
95 * Performs multiple replacements on a string
96 */
97 var translate = function ( source, translations ) {
98 var i;
99 for ( i = 0; i < translations.length; i++ ) {
100 source = source.replace( translations[i][0], translations[i][1] );
101 }
102 return source;
103 };
104
105 /* Pre-processing */
106
107 var ua = nav.userAgent,
108 match,
109 name = uk,
110 layout = uk,
111 layoutversion = uk,
112 platform = uk,
113 version = x;
114
115 if ( match = new RegExp( '(' + wildUserAgents.join( '|' ) + ')' ).exec( ua ) ) {
116 // Takes a userAgent string and translates given text into something we can more easily work with
117 ua = translate( ua, userAgentTranslations );
118 }
119 // Everything will be in lowercase from now on
120 ua = ua.toLowerCase();
121
122 /* Extraction */
123
124 if ( match = new RegExp( '(' + names.join( '|' ) + ')' ).exec( ua ) ) {
125 name = translate( match[1], nameTranslations );
126 }
127 if ( match = new RegExp( '(' + layouts.join( '|' ) + ')' ).exec( ua ) ) {
128 layout = translate( match[1], layoutTranslations );
129 }
130 if ( match = new RegExp( '(' + layoutVersions.join( '|' ) + ')\\\/(\\d+)').exec( ua ) ) {
131 layoutversion = parseInt( match[2], 10 );
132 }
133 if ( match = new RegExp( '(' + platforms.join( '|' ) + ')' ).exec( nav.platform.toLowerCase() ) ) {
134 platform = translate( match[1], platformTranslations );
135 }
136 if ( match = new RegExp( '(' + versionPrefixes.join( '|' ) + ')' + versionSuffix ).exec( ua ) ) {
137 version = match[3];
138 }
139
140 /* Edge Cases -- did I mention about how user agent string lie? */
141
142 // Decode Safari's crazy 400+ version numbers
143 if ( name.match( /safari/ ) && version > 400 ) {
144 version = '2.0';
145 }
146 // Expose Opera 10's lies about being Opera 9.8
147 if ( name === 'opera' && version >= 9.8) {
148 version = ua.match( /version\/([0-9\.]*)/i )[1] || 10;
149 }
150 var versionNumber = parseFloat( version, 10 ) || 0.0;
151
152 /* Caching */
153
154 profileCache[nav.userAgent] = {
155 name: name,
156 layout: layout,
157 layoutVersion: layoutversion,
158 platform: platform,
159 version: version,
160 versionBase: ( version !== x ? Math.floor( versionNumber ).toString() : x ),
161 versionNumber: versionNumber
162 };
163 }
164 return profileCache[nav.userAgent];
165 },
166
167 /**
168 * Checks the current browser against a support map object to determine if the browser has been black-listed or
169 * not. If the browser was not configured specifically it is assumed to work. It is assumed that the body
170 * element is classified as either "ltr" or "rtl". If neither is set, "ltr" is assumed.
171 *
172 * A browser map is in the following format:
173 * {
174 * 'ltr': {
175 * // Multiple rules with configurable operators
176 * 'msie': [['>=', 7], ['!=', 9]],
177 * // Blocked entirely
178 * 'iphone': false
179 * },
180 * 'rtl': {
181 * // Test against a string
182 * 'msie': [['!==', '8.1.2.3']],
183 * // RTL rules do not fall through to LTR rules, you must explicity set each of them
184 * 'iphone': false
185 * }
186 * }
187 *
188 * @param map {Object} Browser support map
189 * @param profile {Object} (optional) a client-profile object.
190 *
191 * @return Boolean true if browser known or assumed to be supported, false if blacklisted
192 */
193 test: function ( map, profile ) {
194 /*jshint evil:true */
195
196 var conditions, dir, i, op, val;
197 profile = $.isPlainObject( profile ) ? profile : $.client.profile();
198
199 dir = $( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
200 // Check over each browser condition to determine if we are running in a compatible client
201 if ( typeof map[dir] !== 'object' || map[dir][profile.name] === undefined ) {
202 // Unknown, so we assume it's working
203 return true;
204 }
205 conditions = map[dir][profile.name];
206 for ( i = 0; i < conditions.length; i++ ) {
207 op = conditions[i][0];
208 val = conditions[i][1];
209 if ( val === false ) {
210 return false;
211 }
212 if ( typeof val === 'string' ) {
213 if ( !( eval( 'profile.version' + op + '"' + val + '"' ) ) ) {
214 return false;
215 }
216 } else if ( typeof val === 'number' ) {
217 if ( !( eval( 'profile.versionNumber' + op + val ) ) ) {
218 return false;
219 }
220 }
221 }
222 return true;
223 }
224 };
225 }( jQuery ) );