Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/useragent/product_isversion.js
4116 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Functions for understanding the version of the browser.
9
* This is pulled out of product.js to ensure that only builds that need
10
* this functionality actually get it, without having to rely on the compiler
11
* to strip out unneeded pieces.
12
*
13
* TODO(nnaze): Move to more appropriate filename/namespace.
14
*/
15
16
17
goog.provide('goog.userAgent.product.isVersion');
18
19
20
goog.require('goog.labs.userAgent.platform');
21
goog.require('goog.string');
22
goog.require('goog.userAgent');
23
goog.require('goog.userAgent.product');
24
25
26
/**
27
* @return {string} The string that describes the version number of the user
28
* agent product. This is a string rather than a number because it may
29
* contain 'b', 'a', and so on.
30
* @private
31
*/
32
goog.userAgent.product.determineVersion_ = function() {
33
'use strict';
34
// All browsers have different ways to detect the version and they all have
35
// different naming schemes.
36
37
if (goog.userAgent.product.FIREFOX) {
38
// Firefox/2.0.0.1 or Firefox/3.5.3
39
return goog.userAgent.product.getFirstRegExpGroup_(/Firefox\/([0-9.]+)/);
40
}
41
42
if (goog.userAgent.product.IE || goog.userAgent.product.EDGE ||
43
goog.userAgent.product.OPERA) {
44
return goog.userAgent.VERSION;
45
}
46
47
if (goog.userAgent.product.CHROME) {
48
// CriOS is Chrome on iOS, but iPadOS 13+ spoofs macOS by default.
49
// So it's possible that CriOS appears to be running on macOS.
50
if (goog.labs.userAgent.platform.isIos() ||
51
goog.labs.userAgent.platform.isMacintosh()) {
52
// CriOS/56.0.2924.79
53
const chromeIosVersion =
54
goog.userAgent.product.getFirstRegExpGroup_(/CriOS\/([0-9.]+)/);
55
if (chromeIosVersion) {
56
return chromeIosVersion;
57
}
58
}
59
// Chrome/4.0.223.1
60
return goog.userAgent.product.getFirstRegExpGroup_(/Chrome\/([0-9.]+)/);
61
}
62
63
// This replicates legacy logic, which considered Safari and iOS to be
64
// different products.
65
if (goog.userAgent.product.SAFARI && !goog.labs.userAgent.platform.isIos()) {
66
// Version/5.0.3
67
//
68
// NOTE: Before version 3, Safari did not report a product version number.
69
// The product version number for these browsers will be the empty string.
70
// They may be differentiated by WebKit version number in goog.userAgent.
71
return goog.userAgent.product.getFirstRegExpGroup_(/Version\/([0-9.]+)/);
72
}
73
74
if (goog.userAgent.product.IPHONE || goog.userAgent.product.IPAD) {
75
// Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1
76
// (KHTML, like Gecko) Version/3.0 Mobile/3A100a Safari/419.3
77
// Version is the browser version, Mobile is the build number. We combine
78
// the version string with the build number: 3.0.3A100a for the example.
79
var arr =
80
goog.userAgent.product.execRegExp_(/Version\/(\S+).*Mobile\/(\S+)/);
81
if (arr) {
82
return arr[1] + '.' + arr[2];
83
}
84
} else if (goog.userAgent.product.ANDROID) {
85
// Mozilla/5.0 (Linux; U; Android 0.5; en-us) AppleWebKit/522+
86
// (KHTML, like Gecko) Safari/419.3
87
//
88
// Mozilla/5.0 (Linux; U; Android 1.0; en-us; dream) AppleWebKit/525.10+
89
// (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2
90
//
91
// Prefer Version number if present, else make do with the OS number
92
var version =
93
goog.userAgent.product.getFirstRegExpGroup_(/Android\s+([0-9.]+)/);
94
if (version) {
95
return version;
96
}
97
98
return goog.userAgent.product.getFirstRegExpGroup_(/Version\/([0-9.]+)/);
99
}
100
101
return '';
102
};
103
104
105
/**
106
* Return the first group of the given regex.
107
* @param {!RegExp} re Regular expression with at least one group.
108
* @return {string} Contents of the first group or an empty string if no match.
109
* @private
110
*/
111
goog.userAgent.product.getFirstRegExpGroup_ = function(re) {
112
'use strict';
113
var arr = goog.userAgent.product.execRegExp_(re);
114
return arr ? arr[1] : '';
115
};
116
117
118
/**
119
* Run regexp's exec() on the userAgent string.
120
* @param {!RegExp} re Regular expression.
121
* @return {?IArrayLike<string>} A result array, or null for no match.
122
* @private
123
*/
124
goog.userAgent.product.execRegExp_ = function(re) {
125
'use strict';
126
return re.exec(goog.userAgent.getUserAgentString());
127
};
128
129
130
/**
131
* The version of the user agent. This is a string because it might contain
132
* 'b' (as in beta) as well as multiple dots.
133
* @type {string}
134
*/
135
goog.userAgent.product.VERSION = goog.userAgent.product.determineVersion_();
136
137
138
/**
139
* Whether the user agent product version is higher or the same as the given
140
* version.
141
*
142
* @param {string|number} version The version to check.
143
* @return {boolean} Whether the user agent product version is higher or the
144
* same as the given version.
145
*/
146
goog.userAgent.product.isVersion = function(version) {
147
'use strict';
148
return goog.string.compareVersions(goog.userAgent.product.VERSION, version) >=
149
0;
150
};
151
152