-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
186 lines (152 loc) · 3.56 KB
/
index.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
/**
* Module dependencies
*/
var slice = [].slice;
/**
* Export `Pretty`
*/
module.exports = Pretty;
/**
* Initialize `Pretty`
*
* @param {Object} dom
*/
function Pretty(dom) {
if (!(this instanceof Pretty)) return new Pretty(dom);
this.dom = dom;
}
/**
* Get an HTML representation
*
* @return {String} html
* @api public
*/
Pretty.prototype.html = function() {
var lines = [];
var prefix = '<div class="pretty-html"><div class="line">';
var suffix = '</div></div>';
var line;
this.walk(this.dom, function(node, depth) {
line = '<span class="depth depth-' + depth + '">';
line += repeat(' ', depth);
line += '</span>';
if (3 == node.nodeType) {
line += '<span class="text">' + htmlspaces(node.nodeValue) + '</span class="text">';
} else if (1 == node.nodeType) {
line += '<span class="element">';
line += node.nodeName.toLowerCase();
var attrs = attributes(node.attributes);
var arr = [];
if (!empty(attrs)) {
for (var k in attrs) arr.push(k + " = '" + attrs[k] + "'");
line += ' | ' + arr.join(', ') + '';
}
line += '</span>';
}
lines.push(line);
});
return prefix + lines.join('</div><div class="line">') + suffix;
};
/**
* Get a console representation
*
* @return {String} html
* @api public
*/
Pretty.prototype.text = function() {
var lines = [];
var line;
this.walk(this.dom, function(node, depth) {
line = repeat(' ', depth);
if (3 == node.nodeType) {
line += "'" + spaces(node.nodeValue) + "'";
} else if (1 == node.nodeType) {
line += '[ ' + node.nodeName.toLowerCase();
var attrs = attributes(node.attributes);
var arr = [];
if (!empty(attrs)) {
for (var k in attrs) arr.push(k + " = '" + attrs[k] + "'");
line += ' | ' + arr.join(', ') + '';
}
line += ' ]';
}
lines.push(line);
})
return lines.join('\n');
};
/**
* Walk the DOM tree
*/
Pretty.prototype.walk = function(node, fn, depth) {
depth = depth || 0;
fn(node, depth);
var children = node.childNodes;
for (var i = 0, len = children.length; i < len; i++) {
this.walk(children[i], fn, depth + 1);
}
};
/**
* Parse attributes
*
* @param {Object|NamedNodeMap} attrs
* @return {Object}
*/
function attributes(attrs) {
if (undefined == attrs.item && undefined == attrs.length) return attrs;
attrs = slice.call(attrs);
var out = {};
for (var i = 0, len = attrs.length; i < len; i++) {
out[attrs[i].name] = attrs[i].value;
}
return out;
}
/**
* Repeat a string `n` times
*
* @param {String} str
* @param {Number} n
* @return {String}
*/
function repeat(str, n) {
return new Array(+n + 1).join(str);
}
/**
* Check if the object is empty
*
* @param {Object} obj
* @return {Boolean}
*/
function empty(obj) {
for(var key in obj) {
if(obj.hasOwnProperty(key)) return false;
}
return true;
}
/**
* Convert text into visible spaces
*
* @param {String} str
* @return {String} str
* @api private
*/
function spaces(str) {
return str
.replace(/ /g, '·')
.replace(/\r/g, '¬')
.replace(/\t/g, '‣')
.replace(/\n/g, '¬')
}
/**
* Convert text into (styleable) visible spaces
*
* @param {String} str
* @return {String} str
* @api private
*/
function htmlspaces(str) {
return str
.replace(/ /g, '<span class="whitespace space">·</span>')
.replace(/\r/g, '<span class="whitespace newline">¬</span>')
.replace(/\t/g, '<span class="whitespace tab">‣</span>')
.replace(/\n/g, '<span class="whitespace newline">¬</span>')
}