-
Notifications
You must be signed in to change notification settings - Fork 4
/
example.js
63 lines (48 loc) · 1.36 KB
/
example.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
/**
* Module Dependencies
*/
var direction = process.argv[2] || 'next';
var html = '<body>hi<article><em>whatever</em>omg<strong></strong></article>bye</body>';
var parser = require('mini-html-parser');
var dom = parser(html).parse();
var iterator = require('./');
var chalk = require('chalk');
var current = chalk.cyan.underline.bold;
var method = chalk.green.bold;
process.stdout.write('\u001B[2J\u001B[0;0f');
var it = iterator(dom).revisit(false);
!!process.argv[2] ? it.closing() : it.opening();
color(it);
traverse(direction);
function traverse(dir) {
var dirs = [].slice.call(arguments);
var remaining = dirs.pop();
var limit = 100;
var node;
if (!dirs.length) {
node = it[remaining]();
if (node) color(it, remaining)
}
for (var i = 0, len = dirs.length; i < len; i++) {
node = it[dirs[i]]();
color(it, dir[i]);
};
while (node && limit--) {
node = it[remaining]();
if (node) color(it, remaining);
}
}
function color(it, dir) {
var node = it.node || it.node;
var type = node.nodeType;
var name = node.nodeName.toLowerCase();
var out = '';
if (3 == type) {
out = node.nodeValue;
} else if (1 == type) {
out = it.atClosing() ? '</' + name + '>' : '<' + name + '>';
}
var prefix = !dir ? ' it.node: ' : 'it.' + dir + '(): ';
console.log(method(prefix) + html.replace(out, current(out)));
return true;
}