forked from thomaspeklak/hyperspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.js
265 lines (227 loc) · 7.73 KB
/
browser.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
var through = require('through2');
var hyperglue = require('hyperglue');
var domify = require('domify');
var keyOf = require('./lib/key_of.js');
var json = require('jsonify');
module.exports = function (html, opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
if (!opts) opts = {};
var keyName = opts.key === true ? 'key' : opts.key;
var kattr = opts.attr === undefined ? (opts.key && 'key') : opts.attr;
var kof = opts.key === true
? function () { return true }
: keyOf(keyName)
;
var elements = {};
var className = classNameOf(html);
var tr = through.obj(write, end);
function write (line, enc, next) {
var row;
if (Buffer.isBuffer(line)) {
line = line.toString('utf8');
}
if (typeof line === 'string') {
try { row = json.parse(line) }
catch (err) { this.emit('error', err) }
}
else row = line;
if (opts.key && row && row.type === 'del') {
var k = kof(row);
if (k && elements[k]) {
this.emit('delete', elements[k]);
return next();
}
}
var res = cb.call(this, row);
if (!res) return;
var keys = objectKeys(res);
var streams = [];
for (var i = 0; i < keys.length; i++) (function (key) {
var x = res[key];
if (isStream(x)) {
delete res[key];
streams.push([ key, x ]);
}
else if (x && typeof x === 'object' && isStream(x._html)) {
var st = x._html;
delete x._html;
streams.push([ key, st ]);
}
else if (x && typeof x === 'object' && isStream(x._text)) {
var st = x._text;
delete x._text;
streams.push([ key, st ]);
}
})(keys[i]);
var type, elem;
var k = kof && kof(row);
if (k && elements[k]) {
elem = hyperglue(elements[k], res);
type = 'update';
}
else {
elem = hyperglue(html, res);
type = 'element';
}
if (k) elements[k] = elem;
if (k && kattr && row[keyName]) {
elem.setAttribute(kattr, row[keyName]);
}
for (var i = 0; i < streams.length; i++) (function (ks) {
var key = ks[0], stream = ks[1];
tr.emit('stream', stream, elem);
var cur = elem.querySelector(key);
if (!cur) return;
stream.on('element', function (elem) {
cur.appendChild(elem);
stream.removeListener('data', ondata);
});
stream.on('data', ondata);
function ondata (e) { cur.innerHTML += e }
})(streams[i]);
this.emit(type, elem);
if (opts.key !== true) this.push(elem.outerHTML);
next();
}
function end () {
if (opts.key === true) {
this.push(elements[true].outerHTML);
}
this.push(null);
}
tr.prependTo = function (t) {
var target = getTarget(t);
tr.on('element', function (elem) {
target.insertBefore(elem, target.childNodes[0]);
});
tr.on('delete', function (elem) {
if (hasChild(target, elem)) target.removeChild(elem);
});
return tr;
};
tr.appendTo = function (t) {
var target = getTarget(t);
tr.on('element', function (elem) {
target.appendChild(elem);
});
tr.on('delete', function (elem) {
if (hasChild(target, elem)) target.removeChild(elem);
});
return tr;
};
tr.sortTo = function (t, cmp) {
if (opts.key && cmp === undefined) {
cmp = function (a, b) {
var ka = a.getAttribute(opts.key);
var kb = b.getAttribute(opts.key);
return ka < kb ? -1 : 1;
};
}
else if (typeof cmp === 'string') {
cmp = (function (str) {
var flip = /^~/.test(str);
if (flip) str = str.replace(/^~/, '');
var n = flip ? 1 : -1;
return function (a, b) {
var qa = a.querySelector(str);
var xa = qa && qa.textContent || qa.innerText;
var qb = b.querySelector(str);
var xb = qb && qb.textContent || qb.innerText;
if (isNum(xa) && isNum(xb)) {
return Number(xa) < Number(xb) ? -n : n;
}
else return xa < xb ? -n : n;
};
})(cmp);
}
if (typeof cmp !== 'function') {
throw new Error('comparison function not provided');
}
var target = getElem(t);
var sorted = [].slice.call(target.getElementsByClassName(className));
sorted.sort(cmp);
for (var i = 0; i < sorted.length; i++) {
if (target.childNodes[i] === sorted[i]) continue;
target.removeChild(sorted[i]);
target.insertBefore(sorted[i], target.childNodes[i]);
}
tr.on('element', onupdate);
tr.on('update', function (elem) {
if (hasChild(target, elem)) {
target.removeChild(elem);
}
onupdate(elem);
});
tr.on('delete', function (elem) {
if (hasChild(target, elem)) target.removeChild(elem);
});
getTarget(t, target);
return tr;
function onupdate (elem) {
var nodes = target.getElementsByClassName(className);
for (var i = 0; i < nodes.length; i++) {
if (nodes[i] === elem) continue;
var n = cmp(elem, nodes[i]);
if (n < 0) {
if (hasChild(target, elem)) {
target.removeChild(elem);
}
target.insertBefore(elem, nodes[i]);
return;
}
}
target.appendChild(elem);
}
};
var emittedElements = false;
tr.className = className;
return tr;
function getTarget (t, target) {
if (!target) target = getElem(t);
tr.emit('parent', target);
if (!className) return target;
if (emittedElements) return target;
emittedElements = true;
var elems = target.querySelectorAll('.' + className);
process.nextTick(function(){
for (var i = 0; i < elems.length; i++) {
var elem = elems[i];
var key = opts.key && elem.getAttribute(opts.key);
if (key) elements[key] = elem;
tr.emit('element', elem);
}
});
return target;
}
};
function classNameOf (html) {
var elems = domify(html);
if (elems.length) return elems[0].getAttribute('class');
}
function hasChild (node, child) {
var nodes = node.childNodes;
for (var i = 0; i < nodes.length; i++) {
if (nodes[i] === child) return true;
}
return false;
}
function getElem (target) {
if (typeof target === 'string') {
return document.querySelector(target);
}
return target;
}
function isStream (x) {
return x && typeof x.pipe === 'function';
}
var objectKeys = Object.keys || function (obj) {
var keys = [];
for (var key in obj) keys.push(key);
return keys;
};
function isNum (s) {
return /^\s*(\d*\.\d+|\d+\.?)(e-?\d+)?\s*$/.test(s);
}