Refactor htmlFilter to use function declarations

This commit is contained in:
Henrik Giesel 2021-03-24 18:40:13 +01:00 committed by Damien Elmes
parent 73430d966f
commit 592d73c344

View file

@ -3,31 +3,8 @@
import { nodeIsElement } from "./helpers"; import { nodeIsElement } from "./helpers";
export function filterHTML( const allowedTagsBasic = {};
html: string, const allowedTagsExtended = {};
internal: boolean,
extendedMode: boolean
): string {
// wrap it in <top> as we aren't allowed to change top level elements
const top = document.createElement("ankitop");
top.innerHTML = html;
if (internal) {
filterInternalNode(top);
} else {
filterNode(top, extendedMode);
}
let outHtml = top.innerHTML;
if (!extendedMode && !internal) {
// collapse whitespace
outHtml = outHtml.replace(/[\n\t ]+/g, " ");
}
outHtml = outHtml.trim();
return outHtml;
}
let allowedTagsBasic = {};
let allowedTagsExtended = {};
let TAGS_WITHOUT_ATTRS = ["P", "DIV", "BR", "SUB", "SUP"]; let TAGS_WITHOUT_ATTRS = ["P", "DIV", "BR", "SUB", "SUP"];
for (const tag of TAGS_WITHOUT_ATTRS) { for (const tag of TAGS_WITHOUT_ATTRS) {
@ -77,11 +54,11 @@ const allowedStyling = {
"text-decoration-line": true, "text-decoration-line": true,
}; };
let isNightMode = function (): boolean { function isNightMode(): boolean {
return document.body.classList.contains("nightMode"); return document.body.classList.contains("nightMode");
}; }
let filterExternalSpan = function (elem: HTMLElement) { function filterExternalSpan(elem: HTMLElement): void {
// filter out attributes // filter out attributes
for (const attr of [...elem.attributes]) { for (const attr of [...elem.attributes]) {
const attrName = attr.name.toUpperCase(); const attrName = attr.name.toUpperCase();
@ -105,7 +82,7 @@ let filterExternalSpan = function (elem: HTMLElement) {
elem.style.removeProperty(name); elem.style.removeProperty(name);
} }
} }
}; }
allowedTagsExtended["SPAN"] = filterExternalSpan; allowedTagsExtended["SPAN"] = filterExternalSpan;
@ -117,7 +94,7 @@ function isHTMLElement(elem: Element): elem is HTMLElement {
} }
// filtering from another field // filtering from another field
let filterInternalNode = function (elem: Element) { function filterInternalNode(elem: Element): void {
if (isHTMLElement(elem)) { if (isHTMLElement(elem)) {
elem.style.removeProperty("background-color"); elem.style.removeProperty("background-color");
elem.style.removeProperty("font-size"); elem.style.removeProperty("font-size");
@ -128,10 +105,10 @@ let filterInternalNode = function (elem: Element) {
const child = elem.children[i]; const child = elem.children[i];
filterInternalNode(child); filterInternalNode(child);
} }
}; }
// filtering from external sources // filtering from external sources
let filterNode = function (node: Node, extendedMode: boolean): void { function filterNode(node: Node, extendedMode: boolean): void {
if (node.nodeType === Node.COMMENT_NODE) { if (node.nodeType === Node.COMMENT_NODE) {
node.parentNode.removeChild(node); node.parentNode.removeChild(node);
return; return;
@ -174,4 +151,27 @@ let filterNode = function (node: Node, extendedMode: boolean): void {
} }
} }
} }
}; }
export function filterHTML(
html: string,
internal: boolean,
extendedMode: boolean
): string {
// wrap it in <top> as we aren't allowed to change top level elements
const top = document.createElement("ankitop");
top.innerHTML = html;
if (internal) {
filterInternalNode(top);
} else {
filterNode(top, extendedMode);
}
let outHtml = top.innerHTML;
if (!extendedMode && !internal) {
// collapse whitespace
outHtml = outHtml.replace(/[\n\t ]+/g, " ");
}
outHtml = outHtml.trim();
return outHtml;
}