代码改变世界

CSS代码格式化和加密化

2011-03-04 14:29  沐海  阅读(449)  评论(0编辑  收藏  举报
<html> 
 <head> 
  <title>CSS代码格式化和加密化</title> 
  <meta name="Generator" content="EditPlus"> 
  <meta name="Author" content=""> 
  <meta name="Keywords" content=""> 
  <meta name="Description" content=""> 
 </head> 
 <body> 
<script language="JavaScript"> 
<!-- 
function $() { 
  var elements = new Array(); 
  for (var i = 0; i < arguments.length; i++) { 
  var element = arguments[i]; 
  if (typeof element == 'string') 
    element = document.getElementById(element); 
  if (arguments.length == 1)  
    return element;    
  elements.push(element); 
  }    
  return elements; 
} 
function CSSencode(code) 
{ 
 code = code.replace(/\n/ig,''); 
 code = code.replace(/(\s){2,}/ig,'$1'); 
 code = code.replace(/\t/ig,''); 
 code = code.replace(/\n\}/ig,'\}'); 
 code = code.replace(/\n\{\s*/ig,'\{'); 
 code = code.replace(/(\S)\s*\}/ig,'$1\}'); 
 code = code.replace(/(\S)\s*\{/ig,'$1\{'); 
 code = code.replace(/\{\s*(\S)/ig,'\{$1'); 
 return code; 
} 
function CSSdecode(code) 
{ 
 code = code.replace(/(\s){2,}/ig,'$1'); 
 code = code.replace(/(\S)\s*\{/ig,'$1\n{'); 
 code = code.replace(/\*\/(.[^\}\{]*)}/ig,'\*\/\n$1}'); 
 code = code.replace(/\/\*/ig,'\n\/\*'); 
 code = code.replace(/;\s*(\S)/ig,';\n\t$1'); 
 code = code.replace(/\}\s*(\S)/ig,'\}\n$1'); 
 code = code.replace(/\n\s*\}/ig,'\n\}'); 
 code = code.replace(/\{\s*(\S)/ig,'\{\n\t$1'); 
 code = code.replace(/(\S)\s*\*\//ig,'$1\*\/'); 
 code = code.replace(/\*\/\s*([^\}\{]\S)/ig,'\*\/\n\t$1'); 
 code = code.replace(/(\S)\}/ig,'$1\n\}'); 
 code = code.replace(/(\n){2,}/ig,'\n'); 
 return code; 
} 
//--> 
</script> 
CSS代码格式化和压缩化<hr> 
<textarea name="" rows="" cols="" style="width:600px;height:300px;" id="code"> 
/*请将CSS代码复制到这里,下面是示例*/ 
</textarea><br> 
<input type="submit" onclick="$('code').value = CSSdecode($('code').value);" value="格式化"> | <input type="submit" onclick="$('code').value = CSSencode($('code').value);" value="压缩化" /> 
 </body>  
</html> 
记录生活、工作、学习点滴!
E-Mail:mahaisong@hotmail.com 欢迎大家讨论。
沐海博客园,我有一颗,卓越的心!