engine.js 4.6 KB

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