mirror of
https://github.com/Modernizr/Modernizr.git
synced 2026-01-09 07:38:03 -05:00
Merge pull request #1667 from patrickkettner/prefixed-css-property
add prefixedCSSValue, to test for prefixed css values
This commit is contained in:
51
src/prefixedCSSValue.js
Normal file
51
src/prefixedCSSValue.js
Normal 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;
|
||||
});
|
||||
35
test/browser/src/prefixedCSSValue.js
Normal file
35
test/browser/src/prefixedCSSValue.js
Normal 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();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user