Debian Codegraph @

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

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