Skip to content
This repository has been archived by the owner on Oct 17, 2021. It is now read-only.

Commit

Permalink
Merge branch 'confidence'
Browse files Browse the repository at this point in the history
  • Loading branch information
dmolsen committed Aug 27, 2012
2 parents 0e977dc + 6168f3a commit 0515140
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 50 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Expand Up @@ -2,6 +2,12 @@ THIS CHANGELOG IS AN ATTEMPT TO DOCUMENT CHANGES TO THIS PROJECT.
SUPER MINOR REVISION NUMBERS WON'T SHOW UP IN GITHUB BUT IT'LL HELP
ME ORGANIZE WHEN I MADE A SET OF FIXES

DET-v0.9.0 (Aug. 24, 2012)
- FIX: updated Modernizr to 2.6.1
- FIX: trimmed down the config
- FIX: the test build page now waits for the onload event before building the cookie & redirecting. should address race conditions with a few tests.
- ADD: added per-session tests that existed in Modernizr feature-detects that I wanted for v1.0 of Detector (see http://bit.ly/O14wcZ for full list)

DET-v0.8.5 (Aug. 14, 2012)
- FIX: commmented out call to addToUAList() to increase performance
- ADD: now throwing profiles into directories based on the first two characters of the hash. performance tweak because IE 7+8 create profiles an amazing number of profiles
Expand Down
58 changes: 35 additions & 23 deletions lib/Detector/Detector.php
Expand Up @@ -15,28 +15,33 @@

class Detector {

private static $debug = false; // gets overwritten by the config so changing this won't do anything for you...
private static $debug = false; // gets overwritten by the config so changing this won't do anything for you...

public static $ua;
public static $accept;

private static $coreVersion;
private static $extendedVersion;

public static $foundIn; // this is just for the demo. won't ever really be needed i don't think


private static $uaHash;
private static $sessionID;
private static $cookieID;
private static $uaFeaturesMaxJS; // all the default Modernizr Tests
private static $uaFeaturesMinJS; // NO default tests except media queries, meant to run those in the perrequest folder
private static $uaFeaturesCore;
private static $uaFeaturesExtended;
private static $uaFeaturesPerSession;
private static $uaFeaturesPerRequest;

// version number so that profiles can be rebuilt if necessary
private static $coreVersion = "0.95";
private static $extendedVersion;

// where to find the modernizr scripts
private static $uaFeaturesMaxJS = "lib/modernizr/modernizr.full.js"; // all the default Modernizr Tests
private static $uaFeaturesMinJS = "lib/modernizr/modernizr.slim.js"; // NO default tests except media queries, meant to run those in the perrequest folder

// where to find the tests that should be run against browsers
private static $uaFeaturesCore = "tests/core/";
private static $uaFeaturesExtended = "tests/extended/";
private static $uaFeaturesPerSession = "tests/persession/";
private static $uaFeaturesPerRequest = "tests/perrequest/";

// where to find the user agent profiles
private static $uaDirCore = "user-agents/core/";
private static $uaDirExtended = "user-agents/extended/";

private static $uaDirCore;
private static $uaDirExtended;

private static $featuresScriptWebPath;

Expand All @@ -48,6 +53,8 @@ class Detector {
public static $noJSDefaultFamily;
public static $noCookieFamily;

public static $foundIn; // this is just for the demo. won't ever really be needed i don't think

/**
* Configures the shared variables in Detector so that they can be used in functions that might not need to run Detector::build();
*
Expand Down Expand Up @@ -312,6 +319,12 @@ public static function build() {

// unset the cookie that held the vast amount of test data
setcookie(self::$cookieID,"");
setcookie(self::$cookieID,"",time()-3600);

unset($jsonTemplateCore);
unset($jsonTemplateExtended);
unset($cookiePerSession);
unset($cookiePerRequest);

// add our collected data to the session for use in future requests, also add the per request data
if (isset($_SESSION)) {
Expand Down Expand Up @@ -361,6 +374,7 @@ public static function persession() {
readfile(__DIR__ . '/' . self::$uaFeaturesMinJS);
self::readDirFiles(self::$uaFeaturesPerSession);
self::readDirFiles(self::$uaFeaturesPerRequest);
readfile(__DIR__. '/lib/modernizr/onload.js');
print self::_mer() . "</script></head><body><noscript><meta http-equiv='refresh' content='0; url=".self::buildNoscriptLink()."'></noscript></body></html>";
exit;
}
Expand All @@ -377,9 +391,9 @@ public static function buildTestPage() {
self::readDirFiles(self::$uaFeaturesExtended);
self::readDirFiles(self::$uaFeaturesPerSession);
self::readDirFiles(self::$uaFeaturesPerRequest);
readfile(__DIR__. '/lib/modernizr/onload.js');
print self::_mer() . "</script></head><body><noscript><meta http-equiv='refresh' content='0; url=".self::buildNoscriptLink()."'></noscript></body></html>";
exit;

exit;
}

/**
Expand All @@ -392,7 +406,7 @@ public static function buildTestPage() {
* @return {String} the HTML & JavaScript that tracks the per request test
*/
private static function _mer($reload = true, $cookieExtra = '') {
$output = "".
$output = "function cm() {".
"var m=Modernizr;var c='';var k=''; var f;".
"for(f in m){".
"var j='';".
Expand Down Expand Up @@ -428,7 +442,7 @@ private static function _mer($reload = true, $cookieExtra = '') {
$output .= "document.location.reload();";
}
$output .= "}";
$output .= "}catch(e){}"."";
$output .= "}catch(e){}"."}";
return $output;
}

Expand Down Expand Up @@ -532,9 +546,7 @@ private static function writeUAFile($jsonEncoded,$uaFilePath) {
mkdir(__DIR__."/".self::$uaDirExtended.$dir);
chmod(__DIR__."/".self::$uaDirCore.$dir,0775);
}
$fp = fopen($uaFilePath, "w");
fwrite($fp, $jsonEncoded);
fclose($fp);
file_put_contents($uaFilePath,$jsonEncoded);
chmod($uaFilePath,0664);
}

Expand Down Expand Up @@ -593,7 +605,7 @@ private static function parseCookie($cookieExtension,$obj,$default = false) {
foreach($uaFeatures as $key => $value) {
if ((strpos($key,$cookieExtension."-") !== false) || (($cookieExtension == 'core') && (strpos($key,"extended-") === false) && (strpos($key,"pr-") === false) && (strpos($key,"ps-") === false))) {
$key = str_replace($cookieExtension."-", "", $key);
if (is_object($value)) {
if (is_object($value) && (strpos($key,"_") !== 0)) {
foreach ($value as $vkey => $vvalue) {
if ($vvalue == "probably") { // hack for modernizr
$value->$vkey = true;
Expand Down
23 changes: 4 additions & 19 deletions lib/Detector/config/config.ini.default
@@ -1,31 +1,16 @@
/*
* Configuration options for Detector
* If config.ini doesn't exist Detector will attempt to read in config.ini.copy
* and write out config.ini.
* Configuration Options for Detector
* If config.ini doesn't exist Detector will attempt to read in config.ini.default
* and write out the config.ini file.
*/

// if we should run in debug mode. only useful if we want to view the site populated
// w/ other Detector profiles
debug = false

// version number so that core and extended profiles can be rebuilt if necessary
coreVersion = "0.9"
// version number so that extended profiles can be rebuilt if necessary
extendedVersion = "1.0"

// where to find the modernizr scripts
uaFeaturesMaxJS = "lib/modernizr/modernizr.2.5.2.min.custom.js"
uaFeaturesMinJS = "lib/modernizr/modernizr.2.5.2.min.no-tests.js"

// where to find the tests that should be run against browsers
uaFeaturesCore = "tests/core/"
uaFeaturesExtended = "tests/extended/"
uaFeaturesPerSession = "tests/persession/"
uaFeaturesPerRequest = "tests/perrequest/"

// where to find the user agent profiles
uaDirCore = "user-agents/core/"
uaDirExtended = "user-agents/extended/"

// where to find the features.js.php file from the web
featuresScriptWebPath = "/js/";

Expand Down
4 changes: 0 additions & 4 deletions lib/Detector/lib/modernizr/modernizr.2.5.2.min.custom.js

This file was deleted.

4 changes: 0 additions & 4 deletions lib/Detector/lib/modernizr/modernizr.2.5.2.min.no-tests.js

This file was deleted.

4 changes: 4 additions & 0 deletions lib/Detector/lib/modernizr/modernizr.full.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions lib/Detector/lib/modernizr/modernizr.slim.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions lib/Detector/lib/modernizr/onload.js
@@ -0,0 +1,8 @@
// handle older versions of IE with onload
if (window.addEventListener) {
window.addEventListener("load",cm,false);
} else if (window.attachEvent ) {
window.attachEvent("onload",cm);
} else if (window.onLoad) {
window.onload = cm;
}
3 changes: 3 additions & 0 deletions lib/Detector/tests/persession/_webgl.js
@@ -0,0 +1,3 @@
Modernizr.addTest('ps-webgl', function() {
return !!window.WebGLRenderingContext;
});
10 changes: 10 additions & 0 deletions lib/Detector/tests/persession/battery-level.js
@@ -0,0 +1,10 @@
// Low Battery Level
// Enable a developer to remove CPU intensive CSS/JS when battery is low
// developer.mozilla.org/en/DOM/window.navigator.mozBattery
// By: Paul Sayre

Modernizr.addTest('ps-lowbattery', function () {
var minLevel = 0.20,
battery = Modernizr.prefixed('battery', navigator);
return !!(battery && !battery.charging && battery.level <= minLevel);
});
13 changes: 13 additions & 0 deletions lib/Detector/tests/persession/cookies.js
@@ -0,0 +1,13 @@
// by tauren
// https://github.com/Modernizr/Modernizr/issues/191

Modernizr.addTest('ps-cookies', function () {
// Quick test if browser has cookieEnabled host property
if (navigator.cookieEnabled) return true;
// Create cookie
document.cookie = "cookietest=1";
var ret = document.cookie.indexOf("cookietest=") != -1;
// Delete cookie
document.cookie = "cookietest=1; expires=Thu, 01-Jan-1970 00:00:01 GMT";
return ret;
});
11 changes: 11 additions & 0 deletions lib/Detector/tests/persession/ie8compat.js
@@ -0,0 +1,11 @@
// IE8 compat mode aka Fake IE7
// by Erich Ocean

// In this case, IE8 will be acting as IE7. You may choose to remove features in this case.

// related:
// james.padolsey.com/javascript/detect-ie-in-js-using-conditional-comments/

Modernizr.addTest('ps-ie8compat',function(){
return (!window.addEventListener && document.documentMode && document.documentMode === 7);
});
22 changes: 22 additions & 0 deletions lib/Detector/tests/persession/network-connection.js
@@ -0,0 +1,22 @@
// determining low-bandwidth via navigator.connection

// There are two iterations of the navigator.connection interface:

// The first is present in Android 2.2+ and only in the Browser (not WebView)
// : docs.phonegap.com/en/1.2.0/phonegap_connection_connection.md.html#connection.type
// : davidbcalhoun.com/2010/using-navigator-connection-android

// The second is specced at dev.w3.org/2009/dap/netinfo/ and perhaps landing in WebKit
// : bugs.webkit.org/show_bug.cgi?id=73528

// unknown devices are assumed as fast
// for more rigorous network testing, consider boomerang.js: github.com/bluesmoon/boomerang/

Modernizr.addTest('ps-lowbandwidth', function() {

var connection = navigator.connection || { type: 0 }; // polyfill

return connection.type == 3 || // connection.CELL_2G
connection.type == 4 || // connection.CELL_3G
/^[23]g$/.test(connection.type); // string value in new spec
});
26 changes: 26 additions & 0 deletions lib/Detector/tests/persession/unicode.js
@@ -0,0 +1,26 @@
/**
* Unicode special character support
*
* Detection is made by testing missing glyph box rendering against star character
* If widths are the same, this "probably" means the browser didn't support the star character and rendered a glyph box instead
* Just need to ensure the font characters have different widths
*
* Warning : positive Unicode support doesn't mean you can use it inside <title>, this seams more related to OS & Language packs
*/
Modernizr.addTest('ps-unicode', function() {

var bool,
missingGlyph = document.createElement('span'),
star = document.createElement('span');

Modernizr.testStyles('#modernizr{font-family:Arial,sans;font-size:300em;}', function(node) {
missingGlyph.innerHTML = '&#5987';
star.innerHTML = '&#9734';
node.appendChild(missingGlyph);
node.appendChild(star);
bool = 'offsetWidth' in missingGlyph && missingGlyph.offsetWidth !== star.offsetWidth;
});

return bool;

});

0 comments on commit 0515140

Please sign in to comment.