-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml-node.js
More file actions
executable file
·167 lines (121 loc) · 3.41 KB
/
Copy pathhtml-node.js
File metadata and controls
executable file
·167 lines (121 loc) · 3.41 KB
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
function HtmlNode (node) {
if (typeof node === 'string') {
var tmp = document.createElement('p');
// ie8 bug
tmp.innerHTML = node;
node = tmp;
this.noRoot = true;
}
this.rootNode = node;
}
HtmlNode.SafeNodeNames = /^(p|a|span|b|i|img)$/i;
HtmlNode.UnsafeNodeNames = /^(script|link|input|select|button|meta|style)$/i;
HtmlNode.EscapeMap = {
'<': '<',
'>': '>',
'&': '&'
};
HtmlNode.escape = function (text) {
return text.replace(/[<>&]/g, function (char) {
return HtmlNode.EscapeMap[char];
});
};
HtmlNode.prototype = {
safeHTML: function () {
var
ret = '',
rootNode = this.rootNode,
nodeType = rootNode.nodeType;
if (nodeType === 3) {
ret += HtmlNode.escape(rootNode.nodeValue);
}
else if (nodeType === 1) {
// bug: img 标签会被过滤
if (this.innerText() || this.hasImg()) {
ret += this.safeOuterHTML();
}
else {
if (this.isBr()) {
ret += '\n';
}
else if (this.isImg()) {
ret += '<img' + this.attribute('src') + this.attribute('alt') + ' />';
}
else {
//
}
}
}
else {
//
}
return ret;
},
hasImg: function () {
return Boolean(this.rootNode.getElementsByTagName('img').length);
},
isUnsafeNode: function () {
var nodeName = this.nodeName();
if (HtmlNode.UnsafeNodeNames.test(nodeName)) {
return true;
}
else {
return false;
}
},
attribute: function (name) {
var ret = '',
attrValue = this.rootNode.getAttribute(name);
if (attrValue) {
ret += ' ' + name + '="' + attrValue + '"';
}
return ret;
},
safeOuterHTML: function () {
if (this.isUnsafeNode()) {
return '';
}
var name = this.safeNodeName(),
rootNode = this.rootNode,
nodes = rootNode.childNodes,
node,
ret = '';
if (!this.noRoot) {
if (name === 'a') {
ret += '<a' + this.attribute('href') + this.attribute('target') + this.attribute('download') + '>';
}
else {
ret += '<' + name + '>';
}
}
for (var i = 0 ; i < nodes.length; ++i) {
node = nodes[i];
ret += new HtmlNode(node).safeHTML();
}
if (!this.noRoot) {
ret += '</' + name + '>';
}
return ret;
},
innerText: function () {
var innerText = this.rootNode.innerText || this.rootNode.textContent || '';
innerText = innerText.replace(/^[\s\r\n]+|[\s\r\n]+$/g, '');
return innerText;
},
nodeName: function () {
return this.rootNode.nodeName.toLowerCase();
},
isImg: function () {
return this.nodeName() === 'img' && this.rootNode.src;
},
isBr: function () {
return this.nodeName() === 'br';
},
safeNodeName: function () {
var nodeName = this.nodeName();
if (!HtmlNode.SafeNodeNames.test(nodeName)) {
nodeName = 'p';
}
return nodeName;
}
};