product_isversion.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2009 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 Functions for understanding the version of the browser.
  16. * This is pulled out of product.js to ensure that only builds that need
  17. * this functionality actually get it, without having to rely on the compiler
  18. * to strip out unneeded pieces.
  19. *
  20. * TODO(nnaze): Move to more appropriate filename/namespace.
  21. *
  22. */
  23. goog.provide('goog.userAgent.product.isVersion');
  24. goog.require('goog.labs.userAgent.platform');
  25. goog.require('goog.string');
  26. goog.require('goog.userAgent');
  27. goog.require('goog.userAgent.product');
  28. /**
  29. * @return {string} The string that describes the version number of the user
  30. * agent product. This is a string rather than a number because it may
  31. * contain 'b', 'a', and so on.
  32. * @private
  33. */
  34. goog.userAgent.product.determineVersion_ = function() {
  35. // All browsers have different ways to detect the version and they all have
  36. // different naming schemes.
  37. if (goog.userAgent.product.FIREFOX) {
  38. // Firefox/2.0.0.1 or Firefox/3.5.3
  39. return goog.userAgent.product.getFirstRegExpGroup_(/Firefox\/([0-9.]+)/);
  40. }
  41. if (goog.userAgent.product.IE || goog.userAgent.product.EDGE ||
  42. goog.userAgent.product.OPERA) {
  43. return goog.userAgent.VERSION;
  44. }
  45. if (goog.userAgent.product.CHROME) {
  46. if (goog.labs.userAgent.platform.isIos()) {
  47. // CriOS/56.0.2924.79
  48. return goog.userAgent.product.getFirstRegExpGroup_(/CriOS\/([0-9.]+)/);
  49. }
  50. // Chrome/4.0.223.1
  51. return goog.userAgent.product.getFirstRegExpGroup_(/Chrome\/([0-9.]+)/);
  52. }
  53. // This replicates legacy logic, which considered Safari and iOS to be
  54. // different products.
  55. if (goog.userAgent.product.SAFARI && !goog.labs.userAgent.platform.isIos()) {
  56. // Version/5.0.3
  57. //
  58. // NOTE: Before version 3, Safari did not report a product version number.
  59. // The product version number for these browsers will be the empty string.
  60. // They may be differentiated by WebKit version number in goog.userAgent.
  61. return goog.userAgent.product.getFirstRegExpGroup_(/Version\/([0-9.]+)/);
  62. }
  63. if (goog.userAgent.product.IPHONE || goog.userAgent.product.IPAD) {
  64. // Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1
  65. // (KHTML, like Gecko) Version/3.0 Mobile/3A100a Safari/419.3
  66. // Version is the browser version, Mobile is the build number. We combine
  67. // the version string with the build number: 3.0.3A100a for the example.
  68. var arr =
  69. goog.userAgent.product.execRegExp_(/Version\/(\S+).*Mobile\/(\S+)/);
  70. if (arr) {
  71. return arr[1] + '.' + arr[2];
  72. }
  73. } else if (goog.userAgent.product.ANDROID) {
  74. // Mozilla/5.0 (Linux; U; Android 0.5; en-us) AppleWebKit/522+
  75. // (KHTML, like Gecko) Safari/419.3
  76. //
  77. // Mozilla/5.0 (Linux; U; Android 1.0; en-us; dream) AppleWebKit/525.10+
  78. // (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2
  79. //
  80. // Prefer Version number if present, else make do with the OS number
  81. var version =
  82. goog.userAgent.product.getFirstRegExpGroup_(/Android\s+([0-9.]+)/);
  83. if (version) {
  84. return version;
  85. }
  86. return goog.userAgent.product.getFirstRegExpGroup_(/Version\/([0-9.]+)/);
  87. }
  88. return '';
  89. };
  90. /**
  91. * Return the first group of the given regex.
  92. * @param {!RegExp} re Regular expression with at least one group.
  93. * @return {string} Contents of the first group or an empty string if no match.
  94. * @private
  95. */
  96. goog.userAgent.product.getFirstRegExpGroup_ = function(re) {
  97. var arr = goog.userAgent.product.execRegExp_(re);
  98. return arr ? arr[1] : '';
  99. };
  100. /**
  101. * Run regexp's exec() on the userAgent string.
  102. * @param {!RegExp} re Regular expression.
  103. * @return {?Array<?>} A result array, or null for no match.
  104. * @private
  105. */
  106. goog.userAgent.product.execRegExp_ = function(re) {
  107. return re.exec(goog.userAgent.getUserAgentString());
  108. };
  109. /**
  110. * The version of the user agent. This is a string because it might contain
  111. * 'b' (as in beta) as well as multiple dots.
  112. * @type {string}
  113. */
  114. goog.userAgent.product.VERSION = goog.userAgent.product.determineVersion_();
  115. /**
  116. * Whether the user agent product version is higher or the same as the given
  117. * version.
  118. *
  119. * @param {string|number} version The version to check.
  120. * @return {boolean} Whether the user agent product version is higher or the
  121. * same as the given version.
  122. */
  123. goog.userAgent.product.isVersion = function(version) {
  124. return goog.string.compareVersions(goog.userAgent.product.VERSION, version) >=
  125. 0;
  126. };