123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- // 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;
- };
|