JavaScript function code for CSS code compression and formatting

Source: Internet
Author: User

CopyCode The Code is as follows: var lcsscoder = {
Format: function (s) {// format the code
S = S. Replace (/\ s * ([\{\}\:\;\,]) \ s */g, "$1 ");
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 ");
S = S. Replace (//; \ s *;/g, ";"); // clear the continuous semicolon
Return S;
},
Pack: function (s) {// compress code
S = S. Replace (/\ * (. | \ n )*? \ * \ // G, ""); // Delete comment
S = S. Replace (/\ s * ([\{\}\:\;\,]) \ s */g, "$1 ");
S = S. Replace (/\, [\ s \. \ # \ D] * \ {/g, "{"); // fault tolerance Processing
S = S. Replace (//; \ s *;/g, ";"); // clear the continuous semicolon
Return S;
}
};

DEMO code:Xmlns = "http://www.w3.org/1999/xhtml" lang = "ZH-CN">



Original code:
/* Pagehead */<br/> # pagehead {float: Left; padding: 3px 0; border-bottom: 1px solid # b0bec7; margin-bottom: 6px ;} <br/> # hcity Li {float: Left; padding: 0 10px 0 0;} <br/> # hcity Li. FST {padding: 0 ;}< br/># hright {<br/> float: Right; padding: 0 5px 0 20px; <br/> background: URL (img/ico_home.gif) No-repeat 0 50%;} <br/>. subpage # hright {background-image: URL (img/ico_favorite.gif) ;}</P> <p>/* pagebody */<br/> # pagebody {float: left; padding: 4px 0 0 0;} <br/>. scol {float: Left; width: 174px ;}< br/>. mcol {float: Right; width: 678px; overflow: hidden ;}</P> <p> # logo {Height: 66px; margin: 0; padding: 0 ;} <br/>/* maillogin */<br/> # maillogin {Height: 32px;} <br/> # maillogin Li {float: Left ;} <br/> # maillogin li.txt {Line-Height: 20px;} <br/> # maillogin Li. indium {padding: 0 2px ;}< br/> # maillogin Li. PLD input {<br/> margin: 0; padding: 0 4px; Border: 1px solid # 7a9dbb; Font: bold 12px/18px tahoma, Geneva, Arial, sans-serif; height: 18px ;}< br/> # maillogin Li. indium input. notclicked {color: # CCC; font-weight: normal ;}< br/> # maillogin Li. sel {padding: 1px 10px 0 0 0 ;}< br/> # maillogin Li. BTN {padding: 1px 12px 0 5px;} <br/>
After conversion:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.