-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-attribute-polyfill.ts
176 lines (156 loc) · 5.07 KB
/
custom-attribute-polyfill.ts
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// https://github.com/WICG/webcomponents/issues/1029
const observerConfig: MutationObserverInit = {
attributes: true,
subtree: true,
attributeOldValue: true,
characterData: false,
characterDataOldValue: false,
childList: true,
};
const registries = new WeakMap<
object,
{ observer: MutationObserver; attributes: string[] }
>();
export class Attribute {
#host: Element;
#name: string;
public get host() {
return this.#host;
}
public get value() {
return this.#host.getAttribute(this.#name);
}
constructor(name: string, host: Element) {
this.#host = host;
this.#name = name;
}
connectedCallback(_value: string | null) {
// console.log("native created");
}
changedCallback(_newValue: string | null, _oldValue?: string | null) {
// console.log("native changed");
}
disconnectedCallback() {
// console.log("native removed");
}
}
type CustomAttribute<T extends Attribute> = new (...args: any[]) => T;
export function registerAttribute<T extends Attribute>(
name: string,
customAttribute: CustomAttribute<T>,
root: Document | DocumentFragment | Element = document,
childList = true
) {
const observedElements = new WeakMap<Element, Attribute>();
const existingObserver = registries.get(root);
let observer: MutationObserver;
let attributeFilter = [name];
if (existingObserver) {
// Attribute observer already defined
if (existingObserver.attributes.includes(name)) {
console.error(
`Failed to execute 'registerAttribute': the name "${name}" has already been used within the scope of ${root}.`
);
return;
}
observer = existingObserver.observer;
attributeFilter = [...attributeFilter, ...existingObserver.attributes];
// Reset observer to start observing with new attribute filter
observer.disconnect();
} else {
observer = new MutationObserver(mutationHandler);
}
function mutationHandler(mutationList) {
for (let record of mutationList) {
// Element (or parent of element) got removed
if (record.type === "childList" && record.removedNodes.length > 0) {
for (let removedNode of record.removedNodes) {
if (removedNode instanceof Element) {
if (removedNode.hasAttribute(name)) {
if (observedElements.has(removedNode))
observedElements.get(removedNode)?.disconnectedCallback();
} else {
removedNode.querySelectorAll(`[${name}]`).forEach((node) => {
if (observedElements.has(node))
observedElements.get(node)?.disconnectedCallback();
});
}
}
}
return;
}
// Element with attribute got added
if (record.type === "childList" && record.addedNodes.length > 0) {
for (let addedNode of record.addedNodes) {
if (addedNode instanceof Element) {
if (addedNode.hasAttribute(name)) {
newAttribute(addedNode);
} else {
addedNode.querySelectorAll(`[${name}]`).forEach((node) => {
newAttribute(node);
});
}
}
}
return;
}
if (record.type === "attributes" && record.target instanceof Element) {
const newValue = record.target.getAttribute(name);
const oldValue = record.oldValue;
if (oldValue === null) {
// New attribute
newAttribute(record.target);
} else if (newValue === null && observedElements.has(record.target)) {
// Deleted
const cls = observedElements.get(record.target)!;
cls.disconnectedCallback();
observedElements.delete(record.target);
} else if (
newValue !== record.oldValue &&
observedElements.has(record.target)
) {
// Change
const cls = observedElements.get(record.target)!;
cls.changedCallback(newValue, record.oldValue);
}
}
}
}
function newAttribute(element: Element) {
const cls = new customAttribute(name, element);
cls.connectedCallback(element.getAttribute(name));
observedElements.set(element, cls);
}
// Start listening to changes
observer.observe(root, { ...observerConfig, attributeFilter, childList });
// Initial pass
if (childList) {
root.querySelectorAll(`[${name}]`).forEach((element) => {
newAttribute(element);
});
} else if (root instanceof Element) {
newAttribute(root);
} else {
// Throw error or what?
}
}
class TestingAttribute extends Attribute {
clickHandler() {
console.log(this.value);
}
connectedCallback(value: string | null): void {
console.log("testing attribute connected", value);
this.host.addEventListener("click", this.clickHandler);
}
changedCallback(
newValue: string | null,
oldValue?: string | null | undefined
): void {
console.log(oldValue, newValue);
}
disconnectedCallback(): void {
console.log("testing attribute disconnected");
this.host.removeEventListener("click", this.clickHandler);
}
}
registerAttribute("custom-attribute", TestingAttribute);