Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/labs/useragent/verifier.js
4565 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Methods to verify IE versions.
9
* TODO(johnlenz): delete this remove this file on the experiment is complete.
10
*/
11
12
goog.module('goog.labs.useragent.verifier');
13
goog.module.declareLegacyNamespace();
14
15
/** @const */
16
const NOT_IE = 0;
17
18
/**
19
* Detect the current IE version using runtime behavior, returns 0 if a version
20
* of IE is not detected.
21
* @return {number}
22
*/
23
function detectIeVersionByBehavior() {
24
if (document.all) {
25
if (!document.compatMode) {
26
return 5;
27
}
28
if (!window.XMLHttpRequest) {
29
return 6;
30
}
31
if (!document.querySelector) {
32
return 7;
33
}
34
if (!document.addEventListener) {
35
return 8;
36
}
37
if (!window.atob) {
38
return 9;
39
}
40
41
return 10;
42
}
43
if (!(window.ActiveXObject) && 'ActiveXObject' in window) {
44
return 11;
45
}
46
47
return NOT_IE;
48
}
49
50
/**
51
* Detect the current IE version using MSIE version presented in the user agent
52
* string (This will not detected IE 11 which does not present a MSIE version),
53
* or zero if IE is not detected.
54
* @return {number}
55
*/
56
function detectIeVersionByNavigator() {
57
const ua = navigator.userAgent.toLowerCase();
58
if (ua.indexOf('msie') != -1) {
59
const value = parseInt(ua.split('msie')[1], 10);
60
if (typeof value == 'number' && !isNaN(value)) {
61
return value;
62
}
63
}
64
65
return NOT_IE;
66
}
67
68
/**
69
* Correct the actual IE version based on the Trident version in the user agent
70
* string. This adjusts for IE's "compatiblity modes".
71
* @return {number}
72
*/
73
function getCorrectedIEVersionByNavigator() {
74
const ua = navigator.userAgent;
75
if (/Trident/.test(ua) || /MSIE/.test(ua)) {
76
return getIEVersion(ua);
77
} else {
78
return NOT_IE;
79
}
80
}
81
82
/**
83
* Get corrected IE version, see goog.labs.userAgent.browser.getIEVersion_
84
* @param {string} userAgent the User-Agent.
85
* @return {number}
86
*/
87
function getIEVersion(userAgent) {
88
// IE11 may identify itself as MSIE 9.0 or MSIE 10.0 due to an IE 11 upgrade
89
// bug. Example UA:
90
// Mozilla/5.0 (MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; rv:11.0)
91
// like Gecko.
92
const rv = /rv: *([\d\.]*)/.exec(userAgent);
93
if (rv && rv[1]) {
94
return Number(rv[1]);
95
}
96
97
const msie = /MSIE +([\d\.]+)/.exec(userAgent);
98
if (msie && msie[1]) {
99
// IE in compatibility mode usually identifies itself as MSIE 7.0; in this
100
// case, use the Trident version to determine the version of IE. For more
101
// details, see the links above.
102
const tridentVersion = /Trident\/(\d.\d)/.exec(userAgent);
103
if (msie[1] == '7.0') {
104
if (tridentVersion && tridentVersion[1]) {
105
switch (tridentVersion[1]) {
106
case '4.0':
107
return 8;
108
case '5.0':
109
return 9;
110
case '6.0':
111
return 10;
112
case '7.0':
113
return 11;
114
}
115
} else {
116
return 7;
117
}
118
} else {
119
return Number(msie[1]);
120
}
121
}
122
return NOT_IE;
123
}
124
125
exports = {
126
NOT_IE,
127
detectIeVersionByBehavior,
128
detectIeVersionByNavigator,
129
getCorrectedIEVersionByNavigator,
130
};
131
132