123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- /**
- * Copyright (c) 2013-present, Facebook, Inc.
- *
- * This source code is licensed under the MIT license found in the
- * LICENSE file in the root directory of this source tree.
- *
- * @providesModule UserAgent
- */
- 'use strict';
- const UserAgentData = require('./UserAgentData');
- const VersionRange = require('./VersionRange');
- const mapObject = require('./mapObject');
- const memoizeStringOnly = require('./memoizeStringOnly');
- /**
- * Checks to see whether `name` and `version` satisfy `query`.
- *
- * @param {string} name Name of the browser, device, engine or platform
- * @param {?string} version Version of the browser, engine or platform
- * @param {string} query Query of form "Name [range expression]"
- * @param {?function} normalizer Optional pre-processor for range expression
- * @return {boolean}
- */
- function compare(name, version, query, normalizer) {
- // check for exact match with no version
- if (name === query) {
- return true;
- }
- // check for non-matching names
- if (!query.startsWith(name)) {
- return false;
- }
- // full comparison with version
- let range = query.slice(name.length);
- if (version) {
- range = normalizer ? normalizer(range) : range;
- return VersionRange.contains(range, version);
- }
- return false;
- }
- /**
- * Normalizes `version` by stripping any "NT" prefix, but only on the Windows
- * platform.
- *
- * Mimics the stripping performed by the `UserAgentWindowsPlatform` PHP class.
- *
- * @param {string} version
- * @return {string}
- */
- function normalizePlatformVersion(version) {
- if (UserAgentData.platformName === 'Windows') {
- return version.replace(/^\s*NT/, '');
- }
- return version;
- }
- /**
- * Provides client-side access to the authoritative PHP-generated User Agent
- * information supplied by the server.
- */
- const UserAgent = {
- /**
- * Check if the User Agent browser matches `query`.
- *
- * `query` should be a string like "Chrome" or "Chrome > 33".
- *
- * Valid browser names include:
- *
- * - ACCESS NetFront
- * - AOL
- * - Amazon Silk
- * - Android
- * - BlackBerry
- * - BlackBerry PlayBook
- * - Chrome
- * - Chrome for iOS
- * - Chrome frame
- * - Facebook PHP SDK
- * - Facebook for iOS
- * - Firefox
- * - IE
- * - IE Mobile
- * - Mobile Safari
- * - Motorola Internet Browser
- * - Nokia
- * - Openwave Mobile Browser
- * - Opera
- * - Opera Mini
- * - Opera Mobile
- * - Safari
- * - UIWebView
- * - Unknown
- * - webOS
- * - etc...
- *
- * An authoritative list can be found in the PHP `BrowserDetector` class and
- * related classes in the same file (see calls to `new UserAgentBrowser` here:
- * https://fburl.com/50728104).
- *
- * @note Function results are memoized
- *
- * @param {string} query Query of the form "Name [range expression]"
- * @return {boolean}
- */
- isBrowser(query) {
- return compare(UserAgentData.browserName, UserAgentData.browserFullVersion, query);
- },
- /**
- * Check if the User Agent browser uses a 32 or 64 bit architecture.
- *
- * @note Function results are memoized
- *
- * @param {string} query Query of the form "32" or "64".
- * @return {boolean}
- */
- isBrowserArchitecture(query) {
- return compare(UserAgentData.browserArchitecture, null, query);
- },
- /**
- * Check if the User Agent device matches `query`.
- *
- * `query` should be a string like "iPhone" or "iPad".
- *
- * Valid device names include:
- *
- * - Kindle
- * - Kindle Fire
- * - Unknown
- * - iPad
- * - iPhone
- * - iPod
- * - etc...
- *
- * An authoritative list can be found in the PHP `DeviceDetector` class and
- * related classes in the same file (see calls to `new UserAgentDevice` here:
- * https://fburl.com/50728332).
- *
- * @note Function results are memoized
- *
- * @param {string} query Query of the form "Name"
- * @return {boolean}
- */
- isDevice(query) {
- return compare(UserAgentData.deviceName, null, query);
- },
- /**
- * Check if the User Agent rendering engine matches `query`.
- *
- * `query` should be a string like "WebKit" or "WebKit >= 537".
- *
- * Valid engine names include:
- *
- * - Gecko
- * - Presto
- * - Trident
- * - WebKit
- * - etc...
- *
- * An authoritative list can be found in the PHP `RenderingEngineDetector`
- * class related classes in the same file (see calls to `new
- * UserAgentRenderingEngine` here: https://fburl.com/50728617).
- *
- * @note Function results are memoized
- *
- * @param {string} query Query of the form "Name [range expression]"
- * @return {boolean}
- */
- isEngine(query) {
- return compare(UserAgentData.engineName, UserAgentData.engineVersion, query);
- },
- /**
- * Check if the User Agent platform matches `query`.
- *
- * `query` should be a string like "Windows" or "iOS 5 - 6".
- *
- * Valid platform names include:
- *
- * - Android
- * - BlackBerry OS
- * - Java ME
- * - Linux
- * - Mac OS X
- * - Mac OS X Calendar
- * - Mac OS X Internet Account
- * - Symbian
- * - SymbianOS
- * - Windows
- * - Windows Mobile
- * - Windows Phone
- * - iOS
- * - iOS Facebook Integration Account
- * - iOS Facebook Social Sharing UI
- * - webOS
- * - Chrome OS
- * - etc...
- *
- * An authoritative list can be found in the PHP `PlatformDetector` class and
- * related classes in the same file (see calls to `new UserAgentPlatform`
- * here: https://fburl.com/50729226).
- *
- * @note Function results are memoized
- *
- * @param {string} query Query of the form "Name [range expression]"
- * @return {boolean}
- */
- isPlatform(query) {
- return compare(UserAgentData.platformName, UserAgentData.platformFullVersion, query, normalizePlatformVersion);
- },
- /**
- * Check if the User Agent platform is a 32 or 64 bit architecture.
- *
- * @note Function results are memoized
- *
- * @param {string} query Query of the form "32" or "64".
- * @return {boolean}
- */
- isPlatformArchitecture(query) {
- return compare(UserAgentData.platformArchitecture, null, query);
- }
- };
- module.exports = mapObject(UserAgent, memoizeStringOnly);
|