finalize.js

by JamesKyle

JavaScript

(function () {
    // Prevent to read twice
    if ($.cssFinalize) {
        return;
    }

    $.cssFinalizeSetup = {
        // Which node CSS3 Finalize should read and add vendor prefixes
        node : 'style,link',
        // If it should add the vendor prefixes
        append : true,
        // This will be called for each nodes after vendor prefixes have been appended
        callback : function(css) {}
    };

    $.fn.cssFinalize = function(options) {
        if (!options || typeof options != 'object') {
            options = {};
        }
        options.node = this;
        $.cssFinalize(options);
        return this;
    };
    
    $.cssFinalize = function(options) {
        var div = document.createElement('div');
        
        options = $.extend({}, $.cssFinalizeSetup, options);
        
        // PropertyRules
        var supportRules = [];

        // Get current vendor prefix
        var currentPrefix;
        if (window.getComputedStyle) {
            var styles = getComputedStyle(document.documentElement, null);

            if (styles.length) {
                for(var i = 0; i < styles.length ; i++) {
                    if (styles[i].charAt(0) === '-') {
                        var pos = styles[i].indexOf('-',1);
                        supportRules.push(styles[i].substr(pos+1));

                        currentPrefix = styles[i].substr(1, pos-1);
                    }
                }
            } else {
                // In Opera CSSStyleDeclaration objects returned by getComputedStyle have length 0
                var deCamelCase = function(str) {
                    return str.replace(/[A-Z]/g, function($0) { return '-' + $0.toLowerCase() });
                }
                for(var i in styles) {
                    var style = deCamelCase(i);
                    if (style.indexOf('-o-') === 0) {
                        supportRules.push(style.substr(3));
                    }
                }
                currentPrefix =...