-
Notifications
You must be signed in to change notification settings - Fork 1
/
outer.js
81 lines (74 loc) · 1.96 KB
/
outer.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
module.exports = Outer
var Deque = require('double-ended-queue')
var util = require('util')
var cssauron = require('./cssauron')
var Rheo = require('./rheo')
var Document = require('./document')
util.inherits(Outer, Rheo)
function Outer (selector, callback) {
Rheo.call(this, {objectMode: true})
this.callback = callback
this.state = BEFORE
this.check = cssauron(selector)
this.before = new Deque()
this.after = new Deque()
this.through = new Deque()
this.open = null
this.document = new Document()
}
var BEFORE = {}
var THROUGH = {}
var AFTER = {}
Outer.prototype._transform = function (queue, enc, cb) {
var obj = queue.dequeue()
while (obj !== undefined) {
switch (this.state) {
case BEFORE:
if (obj.type === 'open') {
if (this.check(obj)) {
this.open = obj
this.parent = obj.parent
this.document.add_child(obj)
this.through.enqueue(obj)
this.state = THROUGH
} else {
this.before.enqueue(obj)
}
} else {
this.before.enqueue(obj)
}
break
case THROUGH:
if (obj.type === 'close' && obj.open === this.open) this.state = AFTER
this.through.enqueue(obj)
break
case AFTER:
this.after.enqueue(obj)
break
}
obj = queue.dequeue()
}
cb()
}
Outer.prototype._flush = function (cb) {
var accum = new Deque()
var self = this
var rheo = new Rheo({objectMode: true})
var callback = this.callback
rheo.end(this.through)
var ret = callback(rheo)
this.push(this.before)
ret.on('data', function (queue) {
accum.enqueue.apply(accum, queue.toArray())
})
ret.once('end', function (queue) {
if (queue) accum.enqueue.apply(accum, queue.toArray())
if (!accum.isEmpty()) {
var document = accum.peekFront().parent
document.move_children(self.parent)
}
accum.enqueue.apply(accum, self.after.toArray())
self.push(accum)
cb()
})
}