/
select.js
234 lines (202 loc) · 7.03 KB
/
select.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
var Stream = require('stream').Stream;
var async = require('async');
var createNode = require('./node');
module.exports = function (parser, opts) {
var stream = new Stream;
stream.writable = true;
stream.readable = true;
var selectors = [];
stream.select = function (s, fn) {
var sel = createSelector(s, fn, opts.special);
selectors.push(sel);
return stream;
};
stream.update = function (s, fn) {
return stream.select(s, function (node) {
if (typeof fn === 'function') {
node.update(function (html) { return fn(html, node); });
}
else node.update(fn);
});
};
stream.remove = function (s, fn) {
return stream.select(s, function (node) {
node.remove();
if (typeof fn === 'function') fn(node);
});
};
stream.replace = function (s, fn) {
return stream.select(s, function (node) {
if (typeof fn === 'function') {
node.replace(function (html) { return fn(html, node); });
}
else node.replace(fn);
});
};
var updating = false;
stream.pre = function (name, t, cb) {
var this_ = this;
function callback() {
if (cb) { cb.apply(this_, arguments); }
}
if (name === 'open') {
selectors.forEach(function (sel) {
sel(t, parser);
if (sel.removing) updating = true;
});
callback();
}
else if (name === 'close') {
var level = parser.tags.reduce(function (sum, t) {
return sum + (opts.special.indexOf(t.name) < 0 ? 1 : 0);
}, 0);
async.forEach(selectors, function(sel, done) {
var up = sel.updating;
async.filter(sel.pending, function(p, next) {
var done = level < p.level;
if (done && sel.removing) {
p.callback.call(stream, p.buffered, function (cb) {
sel.nextRaw.push(cb);
}, function() {
next(! done);
});
}
else {
if (done) p.callback.call(stream, p.buffered, undefined, function() {
next (! done);
});
else next(! done);
}
}, function(pending) {
sel.pending = pending;
if (up && !sel.updating) {
if (sel.removing) {
sel.removeOnPost = true;
}
else updating = false;
}
done();
});
}, callback);
}
};
stream.post = function (name, t) {
if (name === 'open') {
selectors.forEach(function (sel) {
if (sel.updating) {
updating = true;
}
sel.pending.forEach(function (p) {
if (p.writes === 0) {
p.buffered = '';
}
if (typeof p.writes === 'number') p.writes ++;
});
});
for (var i = 0; i < parser.tags.length; i++) {
var t = parser.tags[i];
if (opts.special.indexOf(t.name) >= 0) {
parser.tags.splice(i, 1);
i--;
}
}
}
else if (name == 'close') {
selectors.forEach(function (sel) {
if (sel.removeOnPost) {
updating = false;
sel.removing = false;
sel.removeOnPost = false;
}
});
}
};
stream.raw = function (s, done) {
async.forEach(selectors, function (sel, next) {
async.forEach(sel.nextRaw.splice(0), function(cb, next) {
cb(s, next);
}, function() {
sel.pending.forEach(function (p) {
p.buffered += s;
});
next();
});
}, function() {
if (!updating) stream.emit('data', s);
done();
});
};
return stream;
};
function createSelector (selector, fn, special) {
var saveSiblings = false;
var parts = selector.split(/([\s>+]+)/).map(function (s) {
if (s.match(/^\s+$/)) return;
var op = s.trim();
if (op === '+') saveSiblings = true;
if (op === '>' || op === '+') return { combinator : op };
var m = {
name : s.match(/^([\w-]+|\*)/),
class : s.match(/\.([\w-]+)/),
id : s.match(/#([\w-]+)/),
pseudo : s.match(/:([\w-]+)/),
attribute : s.match(/\[([^\]]+)\]/),
};
return {
name : m.name && m.name[1].toUpperCase(),
class : m.class && m.class[1],
id : m.id && m.id[1],
pseudo : m.pseudo && m.pseudo[1],
attribute : m.attribute && m.attribute[1],
};
}).filter(Boolean);
var depth = parts.reduce(function (sum, s) {
return sum + (s.combinator ? 0 : 1);
}, 0);
var siblings = [];
var sel = function (tag, parser) {
var tags = parser.tags;
if (!siblings[tags.length]) siblings[tags.length] = [];
siblings[tags.length].push(tag);
if (depth > tags.length) return;
// hypothesis: the selector matches
var j = parts.length - 1;
var i = tags.length - 1;
function check (t, p) {
// try to falsify the hypothesis on each tag/part match:
if (p.name !== '*' && p.name && p.name !== t.name) return false;
if (p.class && p.class !== t.attributes.class) return false;
if (p.id && p.id !== t.attributes.id) return false;
return true;
}
for (; j >= 0; j--, i--) {
var t = tags[i];
var p = parts[j];
if (p.combinator === '>') {
for (var ii = i; ii >= 0; ii--) {
if (check(tags[ii], parts[j-1])) {
break;
}
}
if (ii < 0) return;
j -= 2;
}
else if (p.combinator === '+') {
var ts = siblings[i + 2];
t = ts[ts.length - 2];
if (!t) return;
p = parts[j - 1];
if (!check(t, p)) return;
j -= 2;
}
else if (!check(t, p)) return;
}
var node = createNode(tag, sel, tags.length);
fn(node);
node.expired = true;
};
sel.special = special;
sel.pending = [];
sel.nextRaw = [];
return sel;
}