# lib/goog/useragent/product_isversion.js

    1// Copyright 2009 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 at6//7//      http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// 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 and13// limitations under the License.1415/**16 * @fileoverview Functions for understanding the version of the browser.17 * This is pulled out of product.js to ensure that only builds that need18 * this functionality actually get it, without having to rely on the compiler19 * to strip out unneeded pieces.20 *21 * TODO(nnaze): Move to more appropriate filename/namespace.22 *23 */242526goog.provide('goog.userAgent.product.isVersion');272829goog.require('goog.userAgent.product');303132/**33 * @return {string} The string that describes the version number of the user34 *     agent product.  This is a string rather than a number because it may35 *     contain 'b', 'a', and so on.36 * @private37 */38goog.userAgent.product.determineVersion_ = function() {39  // All browsers have different ways to detect the version and they all have40  // different naming schemes.4142  if (goog.userAgent.product.FIREFOX) {43    // Firefox/2.0.0.1 or Firefox/3.5.344    return goog.userAgent.product.getFirstRegExpGroup_(/Firefox\/([0-9.]+)/);45  }4647  if (goog.userAgent.product.IE || goog.userAgent.product.OPERA) {48    return goog.userAgent.VERSION;49  }5051  if (goog.userAgent.product.CHROME) {52    // Chrome/4.0.223.153    return goog.userAgent.product.getFirstRegExpGroup_(/Chrome\/([0-9.]+)/);54  }5556  if (goog.userAgent.product.SAFARI) {57    // Version/5.0.358    //59    // NOTE: Before version 3, Safari did not report a product version number.60    // The product version number for these browsers will be the empty string.61    // They may be differentiated by WebKit version number in goog.userAgent.62    return goog.userAgent.product.getFirstRegExpGroup_(/Version\/([0-9.]+)/);63  }6465  if (goog.userAgent.product.IPHONE || goog.userAgent.product.IPAD) {66    // Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.167    // (KHTML, like Gecko) Version/3.0 Mobile/3A100a Safari/419.368    // Version is the browser version, Mobile is the build number. We combine69    // the version string with the build number: 3.0.3A100a for the example.70    var arr = goog.userAgent.product.execRegExp_(71        /Version\/(\S+).*Mobile\/(\S+)/);72    if (arr) {73      return arr[1] + '.' + arr[2];74    }75  } else if (goog.userAgent.product.ANDROID) {76    // Mozilla/5.0 (Linux; U; Android 0.5; en-us) AppleWebKit/522+77    // (KHTML, like Gecko) Safari/419.378    //79    // Mozilla/5.0 (Linux; U; Android 1.0; en-us; dream) AppleWebKit/525.10+80    // (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.281    //82    // Prefer Version number if present, else make do with the OS number83    var version = goog.userAgent.product.getFirstRegExpGroup_(84        /Android\s+([0-9.]+)/);85    if (version) {86      return version;87    }8889    return goog.userAgent.product.getFirstRegExpGroup_(/Version\/([0-9.]+)/);90  } else if (goog.userAgent.product.CAMINO) {91    return goog.userAgent.product.getFirstRegExpGroup_(/Camino\/([0-9.]+)/);92  }9394  return '';95};969798/**99 * Return the first group of the given regex.100 * @param {!RegExp} re Regular expression with at least one group.101 * @return {string} Contents of the first group or an empty string if no match.102 * @private103 */104goog.userAgent.product.getFirstRegExpGroup_ = function(re) {105  var arr = goog.userAgent.product.execRegExp_(re);106  return arr ? arr[1] : '';107};108109110/**111 * Run regexp's exec() on the userAgent string.112 * @param {!RegExp} re Regular expression.113 * @return {Array} A result array, or null for no match.114 * @private115 */116goog.userAgent.product.execRegExp_ = function(re) {117  return re.exec(goog.userAgent.getUserAgentString());118};119120121/**122 * The version of the user agent. This is a string because it might contain123 * 'b' (as in beta) as well as multiple dots.124 * @type {string}125 */126goog.userAgent.product.VERSION = goog.userAgent.product.determineVersion_();127128129/**130 * Whether the user agent product version is higher or the same as the given131 * version.132 *133 * @param {string|number} version The version to check.134 * @return {boolean} Whether the user agent product version is higher or the135 *     same as the given version.136 */137goog.userAgent.product.isVersion = function(version) {138  return goog.string.compareVersions(139      goog.userAgent.product.VERSION, version) >= 0;140};