Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/labs/useragent/engine.js
4116 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Closure user agent detection.
9
* @see http://en.wikipedia.org/wiki/User_agent
10
* For more information on browser brand, platform, or device see the other
11
* sub-namespaces in goog.labs.userAgent (browser, platform, and device).
12
*/
13
14
goog.module('goog.labs.userAgent.engine');
15
goog.module.declareLegacyNamespace();
16
17
const googArray = goog.require('goog.array');
18
const googString = goog.require('goog.string.internal');
19
const util = goog.require('goog.labs.userAgent.util');
20
21
/**
22
* @return {boolean} Whether the rendering engine is Presto.
23
*/
24
function isPresto() {
25
return util.matchUserAgent('Presto');
26
}
27
28
/**
29
* @return {boolean} Whether the rendering engine is Trident.
30
*/
31
function isTrident() {
32
// IE only started including the Trident token in IE8.
33
return util.matchUserAgent('Trident') || util.matchUserAgent('MSIE');
34
}
35
36
/**
37
* @return {boolean} Whether the rendering engine is EdgeHTML.
38
*/
39
function isEdge() {
40
return util.matchUserAgent('Edge');
41
}
42
43
/**
44
* @return {boolean} Whether the rendering engine is WebKit. This will return
45
* true for Chrome, Blink-based Opera (15+), Edge Chromium and Safari.
46
*/
47
function isWebKit() {
48
return util.matchUserAgentIgnoreCase('WebKit') && !isEdge();
49
}
50
51
/**
52
* @return {boolean} Whether the rendering engine is Gecko.
53
*/
54
function isGecko() {
55
return util.matchUserAgent('Gecko') && !isWebKit() && !isTrident() &&
56
!isEdge();
57
}
58
59
/**
60
* @return {string} The rendering engine's version or empty string if version
61
* can't be determined.
62
*/
63
function getVersion() {
64
const userAgentString = util.getUserAgent();
65
if (userAgentString) {
66
const tuples = util.extractVersionTuples(userAgentString);
67
68
const engineTuple = getEngineTuple(tuples);
69
if (engineTuple) {
70
// In Gecko, the version string is either in the browser info or the
71
// Firefox version. See Gecko user agent string reference:
72
// http://goo.gl/mULqa
73
if (engineTuple[0] == 'Gecko') {
74
return getVersionForKey(tuples, 'Firefox');
75
}
76
77
return engineTuple[1];
78
}
79
80
// MSIE has only one version identifier, and the Trident version is
81
// specified in the parenthetical. IE Edge is covered in the engine tuple
82
// detection.
83
const browserTuple = tuples[0];
84
let info;
85
if (browserTuple && (info = browserTuple[2])) {
86
const match = /Trident\/([^\s;]+)/.exec(info);
87
if (match) {
88
return match[1];
89
}
90
}
91
}
92
return '';
93
}
94
95
/**
96
* @param {!Array<!Array<string>>} tuples Extracted version tuples.
97
* @return {!Array<string>|undefined} The engine tuple or undefined if not
98
* found.
99
*/
100
function getEngineTuple(tuples) {
101
if (!isEdge()) {
102
return tuples[1];
103
}
104
for (let i = 0; i < tuples.length; i++) {
105
const tuple = tuples[i];
106
if (tuple[0] == 'Edge') {
107
return tuple;
108
}
109
}
110
}
111
112
/**
113
* @param {string|number} version The version to check.
114
* @return {boolean} Whether the rendering engine version is higher or the same
115
* as the given version.
116
*/
117
function isVersionOrHigher(version) {
118
return googString.compareVersions(getVersion(), version) >= 0;
119
}
120
121
/**
122
* @param {!Array<!Array<string>>} tuples Version tuples.
123
* @param {string} key The key to look for.
124
* @return {string} The version string of the given key, if present.
125
* Otherwise, the empty string.
126
*/
127
function getVersionForKey(tuples, key) {
128
// TODO(nnaze): Move to util if useful elsewhere.
129
130
const pair = googArray.find(tuples, function(pair) {
131
return key == pair[0];
132
});
133
134
return pair && pair[1] || '';
135
}
136
137
exports = {
138
getVersion,
139
isEdge,
140
isGecko,
141
isPresto,
142
isTrident,
143
isVersionOrHigher,
144
isWebKit,
145
};
146
147