CSS压缩合并算法

HTML2014-12-11 10:42
purifier = function( file_content ) {
    fc = file_content; 
    fc = replace(fc,'\r',''); //删除回车符
    fc = replace(fc,'[\n\t]',' '); //替换为空格(不删除是怕出错)
    fc = replace(fc,"/\*.*?\*/"," "); //删除注释
    fc = replace(fc,"\s*([\{\}\(\)\;\:\,\!])\s*","\1"); //清除符号前后空格
    fc = replace(fc,";\s*;",";"); //删除多余的分号
    fc = replace(fc,'  +',' '); //多空格替换成单空格
    fc = replace(fc,'\,[\s\.\#\d]*\{',"{"); //容错处理
    return string.trim(fc); //删除前后空格并返回
}

以下是在网上找的JS代码:

<script type="text/javascript"> 
<!--
    var lCSSCoder = {
        format: function (s) {//格式化代码
            s = s.replace(/\s*([\{\}\:\;\,])\s*/g, "$1");
            s = s.replace(/;\s*;/g, ";"); //清除连续分号
            s = s.replace(/\,[\s\.\#\d]*{/g, "{");
            s = s.replace(/([^\s])\{([^\s])/g, "$1 {\n\t$2");
            s = s.replace(/([^\s])\}([^\n]*)/g, "$1\n}\n$2");
            s = s.replace(/([^\s]);([^\s\}])/g, "$1;\n\t$2");
            return s;
        },
        pack: function (s) {//压缩代码
            s = s.replace(/\/\*(.|\n)*?\*\//g, ""); //删除注释
            s = s.replace(/\s*([\{\}\:\;\,])\s*/g, "$1");
            s = s.replace(/\,[\s\.\#\d]*\{/g, "{"); //容错处理
            s = s.replace(/;\s*;/g, ";"); //清除连续分号
            s = s.match(/^\s*(\S+(\s+\S+)*)\s*$/); //去掉首尾空白
            return (s == null) ? "" : s[1];
        }
    };
    function CSS(s) {
        $("Code_2").value = lCSSCoder[s]($("Code_1").value);
    }
//-->
</script>

原文链接: http://blog.jtwo.me/css-compression-merging-algorithm