15dcb09036
* Migrate check_copyright to Rust * Add a new lint to check accidental usages of /// in ts/svelte comments * Fix a bunch of incorrect jdoc comments * Move contributor check into minilints Will allow users to detect the issue locally with './ninja check' before pushing to CI. * Make Cargo.toml consistent with other crates
54 lines
1.9 KiB
TypeScript
54 lines
1.9 KiB
TypeScript
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
/** Keep property if true. */
|
|
type StylingPredicate = (property: string, value: string) => boolean;
|
|
|
|
const keep = (_key: string, _value: string) => true;
|
|
const discard = (_key: string, _value: string) => false;
|
|
|
|
/** 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. */
|
|
function filterStyling(
|
|
defaultPredicate: StylingPredicate,
|
|
exceptions: Record<string, StylingPredicate>,
|
|
): (element: HTMLElement) => void {
|
|
return (element: HTMLElement): void => {
|
|
// jsdom does not support @@iterator, so manually iterate
|
|
const toRemove = [] as string[];
|
|
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)) {
|
|
toRemove.push(key);
|
|
}
|
|
}
|
|
for (const key of toRemove) {
|
|
element.style.removeProperty(key);
|
|
}
|
|
};
|
|
}
|
|
|
|
const nightModeExceptions = {
|
|
"font-weight": keep,
|
|
"font-style": keep,
|
|
"text-decoration-line": keep,
|
|
};
|
|
|
|
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,
|
|
});
|