xhprof: Guard against division by 0 when computing percentages
[lhc/web/wiklou.git] / resources / src / jquery / jquery.client.js
1 /**
2 * User-agent detection
3 *
4 * @class jQuery.client
5 * @singleton
6 */
7 ( function ( $ ) {
8
9 /**
10 * @private
11 * @property {Object} profileCache Keyed by userAgent string,
12 * value is the parsed $.client.profile object for that user agent.
13 */
14 var profileCache = {};
15
16 $.client = {
17
18 /**
19 * Get an object containing information about the client.
20 *
21 * @param {Object} [nav] An object with a 'userAgent' and 'platform' property.
22 * Defaults to the global `navigator` object.
23 * @return {Object} The resulting client object will be in the following format:
24 *
25 * {
26 * 'name': 'firefox',
27 * 'layout': 'gecko',
28 * 'layoutVersion': 20101026,
29 * 'platform': 'linux'
30 * 'version': '3.5.1',
31 * 'versionBase': '3',
32 * 'versionNumber': 3.5,
33 * }
34 */
35 profile: function ( nav ) {
36 /*jshint boss: true */
37
38 if ( nav === undefined ) {
39 nav = window.navigator;
40 }
41
42 // Use the cached version if possible
43 if ( profileCache[ nav.userAgent + '|' + nav.platform ] !== undefined ) {
44 return profileCache[ nav.userAgent + '|' + nav.platform ];
45 }
46
47 var
48 versionNumber,
49 key = nav.userAgent + '|' + nav.platform,
50
51 // Configuration
52
53 // Name of browsers or layout engines we don't recognize
54 uk = 'unknown',
55 // Generic version digit
56 x = 'x',
57 // Strings found in user agent strings that need to be conformed
58 wildUserAgents = ['Opera', 'Navigator', 'Minefield', 'KHTML', 'Chrome', 'PLAYSTATION 3', 'Iceweasel'],
59 // Translations for conforming user agent strings
60 userAgentTranslations = [
61 // Tons of browsers lie about being something they are not
62 [/(Firefox|MSIE|KHTML,?\slike\sGecko|Konqueror)/, ''],
63 // Chrome lives in the shadow of Safari still
64 ['Chrome Safari', 'Chrome'],
65 // KHTML is the layout engine not the browser - LIES!
66 ['KHTML', 'Konqueror'],
67 // Firefox nightly builds
68 ['Minefield', 'Firefox'],
69 // This helps keep different versions consistent
70 ['Navigator', 'Netscape'],
71 // This prevents version extraction issues, otherwise translation would happen later
72 ['PLAYSTATION 3', 'PS3']
73 ],
74 // Strings which precede a version number in a user agent string - combined and used as
75 // match 1 in version detection
76 versionPrefixes = [
77 'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'netscape6', 'opera', 'version', 'konqueror',
78 'lynx', 'msie', 'safari', 'ps3', 'android'
79 ],
80 // Used as matches 2, 3 and 4 in version extraction - 3 is used as actual version number
81 versionSuffix = '(\\/|\\;?\\s|)([a-z0-9\\.\\+]*?)(\\;|dev|rel|\\)|\\s|$)',
82 // Names of known browsers
83 names = [
84 'camino', 'chrome', 'firefox', 'iceweasel', 'netscape', 'konqueror', 'lynx', 'msie', 'opera',
85 'safari', 'ipod', 'iphone', 'blackberry', 'ps3', 'rekonq', 'android'
86 ],
87 // Tanslations for conforming browser names
88 nameTranslations = [],
89 // Names of known layout engines
90 layouts = ['gecko', 'konqueror', 'msie', 'trident', 'opera', 'webkit'],
91 // Translations for conforming layout names
92 layoutTranslations = [ ['konqueror', 'khtml'], ['msie', 'trident'], ['opera', 'presto'] ],
93 // Names of supported layout engines for version number
94 layoutVersions = ['applewebkit', 'gecko', 'trident'],
95 // Names of known operating systems
96 platforms = ['win', 'wow64', 'mac', 'linux', 'sunos', 'solaris', 'iphone'],
97 // Translations for conforming operating system names
98 platformTranslations = [ ['sunos', 'solaris'], ['wow64', 'win'] ],
99
100 /**
101 * Performs multiple replacements on a string
102 * @ignore
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 * {
209 * // Multiple rules with configurable operators
210 * 'msie': [['>=', 7], ['!=', 9]],
211 * // Match no versions
212 * 'iphone': false,
213 * // Match any version
214 * 'android': null
215 * }
216 *
217 * It can optionally be split into ltr/rtl sections:
218 *
219 * {
220 * 'ltr': {
221 * 'android': null,
222 * 'iphone': false
223 * },
224 * 'rtl': {
225 * 'android': false,
226 * // rules are not inherited from ltr
227 * 'iphone': false
228 * }
229 * }
230 *
231 * @param {Object} map Browser support map
232 * @param {Object} [profile] A client-profile object
233 * @param {boolean} [exactMatchOnly=false] Only return true if the browser is matched, otherwise
234 * returns true if the browser is not found.
235 *
236 * @return {boolean} The current browser is in the support map
237 */
238 test: function ( map, profile, exactMatchOnly ) {
239 /*jshint evil: true */
240
241 var conditions, dir, i, op, val, j, pieceVersion, pieceVal, compare;
242 profile = $.isPlainObject( profile ) ? profile : $.client.profile();
243 if ( map.ltr && map.rtl ) {
244 dir = $( 'body' ).is( '.rtl' ) ? 'rtl' : 'ltr';
245 map = map[dir];
246 }
247 // Check over each browser condition to determine if we are running in a compatible client
248 if ( typeof map !== 'object' || map[profile.name] === undefined ) {
249 // Not found, return true if exactMatchOnly not set, false otherwise
250 return !exactMatchOnly;
251 }
252 conditions = map[profile.name];
253 if ( conditions === false ) {
254 // Match no versions
255 return false;
256 }
257 if ( conditions === null ) {
258 // Match all versions
259 return true;
260 }
261 for ( i = 0; i < conditions.length; i++ ) {
262 op = conditions[i][0];
263 val = conditions[i][1];
264 if ( typeof val === 'string' ) {
265 // Perform a component-wise comparison of versions, similar to PHP's version_compare
266 // but simpler. '1.11' is larger than '1.2'.
267 pieceVersion = profile.version.toString().split( '.' );
268 pieceVal = val.split( '.' );
269 // Extend with zeroes to equal length
270 while ( pieceVersion.length < pieceVal.length ) {
271 pieceVersion.push( '0' );
272 }
273 while ( pieceVal.length < pieceVersion.length ) {
274 pieceVal.push( '0' );
275 }
276 // Compare components
277 compare = 0;
278 for ( j = 0; j < pieceVersion.length; j++ ) {
279 if ( Number( pieceVersion[j] ) < Number( pieceVal[j] ) ) {
280 compare = -1;
281 break;
282 } else if ( Number( pieceVersion[j] ) > Number( pieceVal[j] ) ) {
283 compare = 1;
284 break;
285 }
286 }
287 // compare will be -1, 0 or 1, depending on comparison result
288 if ( !( eval( '' + compare + op + '0' ) ) ) {
289 return false;
290 }
291 } else if ( typeof val === 'number' ) {
292 if ( !( eval( 'profile.versionNumber' + op + val ) ) ) {
293 return false;
294 }
295 }
296 }
297
298 return true;
299 }
300 };
301 }( jQuery ) );