util.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 Utilities used by goog.labs.userAgent tools. These functions
  16. * should not be used outside of goog.labs.userAgent.*.
  17. *
  18. *
  19. * @author nnaze@google.com (Nathan Naze)
  20. */
  21. goog.provide('goog.labs.userAgent.util');
  22. goog.require('goog.string');
  23. /**
  24. * Gets the native userAgent string from navigator if it exists.
  25. * If navigator or navigator.userAgent string is missing, returns an empty
  26. * string.
  27. * @return {string}
  28. * @private
  29. */
  30. goog.labs.userAgent.util.getNativeUserAgentString_ = function() {
  31. var navigator = goog.labs.userAgent.util.getNavigator_();
  32. if (navigator) {
  33. var userAgent = navigator.userAgent;
  34. if (userAgent) {
  35. return userAgent;
  36. }
  37. }
  38. return '';
  39. };
  40. /**
  41. * Getter for the native navigator.
  42. * This is a separate function so it can be stubbed out in testing.
  43. * @return {Navigator}
  44. * @private
  45. */
  46. goog.labs.userAgent.util.getNavigator_ = function() {
  47. return goog.global.navigator;
  48. };
  49. /**
  50. * A possible override for applications which wish to not check
  51. * navigator.userAgent but use a specified value for detection instead.
  52. * @private {string}
  53. */
  54. goog.labs.userAgent.util.userAgent_ =
  55. goog.labs.userAgent.util.getNativeUserAgentString_();
  56. /**
  57. * Applications may override browser detection on the built in
  58. * navigator.userAgent object by setting this string. Set to null to use the
  59. * browser object instead.
  60. * @param {?string=} opt_userAgent The User-Agent override.
  61. */
  62. goog.labs.userAgent.util.setUserAgent = function(opt_userAgent) {
  63. goog.labs.userAgent.util.userAgent_ =
  64. opt_userAgent || goog.labs.userAgent.util.getNativeUserAgentString_();
  65. };
  66. /**
  67. * @return {string} The user agent string.
  68. */
  69. goog.labs.userAgent.util.getUserAgent = function() {
  70. return goog.labs.userAgent.util.userAgent_;
  71. };
  72. /**
  73. * @param {string} str
  74. * @return {boolean} Whether the user agent contains the given string.
  75. */
  76. goog.labs.userAgent.util.matchUserAgent = function(str) {
  77. var userAgent = goog.labs.userAgent.util.getUserAgent();
  78. return goog.string.contains(userAgent, str);
  79. };
  80. /**
  81. * @param {string} str
  82. * @return {boolean} Whether the user agent contains the given string, ignoring
  83. * case.
  84. */
  85. goog.labs.userAgent.util.matchUserAgentIgnoreCase = function(str) {
  86. var userAgent = goog.labs.userAgent.util.getUserAgent();
  87. return goog.string.caseInsensitiveContains(userAgent, str);
  88. };
  89. /**
  90. * Parses the user agent into tuples for each section.
  91. * @param {string} userAgent
  92. * @return {!Array<!Array<string>>} Tuples of key, version, and the contents
  93. * of the parenthetical.
  94. */
  95. goog.labs.userAgent.util.extractVersionTuples = function(userAgent) {
  96. // Matches each section of a user agent string.
  97. // Example UA:
  98. // Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us)
  99. // AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405
  100. // This has three version tuples: Mozilla, AppleWebKit, and Mobile.
  101. var versionRegExp = new RegExp(
  102. // Key. Note that a key may have a space.
  103. // (i.e. 'Mobile Safari' in 'Mobile Safari/5.0')
  104. '(\\w[\\w ]+)' +
  105. '/' + // slash
  106. '([^\\s]+)' + // version (i.e. '5.0b')
  107. '\\s*' + // whitespace
  108. '(?:\\((.*?)\\))?', // parenthetical info. parentheses not matched.
  109. 'g');
  110. var data = [];
  111. var match;
  112. // Iterate and collect the version tuples. Each iteration will be the
  113. // next regex match.
  114. while (match = versionRegExp.exec(userAgent)) {
  115. data.push([
  116. match[1], // key
  117. match[2], // value
  118. // || undefined as this is not undefined in IE7 and IE8
  119. match[3] || undefined // info
  120. ]);
  121. }
  122. return data;
  123. };