Skip to content

Instantly share code, notes, and snippets.

@miya2000
Created June 20, 2010 02:25
Show Gist options
  • Select an option

  • Save miya2000/445495 to your computer and use it in GitHub Desktop.

Select an option

Save miya2000/445495 to your computer and use it in GitHub Desktop.
var getValidCssPropertyName = (function() {
function camelize(str) {
return str.replace(/-([a-z])/g, function($0,$1) { return $1.toUpperCase() });
}
function decamelize(str) {
return str.replace( /[A-Z]/g, function($0) { return "-" + $0.toLowerCase() });
}
var tmpStyle = null;
function getValidCssPropertyName(cssStr, forJS) {
if (!cssStr) return '';
if (cssStr == 'float') {
if (!forJS) return 'float';
else return 'cssFloat'/*@cc_on && 'styleFloat' @*/;
}
var style = tmpStyle;
if (style == null) {
style = tmpStyle = document.createElement('div').style;
setTimeout(function() { tmpStyle = null; }, 0);
}
var jsname = camelize(cssStr);
if (jsname in style) return forJS ? jsname : decamelize(cssStr);
jsname = jsname.charAt(0).toUpperCase() + jsname.slice(1);
if (( 'O' + jsname) in style) return forJS ? ('O' + jsname) : ('-o-' + decamelize(cssStr));
if (( 'Moz' + jsname) in style) return forJS ? ('Moz' + jsname) : ('-moz-' + decamelize(cssStr));
if (('Webkit' + jsname) in style) return forJS ? ('Webkit' + jsname) : ('-webkit-' + decamelize(cssStr));
return '';
}
return getValidCssPropertyName;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment