-
Notifications
You must be signed in to change notification settings - Fork 1
/
src.js
42 lines (36 loc) · 830 Bytes
/
src.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
/**
* @param {String} order : order by criteria ('nodes', 'depth' or 'length')
* @param {Element} e
*/
function analyzeDOM(order, e) {
e = e || document;
order = order || 'length';
var data = {
'element': e,
'nodes': e.children.length,
'depth': 1,
'length': e.outerHTML ? e.outerHTML.replace(/\s/g, '').length : 'NA',
'_children': []
};
if (!e.children.length) {
return data;
};
var maxDepth = 0;
for (var i = 0; i < e.children.length; i++) {
var child = analyzeDOM(order, e.children[i]);
maxDepth = child.depth > maxDepth ? child.depth : maxDepth;
data.nodes += child.nodes;
data._children.push(child);
}
data.depth += maxDepth;
data._children.sort(function(a,b) {
if (a[order] < b[order]){
return 1;
}
if (a[order] > b[order]){
return -1;
}
return 0;
});
return data;
}