CyTube/lib/customembed.js

23 lines
618 B
JavaScript
Raw Normal View History

2013-08-03 15:50:41 +00:00
const allowed = ["iframe", "object", "param", "embed"];
const tag_re = /<\s*\/?\s*([a-z]+)(\s*([a-z]+)\s*=\s*('[^']*'|"[^"]*"|[^"'>]*))*\s*>/ig;
function filter(str) {
2014-02-19 03:56:54 +00:00
if (typeof str !== "string") {
return "";
}
2013-08-03 15:50:41 +00:00
str = str.replace(tag_re, function (match, tag) {
if(!~allowed.indexOf(tag.toLowerCase())) {
return match.replace("<", "&lt;").replace(">", "&gt;");
}
return match;
});
str = str.replace(/(\bon\w*\s*=\s*('[^']*'|"[^"]"|[^\s><]*))/ig, function () {
return "";
});
2014-02-19 03:56:54 +00:00
return str.substring(0, 20000);
2013-08-03 15:50:41 +00:00
}
exports.filter = filter;