Path: blob/trunk/third_party/closure/goog/labs/useragent/engine.js
2868 views
// Copyright 2013 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Closure user agent detection.16* @see http://en.wikipedia.org/wiki/User_agent17* For more information on browser brand, platform, or device see the other18* sub-namespaces in goog.labs.userAgent (browser, platform, and device).19*20*/2122goog.provide('goog.labs.userAgent.engine');2324goog.require('goog.array');25goog.require('goog.labs.userAgent.util');26goog.require('goog.string');272829/**30* @return {boolean} Whether the rendering engine is Presto.31*/32goog.labs.userAgent.engine.isPresto = function() {33return goog.labs.userAgent.util.matchUserAgent('Presto');34};353637/**38* @return {boolean} Whether the rendering engine is Trident.39*/40goog.labs.userAgent.engine.isTrident = function() {41// IE only started including the Trident token in IE8.42return goog.labs.userAgent.util.matchUserAgent('Trident') ||43goog.labs.userAgent.util.matchUserAgent('MSIE');44};454647/**48* @return {boolean} Whether the rendering engine is Edge.49*/50goog.labs.userAgent.engine.isEdge = function() {51return goog.labs.userAgent.util.matchUserAgent('Edge');52};535455/**56* @return {boolean} Whether the rendering engine is WebKit.57*/58goog.labs.userAgent.engine.isWebKit = function() {59return goog.labs.userAgent.util.matchUserAgentIgnoreCase('WebKit') &&60!goog.labs.userAgent.engine.isEdge();61};626364/**65* @return {boolean} Whether the rendering engine is Gecko.66*/67goog.labs.userAgent.engine.isGecko = function() {68return goog.labs.userAgent.util.matchUserAgent('Gecko') &&69!goog.labs.userAgent.engine.isWebKit() &&70!goog.labs.userAgent.engine.isTrident() &&71!goog.labs.userAgent.engine.isEdge();72};737475/**76* @return {string} The rendering engine's version or empty string if version77* can't be determined.78*/79goog.labs.userAgent.engine.getVersion = function() {80var userAgentString = goog.labs.userAgent.util.getUserAgent();81if (userAgentString) {82var tuples = goog.labs.userAgent.util.extractVersionTuples(userAgentString);8384var engineTuple = goog.labs.userAgent.engine.getEngineTuple_(tuples);85if (engineTuple) {86// In Gecko, the version string is either in the browser info or the87// Firefox version. See Gecko user agent string reference:88// http://goo.gl/mULqa89if (engineTuple[0] == 'Gecko') {90return goog.labs.userAgent.engine.getVersionForKey_(tuples, 'Firefox');91}9293return engineTuple[1];94}9596// MSIE has only one version identifier, and the Trident version is97// specified in the parenthetical. IE Edge is covered in the engine tuple98// detection.99var browserTuple = tuples[0];100var info;101if (browserTuple && (info = browserTuple[2])) {102var match = /Trident\/([^\s;]+)/.exec(info);103if (match) {104return match[1];105}106}107}108return '';109};110111112/**113* @param {!Array<!Array<string>>} tuples Extracted version tuples.114* @return {!Array<string>|undefined} The engine tuple or undefined if not115* found.116* @private117*/118goog.labs.userAgent.engine.getEngineTuple_ = function(tuples) {119if (!goog.labs.userAgent.engine.isEdge()) {120return tuples[1];121}122for (var i = 0; i < tuples.length; i++) {123var tuple = tuples[i];124if (tuple[0] == 'Edge') {125return tuple;126}127}128};129130131/**132* @param {string|number} version The version to check.133* @return {boolean} Whether the rendering engine version is higher or the same134* as the given version.135*/136goog.labs.userAgent.engine.isVersionOrHigher = function(version) {137return goog.string.compareVersions(138goog.labs.userAgent.engine.getVersion(), version) >= 0;139};140141142/**143* @param {!Array<!Array<string>>} tuples Version tuples.144* @param {string} key The key to look for.145* @return {string} The version string of the given key, if present.146* Otherwise, the empty string.147* @private148*/149goog.labs.userAgent.engine.getVersionForKey_ = function(tuples, key) {150// TODO(nnaze): Move to util if useful elsewhere.151152var pair = goog.array.find(tuples, function(pair) { return key == pair[0]; });153154return pair && pair[1] || '';155};156157158