-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
189 lines (145 loc) · 3.01 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
187
188
189
/**
* Sanitize
*
* sanitize html for safety, clean harmful and noisy nodes.
*
* Copyright (c) 2013 - 2014 by Hsiaoming Yang.
*/
var config = {
keep: [
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
'div', 'p', 'blockquote', 'figure', 'figcaption',
'ul', 'ol', 'li',
'pre', 'code',
'em', 'strong', 'i', 'b',
'a', 'img', 'hr', 'br',
'table', 'td', 'th', 'tr', 'tbody', 'thead', 'tfoot'
],
drop: [
'meta', 'link',
'script', 'noscript', 'style',
'embed', 'iframe', 'frame',
'form', 'input', 'textarea', 'button', 'fieldset'
],
replace: {
'div': 'p',
'i': 'em',
'b': 'strong'
},
attributes: {
'*': [],
'a': ['href'],
'img': ['src', 'width', 'height']
}
};
/**
* Replace node tag name
*/
function replaceTag(node, tagName) {
var repl = document.createElement(tagName);
repl.innerHTML = node.innerHTML;
node.parentNode.replaceChild(repl, node);
return repl;
}
/**
* Trim node attributes
*/
function trimAttributes(node, allowAttrs) {
allowAttrs = allowAttrs.concat(config.attributes['*']);
// clone all attrs
var attrs = Array.prototype.slice.call(node.attributes);
for (var i = 0; i < attrs.length; i++) {
(function(attr) {
if (!~allowAttrs.indexOf(attr.name)) {
node.removeAttributeNode(attr);
}
})(attrs[i]);
}
return node;
}
/**
* Remove this node
*/
function dropNode(node) {
node.parentNode.removeChild(node);
}
/**
* Remove the tag of the node
*/
function unwrap(node) {
var parent = node.parentNode;
if (!parent) {
return;
}
var children = node.childNodes;
for (var i = 0; i < children.length; i++) {
parent.insertBefore(children[i], node);
}
parent.removeChild(node);
}
/**
* Traversal the node tree
*/
function traversal(node, fn, parent) {
if (!node) {
return;
}
var children = node.childNodes;
for (var i = 0; i < children.length; i++) {
fn(children[i]);
traversal(children[i], fn, node);
}
if (parent) {
fn(node);
}
return node;
}
/**
* Sanitize a node, clean the disaster
*/
function clean(node) {
if (node.nodeType === document.TEXT_NODE) {
return node;
}
if (node.nodeType !== document.ELEMENT_NODE) {
return dropNode(node);
}
var tag = node.nodeName.toLowerCase();
if (~config.drop.indexOf(tag)) {
return dropNode(node);
}
if (!~config.keep.indexOf(tag)) {
return unwrap(node);
}
if (config.replace[tag]) {
node = replaceTag(node, config.replace[tag]);
}
trimAttributes(node, config.attributes[tag] || []);
return node;
}
/**
* Clean empty node
*/
function empty(html) {
var regex = /<(\w+)[^>]*><\/\1>/g;
return html.replace(regex, '');
}
/**
* Exports sanitize API
*/
function sanitize(html) {
var node;
if (html.innerHTML) {
node = html;
} else {
node = document.createElement('div');
node.innerHTML = html;
}
node = traversal(node, clean);
return empty(node.innerHTML);
}
/**
* Exports config data
*/
sanitize.config = config;
module.exports = sanitize;