Path: blob/trunk/third_party/closure/goog/labs/useragent/engine.js
4116 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Closure user agent detection.8* @see http://en.wikipedia.org/wiki/User_agent9* For more information on browser brand, platform, or device see the other10* sub-namespaces in goog.labs.userAgent (browser, platform, and device).11*/1213goog.module('goog.labs.userAgent.engine');14goog.module.declareLegacyNamespace();1516const googArray = goog.require('goog.array');17const googString = goog.require('goog.string.internal');18const util = goog.require('goog.labs.userAgent.util');1920/**21* @return {boolean} Whether the rendering engine is Presto.22*/23function isPresto() {24return util.matchUserAgent('Presto');25}2627/**28* @return {boolean} Whether the rendering engine is Trident.29*/30function isTrident() {31// IE only started including the Trident token in IE8.32return util.matchUserAgent('Trident') || util.matchUserAgent('MSIE');33}3435/**36* @return {boolean} Whether the rendering engine is EdgeHTML.37*/38function isEdge() {39return util.matchUserAgent('Edge');40}4142/**43* @return {boolean} Whether the rendering engine is WebKit. This will return44* true for Chrome, Blink-based Opera (15+), Edge Chromium and Safari.45*/46function isWebKit() {47return util.matchUserAgentIgnoreCase('WebKit') && !isEdge();48}4950/**51* @return {boolean} Whether the rendering engine is Gecko.52*/53function isGecko() {54return util.matchUserAgent('Gecko') && !isWebKit() && !isTrident() &&55!isEdge();56}5758/**59* @return {string} The rendering engine's version or empty string if version60* can't be determined.61*/62function getVersion() {63const userAgentString = util.getUserAgent();64if (userAgentString) {65const tuples = util.extractVersionTuples(userAgentString);6667const engineTuple = getEngineTuple(tuples);68if (engineTuple) {69// In Gecko, the version string is either in the browser info or the70// Firefox version. See Gecko user agent string reference:71// http://goo.gl/mULqa72if (engineTuple[0] == 'Gecko') {73return getVersionForKey(tuples, 'Firefox');74}7576return engineTuple[1];77}7879// MSIE has only one version identifier, and the Trident version is80// specified in the parenthetical. IE Edge is covered in the engine tuple81// detection.82const browserTuple = tuples[0];83let info;84if (browserTuple && (info = browserTuple[2])) {85const match = /Trident\/([^\s;]+)/.exec(info);86if (match) {87return match[1];88}89}90}91return '';92}9394/**95* @param {!Array<!Array<string>>} tuples Extracted version tuples.96* @return {!Array<string>|undefined} The engine tuple or undefined if not97* found.98*/99function getEngineTuple(tuples) {100if (!isEdge()) {101return tuples[1];102}103for (let i = 0; i < tuples.length; i++) {104const tuple = tuples[i];105if (tuple[0] == 'Edge') {106return tuple;107}108}109}110111/**112* @param {string|number} version The version to check.113* @return {boolean} Whether the rendering engine version is higher or the same114* as the given version.115*/116function isVersionOrHigher(version) {117return googString.compareVersions(getVersion(), version) >= 0;118}119120/**121* @param {!Array<!Array<string>>} tuples Version tuples.122* @param {string} key The key to look for.123* @return {string} The version string of the given key, if present.124* Otherwise, the empty string.125*/126function getVersionForKey(tuples, key) {127// TODO(nnaze): Move to util if useful elsewhere.128129const pair = googArray.find(tuples, function(pair) {130return key == pair[0];131});132133return pair && pair[1] || '';134}135136exports = {137getVersion,138isEdge,139isGecko,140isPresto,141isTrident,142isVersionOrHigher,143isWebKit,144};145146147