Skip to content

Instantly share code, notes, and snippets.

@itsoli
Last active August 29, 2015 14:03
Show Gist options
  • Save itsoli/3292dbecfe35fb5478f7 to your computer and use it in GitHub Desktop.
Save itsoli/3292dbecfe35fb5478f7 to your computer and use it in GitHub Desktop.
var css3prop = (function () {
'use strict';
function dashedToCamelCase(string) {
return string.replace(/(\-[a-z])/g, function($1) {
return $1.charAt(1).toUpperCase();
});
}
function camelCaseToDashed(string) {
return string.replace(/([A-Z])/g, function($1) {
return '-' + $1.toLowerCase();
});
}
var css3prop = {};
var map = {};
css3prop.js = function (property) {
if (!property) {
return undefined;
}
if (property in map) {
return map[property];
}
var result = undefined;
var style = document.documentElement.style;
if (property in style) {
result = property;
} else {
var vendors = ['ms', 'Moz', 'Webkit', 'O'];
var suffix = property.charAt(0).toUpperCase() + property.slice(1);
for (var i = 0; i < vendors.length; ++i) {
var prop = vendors[i] + suffix;
if (prop in style) {
result = prop;
break;
}
}
}
map[property] = result;
return result;
};
css3prop.css = function (property) {
var result = css3prop.js(dashedToCamelCase(property));
if (result) {
result = camelCaseToDashed(result);
return (result.substring(0, 3) === 'ms-') ? '-' + result : result;
}
return undefined;
};
return css3prop;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment