<html>
    <head>
        <style>    
            textarea{
                width:800px;
                height:250px;
            }
        </style>
        <script>
            function trans(){
                var content = document.getElementById("content");
                //去除注释
                content = removeComment(content.value);
                //去除换行
                content = removeLine(content);
                content = removeSpace(content);    
                //转换成css数组
                var cssList = cssConvert(content);
                for(var i=0;i<cssList.length;i++){
                    alert(cssList[i].attrName + " " +cssList[i].attrValue+ " " +cssList[i].cssNum);
                }
                
                document.getElementById("result").value = content;
            }
            //转换成css数组
            
            function cssConvert(txt){
                var cssList = [];
                var isOpen = false;
                var attrName="";
                var attrValue = "";
                var cssNum = 1;
                for(var i=0;i<txt.length;i++){
                    if(txt[i] == '{') {
                        isOpen = true;
                        continue;
                    }
                    if(txt[i] == '}') {
                        isOpen = false;
                        cssList.push({attrName:attrName,attrValue:attrValue,cssNum:cssNum});
                        cssNum ++;
                        attrValue = "";
                        attrValue = "";
                        continue;
                    }
                    if(isOpen){
                        attrValue += txt[i];
                    } else {
                        attrName += txt[i];
                    }    
                }
                return cssList;
            }
            //去除换行
            function removeLine(txt){
                txt = txt.replace(/<\/?.+?>/g,"");
                txt = txt.replace(/[\r\n]/g, "");
                return txt;
            }
            function removeSpace(txt){
                var isOpen =false;
                for(var i=0;i<txt.length;i++){
                    if(txt[i]=='{'){
                        isOpen = true;
                        if(i-1>=0&&txt[i-1] == ' ') {
                            txt = removeAt(txt,i-1);
                            i--;
                        }
                    }
                    if(txt[i]=='}') isOpen = false;
                    //符合条件的去空格
                    if((i-1>=0&&i+1<txt.length&&txt[i]==' '&&isOpen ==true&&(txt[i-1]==':'||txt[i+1]==':'||txt[i-1]==' '||txt[i+1]==' '||txt[i-1]==';'||txt[i+1]==';'||txt[i-1]=='{'||txt[i-1]=='}'))||(i-1>=0&&txt[i]==' '&&txt[i-1]==' ')||(i-1>=0&&txt[i]==' '&&txt[i-1]=='}')){
                        txt = removeAt(txt,i);
                        i--;
                    } else if(i-1>=0&&i+1<txt.length&&isOpen ==false&&txt[i]==' '&&(txt[i-1]==','||txt[i+1]==','||txt[i-1]==' '||txt[i+1]==' ') ){
                        txt = removeAt(txt,i);
                        i--;
                    }
                }
                return txt;
                //return txt.replace(/\s+/g, "");//.replace(/\s+/g, "");
            }
            function removeAt(txt,i){
                temp = txt.substr(0,i);
                temp += txt.substring(i+1);
                return temp;
            }
            //去除注释
            function removeComment(txt){
                while(true){
                    var start = txt.indexOf("/*");
                    var end = txt.indexOf("*/");
                    var temp = "";
                    if(start >= 0 && end >= 0){
                        temp = txt.substring(0,start);
                        temp += txt.substring(end+2);
                        txt = temp;
                    } else {
                        return txt;
                    }    
                }
            }
        </script>
    </head>
    <body>
        <textarea id="content"></textarea>
        <input type="button" value="转换" onclick="trans()"/>
        <textarea id="result"></textarea>
    </body>
</html>

百度

