# 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 at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // 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 and
13 | // limitations under the License.
14 |
15 | /**
16 | * @fileoverview Closure user agent detection.
17 | * @see http://en.wikipedia.org/wiki/User_agent
18 | * For more information on browser brand, platform, or device see the other
19 | * sub-namespaces in goog.labs.userAgent (browser, platform, and device).
20 | *
21 | */
22 |
23 | goog.provide('goog.labs.userAgent.engine');
24 |
25 | goog.require('goog.array');
26 | goog.require('goog.labs.userAgent.util');
27 | goog.require('goog.string');
28 |
29 |
30 | /**
31 | * @return {boolean} Whether the rendering engine is Presto.
32 | */
33 | goog.labs.userAgent.engine.isPresto = function() {
34 | return goog.labs.userAgent.util.matchUserAgent('Presto');
35 | };
36 |
37 |
38 | /**
39 | * @return {boolean} Whether the rendering engine is Trident.
40 | */
41 | goog.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 | };
46 |
47 |
48 | /**
49 | * @return {boolean} Whether the rendering engine is Edge.
50 | */
51 | goog.labs.userAgent.engine.isEdge = function() {
52 | return goog.labs.userAgent.util.matchUserAgent('Edge');
53 | };
54 |
55 |
56 | /**
57 | * @return {boolean} Whether the rendering engine is WebKit.
58 | */
59 | goog.labs.userAgent.engine.isWebKit = function() {
60 | return goog.labs.userAgent.util.matchUserAgentIgnoreCase('WebKit') &&
61 | !goog.labs.userAgent.engine.isEdge();
62 | };
63 |
64 |
65 | /**
66 | * @return {boolean} Whether the rendering engine is Gecko.
67 | */
68 | goog.labs.userAgent.engine.isGecko = function() {
69 | return goog.labs.userAgent.util.matchUserAgent('Gecko') &&
70 | !goog.labs.userAgent.engine.isWebKit() &&
71 | !goog.labs.userAgent.engine.isTrident() &&
72 | !goog.labs.userAgent.engine.isEdge();
73 | };
74 |
75 |
76 | /**
77 | * @return {string} The rendering engine's version or empty string if version
78 | * can't be determined.
79 | */
80 | goog.labs.userAgent.engine.getVersion = function() {
81 | var userAgentString = goog.labs.userAgent.util.getUserAgent();
82 | if (userAgentString) {
83 | var tuples = goog.labs.userAgent.util.extractVersionTuples(
84 | userAgentString);
85 |
86 | var engineTuple = goog.labs.userAgent.engine.getEngineTuple_(tuples);
87 | if (engineTuple) {
88 | // In Gecko, the version string is either in the browser info or the
89 | // Firefox version. See Gecko user agent string reference:
90 | // http://goo.gl/mULqa
91 | if (engineTuple[0] == 'Gecko') {
92 | return goog.labs.userAgent.engine.getVersionForKey_(
93 | tuples, 'Firefox');
94 | }
95 |
96 | return engineTuple[1];
97 | }
98 |
99 | // MSIE has only one version identifier, and the Trident version is
100 | // specified in the parenthetical. IE Edge is covered in the engine tuple
101 | // detection.
102 | var browserTuple = tuples[0];
103 | var info;
104 | if (browserTuple && (info = browserTuple[2])) {
105 | var match = /Trident\/([^\s;]+)/.exec(info);
106 | if (match) {
107 | return match[1];
108 | }
109 | }
110 | }
111 | return '';
112 | };
113 |
114 |
115 | /**
116 | * @param {!Array<!Array<string>>} tuples Extracted version tuples.
117 | * @return {!Array<string>|undefined} The engine tuple or undefined if not
118 | * found.
119 | * @private
120 | */
121 | goog.labs.userAgent.engine.getEngineTuple_ = function(tuples) {
122 | if (!goog.labs.userAgent.engine.isEdge()) {
123 | return tuples[1];
124 | }
125 | for (var i = 0; i < tuples.length; i++) {
126 | var tuple = tuples[i];
127 | if (tuple[0] == 'Edge') {
128 | return tuple;
129 | }
130 | }
131 | };
132 |
133 |
134 | /**
135 | * @param {string|number} version The version to check.
136 | * @return {boolean} Whether the rendering engine version is higher or the same
137 | * as the given version.
138 | */
139 | goog.labs.userAgent.engine.isVersionOrHigher = function(version) {
140 | return goog.string.compareVersions(goog.labs.userAgent.engine.getVersion(),
141 | version) >= 0;
142 | };
143 |
144 |
145 | /**
146 | * @param {!Array<!Array<string>>} tuples Version tuples.
147 | * @param {string} key The key to look for.
148 | * @return {string} The version string of the given key, if present.
149 | * Otherwise, the empty string.
150 | * @private
151 | */
152 | goog.labs.userAgent.engine.getVersionForKey_ = function(tuples, key) {
153 | // TODO(nnaze): Move to util if useful elsewhere.
154 |
155 | var pair = goog.array.find(tuples, function(pair) {
156 | return key == pair[0];
157 | });
158 |
159 | return pair && pair[1] || '';
160 | };