Debian Codegraph @

closure-library/closure/goog/labs/useragent/engine.js

raw
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(userAgentString);
84
85 var engineTuple = goog.labs.userAgent.engine.getEngineTuple_(tuples);
86 if (engineTuple) {
87 // In Gecko, the version string is either in the browser info or the
88 // Firefox version. See Gecko user agent string reference:
89 // http://goo.gl/mULqa
90 if (engineTuple[0] == 'Gecko') {
91 return goog.labs.userAgent.engine.getVersionForKey_(tuples, 'Firefox');
92 }
93
94 return engineTuple[1];
95 }
96
97 // MSIE has only one version identifier, and the Trident version is
98 // specified in the parenthetical. IE Edge is covered in the engine tuple
99 // detection.
100 var browserTuple = tuples[0];
101 var info;
102 if (browserTuple && (info = browserTuple[2])) {
103 var match = /Trident\/([^\s;]+)/.exec(info);
104 if (match) {
105 return match[1];
106 }
107 }
108 }
109 return '';
110 };
111
112
113 /**
114 * @param {!Array<!Array<string>>} tuples Extracted version tuples.
115 * @return {!Array<string>|undefined} The engine tuple or undefined if not
116 * found.
117 * @private
118 */
119 goog.labs.userAgent.engine.getEngineTuple_ = function(tuples) {
120 if (!goog.labs.userAgent.engine.isEdge()) {
121 return tuples[1];
122 }
123 for (var i = 0; i < tuples.length; i++) {
124 var tuple = tuples[i];
125 if (tuple[0] == 'Edge') {
126 return tuple;
127 }
128 }
129 };
130
131
132 /**
133 * @param {string|number} version The version to check.
134 * @return {boolean} Whether the rendering engine version is higher or the same
135 * as the given version.
136 */
137 goog.labs.userAgent.engine.isVersionOrHigher = function(version) {
138 return goog.string.compareVersions(
139 goog.labs.userAgent.engine.getVersion(), version) >= 0;
140 };
141
142
143 /**
144 * @param {!Array<!Array<string>>} tuples Version tuples.
145 * @param {string} key The key to look for.
146 * @return {string} The version string of the given key, if present.
147 * Otherwise, the empty string.
148 * @private
149 */
150 goog.labs.userAgent.engine.getVersionForKey_ = function(tuples, key) {
151 // TODO(nnaze): Move to util if useful elsewhere.
152
153 var pair = goog.array.find(tuples, function(pair) { return key == pair[0]; });
154
155 return pair && pair[1] || '';
156 };