Merge pull request #1667 from patrickkettner/prefixed-css-property

add prefixedCSSValue, to test for prefixed css values
This commit is contained in:
patrick kettner
2015-09-25 08:18:22 -07:00
2 changed files with 86 additions and 0 deletions

51
src/prefixedCSSValue.js Normal file
View File

@@ -0,0 +1,51 @@
define(['ModernizrProto', 'domPrefixes', 'createElement'], function(ModernizrProto, domPrefixes, createElement) {
/**
* prefixedCSSValue is a way test for prefixed css properties (e.g. display: -webkit-flex)
*
* @memberof Modernizr
* @name Modernizr.prefixedCSSValue
* @optionName Modernizr.prefixedCSSValue()
* @optionProp prefixedCSSValue
* @access public
* @function prefixedCSSValue
* @param {string} prop - String name of the property to test for
* @param {string} value - String value of the non prefixed version of the value you want to test for
* @returns {string|false} The string representing the (possibly prefixed)
* valid version of the property, or `false` when it is unsupported.
* @example
*
* `Modernizr.prefixedCSSValue` is a way test for prefixed css properties (e.g. display: -webkit-flex)
*
* ```js
* Modernizr.prefixedCSSValue('background', 'linear-gradient(left, red, red)')
* ```
*
*/
var prefixedCSSValue = function(prop, value) {
var result = false;
var elem = createElement('div');
var style = elem.style;
if (prop in style) {
var i = domPrefixes.length;
style[prop] = value;
result = style[prop];
while (i-- && !result) {
style[prop] = '-' + domPrefixes[i] + '-' + value;
result = style[prop];
}
}
if (result === '') {
result = false;
}
return result;
};
ModernizrProto.prefixedCSSValue = prefixedCSSValue;
return prefixedCSSValue;
});

View File

@@ -0,0 +1,35 @@
describe('prefixedCSSValue', function() {
var prefixedCSSValue;
var cleanup;
before(function(done) {
var req = requirejs.config({
context: Math.random().toString().slice(2),
baseUrl: '../src',
paths: {cleanup: '../test/cleanup'}
});
req(['cleanup', 'prefixedCSSValue'], function(_cleanup, _prefixedCSSValue) {
prefixedCSSValue = _prefixedCSSValue;
cleanup = _cleanup;
done();
});
});
it('returns the value when it is valid', function() {
expect(prefixedCSSValue('display', 'block')).to.equal('block');
});
it('returns false when the prop is not supported', function() {
expect(prefixedCSSValue('fart', 'block')).to.equal(false);
});
it('returns false when value is not supported', function() {
expect(prefixedCSSValue('display', 'fart')).to.equal(false);
});
after(function() {
cleanup();
});
});