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