-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhtmlToHierarchy.js
147 lines (126 loc) · 3.31 KB
/
htmlToHierarchy.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
define([], function(){
return function (html){
// key method
// Converts an HTML string into one node object
// with a hierarchy of children.
// The result is still in string form but broken into
// the proper structure
var
tags,
lastType,
limit = 30,
nodes = [],
nodeObject = {},
current,
id = 0,
//commentRegExp = /(<!--(.|\s){1,}?-->)/gi,
startTagRegExp = /<\/?\w+[^>]*>|<\w+>/g,
endTagRegExp = /<\/\w*>/,
closedTagRegExp = /<[^\/]\w+[^>]*><\/\w*>/,
newlineRegExp = /[\n\t]/g;
function log(){
//console.log.apply(console, arguments);
}
function uid(){
return 'n-' + (id++);
}
function getChild(){
var o = {
id: uid()
};
nodes.push(o);
if(current){
if(!current.children){ current.children = []; }
current.children.push(o);
o.parentId = current.id;
}
current = o;
//console.log(' c-current', current);
return current;
}
function getParent(){
if(current.parentId){
for(var i = 0; i < nodes.length; i++){
if(nodes[i].id === current.parentId){
current = nodes[i];
break;
}
}
}
//console.log(' p-current', current);
return current;
}
function indexes(){
var tags = {
open: html.search(startTagRegExp),
end: html.search(endTagRegExp),
closed: html.search(closedTagRegExp)
};
log(' tags', tags.open, tags.end, tags.closed);
return tags;
}
while(html.length){
if(limit-- < 0){console.log('LIMIT!!');break;}
// clear RegExp index
startTagRegExp.lastIndex = 0;
endTagRegExp.lastIndex = 0;
html = html.trim().replace(newlineRegExp, '');
log('html ', html );
tags = indexes();
if(tags.open === 0){
nodeObject = getChild();
nodeObject.opentag = startTagRegExp.exec(html);
log(' openttag', nodeObject.opentag[0]);
nodeObject.opentag = nodeObject.opentag[0];
html = html.substring(nodeObject.opentag.length, html.length);
lastType = 'open';
}
if(tags.closed === 0){
log('CLOSED TAG', html);
nodeObject.closetag = endTagRegExp.exec(html);
nodeObject.closetag = nodeObject.closetag[0];
html = html.substring(nodeObject.closetag.length, html.length);
lastType = 'close';
log(' closetag', nodeObject.closetag);
nodeObject = getParent();
continue;
}
log(' -- cont html', html);
tags = indexes();
if(tags.open === 0){
// next node
log(' cont...');
continue;
}
if(tags.end > 0){
// inner text
log(' tags.end');
nodeObject.innerText = html.substring(0, tags.end);
log(' text', nodeObject.innerText);
html = html.replace(nodeObject.innerText, '');
lastType = 'text';
}else{
log(' tags.end', tags.end);
}
tags = indexes();
if(tags.end === 0){
// starting with a closing tag
if(lastType === 'close'){
nodeObject = getParent();
}
nodeObject.closetag = endTagRegExp.exec(html);
nodeObject.closetag = nodeObject.closetag[0];
html = html.substring(nodeObject.closetag.length, html.length);
lastType = 'close';
log(' closetag', nodeObject.closetag);
nodeObject = getParent();
}
}
//log('\n\n\n TREE PARSED', nodes.length);
//nodes.forEach(function(n){
// log(n);
//});
//log('\n\n\n');
return nodes[0];
};
});