自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

原生JS進行CSS格式化和壓縮

開發(fā) 前端
網頁特效很多時候都會遇到CSS被壓縮過的情況,這時查看起來就會非常不方便。而今天我們就要談談原生JS進行CSS格式化和壓縮。

  前言

  一直比較喜歡收集網頁特效,很多時候都會遇到CSS被壓縮過的情況,這時查看起來就會非常不方便,有時為了減少文件大小,也會對自己的CSS進行壓縮,網上提供這樣服務的很多,但都不盡如人意,因此打算自己動手寫一個JS來進行CSS的格式化和壓縮

  原理

  CSS的結構如下:

  選擇器{  css屬性聲明:值;}

  所以對CSS格式化也就比較簡單,大致分為以下幾步;

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

  2、對處理后的字符串按"{"進行分組

  3、遍歷分組,對含有"}"的部分再次以"}"進行分組

  4、對分組后的數據進行處理,主要是加上空格和換行

  對CSS壓縮就比較簡單了,把空格合并,去掉換行就可以了

  格式化

  下面分步對以上步驟進行實現。

  初始化:

  functionformathtmljscss(source, spaceWidth, formatType) {

  this.source = source;

  this.spaceStr = " ";

  if(!isNaN(spaceWidth)) {

  if(spaceWidth >1) {

  this.spaceStr = "";

  for(vari = 0; i <spaceWidth; i++) {

  this.spaceStr += " ";

  }

  }

  else{

  this.spaceStr = " ";

  }

  }

  this.formatType = formatType;

  this.output = [];

  }

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

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

  formathtmljscss.prototype.removeSpace = function() {

  this.source = this.source.replace(/s+| /g, " ")

  .replace(/s*{s*/g, "{")

  .replace(/s*}s*/g, "}")

  .replace(/s*:s*/g, ":")

  .replace(/s*;s*/g, ";");

  }

  2、對處理后的字符串按"{"進行分組

  formathtmljscss.prototype.split = function() {

  varbigqleft = this.source.split("{");

  }

  3、遍歷分組,對含有"}"的部分再次以"}"進行分組

  formathtmljscss.prototype.split = function() {

  varbigqleft = this.source.split("{");

  varbigqright;

  for(vari = 0; i <bigqleft.length; i++) {

  if(bigqleft[i].indexOf("}") != -1) {

  bigqright = bigqleft[i].split("}");

  }

  else{

  }

  }

  }

  4、對分組后的數據進行處理,主要是加上空格和換行

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

  formathtmljscss.prototype.split = function() {

  varbigqleft = this.source.split("{");

  varbigqright;

  for(vari = 0; i <bigqleft.length; i++) {

  if(bigqleft[i].indexOf("}") != -1) {

  bigqright = bigqleft[i].split("}");

  varpv = bigqright[0].split(";");

  for(varj = 0; j <pv.length; j++) {

  pv[j] = this.formatStatement(this.trim(pv[j]),true);

  if(pv[j].length >0) {

  this.output.push(this.spaceStr + pv[j] + "; ");

  }

  }

  this.output.push("} ");

  bigqright[1] = this.trim(this.formatSelect(bigqright[1]));

  if(bigqright[1].length >0) {

  this.output.push(bigqright[1], " { ");

  }

  }

  else{

  this.output.push(this.trim(this.formatSelect(bigqleft[i])), " { ");

  }

  }

  }

  這里調用了幾個方法:trim、formatSelect、formatStatement,下面一一說明。

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

  formathtmljscss.prototype.trim = function(str) {

  returnstr.replace(/(^s*)|(s*$)/g, "");

  }

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

  formathtmljscss.prototype.formatSelect = function(str) {

  returnstr.replace(/./g, " .")

  .replace(/s+/g, " ")

  .replace(/. /g, ".")

  .replace(/s*,s*/g, ",");

  }

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

  formathtmljscss.prototype.formatStatement = function(str, autoCorrect) {

  str = str.replace(/:/g, " : ")

  .replace(/s+/g, " ")

  .replace("# ", "#")

  .replace(/s*px/ig, "px")

  .replace(/s*-s*/g, "-")

  .replace(/s*:/g, ":");

  returnstr;

  }

  調用

  調用部分比較簡單,對于格式化來說就是去掉空格和換行,然后分組處理,對于壓縮來說就是去掉空格和換行:

  formathtmljscss.prototype.formatcss = function() {

  if(this.formatType == "compress") {

  this.removeSpace();

  }

  else{

  this.removeSpace();

  this.split();

  this.source = this.output.join("");

  }

  }

  界面HTML代碼:

