-
Notifications
You must be signed in to change notification settings - Fork 0
/
strip-tags.js
258 lines (212 loc) · 7.66 KB
/
strip-tags.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
'use strict';
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.striptags = factory();
}
}(this, function () {
var STATE_OUTPUT = 0,
STATE_HTML = 1,
STATE_PRE_COMMENT = 2,
STATE_COMMENT = 3,
WHITESPACE = /\s/,
ALLOWED_TAGS_REGEX = /<(\w*)>/g;
function striptags(html, allowableTags, tagReplacement) {
var html = html || '',
state = STATE_OUTPUT,
depth = 0,
output = '',
tagBuffer = '',
inQuote = false,
i, length, c;
if (typeof allowableTags === 'string') {
// Parse the string into an array of tags
allowableTags = parseAllowableTags(allowableTags);
} else if (!Array.isArray(allowableTags)) {
// If it is not an array, explicitly set to null
allowableTags = null;
}
for (i = 0, length = html.length; i < length; i++) {
c = html[i];
switch (c) {
case '<': {
// ignore '<' if inside a quote
if (inQuote) {
break;
}
// '<' followed by a space is not a valid tag, continue
if (html[i + 1] == ' ') {
consumeCharacter(c);
break;
}
// change to STATE_HTML
if (state == STATE_OUTPUT) {
state = STATE_HTML;
consumeCharacter(c);
break;
}
// ignore additional '<' characters when inside a tag
if (state == STATE_HTML) {
depth++;
break;
}
consumeCharacter(c);
break;
}
case '>': {
// something like this is happening: '<<>>'
if (depth) {
depth--;
break;
}
// ignore '>' if inside a quote
if (inQuote) {
break;
}
// an HTML tag was closed
if (state == STATE_HTML) {
inQuote = state = 0;
if (allowableTags) {
tagBuffer += '>';
flushTagBuffer();
}
break;
}
// '<!' met its ending '>'
if (state == STATE_PRE_COMMENT) {
inQuote = state = 0;
tagBuffer = '';
break;
}
// if last two characters were '--', then end comment
if (state == STATE_COMMENT &&
html[i - 1] == '-' &&
html[i - 2] == '-') {
inQuote = state = 0;
tagBuffer = '';
break;
}
consumeCharacter(c);
break;
}
// catch both single and double quotes
case '"':
case '\'': {
if (state == STATE_HTML) {
if (inQuote == c) {
// end quote found
inQuote = false;
} else if (!inQuote) {
// start quote only if not already in one
inQuote = c;
}
}
consumeCharacter(c);
break;
}
case '!': {
if (state == STATE_HTML &&
html[i - 1] == '<') {
// looks like we might be starting a comment
state = STATE_PRE_COMMENT;
break;
}
consumeCharacter(c);
break;
}
case '-': {
// if the previous two characters were '!-', this is a comment
if (state == STATE_PRE_COMMENT &&
html[i - 1] == '-' &&
html[i - 2] == '!') {
state = STATE_COMMENT;
break;
}
consumeCharacter(c);
break;
}
case 'E':
case 'e': {
// check for DOCTYPE, because it looks like a comment and isn't
if (state == STATE_PRE_COMMENT &&
html.substr(i - 6, 7).toLowerCase() == 'doctype') {
state = STATE_HTML;
break;
}
consumeCharacter(c);
break;
}
default: {
consumeCharacter(c);
}
}
}
function consumeCharacter(c) {
if (state == STATE_OUTPUT) {
output += c;
} else if (allowableTags && state == STATE_HTML) {
tagBuffer += c;
}
}
function flushTagBuffer() {
var normalized = '',
nonWhitespaceSeen = false,
i, length, c;
normalizeTagBuffer:
for (i = 0, length = tagBuffer.length; i < length; i++) {
c = tagBuffer[i].toLowerCase();
switch (c) {
case '<': {
break;
}
case '>': {
break normalizeTagBuffer;
}
case '/': {
nonWhitespaceSeen = true;
break;
}
default: {
if (!c.match(WHITESPACE)) {
nonWhitespaceSeen = true;
normalized += c;
} else if (nonWhitespaceSeen) {
break normalizeTagBuffer;
}
}
}
}
if (allowableTags.indexOf(normalized) !== -1) {
output += tagBuffer;
} else if (tagReplacement) {
output += tagReplacement;
}
tagBuffer = '';
}
return output;
}
/**
* Return an array containing tags that are allowed to pass through the
* algorithm.
*
* @param string allowableTags A string of tags to allow (e.g. "<b><strong>").
* @return array|null An array of allowed tags or null if none.
*/
function parseAllowableTags(allowableTags) {
var tagsArray = [],
match;
while ((match = ALLOWED_TAGS_REGEX.exec(allowableTags)) !== null) {
tagsArray.push(match[1]);
}
return tagsArray.length !== 0 ? tagsArray : null;
}
return striptags;
}));