http://www.ibm.com/developerworks/cn/web/wa-lo-firefox-ext/index.html的更多相关文章

  1. http://www.ibm.com/developerworks/cn/web/wa-aj-jsonp1/index.html

    http://www.ibm.com/developerworks/cn/web/wa-aj-jsonp1/index.html

  2. http://www.ibm.com/developerworks/cn/java/j-lo-junit-src/

    http://www.ibm.com/developerworks/cn/java/j-lo-junit-src/

  3. http://www.ibm.com/developerworks/cn/java/j-lo-hotswapcls/

    http://www.ibm.com/developerworks/cn/java/j-lo-hotswapcls/

  4. http://www.ibm.com/developerworks/cn/opensource/os-cn-cas/

    http://www.ibm.com/developerworks/cn/opensource/os-cn-cas/

  5. Infrastructure as Code 行为驱动开发指南 https://www.ibm.com/developerworks/cn/devops/d-bbd-guide-iac/index.html

    Infrastructure as Code 行为驱动开发指南 https://www.ibm.com/developerworks/cn/devops/d-bbd-guide-iac/index.h ...

  6. 使用 CAS 在 Tomcat 中实现单点登录 http://www.ibm.com/developerworks/cn/opensource/os-cn-cas/

    developerWorks 中国 技术主题 Open source 文档库 使用 CAS 在 Tomcat 中实现单点登录 单点登录(Single Sign On , 简称 SSO )是目前比较流行 ...

  7. Java Content Repository API 简介 转自(https://www.ibm.com/developerworks/cn/java/j-jcr/)

    Java Content Repository API 简介 1 如果曾经试过开发内容管理应用程序,那么您应当非常清楚在实现内容系统时所遇到的固有难题.这个领地有点支离破碎,许多供应商都有自己的私有仓 ...

  8. robotframework安装和配置【转IBM:https://www.ibm.com/developerworks/cn/opensource/os-cn-robot-framework/index.html】

    内容   概览 Robot Framework 介绍 Robot Framework 的安装和配置 RIDE 编辑器介绍 创建测试项目 简单的测试用例的编写 总结 相关主题 评论   Robot Fr ...

  9. CDH5X 安装oozie报错To enable Oozie web console install the Ext JS library.

    最近在CDH5.X 安装oozie 服务,服务安装完毕,访问oozie server ui,报如下错误: 页面提示: Oozie web console is disabled.To enable O ...

随机推荐

  1. Python手记

    字符串的拼接 1.“+”,如果是字符和数字相连,要使用str()函数对于数字进行字符转化: 2.join() 3.",",链接的两个字符串之间用空格做关联 4.占位符 tmp += ...

  2. C++多态的实现条件

    #include <iostream> class Person{ public: virtual void say(){ std::cout<<"person&qu ...

  3. #if _MSC_VER > 1000 #pragma once #endif

    #if _MSC_VER > 1000 #pragma once #endif 解释: 这是微软的预编译控制. 在_MSC_VER较小时,它对一些东西的支持与新版不同 _MSC_VER分解如下: ...

  4. Redux API之Store

    Store Store 就是用来维持应用所有的 state 树 的一个对象. 改变 store 内 state 的惟一途径是对它 dispatch 一个action. Store 不是类.它只是有几个 ...

  5. [hiho第92周]Miller-Rabin素性测试的c++实现

    证明: 如果n是素数,整数$a$ 与$n$ 互素,即$n$ 不整除$a$ ,则${a^{n - 1}} \equiv 1(\bmod n)$ ,如果能找到一个与$n$ 互素的整数$a$ ,是的上式不成 ...

  6. 使用VSTO写的一个工作证打印软件

    转行做HR近2年.最近公司要做工牌,工牌上要打印照片,姓名,工号和部门等信息.一共1000多人,如果手工排版手工打印的话,估计要死人. 于是coding的老毛病又犯了,想写个程序来打印工牌.还是拿最近 ...

  7. HDU 2112 HDU Today Dij+map转化

    HDU Today 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并 ...

  8. Solr highlight

    hl.preserveMulti 默认是false.Set to true to perform highlighting on all values of a multivalued field a ...

  9. hdu2612(dijkstra)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 题意:给出一个n*m的矩阵,' . ' 表示可以走的路, ' # '表示不能走的路 ,’ @'表 ...

  10. 「BZOJ2127」happiness(最小割)

    题目描述 高一一班的座位表是个n*m的矩阵,经过一个学期的相处,每个同学和前后左右相邻的同学互相成为了好朋友.这学期要分文理科了,每个同学对于选择文科与理科有着自己的喜悦值,而一对好朋友如果能同时选文 ...