[[63738]]

[[63739]]

View Code

  <div id="content">

  <div class="container">

  <div class="box">

  <div class="main">

  <h2>CSS格式化/壓縮</h2>

  <div id="blurb">

  <fieldset id="options">

  <button id="submit">

  <span>格式化 / 壓縮  <img alt="格式化"src="/images/29.png"/></span>

</button><br/>

  <span>縮進:</span>

  <ul>

  <li>

  <select name="tabsize"id="tabsize">

  <option value="1">tab鍵縮進</option>

  <option value="2">2空格縮進</option>

  <option selected="selected"value="4">4空格縮進</option>

  </select>

  </li>

  </ul><br />

  <span>類型:</span><br />

  <input type="radio"name="format_type"value="format"checked="checked"id="format_format"/><label for="format_format">格式化</label>

  <input type="radio"name="format_type"value="compress"id="format_compress"/><label for="format_compress">壓縮</label>

  </fieldset>

  </div>

  <div id="beauty">

  <fieldset id="textarea-wrap">

  <textarea rows="20"cols="40"id="source"></textarea>

  </fieldset>

  </div>

  </div>

  </div>

  </div>

  </div>

  跟頁面元素按鈕綁定事件:

[[63738]]

[[63739]]

View Code

  window.onload = function() {

  varsubmitBtn = document.getElementById("submit");

  vartabsize = document.getElementById("tabsize");

  varsourceCon = document.getElementById("source");

  varsize = 4;

  varformatType = "format";

  submitBtn.onclick = function() {

  varradios = document.getElementsByName("format_type");

  for(i = 0; i <radios.length; i++) {

  if(radios[i].checked) {

  formatType = radios[i].value;

  break;

  }

  }

  varformat = newformathtmljscss(sourceCon.value, size, formatType);

  format.formatcss();

  sourceCon.value = format.source;

  }

  tabsize.onchange = function() {

  size = this.options[this.options.selectedIndex].value;

  submitBtn.click();

  returnfalse;

  }

  }

  演示

  歡迎大家測試挑刺哈!

[[63740]]

作者:Artwl 出處:http://artwl.cnblogs.com

  本文首發(fā)博客園,版權歸作者跟博客園共有。轉載必須保留本段聲明,并在頁面顯著位置給出本文鏈接,否則保留追究法律責任的權利。

  推薦工具:在線測試正則表達式、JS/HTML在線格式化

責任編輯:彭凡 來源: 博客園
相關推薦

2012-03-27 09:42:57

JavaScriptCSS

2018-12-03 09:10:07

Linux驅動器命令

2020-11-03 10:21:33

MySQL

2009-08-03 14:25:59

C#日期格式化

2013-02-01 10:14:14

Visual Stud

2024-01-08 22:03:22

python代碼開發(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格式化時間

2013-07-08 17:41:53

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

2018-11-02 10:45:35

windowsU盤格式化

2010-07-16 16:00:08

Perl格式化輸出

2015-01-07 15:21:30

Android Stu代碼格式化

2019-05-17 13:20:57

Black格式化工具Python

2010-07-16 15:23:34

Perl格式化輸出

2010-07-16 15:44:57

Perl格式化輸出

2010-07-29 11:03:53

Flex代碼格式化
點贊
收藏

51CTO技術棧公眾號