-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
56 lines (50 loc) · 1 KB
/
parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var css = require('css');
function parse(rawCss) {
var ast = css.parse(rawCss, {
silent: true,
});
var selectors = [];
visit(ast, function(node){
if (node.type==='rule') {
var selectorInfos = node.selectors.map(function(sel){
return {
selector: sel,
position: {
line: node.position.start.line,
column: node.position.start.column
},
file: '',
count: 0
}
});
Array.prototype.push.apply(selectors, selectorInfos);
}
});
return selectors;
}
function visit(node, visitor) {
switch(node.type) {
case 'stylesheet':
visitor(node);
for(var rule of node.stylesheet.rules) {
visit(rule, visitor);
}
if (node.parsingErrors && node.parsingErrors.length) {
console.error(node.parsingErrors);
}
break;
case 'rule':
visitor(node);
break;
case 'media':
visitor(node);
for(var rule of node.rules) {
visit(rule, visitor);
}
break;
case 'comment':
default:
break;
}
}
module.exports = parse;