Skip to content

Commit

Permalink
Merge pull request #2207 from AnalyticalGraphicsInc/detectFirefox
Browse files Browse the repository at this point in the history
Add isFirefox and firefoxVersion to FeatureDetection
  • Loading branch information
shunter committed Oct 22, 2014
2 parents a13be7c + 26838c9 commit f8c8d43
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 18 deletions.
47 changes: 33 additions & 14 deletions Source/Core/FeatureDetection.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ define([
var chromeVersionResult;
function isChrome() {
if (!defined(isChromeResult)) {
isChromeResult = false;

var fields = (/ Chrome\/([\.0-9]+)/).exec(navigator.userAgent);
if (fields === null) {
isChromeResult = false;
} else {
if (fields !== null) {
isChromeResult = true;
chromeVersionResult = extractVersion(fields[1]);
}
Expand All @@ -41,14 +41,12 @@ define([
var safariVersionResult;
function isSafari() {
if (!defined(isSafariResult)) {
isSafariResult = false;

// Chrome contains Safari in the user agent too
if (isChrome() || !(/ Safari\/[\.0-9]+/).test(navigator.userAgent)) {
isSafariResult = false;
} else {
if (!isChrome() && (/ Safari\/[\.0-9]+/).test(navigator.userAgent)) {
var fields = (/ Version\/([\.0-9]+)/).exec(navigator.userAgent);
if (fields === null) {
isSafariResult = false;
} else {
if (fields !== null) {
isSafariResult = true;
safariVersionResult = extractVersion(fields[1]);
}
Expand All @@ -66,10 +64,10 @@ define([
var webkitVersionResult;
function isWebkit() {
if (!defined(isWebkitResult)) {
isWebkitResult = false;

var fields = (/ AppleWebKit\/([\.0-9]+)(\+?)/).exec(navigator.userAgent);
if (fields === null) {
isWebkitResult = false;
} else {
if (fields !== null) {
isWebkitResult = true;
webkitVersionResult = extractVersion(fields[1]);
webkitVersionResult.isNightly = !!fields[2];
Expand All @@ -87,6 +85,8 @@ define([
var internetExplorerVersionResult;
function isInternetExplorer() {
if (!defined(isInternetExplorerResult)) {
isInternetExplorerResult = false;

var fields;
if (navigator.appName === 'Microsoft Internet Explorer') {
fields = /MSIE ([0-9]{1,}[\.0-9]{0,})/.exec(navigator.userAgent);
Expand All @@ -100,8 +100,6 @@ define([
isInternetExplorerResult = true;
internetExplorerVersionResult = extractVersion(fields[1]);
}
} else {
isInternetExplorerResult = false;
}
}
return isInternetExplorerResult;
Expand All @@ -111,6 +109,25 @@ define([
return isInternetExplorer() && internetExplorerVersionResult;
}

var isFirefoxResult;
var firefoxVersionResult;
function isFirefox() {
if (!defined(isFirefoxResult)) {
isFirefoxResult = false;

var fields = /Firefox\/([\.0-9]+)/.exec(navigator.userAgent);
if (fields !== null) {
isFirefoxResult = true;
firefoxVersionResult = extractVersion(fields[1]);
}
}
return isFirefoxResult;
}

function firefoxVersion() {
return isFirefox() && firefoxVersionResult;
}

/**
* A set of functions to detect whether the current browser supports
* various features.
Expand All @@ -127,6 +144,8 @@ define([
webkitVersion : webkitVersion,
isInternetExplorer : isInternetExplorer,
internetExplorerVersion : internetExplorerVersion,
isFirefox : isFirefox,
firefoxVersion : firefoxVersion,
hardwareConcurrency : defaultValue(navigator.hardwareConcurrency, 3)
};

Expand Down
88 changes: 84 additions & 4 deletions Specs/Core/FeatureDetectionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,93 @@ defineSuite([
"use strict";
/*global jasmine,describe,xdescribe,it,xit,expect,beforeEach,afterEach,beforeAll,afterAll,spyOn,runs,waits,waitsFor*/

//generally, these tests just make sure the function runs, the test can't expect a value of true or false
it('detects fullscreen support', function() {
//just make sure the function runs, the test can't expect a value of true or false
expect(FeatureDetection.supportsFullscreen()).toBeDefined();
var supportsFullscreen = FeatureDetection.supportsFullscreen();
expect(typeof supportsFullscreen).toEqual('boolean');
});

it('detects web worker support', function() {
//just make sure the function runs, the test can't expect a value of true or false
expect(FeatureDetection.supportsWebWorkers()).toBeDefined();
var supportsWebWorkers = FeatureDetection.supportsWebWorkers();
expect(typeof supportsWebWorkers).toEqual('boolean');
});

it('detects typed array support', function() {
var supportsTypedArrays = FeatureDetection.supportsTypedArrays();
expect(typeof supportsTypedArrays).toEqual('boolean');
});

function checkVersionArray(array) {
expect(Array.isArray(array)).toEqual(true);
array.forEach(function(d) {
expect(typeof d).toEqual('number');
});
}

it('detects Chrome', function() {
var isChrome = FeatureDetection.isChrome();
expect(typeof isChrome).toEqual('boolean');

if (isChrome) {
var chromeVersion = FeatureDetection.chromeVersion();
checkVersionArray(chromeVersion);

/*global console*/
console.log('detected Chrome ' + chromeVersion.join('.'));
}
});

it('detects Safari', function() {
var isSafari = FeatureDetection.isSafari();
expect(typeof isSafari).toEqual('boolean');

if (isSafari) {
var safariVersion = FeatureDetection.safariVersion();
checkVersionArray(safariVersion);

/*global console*/
console.log('detected Safari ' + safariVersion.join('.'));
}
});

it('detects Webkit', function() {
var isWebkit = FeatureDetection.isWebkit();
expect(typeof isWebkit).toEqual('boolean');

if (isWebkit) {
var webkitVersion = FeatureDetection.webkitVersion();
checkVersionArray(webkitVersion);
expect(typeof webkitVersion.isNightly).toEqual('boolean');

/*global console*/
console.log('detected Webkit ' + webkitVersion.join('.') + (webkitVersion.isNightly ? ' (Nightly)' : ''));
}
});

it('detects Internet Explorer', function() {
var isInternetExplorer = FeatureDetection.isInternetExplorer();
expect(typeof isInternetExplorer).toEqual('boolean');

if (isInternetExplorer) {
var internetExplorerVersion = FeatureDetection.internetExplorerVersion();
checkVersionArray(internetExplorerVersion);

/*global console*/
console.log('detected Internet Explorer ' + internetExplorerVersion.join('.'));
}
});

it('detects Firefox', function() {
var isFirefox = FeatureDetection.isFirefox();
expect(typeof isFirefox).toEqual('boolean');

if (isFirefox) {
var firefoxVersion = FeatureDetection.firefoxVersion();

checkVersionArray(firefoxVersion);

/*global console*/
console.log('detected Firefox ' + firefoxVersion.join('.'));
}
});
});

0 comments on commit f8c8d43

Please sign in to comment.