anki/ts/html-filter/node.ts
Damien Elmes b2049209ff Re-enable formatting for .ts files
There are some style differences compared to prettier, and not all are
necessarily an improvement, but it's much faster now.
2022-11-28 09:33:04 +10:00

36 lines
995 B
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
export function removeNode(element: Node): void {
element.parentNode?.removeChild(element);
}
function iterateElement(
filter: (node: Node) => void,
fragment: DocumentFragment | Element,
): void {
for (const child of [...fragment.childNodes]) {
filter(child);
}
}
export const filterNode = (elementFilter: (element: Element) => void) => (node: Node): void => {
switch (node.nodeType) {
case Node.COMMENT_NODE:
removeNode(node);
break;
case Node.DOCUMENT_FRAGMENT_NODE:
iterateElement(filterNode(elementFilter), node as DocumentFragment);
break;
case Node.ELEMENT_NODE:
iterateElement(filterNode(elementFilter), node as Element);
elementFilter(node as Element);
break;
default:
// do nothing
}
};