UNPKG

865 BJavaScriptView Raw
1
2var through = require('through2');
3
4// object mode transform stream takes tokenized css and yields complete,
5// parseable rules or at-rules as strings.
6module.exports = function match() {
7 var current = null, depth = 0;
8 function write(token, enc, next) {
9 var type = token[0], buf = token[1];
10 if(depth === 0 && current) {
11 this.push({content: Buffer.concat(current).toString()});
12 current = null;
13 return next();
14 }
15 if(('rule_start' === type || 'atrule_start' === type))
16 depth++;
17 if(depth > 0 && !current)
18 current = [];
19 if('rule_end' === type || 'atrule_end' === type)
20 depth--;
21 if(current) current.push(buf);
22
23 next();
24 }
25
26 function end(next) {
27 if(current) this.push({content: Buffer.concat(current).toString()});
28 this.push(null);
29 next();
30 }
31 return through.obj(write, end);
32}