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
|
|
|
|
|
2021-03-24 23:35:51 +01:00
|
|
|
export function removeNode(element: Node): void {
|
|
|
|
element.parentNode?.removeChild(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
function iterateElement(
|
|
|
|
filter: (node: Node) => void,
|
2021-10-19 01:06:00 +02:00
|
|
|
fragment: DocumentFragment | Element,
|
2021-03-24 23:35:51 +01:00
|
|
|
): void {
|
|
|
|
for (const child of [...fragment.childNodes]) {
|
|
|
|
filter(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 00:33:04 +01:00
|
|
|
export const filterNode = (elementFilter: (element: Element) => void) => (node: Node): void => {
|
|
|
|
switch (node.nodeType) {
|
|
|
|
case Node.COMMENT_NODE:
|
|
|
|
removeNode(node);
|
|
|
|
break;
|
2021-03-24 23:35:51 +01:00
|
|
|
|
2022-11-28 00:33:04 +01:00
|
|
|
case Node.DOCUMENT_FRAGMENT_NODE:
|
|
|
|
iterateElement(filterNode(elementFilter), node as DocumentFragment);
|
|
|
|
break;
|
2021-03-24 23:35:51 +01:00
|
|
|
|
2022-11-28 00:33:04 +01:00
|
|
|
case Node.ELEMENT_NODE:
|
|
|
|
iterateElement(filterNode(elementFilter), node as Element);
|
|
|
|
elementFilter(node as Element);
|
|
|
|
break;
|
2021-03-24 23:35:51 +01:00
|
|
|
|
2022-11-28 00:33:04 +01:00
|
|
|
default:
|
2021-05-26 01:21:33 +02:00
|
|
|
// do nothing
|
2022-11-28 00:33:04 +01:00
|
|
|
}
|
|
|
|
};
|