# lib/goog/labs/useragent/engine.js

    1// Copyright 2013 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 Closure user agent detection.17 * @see http://en.wikipedia.org/wiki/User_agent18 * For more information on browser brand, platform, or device see the other19 * sub-namespaces in goog.labs.userAgent (browser, platform, and device).20 *21 */2223goog.provide('goog.labs.userAgent.engine');2425goog.require('goog.array');26goog.require('goog.labs.userAgent.util');27goog.require('goog.string');282930/**31 * @return {boolean} Whether the rendering engine is Presto.32 */33goog.labs.userAgent.engine.isPresto = function() {34  return goog.labs.userAgent.util.matchUserAgent('Presto');35};363738/**39 * @return {boolean} Whether the rendering engine is Trident.40 */41goog.labs.userAgent.engine.isTrident = function() {42  // IE only started including the Trident token in IE8.43  return goog.labs.userAgent.util.matchUserAgent('Trident') ||44      goog.labs.userAgent.util.matchUserAgent('MSIE');45};464748/**49 * @return {boolean} Whether the rendering engine is WebKit.50 */51goog.labs.userAgent.engine.isWebKit = function() {52  return goog.labs.userAgent.util.matchUserAgentIgnoreCase('WebKit');53};545556/**57 * @return {boolean} Whether the rendering engine is Gecko.58 */59goog.labs.userAgent.engine.isGecko = function() {60  return goog.labs.userAgent.util.matchUserAgent('Gecko') &&61      !goog.labs.userAgent.engine.isWebKit() &&62      !goog.labs.userAgent.engine.isTrident();63};646566/**67 * @return {string} The rendering engine's version or empty string if version68 *     can't be determined.69 */70goog.labs.userAgent.engine.getVersion = function() {71  var userAgentString = goog.labs.userAgent.util.getUserAgent();72  if (userAgentString) {73    var tuples = goog.labs.userAgent.util.extractVersionTuples(74        userAgentString);7576    var engineTuple = tuples[1];77    if (engineTuple) {78      // In Gecko, the version string is either in the browser info or the79      // Firefox version.  See Gecko user agent string reference:80      // http://goo.gl/mULqa81      if (engineTuple[0] == 'Gecko') {82        return goog.labs.userAgent.engine.getVersionForKey_(83            tuples, 'Firefox');84      }8586      return engineTuple[1];87    }8889    // IE has only one version identifier, and the Trident version is90    // specified in the parenthetical.91    var browserTuple = tuples[0];92    var info;93    if (browserTuple && (info = browserTuple[2])) {94      var match = /Trident\/([^\s;]+)/.exec(info);95      if (match) {96        return match[1];97      }98    }99  }100  return '';101};102103104/**105 * @param {string|number} version The version to check.106 * @return {boolean} Whether the rendering engine version is higher or the same107 *     as the given version.108 */109goog.labs.userAgent.engine.isVersionOrHigher = function(version) {110  return goog.string.compareVersions(goog.labs.userAgent.engine.getVersion(),111                                     version) >= 0;112};113114115/**116 * @param {!Array.<!Array.<string>>} tuples Version tuples.117 * @param {string} key The key to look for.118 * @return {string} The version string of the given key, if present.119 *     Otherwise, the empty string.120 * @private121 */122goog.labs.userAgent.engine.getVersionForKey_ = function(tuples, key) {123  // TODO(nnaze): Move to util if useful elsewhere.124125  var pair = goog.array.find(tuples, function(pair) {126    return key == pair[0];127  });128129  return pair && pair[1] || '';130};