Created
June 4, 2012 23:02
-
-
Save CaptainN/2871373 to your computer and use it in GitHub Desktop.
Gets proprietary CSS prefix for current browser - reduced from StyleFix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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