2021-04-13 10:57:08 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2023-05-26 04:49:44 +02:00
|
|
|
/** Keep property if true. */
|
2021-03-24 23:35:51 +01:00
|
|
|
type StylingPredicate = (property: string, value: string) => boolean;
|
|
|
|
|
2022-08-19 04:31:26 +02:00
|
|
|
const keep = (_key: string, _value: string) => true;
|
|
|
|
const discard = (_key: string, _value: string) => false;
|
|
|
|
|
2023-05-26 04:49:44 +02:00
|
|
|
/** Return a function that filters out certain styles.
|
|
|
|
- If the style is listed in `exceptions`, the provided predicate is used.
|
|
|
|
- If the style is not listed, the default predicate is used instead. */
|
2022-08-19 04:31:26 +02:00
|
|
|
function filterStyling(
|
|
|
|
defaultPredicate: StylingPredicate,
|
|
|
|
exceptions: Record<string, StylingPredicate>,
|
|
|
|
): (element: HTMLElement) => void {
|
|
|
|
return (element: HTMLElement): void => {
|
|
|
|
// jsdom does not support @@iterator, so manually iterate
|
2022-09-30 06:05:03 +02:00
|
|
|
const toRemove = [] as string[];
|
2022-08-19 04:31:26 +02:00
|
|
|
for (let i = 0; i < element.style.length; i++) {
|
|
|
|
const key = element.style.item(i);
|
|
|
|
const value = element.style.getPropertyValue(key);
|
|
|
|
const predicate = exceptions[key] ?? defaultPredicate;
|
|
|
|
if (!predicate(key, value)) {
|
2022-09-30 06:05:03 +02:00
|
|
|
toRemove.push(key);
|
2021-05-26 01:21:33 +02:00
|
|
|
}
|
2021-03-24 23:35:51 +01:00
|
|
|
}
|
2022-09-30 06:05:03 +02:00
|
|
|
for (const key of toRemove) {
|
|
|
|
element.style.removeProperty(key);
|
|
|
|
}
|
2021-05-26 01:21:33 +02:00
|
|
|
};
|
2022-08-19 04:31:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const nightModeExceptions = {
|
|
|
|
"font-weight": keep,
|
|
|
|
"font-style": keep,
|
|
|
|
"text-decoration-line": keep,
|
|
|
|
};
|
2021-03-24 23:35:51 +01:00
|
|
|
|
2022-08-19 04:31:26 +02:00
|
|
|
export const filterStylingNightMode = filterStyling(discard, nightModeExceptions);
|
|
|
|
export const filterStylingLightMode = filterStyling(discard, {
|
|
|
|
color: keep,
|
|
|
|
"background-color": (_key: string, value: string) => value != "transparent",
|
|
|
|
...nightModeExceptions,
|
|
|
|
});
|
|
|
|
export const filterStylingInternal = filterStyling(keep, {
|
|
|
|
"font-size": discard,
|
|
|
|
"font-family": discard,
|
|
|
|
width: discard,
|
|
|
|
height: discard,
|
|
|
|
"max-width": discard,
|
|
|
|
"max-height": discard,
|
|
|
|
});
|