Add ESLint & Run it against the system (#4470)

This commit is contained in:
Zach H 2021-11-13 14:49:06 -05:00 committed by ZeldaZach
parent 43eee6b32e
commit f789e02096
106 changed files with 1235 additions and 20242 deletions

View file

@ -1,11 +1,11 @@
import $ from "jquery";
import $ from 'jquery';
export function sanitizeHtml(msg: string): string {
const $div = $("<div>").html(msg);
const $div = $('<div>').html(msg);
const whitelist = {
tags: "br,a,img,center,b,font",
attrs: ["href","color"],
href: ["http://","https://","ftp://","//"]
tags: 'br,a,img,center,b,font',
attrs: ['href', 'color'],
href: ['http://', 'https://', 'ftp://', '//']
};
// remove all tags, attributes, and href protocols except some
@ -17,35 +17,36 @@ export function sanitizeHtml(msg: string): string {
}
function enforceTagWhitelist($el: JQuery<HTMLElement>, tags: string): void {
$el.find("*").not(tags).each(function() {
$el.find('*').not(tags).each(() => {
$(this).replaceWith(this.innerHTML);
});
}
function enforceAttrWhitelist($el: JQuery<HTMLElement>, attrs: string[]): void {
$el.find("*").each(function() {
var attributes = this.attributes;
var i = attributes.length;
while( i-- ) {
var attr = attributes[i];
if( $.inArray(attr.name,attrs) === -1 )
$el.find('*').each(() => {
const attributes = this.attributes;
let i = attributes.length;
while (i--) {
const attr = attributes[i];
if ($.inArray(attr.name, attrs) === -1) {
this.removeAttributeNode(attr);
}
}
});
}
function enforceHrefWhitelist($el: JQuery<HTMLElement>, hrefs: string[]): void {
$el.find("[href]").each(function() {
$el.find('[href]').each(() => {
const $_el = $(this);
const attributeValue = $_el.attr("href");
const attributeValue = $_el.attr('href');
for (let protocol in hrefs) {
if (attributeValue.indexOf(hrefs[protocol]) === 0) {
$_el.attr("target", "_blank");
return;
}
if (attributeValue.indexOf(hrefs[protocol]) === 0) {
$_el.attr('target', '_blank');
return;
}
}
$_el.removeAttr("href");
$_el.removeAttr('href');
});
}
}