1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
// Copyright 2013 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Utilities used by goog.labs.userAgent tools. These functions
* should not be used outside of goog.labs.userAgent.*.
*
*
* @author nnaze@google.com (Nathan Naze)
*/
goog.provide('goog.labs.userAgent.util');
goog.require('goog.string');
/**
* Gets the native userAgent string from navigator if it exists.
* If navigator or navigator.userAgent string is missing, returns an empty
* string.
* @return {string}
* @private
*/
goog.labs.userAgent.util.getNativeUserAgentString_ = function() {
var navigator = goog.labs.userAgent.util.getNavigator_();
if (navigator) {
var userAgent = navigator.userAgent;
if (userAgent) {
return userAgent;
}
}
return '';
};
/**
* Getter for the native navigator.
* This is a separate function so it can be stubbed out in testing.
* @return {Navigator}
* @private
*/
goog.labs.userAgent.util.getNavigator_ = function() {
return goog.global.navigator;
};
/**
* A possible override for applications which wish to not check
* navigator.userAgent but use a specified value for detection instead.
* @private {string}
*/
goog.labs.userAgent.util.userAgent_ =
goog.labs.userAgent.util.getNativeUserAgentString_();
/**
* Applications may override browser detection on the built in
* navigator.userAgent object by setting this string. Set to null to use the
* browser object instead.
* @param {?string=} opt_userAgent The User-Agent override.
*/
goog.labs.userAgent.util.setUserAgent = function(opt_userAgent) {
goog.labs.userAgent.util.userAgent_ =
opt_userAgent || goog.labs.userAgent.util.getNativeUserAgentString_();
};
/**
* @return {string} The user agent string.
*/
goog.labs.userAgent.util.getUserAgent = function() {
return goog.labs.userAgent.util.userAgent_;
};
/**
* @param {string} str
* @return {boolean} Whether the user agent contains the given string.
*/
goog.labs.userAgent.util.matchUserAgent = function(str) {
var userAgent = goog.labs.userAgent.util.getUserAgent();
return goog.string.contains(userAgent, str);
};
/**
* @param {string} str
* @return {boolean} Whether the user agent contains the given string, ignoring
* case.
*/
goog.labs.userAgent.util.matchUserAgentIgnoreCase = function(str) {
var userAgent = goog.labs.userAgent.util.getUserAgent();
return goog.string.caseInsensitiveContains(userAgent, str);
};
/**
* Parses the user agent into tuples for each section.
* @param {string} userAgent
* @return {!Array<!Array<string>>} Tuples of key, version, and the contents
* of the parenthetical.
*/
goog.labs.userAgent.util.extractVersionTuples = function(userAgent) {
// Matches each section of a user agent string.
// Example UA:
// Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us)
// AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405
// This has three version tuples: Mozilla, AppleWebKit, and Mobile.
var versionRegExp = new RegExp(
// Key. Note that a key may have a space.
// (i.e. 'Mobile Safari' in 'Mobile Safari/5.0')
'(\\w[\\w ]+)' +
'/' + // slash
'([^\\s]+)' + // version (i.e. '5.0b')
'\\s*' + // whitespace
'(?:\\((.*?)\\))?', // parenthetical info. parentheses not matched.
'g');
var data = [];
var match;
// Iterate and collect the version tuples. Each iteration will be the
// next regex match.
while (match = versionRegExp.exec(userAgent)) {
data.push([
match[1], // key
match[2], // value
// || undefined as this is not undefined in IE7 and IE8
match[3] || undefined // info
]);
}
return data;
};
|