Adjust margin between h2 and special page table element
[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 {Object} profileCache 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 {Object} nav 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
41 // Use the cached version if possible
42 if ( profileCache[ nav.userAgent + '|' + nav.platform ] !== undefined ) {
43 return profileCache[ nav.userAgent + '|' + nav.platform ];
44 }
45
46 var
47 versionNumber,
48 key = nav.userAgent + '|' + nav.platform,
49
50 /* Configuration */
51
52 // Name of browsers or layout engines we don't recognize
53 uk = 'unknown',
54 // Generic version digit
55 x = 'x',
56 // Strings found in user agent strings that need to be conformed
57 wildUserAgents = ['Opera', 'Navigator', 'Minefield', 'KHTML', 'Chrome', 'PLAYSTATION 3', 'Iceweasel'],
58 // Translations for conforming user agent strings
59 userAgentTranslations = [
60 // Tons of browsers lie about being something they are not
61 [/(Firefox|MSIE|KHTML,?\slike\sGecko|Konqueror)/, ''],
62 // Chrome lives in the shadow of Safari still
63 ['Chrome Safari', 'Chrome'],
64 // KHTML is the layout engine not the browser - LIES!
65 ['KHTML', 'Konqueror'],
66 // Firefox nightly builds
67 ['Minefield', 'Firefox'],
68 // This helps keep different versions consistent
69 ['Navigator', 'Netscape'],
70 // This prevents version extraction issues, otherwise translation would happen later
71 ['PLAYSTATION 3', 'PS3']
72 ],
73 // Strings which precede a version number in a user agent string - combined and used as
74 // match 1 in version detection
75 versionPrefixes = [
76 'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'netscape6', 'opera', 'version', 'konqueror',
77 'lynx', 'msie', 'safari', 'ps3', 'android'
78 ],
79 // Used as matches 2, 3 and 4 in version extraction - 3 is used as actual version number
80 versionSuffix = '(\\/|\\;?\\s|)([a-z0-9\\.\\+]*?)(\\;|dev|rel|\\)|\\s|$)',
81 // Names of known browsers
82 names = [
83 'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'konqueror', 'lynx', 'msie', 'opera',
84 'safari', 'ipod', 'iphone', 'blackberry', 'ps3', 'rekonq', 'android'
85 ],
86 // Tanslations for conforming browser names
87 nameTranslations = [],
88 // Names of known layout engines
89 layouts = ['gecko', 'konqueror', 'msie', 'trident', 'opera', 'webkit'],
90 // Translations for conforming layout names
91 layoutTranslations = [ ['konqueror', 'khtml'], ['msie', 'trident'], ['opera', 'presto'] ],
92 // Names of supported layout engines for version number
93 layoutVersions = ['applewebkit', 'gecko', 'trident'],
94 // Names of known operating systems
95 platforms = ['win', 'wow64', 'mac', 'linux', 'sunos', 'solaris', 'iphone'],
96 // Translations for conforming operating system names
97 platformTranslations = [ ['sunos', 'solaris'], ['wow64', 'win'] ],
98
99 /* Methods */
100
101 /**
102 * Performs multiple replacements on a string
103 */
104 translate = function ( source, translations ) {
105 var i;
106 for ( i = 0; i < translations.length; i++ ) {
107 source = source.replace( translations[i][0], translations[i][1] );
108 }
109 return source;
110 },
111
112 /* Pre-processing */
113
114 ua = nav.userAgent,
115 match,
116 name = uk,
117 layout = uk,
118 layoutversion = uk,
119 platform = uk,
120 version = x;
121
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 );
125 }
126 // Everything will be in lowercase from now on
127 ua = ua.toLowerCase();
128
129 /* Extraction */
130
131 if ( match = new RegExp( '(' + names.join( '|' ) + ')' ).exec( ua ) ) {
132 name = translate( match[1], nameTranslations );
133 }
134 if ( match = new RegExp( '(' + layouts.join( '|' ) + ')' ).exec( ua ) ) {
135 layout = translate( match[1], layoutTranslations );
136 }
137 if ( match = new RegExp( '(' + layoutVersions.join( '|' ) + ')\\\/(\\d+)').exec( ua ) ) {
138 layoutversion = parseInt( match[2], 10 );
139 }
140 if ( match = new RegExp( '(' + platforms.join( '|' ) + ')' ).exec( nav.platform.toLowerCase() ) ) {
141 platform = translate( match[1], platformTranslations );
142 }
143 if ( match = new RegExp( '(' + versionPrefixes.join( '|' ) + ')' + versionSuffix ).exec( ua ) ) {
144 version = match[3];
145 }
146
147 /* Edge Cases -- did I mention about how user agent string lie? */
148
149 // Decode Safari's crazy 400+ version numbers
150 if ( name === 'safari' && version > 400 ) {
151 version = '2.0';
152 }
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] ) {
157 version = match[1];
158 } else {
159 version = '10';
160 }
161 }
162 // And Opera 15's lies about being Chrome
163 if ( name === 'chrome' && ( match = ua.match( /\bopr\/([0-9\.]*)/ ) ) ) {
164 if ( match[1] ) {
165 name = 'opera';
166 version = match[1];
167 }
168 }
169 // And IE 11's lies about being not being IE
170 if ( layout === 'trident' && layoutversion >= 7 && ( match = ua.match( /\brv[ :\/]([0-9\.]*)/ ) ) ) {
171 if ( match[1] ) {
172 name = 'msie';
173 version = match[1];
174 }
175 }
176 // And Amazon Silk's lies about being Android on mobile or Safari on desktop
177 if ( match = ua.match( /\bsilk\/([0-9.\-_]*)/ ) ) {
178 if ( match[1] ) {
179 name = 'silk';
180 version = match[1];
181 }
182 }
183
184 versionNumber = parseFloat( version, 10 ) || 0.0;
185
186 /* Caching */
187
188 return profileCache[ key ] = {
189 name: name,
190 layout: layout,
191 layoutVersion: layoutversion,
192 platform: platform,
193 version: version,
194 versionBase: ( version !== x ? Math.floor( versionNumber ).toString() : x ),
195 versionNumber: versionNumber
196 };
197 },
198
199 /**
200 * Checks the current browser against a support map object.
201 *
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').
205 *
206 * A browser map is in the following format:
207 * {
208 * // Multiple rules with configurable operators
209 * 'msie': [['>=', 7], ['!=', 9]],
210 * // Match no versions
211 * 'iphone': false,
212 * // Match any version
213 * 'android': null
214 * }
215 *
216 * It can optionally be split into ltr/rtl sections:
217 * {
218 * 'ltr': {
219 * 'android': null,
220 * 'iphone': false
221 * },
222 * 'rtl': {
223 * 'android': false,
224 * // rules are not inherited from ltr
225 * 'iphone': false
226 * }
227 * }
228 *
229 * @param {Object} map Browser support map
230 * @param {Object} [profile] A client-profile object
231 * @param {boolean} [exactMatchOnly=false] Only return true if the browser is matched, otherwise
232 * returns true if the browser is not found.
233 *
234 * @return {boolean} The current browser is in the support map
235 */
236 test: function ( map, profile, exactMatchOnly ) {
237 /*jshint evil: true */
238
239 var conditions, dir, i, op, val, j, pieceVersion, pieceVal, compare;
240 profile = $.isPlainObject( profile ) ? profile : $.client.profile();
241 if ( map.ltr && map.rtl ) {
242 dir = $( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
243 map = map[dir];
244 }
245 // Check over each browser condition to determine if we are running in a compatible client
246 if ( typeof map !== 'object' || map[profile.name] === undefined ) {
247 // Not found, return true if exactMatchOnly not set, false otherwise
248 return !exactMatchOnly;
249 }
250 conditions = map[profile.name];
251 if ( conditions === false ) {
252 // Match no versions
253 return false;
254 }
255 if ( conditions === null ) {
256 // Match all versions
257 return true;
258 }
259 for ( i = 0; i < conditions.length; i++ ) {
260 op = conditions[i][0];
261 val = conditions[i][1];
262 if ( typeof val === 'string' ) {
263 // Perform a component-wise comparison of versions, similar to PHP's version_compare
264 // but simpler. '1.11' is larger than '1.2'.
265 pieceVersion = profile.version.toString().split( '.' );
266 pieceVal = val.split( '.' );
267 // Extend with zeroes to equal length
268 while ( pieceVersion.length < pieceVal.length ) {
269 pieceVersion.push( '0' );
270 }
271 while ( pieceVal.length < pieceVersion.length ) {
272 pieceVal.push( '0' );
273 }
274 // Compare components
275 compare = 0;
276 for ( j = 0; j < pieceVersion.length; j++ ) {
277 if ( Number( pieceVersion[j] ) < Number( pieceVal[j] ) ) {
278 compare = -1;
279 break;
280 } else if ( Number( pieceVersion[j] ) > Number( pieceVal[j] ) ) {
281 compare = 1;
282 break;
283 }
284 }
285 // compare will be -1, 0 or 1, depending on comparison result
286 if ( !( eval( '' + compare + op + '0' ) ) ) {
287 return false;
288 }
289 } else if ( typeof val === 'number' ) {
290 if ( !( eval( 'profile.versionNumber' + op + val ) ) ) {
291 return false;
292 }
293 }
294 }
295
296 return true;
297 }
298 };
299 }( jQuery ) );