total5 0.0.1-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/503.html +65 -0
- package/CONTRIBUTING.md +55 -0
- package/LICENSE +211 -0
- package/README.md +32 -0
- package/api.js +289 -0
- package/bin/total5 +984 -0
- package/builders.js +1435 -0
- package/bundles.js +457 -0
- package/cache.js +58 -0
- package/changelog.txt +3 -0
- package/cluster.js +320 -0
- package/cms.js +625 -0
- package/controller.js +1419 -0
- package/cron.js +99 -0
- package/debug.js +539 -0
- package/edit.js +469 -0
- package/error.html +49 -0
- package/filestorage.js +1088 -0
- package/flow-flowstream.js +3152 -0
- package/flow.js +209 -0
- package/flowstream.js +1991 -0
- package/global.js +125 -0
- package/helpers/index.js +32 -0
- package/htmlparser.js +650 -0
- package/http.js +81 -0
- package/image.js +773 -0
- package/images.js +747 -0
- package/index.js +2658 -0
- package/jsonschema.js +691 -0
- package/ldap.js +792 -0
- package/mail.js +936 -0
- package/minificators.js +858 -0
- package/nosql-builder.js +440 -0
- package/nosql-querybuilder.js +316 -0
- package/nosql-reader.js +353 -0
- package/nosql-stream.js +617 -0
- package/nosql.js +763 -0
- package/openclient.js +219 -0
- package/package.json +32 -0
- package/pause.html +67 -0
- package/querybuilder.js +1361 -0
- package/release.js +167 -0
- package/routing.js +905 -0
- package/sourcemap.js +160 -0
- package/tangular.js +409 -0
- package/templates.js +145 -0
- package/templates.json +74 -0
- package/tms.js +384 -0
- package/tmsclient.js +125 -0
- package/todo.txt +7 -0
- package/tools/beta.sh +6 -0
- package/tools/release.sh +6 -0
- package/uibuilder.js +206 -0
- package/utils.js +6374 -0
- package/viewengine.js +880 -0
- package/websocket.js +1939 -0
- package/workers.js +129 -0
package/htmlparser.js
ADDED
|
@@ -0,0 +1,650 @@
|
|
|
1
|
+
// Total.js HTML Parser
|
|
2
|
+
// The MIT License
|
|
3
|
+
// Copyright 2023 (c) Peter Širka <petersirka@gmail.com>
|
|
4
|
+
|
|
5
|
+
'use strict';
|
|
6
|
+
|
|
7
|
+
function HTMLElement() {
|
|
8
|
+
this.children = [];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
HTMLElement.prototype = {
|
|
12
|
+
get innerHTML() {
|
|
13
|
+
return this.toString();
|
|
14
|
+
},
|
|
15
|
+
get innerText() {
|
|
16
|
+
return this.toString().removeTags();
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
function parseRule(selector, output) {
|
|
21
|
+
|
|
22
|
+
var rule = {};
|
|
23
|
+
|
|
24
|
+
rule.attrs = [];
|
|
25
|
+
rule.output = output || [];
|
|
26
|
+
|
|
27
|
+
// div div[name="Peter Sirka"]
|
|
28
|
+
// div > div[name="Peter Sirka"]
|
|
29
|
+
|
|
30
|
+
// for (var c of selector) {
|
|
31
|
+
// if (c === '>')
|
|
32
|
+
// console.log(c);
|
|
33
|
+
// }
|
|
34
|
+
|
|
35
|
+
var cache = [];
|
|
36
|
+
|
|
37
|
+
selector = selector.replace(/\[.*?\]/gi, text => '[' + (cache.push(text) - 1) + ']').replace(/(\s)?>(\s)?/, '>').replace(/\s{2}/g, '');
|
|
38
|
+
|
|
39
|
+
var m = selector.match(/>|\s/);
|
|
40
|
+
if (m) {
|
|
41
|
+
var nested = selector.substring(m.index + 1).trim().replace(/\[\d+\]/g, text => cache[+text.substring(1, text.length - 1)]);
|
|
42
|
+
rule.nested = parseRule(nested, rule.output);
|
|
43
|
+
rule.direct = m[0] === '>';
|
|
44
|
+
selector = selector.substring(0, m.index).trim();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
selector = selector.replace(/\[\d+\]/, text => cache[+text.substring(1, text.length - 1)]);
|
|
48
|
+
|
|
49
|
+
var match = selector.match(/[#|.][a-z-_0-9]+/i);
|
|
50
|
+
if (match) {
|
|
51
|
+
for (var m of match) {
|
|
52
|
+
var val = m.substring(1);
|
|
53
|
+
rule.attrs.push({ id: m[0] === '#' ? 'id' : 'class', value: val });
|
|
54
|
+
}
|
|
55
|
+
selector = selector.replace(match, '');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
match = selector.match(/\[.*?\]/i);
|
|
59
|
+
if (match) {
|
|
60
|
+
for (var m of match) {
|
|
61
|
+
var index = m.indexOf('=');
|
|
62
|
+
rule.attrs.push({ id: m.substring(1, index).trim(), value: m.substring(index + 2, m.length - 2).trim() });
|
|
63
|
+
}
|
|
64
|
+
selector = selector.replace(match, '');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
selector = selector.trim();
|
|
68
|
+
rule.tagName = selector[0] === '*' ? '' : selector.toUpperCase();
|
|
69
|
+
|
|
70
|
+
return rule;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
HTMLElement.prototype.browse = function(fn, reverse) {
|
|
74
|
+
|
|
75
|
+
var self = this;
|
|
76
|
+
|
|
77
|
+
var browse = function(children) {
|
|
78
|
+
for (var node of children) {
|
|
79
|
+
if (node && node.tagName) {
|
|
80
|
+
var a = fn(node);
|
|
81
|
+
if (a !== true)
|
|
82
|
+
browse(reverse ? [node.parentNode] : node.children);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
if (reverse && !self.parentNode)
|
|
88
|
+
return;
|
|
89
|
+
|
|
90
|
+
browse(reverse ? [self.parentNode] : self.children);
|
|
91
|
+
return self;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
function extendarr(output) {
|
|
95
|
+
|
|
96
|
+
output.aclass = function(cls) {
|
|
97
|
+
for (var item of this)
|
|
98
|
+
item.aclass(cls);
|
|
99
|
+
return this;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
output.rclass = function(cls) {
|
|
103
|
+
for (var item of this)
|
|
104
|
+
item.rclass(cls);
|
|
105
|
+
return this;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
output.tclass = function(cls, value) {
|
|
109
|
+
for (var item of this)
|
|
110
|
+
item.tclass(cls, value);
|
|
111
|
+
return this;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
output.attr = function(key, value) {
|
|
115
|
+
for (var item of this)
|
|
116
|
+
item.attr(key, value);
|
|
117
|
+
return this;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
output.attrd = function(key, value) {
|
|
121
|
+
for (var item of this)
|
|
122
|
+
item.attrd(key, value);
|
|
123
|
+
return this;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
output.find = function(selector) {
|
|
127
|
+
var arr = [];
|
|
128
|
+
for (var item of this) {
|
|
129
|
+
var result = item.find(selector);
|
|
130
|
+
if (result.length)
|
|
131
|
+
arr.push(result);
|
|
132
|
+
}
|
|
133
|
+
extendarr(arr);
|
|
134
|
+
return this;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
return output;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
HTMLElement.prototype.find = function(selector, reverse) {
|
|
141
|
+
|
|
142
|
+
var self = this;
|
|
143
|
+
var selectors = selector.split(',');
|
|
144
|
+
var rules = [];
|
|
145
|
+
var output = [];
|
|
146
|
+
|
|
147
|
+
for (var sel of selectors)
|
|
148
|
+
rules.push(parseRule(sel.trim()));
|
|
149
|
+
|
|
150
|
+
var browse = function(rule, children, parent) {
|
|
151
|
+
|
|
152
|
+
for (var node of children) {
|
|
153
|
+
|
|
154
|
+
if (!node.tagName)
|
|
155
|
+
continue;
|
|
156
|
+
|
|
157
|
+
var skip = false;
|
|
158
|
+
|
|
159
|
+
if (rule.tagName && rule.tagName !== node.tagName)
|
|
160
|
+
skip = true;
|
|
161
|
+
|
|
162
|
+
if (rule.attrs.length && !skip) {
|
|
163
|
+
|
|
164
|
+
for (var attr of rule.attrs) {
|
|
165
|
+
|
|
166
|
+
switch (attr.id) {
|
|
167
|
+
|
|
168
|
+
case 'class':
|
|
169
|
+
var tmp = node.attrs[attr.id];
|
|
170
|
+
if (tmp) {
|
|
171
|
+
tmp = tmp.split(' ');
|
|
172
|
+
if (!tmp.includes(attr.value))
|
|
173
|
+
skip = true;
|
|
174
|
+
} else
|
|
175
|
+
skip = true;
|
|
176
|
+
break;
|
|
177
|
+
|
|
178
|
+
default:
|
|
179
|
+
if (attr.value) {
|
|
180
|
+
if (node.attrs[attr.id] !== attr.value)
|
|
181
|
+
skip = true;
|
|
182
|
+
} else if (node.attrs[attr.id] === undefined)
|
|
183
|
+
skip = true;
|
|
184
|
+
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (skip)
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
var next = ((reverse && node.parentNode) || (!reverse && node.children.length));
|
|
194
|
+
|
|
195
|
+
if (parent) {
|
|
196
|
+
if (skip && parent.direct)
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (rule.nested) {
|
|
201
|
+
|
|
202
|
+
if (!skip && next)
|
|
203
|
+
browse(rule.nested, reverse ? [node.parentNode] : node.children, rule);
|
|
204
|
+
|
|
205
|
+
// Again same
|
|
206
|
+
if (!parent)
|
|
207
|
+
browse(rule, reverse ? [node.parentNode] : node.children);
|
|
208
|
+
|
|
209
|
+
} else {
|
|
210
|
+
if (!skip)
|
|
211
|
+
rule.output.push(node);
|
|
212
|
+
if (next)
|
|
213
|
+
browse(rule, reverse ? [node.parentNode] : node.children);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
if (reverse && !self.parentNode)
|
|
219
|
+
return output;
|
|
220
|
+
|
|
221
|
+
for (var rule of rules) {
|
|
222
|
+
browse(rule, reverse ? [self.parentNode] : self.children);
|
|
223
|
+
if (rule.output.length)
|
|
224
|
+
output.push.apply(output, rule.output);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
extendarr(output);
|
|
228
|
+
return output;
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
HTMLElement.prototype.parent = function() {
|
|
232
|
+
return this.parentNode;
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
HTMLElement.prototype.closest = function(selector) {
|
|
236
|
+
return this.find(selector, true);
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
HTMLElement.prototype.attrd = function(name, value) {
|
|
240
|
+
return this.attr('data-' + name, value);
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
HTMLElement.prototype.parsecache = function() {
|
|
244
|
+
|
|
245
|
+
var self = this;
|
|
246
|
+
if (self.cache)
|
|
247
|
+
return self;
|
|
248
|
+
|
|
249
|
+
self.cache = {};
|
|
250
|
+
self.cache.css = {};
|
|
251
|
+
self.cache.cls = {};
|
|
252
|
+
|
|
253
|
+
var tmp = self.attrs.class;
|
|
254
|
+
var arr;
|
|
255
|
+
|
|
256
|
+
if (tmp) {
|
|
257
|
+
arr = tmp.split(' ');
|
|
258
|
+
for (var c of arr)
|
|
259
|
+
self.cache.cls[c] = 1;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
var tmp = self.attrs.style;
|
|
263
|
+
if (tmp) {
|
|
264
|
+
arr = tmp.split(';');
|
|
265
|
+
for (var c of arr) {
|
|
266
|
+
var a = c.split(':');
|
|
267
|
+
self.cache.css[a[0]] = a[1];
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return self;
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
HTMLElement.prototype.stringifycache = function() {
|
|
275
|
+
var self = this;
|
|
276
|
+
self.attrs.class = Object.keys(self.cache.cls).join(' ');
|
|
277
|
+
|
|
278
|
+
var tmp = [];
|
|
279
|
+
|
|
280
|
+
for (var key in self.cache.css)
|
|
281
|
+
tmp.push(key + ':' + self.cache.css[key]);
|
|
282
|
+
|
|
283
|
+
if (tmp.length)
|
|
284
|
+
self.attrs.style = tmp.join(';');
|
|
285
|
+
else if (self.attrs.style != null)
|
|
286
|
+
delete self.attrs.style;
|
|
287
|
+
|
|
288
|
+
return self;
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
HTMLElement.prototype.attr = function(name, value) {
|
|
292
|
+
|
|
293
|
+
var self = this;
|
|
294
|
+
|
|
295
|
+
if (value === undefined)
|
|
296
|
+
return self.attrs[name];
|
|
297
|
+
|
|
298
|
+
if (value == null || value == '')
|
|
299
|
+
delete self.attrs[name];
|
|
300
|
+
else
|
|
301
|
+
self.attrs[name] = value + '';
|
|
302
|
+
|
|
303
|
+
return self;
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
HTMLElement.prototype.aclass = function(cls) {
|
|
307
|
+
var self = this;
|
|
308
|
+
self.parsecache();
|
|
309
|
+
var arr = cls.split(/\s|,/);
|
|
310
|
+
for (var m of arr)
|
|
311
|
+
self.cache.cls[m] = 1;
|
|
312
|
+
self.stringifycache();
|
|
313
|
+
return self;
|
|
314
|
+
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
HTMLElement.prototype.hclass = function(cls) {
|
|
318
|
+
var self = this;
|
|
319
|
+
self.parsecache();
|
|
320
|
+
return self.cache.cls[cls] === 1;
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
HTMLElement.prototype.tclass = function(cls, value) {
|
|
324
|
+
var self = this;
|
|
325
|
+
self.parsecache();
|
|
326
|
+
var arr = cls.split(/\s|,/);
|
|
327
|
+
for (var m of arr) {
|
|
328
|
+
if (self.cache.cls[m]) {
|
|
329
|
+
if (!value)
|
|
330
|
+
delete self.cache.cls[m];
|
|
331
|
+
} else {
|
|
332
|
+
if (value || value === undefined)
|
|
333
|
+
self.cache.cls[m] = 1;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
self.stringifycache();
|
|
337
|
+
return self;
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
HTMLElement.prototype.rclass = function(cls) {
|
|
341
|
+
var self = this;
|
|
342
|
+
self.parsecache();
|
|
343
|
+
|
|
344
|
+
var arr = cls.split(/\s|,/);
|
|
345
|
+
for (var m of arr)
|
|
346
|
+
delete self.cache.cls[m];
|
|
347
|
+
|
|
348
|
+
self.stringifycache();
|
|
349
|
+
return self;
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
HTMLElement.prototype.css = function(key, value) {
|
|
353
|
+
var self = this;
|
|
354
|
+
self.parsecache();
|
|
355
|
+
if (typeof(key) === 'object') {
|
|
356
|
+
for (var k of key) {
|
|
357
|
+
value = key[k];
|
|
358
|
+
if (value)
|
|
359
|
+
self.cache.css[k] = value;
|
|
360
|
+
else
|
|
361
|
+
delete self.cache.css[k];
|
|
362
|
+
}
|
|
363
|
+
} else {
|
|
364
|
+
if (value)
|
|
365
|
+
self.cache.css[key] = value;
|
|
366
|
+
else
|
|
367
|
+
delete self.cache.css[key];
|
|
368
|
+
}
|
|
369
|
+
self.stringifycache();
|
|
370
|
+
return self;
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
HTMLElement.prototype.remove = function() {
|
|
374
|
+
var self = this;
|
|
375
|
+
if (self.parentNode) {
|
|
376
|
+
var index = self.parentNode.children.indexOf(self);
|
|
377
|
+
if (index !== -1)
|
|
378
|
+
self.parentNode.children.splice(index, 1);
|
|
379
|
+
}
|
|
380
|
+
return self;
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
HTMLElement.prototype.append = function(str) {
|
|
384
|
+
|
|
385
|
+
var self = this;
|
|
386
|
+
var dom = parseHTML(str);
|
|
387
|
+
|
|
388
|
+
for (var item of dom.children)
|
|
389
|
+
self.children.push(item);
|
|
390
|
+
|
|
391
|
+
return dom.children.length === 1 ? dom.children[0] : dom.children;
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
HTMLElement.prototype.prepend = function(str) {
|
|
395
|
+
|
|
396
|
+
var self = this;
|
|
397
|
+
var dom = parseHTML(str);
|
|
398
|
+
|
|
399
|
+
for (var item of dom.children)
|
|
400
|
+
self.children.unshift(item);
|
|
401
|
+
|
|
402
|
+
return dom;
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
HTMLElement.prototype.text = function() {
|
|
406
|
+
return this.html().removeTags();
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
HTMLElement.prototype.toString = HTMLElement.prototype.html = function(formatted) {
|
|
410
|
+
|
|
411
|
+
var self = this;
|
|
412
|
+
var builder = [];
|
|
413
|
+
|
|
414
|
+
var browse = function(children, level) {
|
|
415
|
+
|
|
416
|
+
for (var item of children) {
|
|
417
|
+
|
|
418
|
+
var indent = formatted && level ? ''.padLeft(level, '\t') : '';
|
|
419
|
+
var tag = item.tagName.toLowerCase();
|
|
420
|
+
var attrs = [];
|
|
421
|
+
|
|
422
|
+
for (var key in item.attrs) {
|
|
423
|
+
var val = item.attrs[key];
|
|
424
|
+
if (!val && (key === 'class' || key.substring(0, 5) === 'data-' || key === 'id'))
|
|
425
|
+
continue;
|
|
426
|
+
attrs.push(key + (val ? ('="' + (val || '') + '"') : ''));
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
switch (item.tagName) {
|
|
430
|
+
case 'TEXT':
|
|
431
|
+
if (item.textContent)
|
|
432
|
+
builder.push(indent + item.textContent);
|
|
433
|
+
break;
|
|
434
|
+
default:
|
|
435
|
+
if (item.unpair) {
|
|
436
|
+
builder.push(indent + '<' + tag + (attrs.length ? (' ' + attrs.join(' ')) : '') + ' />');
|
|
437
|
+
} else {
|
|
438
|
+
builder.push(indent + '<' + tag + (attrs.length ? (' ' + attrs.join(' ')) : '') + '>' + (item.children.length ? '' : ('</' + tag + '>')));
|
|
439
|
+
if (item.children.length) {
|
|
440
|
+
browse(item.children, level + 1);
|
|
441
|
+
builder.push(indent + '</' + tag + '>');
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
break;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
|
|
449
|
+
browse(self.tagName ? [self] : self.children, 0);
|
|
450
|
+
return builder.join(formatted ? '\n' : '');
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
function removeComments(html) {
|
|
454
|
+
var tagBeg = '<!--';
|
|
455
|
+
var tagEnd = '-->';
|
|
456
|
+
var beg = html.indexOf(tagBeg);
|
|
457
|
+
var end = 0;
|
|
458
|
+
while (beg !== -1) {
|
|
459
|
+
end = html.indexOf(tagEnd, beg + 4);
|
|
460
|
+
|
|
461
|
+
if (end === -1)
|
|
462
|
+
break;
|
|
463
|
+
|
|
464
|
+
var comment = html.substring(beg, end + 3);
|
|
465
|
+
html = html.replacer(comment, '');
|
|
466
|
+
beg = html.indexOf(tagBeg, beg);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
return html;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function parseHTML(html, trim, onerror) {
|
|
473
|
+
|
|
474
|
+
var makeText = function(parent, str) {
|
|
475
|
+
var obj = new HTMLElement();
|
|
476
|
+
obj.tagName = 'TEXT';
|
|
477
|
+
obj.children = [];
|
|
478
|
+
obj.attrs = {};
|
|
479
|
+
obj.textContent = str;
|
|
480
|
+
obj.parentNode = parent;
|
|
481
|
+
return obj;
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
var parseAttrs = function(str) {
|
|
485
|
+
var attrs = str.match(/[a-z-0-9]+(=("|').*?("|'))?/g);
|
|
486
|
+
var obj = {};
|
|
487
|
+
if (attrs) {
|
|
488
|
+
for (var m of attrs) {
|
|
489
|
+
m = m.trim();
|
|
490
|
+
var index = m.indexOf('=');
|
|
491
|
+
var key, val;
|
|
492
|
+
key = (index === -1 ? m : m.substring(0, index)).trim();
|
|
493
|
+
val = index === -1 ? '' : m.substring(m.indexOf('"', index) + 1, m.lastIndexOf('"')).trim();
|
|
494
|
+
obj[key] = val;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
return obj;
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
var parseElements = function(str, parent) {
|
|
501
|
+
|
|
502
|
+
var counter = 0;
|
|
503
|
+
var count = 0;
|
|
504
|
+
var beg = str.indexOf('<');
|
|
505
|
+
var end = -1;
|
|
506
|
+
var tmp;
|
|
507
|
+
|
|
508
|
+
if (beg !== -1)
|
|
509
|
+
end = str.indexOf('>', beg + 1);
|
|
510
|
+
|
|
511
|
+
if (beg === -1 || end === -1) {
|
|
512
|
+
if (parent) {
|
|
513
|
+
tmp = str;
|
|
514
|
+
if (trim)
|
|
515
|
+
tmp = tmp.trim();
|
|
516
|
+
tmp && parent.children.push(makeText(parent, tmp));
|
|
517
|
+
}
|
|
518
|
+
return '';
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
if (beg > 0) {
|
|
523
|
+
tmp = str.substring(0, beg);
|
|
524
|
+
|
|
525
|
+
if (trim)
|
|
526
|
+
tmp = tmp.trim();
|
|
527
|
+
|
|
528
|
+
if (tmp)
|
|
529
|
+
parent.children.push(makeText(parent, tmp));
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
var node = str.substring(beg + 1, end);
|
|
533
|
+
var dom = new HTMLElement();
|
|
534
|
+
|
|
535
|
+
// Doctype?
|
|
536
|
+
if (node[0] === '!')
|
|
537
|
+
return str.substring(end + 1);
|
|
538
|
+
|
|
539
|
+
if (node[node.length - 1] === '/') {
|
|
540
|
+
node = node.substring(0, node.length - 1);
|
|
541
|
+
dom.unpair = true;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
var tag = node;
|
|
545
|
+
var index = tag.indexOf(' ');
|
|
546
|
+
|
|
547
|
+
if (index > 0) {
|
|
548
|
+
tag = tag.substring(0, index);
|
|
549
|
+
node = node.substring(index + 1);
|
|
550
|
+
} else
|
|
551
|
+
node = '';
|
|
552
|
+
|
|
553
|
+
if (tag.indexOf('/') !== -1) {
|
|
554
|
+
onerror && onerror(tag);
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
dom.tagName = tag.toUpperCase();
|
|
559
|
+
dom.children = [];
|
|
560
|
+
dom.attrs = node ? parseAttrs(node) : {};
|
|
561
|
+
dom.raw = tag;
|
|
562
|
+
dom.parentNode = parent;
|
|
563
|
+
|
|
564
|
+
parent.children.push(dom);
|
|
565
|
+
str = str.substring(end + 1);
|
|
566
|
+
|
|
567
|
+
// Unpair tags
|
|
568
|
+
switch (dom.tagName) {
|
|
569
|
+
case 'BR':
|
|
570
|
+
case 'HR':
|
|
571
|
+
case 'IMG':
|
|
572
|
+
case 'META':
|
|
573
|
+
case 'LINK':
|
|
574
|
+
case 'INPUT':
|
|
575
|
+
dom.unpair = true;
|
|
576
|
+
return str;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
var pos = 0;
|
|
580
|
+
var tagBeg = '<' + dom.raw;
|
|
581
|
+
var tagEnd = '</' + dom.raw + '>';
|
|
582
|
+
|
|
583
|
+
while (true) {
|
|
584
|
+
|
|
585
|
+
if (counter++ > 10000)
|
|
586
|
+
break;
|
|
587
|
+
|
|
588
|
+
beg = str.indexOf(tagBeg, pos);
|
|
589
|
+
end = str.indexOf(tagEnd, pos);
|
|
590
|
+
|
|
591
|
+
// Fallback for the non-exists end tag
|
|
592
|
+
if (end === -1) {
|
|
593
|
+
end = str.length;
|
|
594
|
+
pos = end;
|
|
595
|
+
break;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
if (beg !== -1 && beg < end) {
|
|
599
|
+
count++;
|
|
600
|
+
pos = str.indexOf('>', beg);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
if (beg === -1 || end < beg) {
|
|
604
|
+
|
|
605
|
+
pos = end + tagEnd.length;
|
|
606
|
+
|
|
607
|
+
if (count) {
|
|
608
|
+
count--;
|
|
609
|
+
continue;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
break;
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
var inner = str.substring(0, pos - tagEnd.length);
|
|
617
|
+
|
|
618
|
+
if (inner.indexOf('<') === -1 || (/script|style|template/).test(tag)) {
|
|
619
|
+
if (trim)
|
|
620
|
+
inner = inner.trim();
|
|
621
|
+
if (inner)
|
|
622
|
+
dom.children.push(makeText(dom, inner));
|
|
623
|
+
} else {
|
|
624
|
+
while (inner)
|
|
625
|
+
inner = parseElements(inner, dom);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
str = str.substring(end + tagEnd.length, str.length);
|
|
629
|
+
|
|
630
|
+
if (str && str.indexOf('<') === -1) {
|
|
631
|
+
if (trim)
|
|
632
|
+
str = str.trim();
|
|
633
|
+
if (str)
|
|
634
|
+
parent.children.push(makeText(parent, str));
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
return str;
|
|
638
|
+
};
|
|
639
|
+
|
|
640
|
+
html = removeComments(html);
|
|
641
|
+
|
|
642
|
+
var dom = new HTMLElement();
|
|
643
|
+
|
|
644
|
+
while (html)
|
|
645
|
+
html = parseElements(html, dom);
|
|
646
|
+
|
|
647
|
+
return dom;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
exports.parseHTML = parseHTML;
|
package/http.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// Total.js HTTP Server handler
|
|
2
|
+
// The MIT License
|
|
3
|
+
// Copyright 2023 (c) Peter Širka <petersirka@gmail.com>
|
|
4
|
+
|
|
5
|
+
'use strict';
|
|
6
|
+
|
|
7
|
+
const TController = require('./controller');
|
|
8
|
+
|
|
9
|
+
exports.listen = function(req, res) {
|
|
10
|
+
|
|
11
|
+
// req.ip
|
|
12
|
+
// req.headers
|
|
13
|
+
// req.url
|
|
14
|
+
// req.method
|
|
15
|
+
// req.on('data', function(chunk))
|
|
16
|
+
// req.destroy()
|
|
17
|
+
|
|
18
|
+
// res.writeHead(status, headers)
|
|
19
|
+
// res.pipe();
|
|
20
|
+
// res.write();
|
|
21
|
+
// res.end()
|
|
22
|
+
|
|
23
|
+
// Not supported
|
|
24
|
+
if (req.method === 'HEAD') {
|
|
25
|
+
F.stats.request.blocked++;
|
|
26
|
+
req.destroy();
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
var ctrl = new TController.Controller(req, res);
|
|
31
|
+
|
|
32
|
+
if (F.paused.length) {
|
|
33
|
+
ctrl.fallback(999);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (F.config.$blacklist && F.config.$blacklist.indexOf(ctrl.ip) !== -1) {
|
|
38
|
+
F.stats.request.blocked++;
|
|
39
|
+
ctrl.fallback(400, 'IP address is blocked');
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (F.config.$httpreqlimit) {
|
|
44
|
+
if (F.temporary.ddos[ctrl.ip] > F.config.$httpreqlimit) {
|
|
45
|
+
F.stats.response.ddos++;
|
|
46
|
+
ctrl.fallback(503);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (F.temporary.ddos[ctrl.ip])
|
|
50
|
+
F.temporary.ddos[ctrl.ip]++;
|
|
51
|
+
else
|
|
52
|
+
F.temporary.ddos[ctrl.ip] = 1;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (F.routes.proxies.length && F.TRouting.lookupproxy(ctrl))
|
|
56
|
+
return;
|
|
57
|
+
|
|
58
|
+
if (F.routes.virtual[ctrl.url]) {
|
|
59
|
+
F.routes.virtual[ctrl.url](ctrl);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Pending requests
|
|
64
|
+
F.temporary.pending.push(ctrl);
|
|
65
|
+
|
|
66
|
+
if (!ctrl.uri.file && (F.def.onCORS || F.config.$cors)) {
|
|
67
|
+
if (F.TRouting.lookupcors(ctrl))
|
|
68
|
+
ctrl.$route();
|
|
69
|
+
} else
|
|
70
|
+
ctrl.$route();
|
|
71
|
+
|
|
72
|
+
// stream.headers
|
|
73
|
+
// stream.url
|
|
74
|
+
|
|
75
|
+
// respond(opt);
|
|
76
|
+
// opt.status = 200;
|
|
77
|
+
// opt.headers = {};
|
|
78
|
+
// opt.stream = '';
|
|
79
|
+
// opt.end = '';
|
|
80
|
+
|
|
81
|
+
};
|