Change filter logic in html-filter/element: predicate true, means it stays in

This commit is contained in:
Henrik Giesel 2021-03-25 01:23:54 +01:00 committed by Damien Elmes
parent 519aea2ea8
commit f9259621a6

View file

@ -12,26 +12,26 @@ interface TagsAllowed {
type FilterMethod = (element: Element) => void; type FilterMethod = (element: Element) => void;
function filterOutAttributes( function filterAttributes(
attributePredicate: (attributeName: string) => boolean, attributePredicate: (attributeName: string) => boolean,
element: Element element: Element
): void { ): void {
for (const attr of [...element.attributes]) { for (const attr of [...element.attributes]) {
const attrName = attr.name.toUpperCase(); const attrName = attr.name.toUpperCase();
if (attributePredicate(attrName)) { if (!attributePredicate(attrName)) {
element.removeAttributeNode(attr); element.removeAttributeNode(attr);
} }
} }
} }
function blockAll(element: Element): void { function allowNone(element: Element): void {
filterOutAttributes(() => true, element); filterAttributes(() => false, element);
} }
const blockExcept = (attrs: string[]): FilterMethod => (element: Element): void => const allow = (attrs: string[]): FilterMethod => (element: Element): void =>
filterOutAttributes( filterAttributes(
(attributeName: string) => !attrs.includes(attributeName), (attributeName: string) => attrs.includes(attributeName),
element element
); );
@ -40,7 +40,7 @@ function unwrapElement(element: Element): void {
} }
function filterSpan(element: Element): void { function filterSpan(element: Element): void {
const filterAttrs = blockExcept(["STYLE"]); const filterAttrs = allow(["STYLE"]);
filterAttrs(element); filterAttrs(element);
const filterStyle = isNightMode() ? filterStylingNightMode : filterStylingLightMode; const filterStyle = isNightMode() ? filterStylingNightMode : filterStylingLightMode;
@ -48,44 +48,44 @@ function filterSpan(element: Element): void {
} }
const tagsAllowedBasic: TagsAllowed = { const tagsAllowedBasic: TagsAllowed = {
BR: blockAll, BR: allowNone,
IMG: blockExcept(["SRC"]), IMG: allow(["SRC"]),
DIV: blockAll, DIV: allowNone,
P: blockAll, P: allowNone,
SUB: blockAll, SUB: allowNone,
SUP: blockAll, SUP: allowNone,
TITLE: removeElement, TITLE: removeElement,
}; };
const tagsAllowedExtended: TagsAllowed = { const tagsAllowedExtended: TagsAllowed = {
...tagsAllowedBasic, ...tagsAllowedBasic,
A: blockExcept(["HREF"]), A: allow(["HREF"]),
B: blockAll, B: allowNone,
BLOCKQUOTE: blockAll, BLOCKQUOTE: allowNone,
CODE: blockAll, CODE: allowNone,
DD: blockAll, DD: allowNone,
DL: blockAll, DL: allowNone,
DT: blockAll, DT: allowNone,
EM: blockAll, EM: allowNone,
FONT: blockExcept(["COLOR"]), FONT: allow(["COLOR"]),
H1: blockAll, H1: allowNone,
H2: blockAll, H2: allowNone,
H3: blockAll, H3: allowNone,
I: blockAll, I: allowNone,
LI: blockAll, LI: allowNone,
OL: blockAll, OL: allowNone,
PRE: blockAll, PRE: allowNone,
RP: blockAll, RP: allowNone,
RT: blockAll, RT: allowNone,
RUBY: blockAll, RUBY: allowNone,
SPAN: filterSpan, SPAN: filterSpan,
STRONG: blockAll, STRONG: allowNone,
TABLE: blockAll, TABLE: allowNone,
TD: blockExcept(["COLSPAN", "ROWSPAN"]), TD: allow(["COLSPAN", "ROWSPAN"]),
TH: blockExcept(["COLSPAN", "ROWSPAN"]), TH: allow(["COLSPAN", "ROWSPAN"]),
TR: blockExcept(["ROWSPAN"]), TR: allow(["ROWSPAN"]),
U: blockAll, U: allowNone,
UL: blockAll, UL: allowNone,
}; };
const filterElementTagsAllowed = (tagsAllowed: TagsAllowed) => ( const filterElementTagsAllowed = (tagsAllowed: TagsAllowed) => (