Skip to content

Instantly share code, notes, and snippets.

@CaptainN
Created June 4, 2012 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CaptainN/2871373 to your computer and use it in GitHub Desktop.
Save CaptainN/2871373 to your computer and use it in GitHub Desktop.
Gets proprietary CSS prefix for current browser - reduced from StyleFix
/**
* prefix method reduced from StyleFix 1.0.2
* @author Lea Verou
* MIT license
*/
var prefix = (function() {
if (!window.getComputedStyle) return '';
var prefixes = {},
properties = [],
shorthands = {},
style = getComputedStyle(document.documentElement, null),
dummy = document.createElement('div').style;
// Why are we doing this instead of iterating over properties in a .style object? Cause Webkit won't iterate over those.
var iterate = function(property) {
if(property.charAt(0) === '-') {
properties.push(property);
var parts = property.split('-'),
prefix = parts[1];
// Count prefix uses
prefixes[prefix] = ++prefixes[prefix] || 1;
// This helps determining shorthands
while(parts.length > 3) {
parts.pop();
var shorthand = parts.join('-');
if(supported(shorthand) && properties.indexOf(shorthand) === -1) {
properties.push(shorthand);
}
}
}
},
supported = function(property) {
return property.replace(/-([a-z])/g, function($0, $1) {
return $1.toUpperCase();
}).replace('-','') in dummy;
}
// Some browsers have numerical indices for the properties, some don't
if(style.length > 0) {
for(var i=0; i<style.length; i++) {
iterate(style[i])
}
}
else {
for(var property in style) {
iterate(
property.replace(/[A-Z]/g, function($0) {
return '-' + $0.toLowerCase();
})
);
}
}
// Find most frequently used prefix
var highest = {uses:0};
for(var prefix in prefixes) {
var uses = prefixes[prefix];
if(highest.uses < uses) {
highest = {prefix: prefix, uses: uses};
}
}
return '-' + highest.prefix + '-';
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment