aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/replace-attributes.ts
blob: 443c1d076e1fb52e32fc88302b1824e167706e3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { observeDOM } from "@/utils/observe-dom";

export const replaceAttributes = (elements: any[]) => {
	let count = 0;

	observeDOM(() => {
		for (let i = 0; i < elements.length; i++) {
			if (elements[i]) {
				const element = document.querySelector(elements[i]![0]) as HTMLElement | null;
				if (element) {
					const attributes = elements[i]![1];
					if (attributes.innerHTML) {
						element.innerHTML = attributes.innerHTML;
					}
					delete attributes.innerHTML;
					for (const attribute in attributes) {
						if (attributes[attribute] === null) {
							element.removeAttribute(attribute);
						} else {
							element.setAttribute(attribute, attributes[attribute]);
						}
					}
					elements[i] = undefined;
					count++;
					if (count >= elements.length) {
						return true;
					}
				}
			}
		}

		return false;
	});
};