-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml copy.js
86 lines (85 loc) · 2.4 KB
/
html copy.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
class NMUc {
constructor (text="") {
this.text = text;
this.parse = this.P_parse(text);
console.log(this.parse);
}
P_parse(text) {
let t = text;
let i = 0;
let ret = [];
let tag = "";
let att = "";
let eatt = "";
let child = "";
let tfflag = false;
while (t.length>i) {
if (t[i]=="<") {
tfflag = true;
i++;
while (!(t[i]==">"||t[i]==" ")&&t.length>i) {
tag += t[i];
i++;
}
if (t[i]==" ") {i++;}
while (t[i]!=">"&&t.length>i) {
att += t[i];
i++;
}
i++;
let ocnt = 0;
while (t.length>i) {
if (t[i]=="<") {
if (t[i+1]=="/") {
if (ocnt==0) {i++;break;}
child += "</";
i++;
ocnt--;
}
else {
ocnt++;
}
}
child += t[i];
i++;
}
i+=2;
while (!(t[i]==">")&&t.length>i) {
eatt += t[i];
i++;
}
child = this.P_parse(child);
if (["script","style"].indexOf(tag)==-1) {
ret.push({tag:tag,att:att,child:child})
}
tag = "";
att = "";
eatt = "";
child = "";
}
i++;
}
if (tfflag) {return ret;}
else {return [{tag:"text",att:"",child:text}];}
}
getTXT() {
this.outtxt = ""
this._gettxt(this.parse);
return this.outtxt;
}
_gettxt(data) {
for (let tag of data) {
if (tag.tag=="text") {
this.outtxt += tag.child;
return tag.child;
}
else {
if (["title","h1","h2","h3","h4","h5","h6","h7","div","p"].indexOf(tag.tag)!=-1) {
this.outtxt+="\n";
}
this._gettxt(tag.child);
}
}
return "";
}
}