Path: blob/trunk/third_party/closure/goog/labs/useragent/util.js
2868 views
// Copyright 2013 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Utilities used by goog.labs.userAgent tools. These functions16* should not be used outside of goog.labs.userAgent.*.17*18*19* @author [email protected] (Nathan Naze)20*/2122goog.provide('goog.labs.userAgent.util');2324goog.require('goog.string');252627/**28* Gets the native userAgent string from navigator if it exists.29* If navigator or navigator.userAgent string is missing, returns an empty30* string.31* @return {string}32* @private33*/34goog.labs.userAgent.util.getNativeUserAgentString_ = function() {35var navigator = goog.labs.userAgent.util.getNavigator_();36if (navigator) {37var userAgent = navigator.userAgent;38if (userAgent) {39return userAgent;40}41}42return '';43};444546/**47* Getter for the native navigator.48* This is a separate function so it can be stubbed out in testing.49* @return {Navigator}50* @private51*/52goog.labs.userAgent.util.getNavigator_ = function() {53return goog.global.navigator;54};555657/**58* A possible override for applications which wish to not check59* navigator.userAgent but use a specified value for detection instead.60* @private {string}61*/62goog.labs.userAgent.util.userAgent_ =63goog.labs.userAgent.util.getNativeUserAgentString_();646566/**67* Applications may override browser detection on the built in68* navigator.userAgent object by setting this string. Set to null to use the69* browser object instead.70* @param {?string=} opt_userAgent The User-Agent override.71*/72goog.labs.userAgent.util.setUserAgent = function(opt_userAgent) {73goog.labs.userAgent.util.userAgent_ =74opt_userAgent || goog.labs.userAgent.util.getNativeUserAgentString_();75};767778/**79* @return {string} The user agent string.80*/81goog.labs.userAgent.util.getUserAgent = function() {82return goog.labs.userAgent.util.userAgent_;83};848586/**87* @param {string} str88* @return {boolean} Whether the user agent contains the given string.89*/90goog.labs.userAgent.util.matchUserAgent = function(str) {91var userAgent = goog.labs.userAgent.util.getUserAgent();92return goog.string.contains(userAgent, str);93};949596/**97* @param {string} str98* @return {boolean} Whether the user agent contains the given string, ignoring99* case.100*/101goog.labs.userAgent.util.matchUserAgentIgnoreCase = function(str) {102var userAgent = goog.labs.userAgent.util.getUserAgent();103return goog.string.caseInsensitiveContains(userAgent, str);104};105106107/**108* Parses the user agent into tuples for each section.109* @param {string} userAgent110* @return {!Array<!Array<string>>} Tuples of key, version, and the contents111* of the parenthetical.112*/113goog.labs.userAgent.util.extractVersionTuples = function(userAgent) {114// Matches each section of a user agent string.115// Example UA:116// Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us)117// AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405118// This has three version tuples: Mozilla, AppleWebKit, and Mobile.119120var versionRegExp = new RegExp(121// Key. Note that a key may have a space.122// (i.e. 'Mobile Safari' in 'Mobile Safari/5.0')123'(\\w[\\w ]+)' +124125'/' + // slash126'([^\\s]+)' + // version (i.e. '5.0b')127'\\s*' + // whitespace128'(?:\\((.*?)\\))?', // parenthetical info. parentheses not matched.129'g');130131var data = [];132var match;133134// Iterate and collect the version tuples. Each iteration will be the135// next regex match.136while (match = versionRegExp.exec(userAgent)) {137data.push([138match[1], // key139match[2], // value140// || undefined as this is not undefined in IE7 and IE8141match[3] || undefined // info142]);143}144145return data;146};147148149