-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
318 lines (265 loc) · 8.25 KB
/
Copy pathindex.js
File metadata and controls
318 lines (265 loc) · 8.25 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
* html-filter
*
* @version 4.3.1
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.HtmlFilter = factory());
}(this, (function () { 'use strict';
/**
* HtmlFilter
*
* @author afu
*/
function HtmlFilter() {
// <(xxx)( data-name="lisi") xxx />
// </(xxx)>
// <!--(xxx)-->
// 此正则有四个子模式
// 1. 代表开始标签名称
// 2. 代表整个属性部分 该部分可有可无
// 3. 代表结束标签名称
// 4. 代表注释内容
this.htmlPartsRegex = /(?:<([-\w]+)((?:\s+[-\w]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^>\s]+))?)*)[\S\s]*?\/?>)|(?:<\/([^>]+)>)|(?:<!--([\S|\s]*?)-->)/g;
// |______| |_____| |_________________________| |__________| |_________|
// 开始标签名 属性名 属性值 标签结束 结束标签
// (disabled)
// (title)="()"
// (title)='()'
// (title)=()
this.attributesRegex = /(?:([-\w]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^>\s]+)))?)/g;
/**
* Legal tags
*
* {
* p: null,
* img: {src: 1, width: 1, height: 1},
* ...
* }
*/
this.allowedTags = null;
/**
* Whether allow comment tag
*/
this.allowComment = false;
/**
* result string
*/
this.htmlString = '';
}
HtmlFilter.prototype = {
constructor: HtmlFilter,
reset: function() {
this.htmlString = '';
},
/**
* Determine whether a tag is a selfClosingTag
*
* @param {String} nodeName
* @return Boolean
*/
isSelfClosingTag: function(nodeName) {
return 1 === HtmlFilter.TAGS_SELFCLOSING[nodeName];
},
/**
* Determine whether a attribute is empty
*
* @param {String} attribute
* @return Boolean
*/
isEmptyAttribute: function(attribute) {
return 1 === HtmlFilter.ATTRIBUTES_EMPTY[attribute];
},
/**
* Get the support attributes of a tag
*
* @param {String} nodeName
* @return null | Object
*/
getAllowedAttributes: function(nodeName) {
if(null === this.allowedTags) {
return null;
}
// tag not in white list or tag not support attributes
if(undefined === this.allowedTags[nodeName] || null === this.allowedTags[nodeName]) {
return null;
}
return this.allowedTags[nodeName];
},
/**
* Determine whether the tag is legitimate
*
* @param {String} nodeName
* @return Boolean
*/
isAllowedTag: function(nodeName) {
if(null === this.allowedTags) {
return false;
}
// white list
// null is exists yet
if(undefined !== this.allowedTags[nodeName]) {
return true;
}
return false;
},
filterAttribute: function(name, value) {
return value.replace(/"/g, '');
},
onOpen: function(tagName, attributes) {
var nodeName = tagName.toLowerCase();
var attrs = attributes;
var nodeString = '';
// 非法标签
if(!this.isAllowedTag(nodeName)) {
return;
}
// attributes filter
var allowedAttributes = this.getAllowedAttributes(nodeName);
if(null !== allowedAttributes) {
for(var k in attrs) {
if(undefined === allowedAttributes[k]) {
delete attrs[k];
}
}
}
nodeString = '<' + nodeName;
// null means not support attributes
if(null !== allowedAttributes) {
for(var k in attrs) {
nodeString += (' ' + k + '="' + this.filterAttribute(k, attrs[k]) + '"');
}
}
// selfClosingTag
if(this.isSelfClosingTag(nodeName)) {
nodeString += ' /';
}
nodeString += '>';
this.htmlString += nodeString;
},
onClose: function(tagName) {
var nodeName = tagName.toLowerCase();
// 非法标签
if(!this.isAllowedTag(nodeName)) {
return;
}
this.htmlString += '</' + nodeName + '>';
},
onComment: function(content) {
if(!this.allowComment) {
return;
}
this.onText('<!--' + content + '-->');
},
onText: function(text) {
this.htmlString += text;
},
/**
* filter html
*
* @param {String} html
*/
filter: function(html) {
var parts = null;
// the index at which to start the next match
var lastIndex = 0;
var tagName = '';
// 添加临时节点 以便进行匹配
var htmlString = html + '<htmlfilter />';
// reset first
this.reset();
while( null !== (parts = this.htmlPartsRegex.exec(htmlString)) ) {
// TextNode
if(parts.index > lastIndex) {
var text = htmlString.substring( lastIndex, parts.index );
this.onText(text);
}
lastIndex = this.htmlPartsRegex.lastIndex;
// closing tag
if( (tagName = parts[3]) ) {
this.onClose(tagName);
continue;
}
// opening tag or selfClosingTag
if( (tagName = parts[1]) ) {
var attrParts = null;
var attrs = {};
// attributes
if(parts[2]) {
while( null !== ( attrParts = this.attributesRegex.exec(parts[2]) ) ) {
var attrName = attrParts[1];
var attrValue = '';
if(attrParts[2]) {
attrValue = attrParts[2];
} else if(attrParts[3]) {
attrValue = attrParts[3];
} else if(attrParts[4]) {
attrValue = attrParts[4];
}
if(this.isEmptyAttribute(attrName)) {
attrs[attrName] = attrName;
} else {
attrs[attrName] = attrValue;
}
}
}
this.onOpen(tagName, attrs);
continue;
}
// comment
if( parts[4] ) {
this.onComment(parts[4]);
}
}
return this.getHtml();
},
/**
* get html
*/
getHtml: function() {
return this.htmlString;
}
};
/**
* selfClosingTag
*/
HtmlFilter.TAGS_SELFCLOSING = {
area: 1,
meta: 1,
base: 1,
link: 1,
hr: 1,
br: 1,
wbr: 1,
col: 1,
img: 1,
area: 1,
input: 1,
textarea: 1,
embed: 1,
param: 1,
source: 1,
object: 1
};
/**
* 可以为空的属性
*/
HtmlFilter.ATTRIBUTES_EMPTY = {
checked: 1,
compact: 1,
declare: 1,
defer: 1,
disabled: 1,
ismap: 1,
multiple: 1,
nohref: 1,
noresize: 1,
noshade: 1,
nowrap: 1,
readonly: 1,
selected: 1
};
return HtmlFilter;
})));