You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * Example csscss.js * 60 line code base on mcss we can have a more powerful csscss * @type {[type]} */varmcss=require('mcss');varpath=require('path');varnode=mcss.node;// 改变颜色帮助识别console.logvarcolor=mcss.helper.color;varrulesets=[],// 允许的delaration最大重复率MAX_DUPLICATES=6,// 自定义walker,在这里我们只对rulset感兴趣walkers={'ruleset': function(ast){// 获取ruleset的块中的declaration列表varlist=ast.block.list,selector=node.toStr(ast.selector),res={selector:selector,filename:ast.filename,lineno: ast.lineno,map:{}},sign,map=res.map;list.forEach(function(declaration){if(declaration.type==='declaration'){sign=node.toStr(declaration.property)+':'+mcss.node.toStr(declaration.value);if(!map[sign])map[sign]=true;}});rulesets.push(res);}}// use a map, achieve O(n) to find duplicates.functionfindDupls(a,b){varalist=a.map,blist=b.map,duplicates=[];for(variinblist){if(alist[i])duplicates.push(i)}returnduplicates;}// mcss 实例创建varinstance=mcss({// 这个css 来自 github.com 官网 的css ... 重复率惊人filename: path.join(__dirname,'../data/csscss_1.css'),// 默认是不引入 css file的importCSS: true,// 传入我们的自定义walkerwalkers: [walkers]})// 所有mcss操作(解释、翻译、词法)都由统一的instance开始// 以下是解释instance.interpret().done(function(ast){varlen=rulesets.length,mapa,mapb,jlen,duplicates;for(;len--;){jlen=len;mapa=rulesets[len];for(;jlen--;){mapb=rulesets[jlen];duplicates=findDupls(mapa,mapb);if(duplicates.length>MAX_DUPLICATES){console.log(color(mapa.selector,'red')+' at ('+color(mapa.filename+':'+mapa.lineno,'yellow')+') and \n'+color(mapb.selector,'red')+' at ('+color(mapb.filename+':'+mapb.lineno,'yellow')+') has '+color(duplicates.length,'blue')+' duplicates:\n\t'+duplicates.join(',\n\t'))}}}}).fail(function(err){throwerr;})
The text was updated successfully, but these errors were encountered:
作为parser使用的mcss 可以在80l 代码内 实现 csscss (一个检测冗余css的工具) 的功能,并且更加强大,可以检测多个文件内的重复 并且得到准确的位置信息 和 css 样式生命信息,如图所示
代码如下test/fixtures/csscss.js中可察看
The text was updated successfully, but these errors were encountered: