2016-02-22 09:42:40 -05:00
|
|
|
var whiteListTag = ['style', '!--', 'kbd'];
|
2016-02-11 15:33:21 -05:00
|
|
|
var whiteListAttr = ['id', 'class', 'style'];
|
|
|
|
|
|
|
|
var filterXSSOptions = {
|
|
|
|
allowCommentTag: true,
|
2016-04-20 06:10:43 -04:00
|
|
|
escapeHtml: function (html) {
|
|
|
|
// to allow html comment in multiple lines
|
|
|
|
return html.replace(/<(.*?)>/g, '<$1>');
|
|
|
|
},
|
2016-02-11 15:33:21 -05:00
|
|
|
onIgnoreTag: function (tag, html, options) {
|
|
|
|
// allow style in html
|
2016-02-16 10:51:22 -05:00
|
|
|
if (whiteListTag.indexOf(tag) !== -1) {
|
2016-02-11 15:33:21 -05:00
|
|
|
// do not filter its attributes
|
|
|
|
return html;
|
|
|
|
}
|
|
|
|
},
|
2016-04-20 06:18:52 -04:00
|
|
|
onTagAttr: function (tag, name, value, isWhiteAttr) {
|
|
|
|
// allow href starts with '.' or '/'
|
|
|
|
if (isWhiteAttr && name === 'href' && (value.indexOf('.') == 0 || value.indexOf('/') == 0)) {
|
|
|
|
return name + '="' + filterXSS.escapeAttrValue(value) + '"';
|
|
|
|
}
|
|
|
|
},
|
2016-02-11 15:33:21 -05:00
|
|
|
onIgnoreTagAttr: function (tag, name, value, isWhiteAttr) {
|
|
|
|
// allow attr start with 'data-' or in the whiteListAttr
|
|
|
|
if (name.substr(0, 5) === 'data-' || whiteListAttr.indexOf(name) !== -1) {
|
|
|
|
// escape its value using built-in escapeAttrValue function
|
|
|
|
return name + '="' + filterXSS.escapeAttrValue(value) + '"';
|
2016-02-11 04:45:13 -05:00
|
|
|
}
|
2016-03-04 10:17:59 -05:00
|
|
|
// allow ol specify start number
|
|
|
|
if (tag === 'ol' && name === 'start') {
|
|
|
|
return name + '="' + filterXSS.escapeAttrValue(value) + '"';
|
|
|
|
}
|
2016-02-11 15:33:21 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function preventXSS(html) {
|
|
|
|
return filterXSS(html, filterXSSOptions);
|
2016-02-11 04:45:13 -05:00
|
|
|
}
|