Skip to content

Commit

Permalink
basic feature detection framework idea
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Higgins committed Jul 25, 2010
1 parent 448b672 commit 479c19a
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
74 changes: 74 additions & 0 deletions feature.js
@@ -0,0 +1,74 @@
dojo.provide("plugd.feature");

(function(d){

var tests = {}, htmlelm = d.doc.documentElement;

d.feature = function(test){
// summary: Returns a boolean synchronously to test something.
//
// test: String|Array
// A string get of something to test.
// If an array, all conditions in the array must pass
// each item in the array is to be a string test name
//
// example:
// | if(dojo.feature("opacity")){ /* use png */ }
//
// example:
// | if(dojo.feature(["opacity", "css3", "placeholder"])){
// | // enviroment has opacity, css3 and input placeholder=""
// | }
//
// returns: Boolean
// Or undefined if no test has been created for a given testname

// multi means all must pass to be true:
if(d.isArray(test)){ return d.every(test, d.feature); }

// single test consideration:
if(tests[test] !== true || tests[test] !== false){
if(d.isFunction(tests[test])){
tests[test] = !!tests[test]();
// add a class name to the `html` element for CSS uses
d.addClass(htmlelm, (!tests[test] ? "no-" : "") + test);
}
}

// always return something:
return tests[test];
}

d.feature.test = function(test, testcb, now){
// summary: Add a test condition to this enviroment. key is the testname,
// testcb: Function|Boolean
// If a function, will be run once ever to do a test. the return of
// this function kills the function and returns a boolean.
// now: Boolean?
// Immediately force the execution of this test
// example:
// | d.feature.test("opacity", function(){
// | // add a node. calculate some styles. determine in here if
// | // this enviorment supports "opacity", whatever the hell that means
// | return true;
// | });
//
// example:
// Possible to just set a boolean for the test if you've already run it
// though no class name will be applied to the `html` element.
// | d.feature.test("js-runs", true);
//
// example:
// Force the immediate processing of a passed test: d.feature("somethingfun")
// will always return false, but only execute once, immediately upon registration
// passing a function will trigger `html` class name addition
// | d.feature.test("somethingfun", function(){ return false; }, true);

tests[test] = testcb;
now && d.feature(test);
}

// basic test: js is always enabled if we've run, clearly.
d.feature.test("js", function(){ return true; }, true);

})(dojo);
1 change: 1 addition & 0 deletions tests/base.js
Expand Up @@ -6,4 +6,5 @@ if(dojo.isBrowser){
doh.registerUrl("plugd.tests.node", dojo.moduleUrl("plugd.tests", "node.html"));
doh.registerUrl("plugd.tests.keys", dojo.moduleUrl("plugd.tests", "keys.html"));
doh.registerUrl("plugd.tests.data", dojo.moduleUrl("plugd.tests", "data.html"));
doh.registerUrl("plugd.tests.feature", dojo.moduleUrl("plugd.tests", "feature.html"));
}
32 changes: 32 additions & 0 deletions tests/feature.html
@@ -0,0 +1,32 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Testing dojo.feature</title>

<script type="text/javascript" src="../../dojo/dojo.js" djConfig="isDebug: true"></script>

<script type="text/javascript">
dojo.require("doh.runner");
dojo.require("plugd.feature");

dojo.addOnLoad(function(){

var html = dojo.doc.documentElement;

doh.register("t",
[

function jsclass(t){
// js test always runs
t.t(dojo.hasClass(html, "js"));
}
]
);
doh.run();
});
</script>
</head>
<body>
</body>
</html>

0 comments on commit 479c19a

Please sign in to comment.