Debian Codegraph @

closure-library/closure/goog/useragent/useragent.js

raw
1 // Copyright 2006 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Rendering engine detection.
17 * @see <a href="http://www.useragentstring.com/">User agent strings</a>
18 * For information on the browser brand (such as Safari versus Chrome), see
19 * goog.userAgent.product.
20 * @author [email protected] (Erik Arvidsson)
21 * @see ../demos/useragent.html
22 */
23
24 goog.provide('goog.userAgent');
25
26 goog.require('goog.labs.userAgent.browser');
27 goog.require('goog.labs.userAgent.engine');
28 goog.require('goog.labs.userAgent.platform');
29 goog.require('goog.labs.userAgent.util');
30 goog.require('goog.reflect');
31 goog.require('goog.string');
32
33
34 /**
35 * @define {boolean} Whether we know at compile-time that the browser is IE.
36 */
37 goog.define('goog.userAgent.ASSUME_IE', false);
38
39
40 /**
41 * @define {boolean} Whether we know at compile-time that the browser is EDGE.
42 */
43 goog.define('goog.userAgent.ASSUME_EDGE', false);
44
45
46 /**
47 * @define {boolean} Whether we know at compile-time that the browser is GECKO.
48 */
49 goog.define('goog.userAgent.ASSUME_GECKO', false);
50
51
52 /**
53 * @define {boolean} Whether we know at compile-time that the browser is WEBKIT.
54 */
55 goog.define('goog.userAgent.ASSUME_WEBKIT', false);
56
57
58 /**
59 * @define {boolean} Whether we know at compile-time that the browser is a
60 * mobile device running WebKit e.g. iPhone or Android.
61 */
62 goog.define('goog.userAgent.ASSUME_MOBILE_WEBKIT', false);
63
64
65 /**
66 * @define {boolean} Whether we know at compile-time that the browser is OPERA.
67 */
68 goog.define('goog.userAgent.ASSUME_OPERA', false);
69
70
71 /**
72 * @define {boolean} Whether the
73 * {@code goog.userAgent.isVersionOrHigher}
74 * function will return true for any version.
75 */
76 goog.define('goog.userAgent.ASSUME_ANY_VERSION', false);
77
78
79 /**
80 * Whether we know the browser engine at compile-time.
81 * @type {boolean}
82 * @private
83 */
84 goog.userAgent.BROWSER_KNOWN_ = goog.userAgent.ASSUME_IE ||
85 goog.userAgent.ASSUME_EDGE || goog.userAgent.ASSUME_GECKO ||
86 goog.userAgent.ASSUME_MOBILE_WEBKIT || goog.userAgent.ASSUME_WEBKIT ||
87 goog.userAgent.ASSUME_OPERA;
88
89
90 /**
91 * Returns the userAgent string for the current browser.
92 *
93 * @return {string} The userAgent string.
94 */
95 goog.userAgent.getUserAgentString = function() {
96 return goog.labs.userAgent.util.getUserAgent();
97 };
98
99
100 /**
101 * TODO(nnaze): Change type to "Navigator" and update compilation targets.
102 * @return {?Object} The native navigator object.
103 */
104 goog.userAgent.getNavigator = function() {
105 // Need a local navigator reference instead of using the global one,
106 // to avoid the rare case where they reference different objects.
107 // (in a WorkerPool, for example).
108 return goog.global['navigator'] || null;
109 };
110
111
112 /**
113 * Whether the user agent is Opera.
114 * @type {boolean}
115 */
116 goog.userAgent.OPERA = goog.userAgent.BROWSER_KNOWN_ ?
117 goog.userAgent.ASSUME_OPERA :
118 goog.labs.userAgent.browser.isOpera();
119
120
121 /**
122 * Whether the user agent is Internet Explorer.
123 * @type {boolean}
124 */
125 goog.userAgent.IE = goog.userAgent.BROWSER_KNOWN_ ?
126 goog.userAgent.ASSUME_IE :
127 goog.labs.userAgent.browser.isIE();
128
129
130 /**
131 * Whether the user agent is Microsoft Edge.
132 * @type {boolean}
133 */
134 goog.userAgent.EDGE = goog.userAgent.BROWSER_KNOWN_ ?
135 goog.userAgent.ASSUME_EDGE :
136 goog.labs.userAgent.engine.isEdge();
137
138
139 /**
140 * Whether the user agent is MS Internet Explorer or MS Edge.
141 * @type {boolean}
142 */
143 goog.userAgent.EDGE_OR_IE = goog.userAgent.EDGE || goog.userAgent.IE;
144
145
146 /**
147 * Whether the user agent is Gecko. Gecko is the rendering engine used by
148 * Mozilla, Firefox, and others.
149 * @type {boolean}
150 */
151 goog.userAgent.GECKO = goog.userAgent.BROWSER_KNOWN_ ?
152 goog.userAgent.ASSUME_GECKO :
153 goog.labs.userAgent.engine.isGecko();
154
155
156 /**
157 * Whether the user agent is WebKit. WebKit is the rendering engine that
158 * Safari, Android and others use.
159 * @type {boolean}
160 */
161 goog.userAgent.WEBKIT = goog.userAgent.BROWSER_KNOWN_ ?
162 goog.userAgent.ASSUME_WEBKIT || goog.userAgent.ASSUME_MOBILE_WEBKIT :
163 goog.labs.userAgent.engine.isWebKit();
164
165
166 /**
167 * Whether the user agent is running on a mobile device.
168 *
169 * This is a separate function so that the logic can be tested.
170 *
171 * TODO(nnaze): Investigate swapping in goog.labs.userAgent.device.isMobile().
172 *
173 * @return {boolean} Whether the user agent is running on a mobile device.
174 * @private
175 */
176 goog.userAgent.isMobile_ = function() {
177 return goog.userAgent.WEBKIT &&
178 goog.labs.userAgent.util.matchUserAgent('Mobile');
179 };
180
181
182 /**
183 * Whether the user agent is running on a mobile device.
184 *
185 * TODO(nnaze): Consider deprecating MOBILE when labs.userAgent
186 * is promoted as the gecko/webkit logic is likely inaccurate.
187 *
188 * @type {boolean}
189 */
190 goog.userAgent.MOBILE =
191 goog.userAgent.ASSUME_MOBILE_WEBKIT || goog.userAgent.isMobile_();
192
193
194 /**
195 * Used while transitioning code to use WEBKIT instead.
196 * @type {boolean}
197 * @deprecated Use {@link goog.userAgent.product.SAFARI} instead.
198 * TODO(nicksantos): Delete this from goog.userAgent.
199 */
200 goog.userAgent.SAFARI = goog.userAgent.WEBKIT;
201
202
203 /**
204 * @return {string} the platform (operating system) the user agent is running
205 * on. Default to empty string because navigator.platform may not be defined
206 * (on Rhino, for example).
207 * @private
208 */
209 goog.userAgent.determinePlatform_ = function() {
210 var navigator = goog.userAgent.getNavigator();
211 return navigator && navigator.platform || '';
212 };
213
214
215 /**
216 * The platform (operating system) the user agent is running on. Default to
217 * empty string because navigator.platform may not be defined (on Rhino, for
218 * example).
219 * @type {string}
220 */
221 goog.userAgent.PLATFORM = goog.userAgent.determinePlatform_();
222
223
224 /**
225 * @define {boolean} Whether the user agent is running on a Macintosh operating
226 * system.
227 */
228 goog.define('goog.userAgent.ASSUME_MAC', false);
229
230
231 /**
232 * @define {boolean} Whether the user agent is running on a Windows operating
233 * system.
234 */
235 goog.define('goog.userAgent.ASSUME_WINDOWS', false);
236
237
238 /**
239 * @define {boolean} Whether the user agent is running on a Linux operating
240 * system.
241 */
242 goog.define('goog.userAgent.ASSUME_LINUX', false);
243
244
245 /**
246 * @define {boolean} Whether the user agent is running on a X11 windowing
247 * system.
248 */
249 goog.define('goog.userAgent.ASSUME_X11', false);
250
251
252 /**
253 * @define {boolean} Whether the user agent is running on Android.
254 */
255 goog.define('goog.userAgent.ASSUME_ANDROID', false);
256
257
258 /**
259 * @define {boolean} Whether the user agent is running on an iPhone.
260 */
261 goog.define('goog.userAgent.ASSUME_IPHONE', false);
262
263
264 /**
265 * @define {boolean} Whether the user agent is running on an iPad.
266 */
267 goog.define('goog.userAgent.ASSUME_IPAD', false);
268
269
270 /**
271 * @define {boolean} Whether the user agent is running on an iPod.
272 */
273 goog.define('goog.userAgent.ASSUME_IPOD', false);
274
275
276 /**
277 * @type {boolean}
278 * @private
279 */
280 goog.userAgent.PLATFORM_KNOWN_ = goog.userAgent.ASSUME_MAC ||
281 goog.userAgent.ASSUME_WINDOWS || goog.userAgent.ASSUME_LINUX ||
282 goog.userAgent.ASSUME_X11 || goog.userAgent.ASSUME_ANDROID ||
283 goog.userAgent.ASSUME_IPHONE || goog.userAgent.ASSUME_IPAD ||
284 goog.userAgent.ASSUME_IPOD;
285
286
287 /**
288 * Whether the user agent is running on a Macintosh operating system.
289 * @type {boolean}
290 */
291 goog.userAgent.MAC = goog.userAgent.PLATFORM_KNOWN_ ?
292 goog.userAgent.ASSUME_MAC :
293 goog.labs.userAgent.platform.isMacintosh();
294
295
296 /**
297 * Whether the user agent is running on a Windows operating system.
298 * @type {boolean}
299 */
300 goog.userAgent.WINDOWS = goog.userAgent.PLATFORM_KNOWN_ ?
301 goog.userAgent.ASSUME_WINDOWS :
302 goog.labs.userAgent.platform.isWindows();
303
304
305 /**
306 * Whether the user agent is Linux per the legacy behavior of
307 * goog.userAgent.LINUX, which considered ChromeOS to also be
308 * Linux.
309 * @return {boolean}
310 * @private
311 */
312 goog.userAgent.isLegacyLinux_ = function() {
313 return goog.labs.userAgent.platform.isLinux() ||
314 goog.labs.userAgent.platform.isChromeOS();
315 };
316
317
318 /**
319 * Whether the user agent is running on a Linux operating system.
320 *
321 * Note that goog.userAgent.LINUX considers ChromeOS to be Linux,
322 * while goog.labs.userAgent.platform considers ChromeOS and
323 * Linux to be different OSes.
324 *
325 * @type {boolean}
326 */
327 goog.userAgent.LINUX = goog.userAgent.PLATFORM_KNOWN_ ?
328 goog.userAgent.ASSUME_LINUX :
329 goog.userAgent.isLegacyLinux_();
330
331
332 /**
333 * @return {boolean} Whether the user agent is an X11 windowing system.
334 * @private
335 */
336 goog.userAgent.isX11_ = function() {
337 var navigator = goog.userAgent.getNavigator();
338 return !!navigator &&
339 goog.string.contains(navigator['appVersion'] || '', 'X11');
340 };
341
342
343 /**
344 * Whether the user agent is running on a X11 windowing system.
345 * @type {boolean}
346 */
347 goog.userAgent.X11 = goog.userAgent.PLATFORM_KNOWN_ ?
348 goog.userAgent.ASSUME_X11 :
349 goog.userAgent.isX11_();
350
351
352 /**
353 * Whether the user agent is running on Android.
354 * @type {boolean}
355 */
356 goog.userAgent.ANDROID = goog.userAgent.PLATFORM_KNOWN_ ?
357 goog.userAgent.ASSUME_ANDROID :
358 goog.labs.userAgent.platform.isAndroid();
359
360
361 /**
362 * Whether the user agent is running on an iPhone.
363 * @type {boolean}
364 */
365 goog.userAgent.IPHONE = goog.userAgent.PLATFORM_KNOWN_ ?
366 goog.userAgent.ASSUME_IPHONE :
367 goog.labs.userAgent.platform.isIphone();
368
369
370 /**
371 * Whether the user agent is running on an iPad.
372 * @type {boolean}
373 */
374 goog.userAgent.IPAD = goog.userAgent.PLATFORM_KNOWN_ ?
375 goog.userAgent.ASSUME_IPAD :
376 goog.labs.userAgent.platform.isIpad();
377
378
379 /**
380 * Whether the user agent is running on an iPod.
381 * @type {boolean}
382 */
383 goog.userAgent.IPOD = goog.userAgent.PLATFORM_KNOWN_ ?
384 goog.userAgent.ASSUME_IPOD :
385 goog.labs.userAgent.platform.isIpod();
386
387
388 /**
389 * Whether the user agent is running on iOS.
390 * @type {boolean}
391 */
392 goog.userAgent.IOS = goog.userAgent.PLATFORM_KNOWN_ ?
393 (goog.userAgent.ASSUME_IPHONE || goog.userAgent.ASSUME_IPAD ||
394 goog.userAgent.ASSUME_IPOD) :
395 goog.labs.userAgent.platform.isIos();
396
397 /**
398 * @return {string} The string that describes the version number of the user
399 * agent.
400 * @private
401 */
402 goog.userAgent.determineVersion_ = function() {
403 // All browsers have different ways to detect the version and they all have
404 // different naming schemes.
405 // version is a string rather than a number because it may contain 'b', 'a',
406 // and so on.
407 var version = '';
408 var arr = goog.userAgent.getVersionRegexResult_();
409 if (arr) {
410 version = arr ? arr[1] : '';
411 }
412
413 if (goog.userAgent.IE) {
414 // IE9 can be in document mode 9 but be reporting an inconsistent user agent
415 // version. If it is identifying as a version lower than 9 we take the
416 // documentMode as the version instead. IE8 has similar behavior.
417 // It is recommended to set the X-UA-Compatible header to ensure that IE9
418 // uses documentMode 9.
419 var docMode = goog.userAgent.getDocumentMode_();
420 if (docMode != null && docMode > parseFloat(version)) {
421 return String(docMode);
422 }
423 }
424
425 return version;
426 };
427
428
429 /**
430 * @return {?Array|undefined} The version regex matches from parsing the user
431 * agent string. These regex statements must be executed inline so they can
432 * be compiled out by the closure compiler with the rest of the useragent
433 * detection logic when ASSUME_* is specified.
434 * @private
435 */
436 goog.userAgent.getVersionRegexResult_ = function() {
437 var userAgent = goog.userAgent.getUserAgentString();
438 if (goog.userAgent.GECKO) {
439 return /rv\:([^\);]+)(\)|;)/.exec(userAgent);
440 }
441 if (goog.userAgent.EDGE) {
442 return /Edge\/([\d\.]+)/.exec(userAgent);
443 }
444 if (goog.userAgent.IE) {
445 return /\b(?:MSIE|rv)[: ]([^\);]+)(\)|;)/.exec(userAgent);
446 }
447 if (goog.userAgent.WEBKIT) {
448 // WebKit/125.4
449 return /WebKit\/(\S+)/.exec(userAgent);
450 }
451 if (goog.userAgent.OPERA) {
452 // If none of the above browsers were detected but the browser is Opera, the
453 // only string that is of interest is 'Version/<number>'.
454 return /(?:Version)[ \/]?(\S+)/.exec(userAgent);
455 }
456 return undefined;
457 };
458
459
460 /**
461 * @return {number|undefined} Returns the document mode (for testing).
462 * @private
463 */
464 goog.userAgent.getDocumentMode_ = function() {
465 // NOTE(user): goog.userAgent may be used in context where there is no DOM.
466 var doc = goog.global['document'];
467 return doc ? doc['documentMode'] : undefined;
468 };
469
470
471 /**
472 * The version of the user agent. This is a string because it might contain
473 * 'b' (as in beta) as well as multiple dots.
474 * @type {string}
475 */
476 goog.userAgent.VERSION = goog.userAgent.determineVersion_();
477
478
479 /**
480 * Compares two version numbers.
481 *
482 * @param {string} v1 Version of first item.
483 * @param {string} v2 Version of second item.
484 *
485 * @return {number} 1 if first argument is higher
486 * 0 if arguments are equal
487 * -1 if second argument is higher.
488 * @deprecated Use goog.string.compareVersions.
489 */
490 goog.userAgent.compare = function(v1, v2) {
491 return goog.string.compareVersions(v1, v2);
492 };
493
494
495 /**
496 * Cache for {@link goog.userAgent.isVersionOrHigher}.
497 * Calls to compareVersions are surprisingly expensive and, as a browser's
498 * version number is unlikely to change during a session, we cache the results.
499 * @const
500 * @private
501 */
502 goog.userAgent.isVersionOrHigherCache_ = {};
503
504
505 /**
506 * Whether the user agent version is higher or the same as the given version.
507 * NOTE: When checking the version numbers for Firefox and Safari, be sure to
508 * use the engine's version, not the browser's version number. For example,
509 * Firefox 3.0 corresponds to Gecko 1.9 and Safari 3.0 to Webkit 522.11.
510 * Opera and Internet Explorer versions match the product release number.<br>
511 * @see <a href="http://en.wikipedia.org/wiki/Safari_version_history">
512 * Webkit</a>
513 * @see <a href="http://en.wikipedia.org/wiki/Gecko_engine">Gecko</a>
514 *
515 * @param {string|number} version The version to check.
516 * @return {boolean} Whether the user agent version is higher or the same as
517 * the given version.
518 */
519 goog.userAgent.isVersionOrHigher = function(version) {
520 return goog.userAgent.ASSUME_ANY_VERSION ||
521 goog.reflect.cache(
522 goog.userAgent.isVersionOrHigherCache_, version, function() {
523 return goog.string.compareVersions(
524 goog.userAgent.VERSION, version) >= 0;
525 });
526 };
527
528
529 /**
530 * Deprecated alias to {@code goog.userAgent.isVersionOrHigher}.
531 * @param {string|number} version The version to check.
532 * @return {boolean} Whether the user agent version is higher or the same as
533 * the given version.
534 * @deprecated Use goog.userAgent.isVersionOrHigher().
535 */
536 goog.userAgent.isVersion = goog.userAgent.isVersionOrHigher;
537
538
539 /**
540 * Whether the IE effective document mode is higher or the same as the given
541 * document mode version.
542 * NOTE: Only for IE, return false for another browser.
543 *
544 * @param {number} documentMode The document mode version to check.
545 * @return {boolean} Whether the IE effective document mode is higher or the
546 * same as the given version.
547 */
548 goog.userAgent.isDocumentModeOrHigher = function(documentMode) {
549 return Number(goog.userAgent.DOCUMENT_MODE) >= documentMode;
550 };
551
552
553 /**
554 * Deprecated alias to {@code goog.userAgent.isDocumentModeOrHigher}.
555 * @param {number} version The version to check.
556 * @return {boolean} Whether the IE effective document mode is higher or the
557 * same as the given version.
558 * @deprecated Use goog.userAgent.isDocumentModeOrHigher().
559 */
560 goog.userAgent.isDocumentMode = goog.userAgent.isDocumentModeOrHigher;
561
562
563 /**
564 * For IE version < 7, documentMode is undefined, so attempt to use the
565 * CSS1Compat property to see if we are in standards mode. If we are in
566 * standards mode, treat the browser version as the document mode. Otherwise,
567 * IE is emulating version 5.
568 * @type {number|undefined}
569 * @const
570 */
571 goog.userAgent.DOCUMENT_MODE = (function() {
572 var doc = goog.global['document'];
573 var mode = goog.userAgent.getDocumentMode_();
574 if (!doc || !goog.userAgent.IE) {
575 return undefined;
576 }
577 return mode || (doc['compatMode'] == 'CSS1Compat' ?
578 parseInt(goog.userAgent.VERSION, 10) :
579 5);
580 })();