Skip to content

Commit

Permalink
Revised version of nativeCSSDetect - this time only allows `(prop, va…
Browse files Browse the repository at this point in the history
…lue)` interface, because it's probably more common. Also updated `flexbox` and `flexboxlegacy` tests to show how it could be used.
  • Loading branch information
stucox committed Feb 19, 2013
1 parent 90b1874 commit 82c9a43
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
11 changes: 9 additions & 2 deletions feature-detects/css/flexbox.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
define(['Modernizr', 'testAllProps'], function( Modernizr, testAllProps ) {
define(['Modernizr', 'testAllProps', 'nativeCSSDetect'], function( Modernizr, testAllProps, nativeCSSDetect ) {
// The *new* flexbox
// dev.w3.org/csswg/css3-flexbox
Modernizr.addTest('flexbox', testAllProps('flexWrap'));
Modernizr.addTest('flexbox', function () {
var result = nativeCSSDetect('flex-wrap', 'wrap', true);
if (typeof result !== 'undefined') {
return result;
}
// Failing that, do it the old way
return testAllProps('flexWrap');
});
});
11 changes: 9 additions & 2 deletions feature-detects/css/flexboxlegacy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
define(['Modernizr', 'testAllProps'], function( Modernizr, testAllProps ) {
define(['Modernizr', 'testAllProps', 'nativeCSSDetect'], function( Modernizr, testAllProps, nativeCSSDetect ) {
// The *old* flexbox
// www.w3.org/TR/2009/WD-css3-flexbox-20090723/
Modernizr.addTest('flexboxlegacy', testAllProps('boxDirection'));
Modernizr.addTest('flexboxlegacy', function () {
var result = nativeCSSDetect('box-direction', 'reverse', true);
if (typeof result !== 'undefined') {
return result;
}
// Failing that, do it the old way
return testAllProps('boxDirection');
});
});
38 changes: 28 additions & 10 deletions src/nativeCSSDetect.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
define(['injectElementWithStyles'], function ( injectElementWithStyles ) {
define(['injectElementWithStyles', 'prefixes'], function ( injectElementWithStyles, prefixes ) {
// Function to allow us to use native feature detection functionality if available.
// Only accepts the string notation, i.e.: `nativeCSSDetect('(display:flex)')`, not
// `nativeCSSDetect('display', 'flex')`
function nativeCSSDetect ( str ) {
// Start with the W3C version: http://www.w3.org/TR/css3-conditional/#the-css-interface
// Follows `(DOMString property, DOMString value)` interface
function nativeCSSDetect ( property, value, isPropPrefixed, isValuePrefixed ) {
var propPrefixes = isPropPrefixed ? prefixes : [''],
valuePrefixes = isValuePrefixed ? prefixes : [''],
i = propPrefixes.length;
// Start with the JS API: http://www.w3.org/TR/css3-conditional/#the-css-interface
if ('CSS' in window && 'supports' in window.CSS) {
return window.CSS.supports(str);
// Try every prefixed variant, of both property and value
while (i--) {
j = valuePrefixes.length;
while (j--) {
if (window.CSS.supports(prefixes[i] + property, prefixes[j] + value)) {
return true;
}
}
}
return false;
}
// Otherwise fall back to the at-rule for Opera: they made a bit of a hash of
// `window.CSS.supports()`, by implementing it as `window.supportsCSS()` which
// only supported f(property, value) calls rather than f(conditionText)
// Otherwise fall back to at-rule
else if ('CSSSupportsRule' in window) {
return injectElementWithStyles('@supports ' + str + ' { #modernizr { position: absolute; } }', function( node ) {
// Build a condition string for every prefixed variant
var conditionText = [];
while (i--) {
j = valuePrefixes.length;
while (j--) {
conditionText.push('(' + prefixes[i] + property + ':' + prefixes[j] + value + ')');
}
}
conditionText = conditionText.join(' or ');
return injectElementWithStyles('@supports (' + conditionText + ') { #modernizr { position: absolute; } }', function( node ) {
return (window.getComputedStyle ?
getComputedStyle(node, null) :
node.currentStyle)['position'] == 'absolute';
Expand Down

0 comments on commit 82c9a43

Please sign in to comment.