Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

codified the behavior of prefixed(arg,arg,arg) with lots of tests. ref

  • Loading branch information...
commit fe9892481832e70ed79529f96452c7f691c4cfc9 1 parent d7b3d35
@paulirish paulirish authored
View
2  feature-detects/battery-level.js
@@ -6,6 +6,6 @@
Modernizr.addTest('lowbattery', function () {
var minLevel = 0.20,
- battery = navigator[Modernizr.prefixed('battery', navigator)];
+ battery = Modernizr.prefixed('battery', navigator);
return !!(battery && !battery.charging && battery.level <= minLevel);
});
View
2  feature-detects/quota-management-api.js
@@ -7,6 +7,6 @@
// By Addy Osmani
Modernizr.addTest('quotamanagement', function(){
- var storage = window[ Modernizr.prefixed('StorageInfo', window) ];
+ var storage = Modernizr.prefixed('StorageInfo', window);
return !!('TEMPORARY' in storage && 'PERSISTENT' in storage);
});
View
16 modernizr.js
@@ -295,7 +295,12 @@ window.Modernizr = (function( window, document, undefined ) {
function testDOMProps( props, obj, elem ) {
for ( var i in props ) {
if ( obj[ props[i] ] !== undefined) {
- return elem ? obj[props[i]].bind(elem) : obj[props[i]];
+ // bind!
+ return elem ? obj[props[i]].bind(elem) :
+ // return the property name as a string
+ elem === false ? props[i] :
+ // return the unbound function or obj or value
+ obj[props[i]];
}
}
return false;
@@ -312,8 +317,11 @@ window.Modernizr = (function( window, document, undefined ) {
var ucProp = prop.charAt(0).toUpperCase() + prop.substr(1),
props = (prop + ' ' + cssomPrefixes.join(ucProp + ' ') + ucProp).split(' ');
- if(!is(prefixed, "object")) {
+ // did they call .prefixed('boxSizing') or are we just testing a prop?
+ if(is(prefixed, "string") || is(prefixed, "undefined")) {
return testProps(props, prefixed);
+
+ // otherwise, they called .prefixed('requestAnimationFrame', window[, elem])
} else {
props = (prop + ' ' + (domPrefixes).join(ucProp + ' ') + ucProp).split(' ');
return testDOMProps(props, prefixed, elem);
@@ -1145,10 +1153,10 @@ window.Modernizr = (function( window, document, undefined ) {
// Remove "no-js" class from <html> element, if it exists:
- docElement.className = docElement.className.replace(/(^|\s)no-js(\s|$)/, '$1$2')
+ docElement.className = docElement.className.replace(/(^|\s)no-js(\s|$)/, '$1$2') +
// Add the new classes to the <html> element.
- + (enableClasses ? ' js ' + classes.join(' ') : '');
+ (enableClasses ? ' js ' + classes.join(' ') : '');
return Modernizr;
View
3  test/index.html
@@ -29,8 +29,9 @@
textarea { width: 100%; min-height: 75px;}
#caniusetrigger { font-size: 38px; font-family: monospace; display:block; }
</style>
-
+ <!--
<script src="https://raw.github.com/Modernizr/Modernizr/master/modernizr.js"></script>
+ -->
<script>window.Modernizr || document.write('<script src="../modernizr.js"><\/script>')</script>
<script src="js/lib/polyfills.js"></script>
View
93 test/js/unit.js
@@ -433,7 +433,7 @@ test('Modernizr.testAllProps()',function(){
-test('Modernizr.prefixed()', function(){
+test('Modernizr.prefixed() - css and DOM resolving', function(){
// https://gist.github.com/523692
function gimmePrefix(prop, obj){
@@ -467,9 +467,9 @@ test('Modernizr.prefixed()', function(){
var propArr = ['transition', 'backgroundSize', 'boxSizing', 'borderImage',
'borderRadius', 'boxShadow', 'columnCount'];
- var domPropArr = [{ 'prop': 'requestAnimationFrame', 'obj': window },
- { 'prop': 'querySelectorAll', 'obj': document },
- { 'prop': 'matchesSelector', 'obj': document.createElement('div') }];
+ var domPropArr = [{ 'prop': 'requestAnimationFrame', 'obj': window },
+ { 'prop': 'querySelectorAll', 'obj': document },
+ { 'prop': 'matchesSelector', 'obj': document.createElement('div') }];
for (var i = -1, len = propArr.length; ++i < len; ){
var prop = propArr[i];
@@ -487,6 +487,91 @@ test('Modernizr.prefixed()', function(){
});
+// FIXME: so like all of these are whitelisting for webkit. i'd like to improve that.
+test('Modernizr.prefixed autobind', function(){
+
+ if (window.webkitRequestAnimationFrame){
+ // rAF returns a function
+ equals(
+ 'function',
+ typeof Modernizr.prefixed('requestAnimationFrame', window),
+ "Modernizr.prefixed('requestAnimationFrame', window) returns a function")
+
+ // unless we false it to a string
+ equals(
+ 'webkitRequestAnimationFrame',
+ Modernizr.prefixed('requestAnimationFrame', window, false),
+ "Modernizr.prefixed('requestAnimationFrame', window, false) returns a string (the prop name)")
+
+ }
+
+ if (document.body.webkitMatchesSelector){
+
+ var fn = Modernizr.prefixed('matchesSelector', HTMLElement.prototype, document.body);
+
+ //returns function
+ equals(
+ 'function',
+ typeof fn,
+ "Modernizr.prefixed('matchesSelector', HTMLElement.prototype, document.body) returns a function");
+
+ // fn scoping
+ equals(
+ true,
+ fn('body'),
+ "Modernizr.prefixed('matchesSelector', HTMLElement.prototype, document.body) is scoped to the body")
+
+ }
+
+ if (window.webkitNotifications){
+ // should be an object.
+
+ equals(
+ 'object',
+ typeof Modernizr.prefixed('Notifications', window),
+ "Modernizr.prefixed('Notifications') returns an object");
+
+ }
+
+ if (typeof document.webkitIsFullScreen !== 'undefined'){
+ // boolean
+
+ equals(
+ 'boolean',
+ typeof Modernizr.prefixed('isFullScreen', document),
+ "Modernizr.prefixed('isFullScreen') returns a boolean");
+ }
+
+ if (document.body.style.webkitAnimation){
+ // string
+
+ equals(
+ '',
+ typeof Modernizr.prefixed('animation', document.body.style),
+ "Modernizr.prefixed('animation', document.body.style) returns value of that, as a string");
+
+ equals(
+ 'webkitAnimation',
+ typeof Modernizr.prefixed('animation', document.body.style, false),
+ "Modernizr.prefixed('animation', document.body.style, false) returns the name of the property: webkitAnimation");
+
+ // I don't know how to handle this. currently it returns a string, but should it return the name of the property instead?
+ }
+
+ equals(
+ false,
+ Modernizr.prefixed('doSomethingAmazing$#$', window),
+ "Modernizr.prefixed('doSomethingAmazing$#$', window) : Gobbledygook with prefixed(str,obj) returns false");
+
+ equals(
+ false,
+ Modernizr.prefixed('doSomethingAmazing$#$', window, document.body),
+ "Modernizr.prefixed('doSomethingAmazing$#$', window) : Gobbledygook with prefixed(str,obj, scope) returns false");
+
+
+});
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.