Path: blob/trunk/third_party/closure/goog/useragent/product_isversion.js
4116 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Functions for understanding the version of the browser.8* This is pulled out of product.js to ensure that only builds that need9* this functionality actually get it, without having to rely on the compiler10* to strip out unneeded pieces.11*12* TODO(nnaze): Move to more appropriate filename/namespace.13*/141516goog.provide('goog.userAgent.product.isVersion');171819goog.require('goog.labs.userAgent.platform');20goog.require('goog.string');21goog.require('goog.userAgent');22goog.require('goog.userAgent.product');232425/**26* @return {string} The string that describes the version number of the user27* agent product. This is a string rather than a number because it may28* contain 'b', 'a', and so on.29* @private30*/31goog.userAgent.product.determineVersion_ = function() {32'use strict';33// All browsers have different ways to detect the version and they all have34// different naming schemes.3536if (goog.userAgent.product.FIREFOX) {37// Firefox/2.0.0.1 or Firefox/3.5.338return goog.userAgent.product.getFirstRegExpGroup_(/Firefox\/([0-9.]+)/);39}4041if (goog.userAgent.product.IE || goog.userAgent.product.EDGE ||42goog.userAgent.product.OPERA) {43return goog.userAgent.VERSION;44}4546if (goog.userAgent.product.CHROME) {47// CriOS is Chrome on iOS, but iPadOS 13+ spoofs macOS by default.48// So it's possible that CriOS appears to be running on macOS.49if (goog.labs.userAgent.platform.isIos() ||50goog.labs.userAgent.platform.isMacintosh()) {51// CriOS/56.0.2924.7952const chromeIosVersion =53goog.userAgent.product.getFirstRegExpGroup_(/CriOS\/([0-9.]+)/);54if (chromeIosVersion) {55return chromeIosVersion;56}57}58// Chrome/4.0.223.159return goog.userAgent.product.getFirstRegExpGroup_(/Chrome\/([0-9.]+)/);60}6162// This replicates legacy logic, which considered Safari and iOS to be63// different products.64if (goog.userAgent.product.SAFARI && !goog.labs.userAgent.platform.isIos()) {65// Version/5.0.366//67// NOTE: Before version 3, Safari did not report a product version number.68// The product version number for these browsers will be the empty string.69// They may be differentiated by WebKit version number in goog.userAgent.70return goog.userAgent.product.getFirstRegExpGroup_(/Version\/([0-9.]+)/);71}7273if (goog.userAgent.product.IPHONE || goog.userAgent.product.IPAD) {74// Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.175// (KHTML, like Gecko) Version/3.0 Mobile/3A100a Safari/419.376// Version is the browser version, Mobile is the build number. We combine77// the version string with the build number: 3.0.3A100a for the example.78var arr =79goog.userAgent.product.execRegExp_(/Version\/(\S+).*Mobile\/(\S+)/);80if (arr) {81return arr[1] + '.' + arr[2];82}83} else if (goog.userAgent.product.ANDROID) {84// Mozilla/5.0 (Linux; U; Android 0.5; en-us) AppleWebKit/522+85// (KHTML, like Gecko) Safari/419.386//87// Mozilla/5.0 (Linux; U; Android 1.0; en-us; dream) AppleWebKit/525.10+88// (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.289//90// Prefer Version number if present, else make do with the OS number91var version =92goog.userAgent.product.getFirstRegExpGroup_(/Android\s+([0-9.]+)/);93if (version) {94return version;95}9697return goog.userAgent.product.getFirstRegExpGroup_(/Version\/([0-9.]+)/);98}99100return '';101};102103104/**105* Return the first group of the given regex.106* @param {!RegExp} re Regular expression with at least one group.107* @return {string} Contents of the first group or an empty string if no match.108* @private109*/110goog.userAgent.product.getFirstRegExpGroup_ = function(re) {111'use strict';112var arr = goog.userAgent.product.execRegExp_(re);113return arr ? arr[1] : '';114};115116117/**118* Run regexp's exec() on the userAgent string.119* @param {!RegExp} re Regular expression.120* @return {?IArrayLike<string>} A result array, or null for no match.121* @private122*/123goog.userAgent.product.execRegExp_ = function(re) {124'use strict';125return re.exec(goog.userAgent.getUserAgentString());126};127128129/**130* The version of the user agent. This is a string because it might contain131* 'b' (as in beta) as well as multiple dots.132* @type {string}133*/134goog.userAgent.product.VERSION = goog.userAgent.product.determineVersion_();135136137/**138* Whether the user agent product version is higher or the same as the given139* version.140*141* @param {string|number} version The version to check.142* @return {boolean} Whether the user agent product version is higher or the143* same as the given version.144*/145goog.userAgent.product.isVersion = function(version) {146'use strict';147return goog.string.compareVersions(goog.userAgent.product.VERSION, version) >=1480;149};150151152