偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

用原生JS進(jìn)行CSS格式化和壓縮

開(kāi)發(fā) 前端
一直比較喜歡收集網(wǎng)頁(yè)特效,很多時(shí)候都會(huì)遇到CSS被壓縮過(guò)的情況,這時(shí)查看起來(lái)就會(huì)非常不方便,有時(shí)為了減少文件大小,也會(huì)對(duì)自己的CSS進(jìn)行壓縮,網(wǎng)上提供這樣服務(wù)的很多,但都不盡如人意,因此打算自己動(dòng)手寫(xiě)一個(gè)JS來(lái)進(jìn)行CSS的格式化和壓縮。

前  言

一直比較喜歡收集網(wǎng)頁(yè)特效,很多時(shí)候都會(huì)遇到CSS被壓縮過(guò)的情況,這時(shí)查看起來(lái)就會(huì)非常不方便,有時(shí)為了減少文件大小,也會(huì)對(duì)自己的CSS進(jìn)行壓縮,網(wǎng)上提供這樣服務(wù)的很多,但都不盡如人意,因此打算自己動(dòng)手寫(xiě)一個(gè)JS來(lái)進(jìn)行CSS的格式化和壓縮。

原  理

CSS的結(jié)構(gòu)如下:

  1. 選擇器{  
  2.   css屬性聲明:值;  

所以對(duì)CSS格式化也就比較簡(jiǎn)單,大致分為以下幾步;

1、把多個(gè)空格合并成一個(gè),去掉換行

2、對(duì)處理后的字符串按"{"進(jìn)行分組

3、遍歷分組,對(duì)含有"}"的部分再次以"}"進(jìn)行分組

4、對(duì)分組后的數(shù)據(jù)進(jìn)行處理,主要是加上空格和換行

對(duì)CSS壓縮就比較簡(jiǎn)單了,把空格合并,去掉換行就可以了

 

格式化

下面分步對(duì)以上步驟進(jìn)行實(shí)現(xiàn)。

初始化:

  1. function formathtmljscss(source, spaceWidth, formatType) {  
  2.     this.source = source;  
  3.     this.spaceStr = "    ";  
  4.     if (!isNaN(spaceWidth)) {  
  5.         if (spaceWidth > 1) {  
  6.             this.spaceStr = "";  
  7.             for (var i = 0; i < spaceWidth; i++) {  
  8.                 this.spaceStr += " ";  
  9.             }  
  10.         }  
  11.         else {  
  12.             this.spaceStr = "\t";  
  13.         }  
  14.     }  
  15.     this.formatType = formatType;  
  16.     this.output = [];  

這里幾個(gè)參數(shù)分別是要格式化的CSS代碼、CSS屬性聲明前空格寬度,類(lèi)型(格式化/壓縮)

1、把多個(gè)空格合并成一個(gè),去掉換行:

  1. formathtmljscss.prototype.removeSpace = function () {  
  2.     this.source = this.source.replace(/\s+|\n/g, " ")  
  3.         .replace(/\s*{\s*/g, "{")  
  4.         .replace(/\s*}\s*/g, "}")  
  5.         .replace(/\s*:\s*/g, ":")  
  6.         .replace(/\s*;\s*/g, ";");  

2、對(duì)處理后的字符串按"{"進(jìn)行分組

  1. formathtmljscss.prototype.split = function () {  
  2.     var bigqleft = this.source.split("{");  

3、遍歷分組,對(duì)含有"}"的部分再次以"}"進(jìn)行分組

  1. formathtmljscss.prototype.split = function () {  
  2.     var bigqleft = this.source.split("{");  
  3.     var bigqright;  
  4.     for (var i = 0; i < bigqleft.length; i++) {  
  5.         if (bigqleft[i].indexOf("}") != -1) {  
  6.             bigqright = bigqleft[i].split("}");  
  7.         }  
  8.         else {  
  9.              
  10.         }  
  11.     }  

4、對(duì)分組后的數(shù)據(jù)進(jìn)行處理,主要是加上空格和換行

這里的處理主要分為,把CSS屬性聲明和值部分取出來(lái),然后加上空格和換行:

  1. formathtmljscss.prototype.split = function () {  
  2.     var bigqleft = this.source.split("{");  
  3.     var bigqright;  
  4.     for (var i = 0; i < bigqleft.length; i++) {  
  5.         if (bigqleft[i].indexOf("}") != -1) {  
  6.             bigqright = bigqleft[i].split("}");  
  7.             var pv = bigqright[0].split(";");  
  8.             for (var j = 0; j < pv.length; j++) {  
  9.                 pv[j] = this.formatStatement(this.trim(pv[j]),true);  
  10.                 if (pv[j].length > 0) {  
  11.                     this.output.push(this.spaceStr + pv[j] + ";\n");  
  12.                 }  
  13.             }  
  14.             this.output.push("}\n");  
  15.             bigqright[1] = this.trim(this.formatSelect(bigqright[1]));  
  16.             if (bigqright[1].length > 0) {  
  17.                 this.output.push(bigqright[1], " {\n");  
  18.             }  
  19.         }  
  20.         else {  
  21.             this.output.push(this.trim(this.formatSelect(bigqleft[i])), " {\n");  
  22.         }  
  23.     }  

這里調(diào)用了幾個(gè)方法:trim、formatSelect、formatStatement,下面一一說(shuō)明。

trim:從命名就可以看出是去除首尾空格;

  1. formathtmljscss.prototype.trim = function (str) {  
  2.     return str.replace(/(^\s*)|(\s*$)/g, "");  

formatSelect:是處理選擇器部分語(yǔ)法,做法就是給"."前面加上空格,把","前后的空格去掉,把多個(gè)空格合并為一個(gè):

  1. formathtmljscss.prototype.formatSelect = function (str) {  
  2.     return str.replace(/\./g, " .")  
  3.         .replace(/\s+/g, " ")  
  4.         .replace(/\. /g, ".")  
  5.         .replace(/\s*,\s*/g, ",");  

formatStatement:是處理“css屬性聲明:值;”部分的語(yǔ)法,做法就是給":"后面加上空格,把多個(gè)空格合并為一個(gè),去掉“#”后面的空格,去掉"px"前面的空格,去掉"-"兩邊的空格,去掉":"前面的空格:

  1. formathtmljscss.prototype.formatStatement = function (str, autoCorrect) {  
  2.     str = str.replace(/:/g, " : ")  
  3.         .replace(/\s+/g, " ")  
  4.         .replace("# ""#")  
  5.         .replace(/\s*px/ig, "px")  
  6.         .replace(/\s*-\s*/g, "-")  
  7.         .replace(/\s*:/g, ":");  
  8.  
  9.     return str;  

調(diào)  用

調(diào)用部分比較簡(jiǎn)單,對(duì)于格式化來(lái)說(shuō)就是去掉空格和換行,然后分組處理,對(duì)于壓縮來(lái)說(shuō)就是去掉空格和換行:

  1. formathtmljscss.prototype.formatcss = function () {  
  2.     if (this.formatType == "compress") {  
  3.         this.removeSpace();  
  4.     }  
  5.     else {  
  6.         this.removeSpace();  
  7.         this.split();  
  8.         this.source = this.output.join("");  
  9.     }  

界面HTML代碼:

  1. <div id="content"> 
  2.         <div class="container"> 
  3.             <div class="box"> 
  4.                 <div class="main"> 
  5.                     <h2>CSS格式化/壓縮</h2> 
  6.                     <div id="blurb"> 
  7.                         <fieldset id="options"> 
  8.                             <button id="submit"> 
  9.                                 <span>格式化 / 壓縮&nbsp;&nbsp;<img alt="格式化" src="/images/29.png"/></span> 
  10.                             </button><br/> 
  11.                             <span>縮進(jìn):</span> 
  12.                             <ul> 
  13.                                 <li> 
  14.                                     <select name="tabsize" id="tabsize"> 
  15.                                         <option value="1">tab鍵縮進(jìn)</option> 
  16.                                         <option value="2">2空格縮進(jìn)</option> 
  17.                                         <option selected="selected" value="4">4空格縮進(jìn)</option> 
  18.                                     </select> 
  19.                                 </li> 
  20.                             </ul><br /> 
  21.                             <span>類(lèi)型:</span><br /> 
  22.                             <input type="radio" name="format_type" value="format" checked="checked" id="format_format" /><label for="format_format">格式化</label> 
  23.                             <input type="radio" name="format_type" value="compress" id="format_compress" /><label for="format_compress">壓縮</label> 
  24.                         </fieldset> 
  25.                     </div> 
  26.                     <div id="beauty"> 
  27.                         <fieldset id="textarea-wrap"> 
  28.                             <textarea rows="20" cols="40" id="source"></textarea> 
  29.                         </fieldset> 
  30.                     </div> 
  31.                 </div> 
  32.             </div> 
  33.         </div> 
  34.     </div> 

跟頁(yè)面元素按鈕綁定事件:

  1. window.onload = function () {  
  2.     var submitBtn = document.getElementById("submit");  
  3.     var tabsize = document.getElementById("tabsize");  
  4.     var sourceCon = document.getElementById("source");  
  5.     var size = 4;  
  6.     var formatType = "format";  
  7.     submitBtn.onclick = function () {  
  8.         var radios = document.getElementsByName("format_type");  
  9.         for (i = 0; i < radios.length; i++) {  
  10.             if (radios[i].checked) {  
  11.                 formatType = radios[i].value;  
  12.                 break;  
  13.             }  
  14.         }  
  15.         var format = new formathtmljscss(sourceCon.value, size, formatType);  
  16.         format.formatcss();  
  17.         sourceCon.value = format.source;  
  18.     }  
  19.     tabsize.onchange = function () {  
  20.         size = this.options[this.options.selectedIndex].value;  
  21.         submitBtn.click();  
  22.         return false;  
  23.     }  

演示(請(qǐng)進(jìn)原文)

原文鏈接:http://www.cnblogs.com/artwl/archive/2012/03/25/2416909.html

【編輯推薦】

  1. 10件有關(guān)JavaScript讓人費(fèi)解的事情
  2. 面向?qū)ο蟮腏avaScript基本知識(shí)指南大全
  3. 是時(shí)候開(kāi)始使用JavaScript嚴(yán)格模式了
  4. 好用的高質(zhì)量JavaScript庫(kù)一覽
  5. 一位反JavaScript主義者的覺(jué)醒
責(zé)任編輯:林師授 來(lái)源: Artwl的博客
相關(guān)推薦

2012-03-26 10:45:34

CSS

2018-12-03 09:10:07

Linux驅(qū)動(dòng)器命令

2020-11-03 10:21:33

MySQL

2013-02-01 10:14:14

Visual Stud

2009-08-03 14:25:59

C#日期格式化

2024-01-08 22:03:22

python代碼開(kāi)發(fā)

2022-05-09 08:17:37

InstantJava字符

2010-08-03 15:36:38

FlexBuilder

2024-12-09 08:10:00

Python字符串格式化

2010-07-29 11:12:30

Flex日期格式化

2009-08-03 16:24:05

C#格式化

2023-06-13 07:50:49

Gopher格式化時(shí)間

2013-07-08 17:41:53

Linux 系統(tǒng)U盤(pán)格式化

2019-05-17 13:20:57

Black格式化工具Python

2010-07-16 15:23:34

Perl格式化輸出

2010-07-29 11:03:53

Flex代碼格式化

2010-07-16 15:44:57

Perl格式化輸出

2010-10-28 15:32:42

oracle日期格式化

2018-11-02 10:45:35

windowsU盤(pán)格式化

2010-07-16 16:00:08

Perl格式化輸出
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)