-
-
Notifications
You must be signed in to change notification settings - Fork 271
/
ExternalMutationTracker.ts
316 lines (263 loc) · 11 KB
/
ExternalMutationTracker.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import ElementChanges from './ElementChanges';
/**
* Uses MutationObserver to track changes to the DOM inside a component.
*
* This is meant to track changes that are made by external code - i.e. not
* a change from a component re-render.
*/
export default class {
private element: Element;
private shouldTrackChangeCallback: (element: Element) => boolean;
private mutationObserver: MutationObserver;
private changedElements: WeakMap<Element, ElementChanges> = new WeakMap();
/** For testing */
public changedElementsCount = 0;
private addedElements: Array<Element> = [];
private removedElements: Array<Element> = [];
private isStarted = false;
constructor(element: Element, shouldTrackChangeCallback: (element: Element) => boolean) {
this.element = element;
this.shouldTrackChangeCallback = shouldTrackChangeCallback;
this.mutationObserver = new MutationObserver(this.onMutations.bind(this));
}
start(): void {
if (this.isStarted) {
return;
}
this.mutationObserver.observe(this.element, {
childList: true,
subtree: true,
attributes: true,
attributeOldValue: true,
});
this.isStarted = true;
}
stop(): void {
if (this.isStarted) {
this.mutationObserver.disconnect();
this.isStarted = false;
}
}
getChangedElement(element: Element): ElementChanges|null {
return this.changedElements.has(element) ? this.changedElements.get(element) as ElementChanges : null;
}
getAddedElements(): Element[] {
return this.addedElements;
}
wasElementAdded(element: Element): boolean {
return this.addedElements.includes(element);
}
/**
* Forces any pending mutations to be handled immediately, then clears the queue.
*/
handlePendingChanges(): void {
this.onMutations(this.mutationObserver.takeRecords());
}
private onMutations(mutations: MutationRecord[]): void {
// keyed by the Element the mutation occurred on, with an array of attribute string names
const handledAttributeMutations: WeakMap<Element, string[]> = new WeakMap();
for (const mutation of mutations) {
const element = mutation.target as Element;
// ignore mutations that are not inside this component - e.g. inside a child
if (!this.shouldTrackChangeCallback(element)) {
continue;
}
if (this.isElementAddedByTranslation(element)) {
continue;
}
// ignore changes in elements that were externally-added
let isChangeInAddedElement = false;
for (const addedElement of this.addedElements) {
if (addedElement.contains(element)) {
isChangeInAddedElement = true;
break;
}
}
if (isChangeInAddedElement) {
continue;
}
switch (mutation.type) {
case 'childList':
this.handleChildListMutation(mutation);
break;
case 'attributes':
if (!handledAttributeMutations.has(element)) {
handledAttributeMutations.set(element, []);
}
// only process the first attribute mutation: it will have the
// true original value, and we'll look at the true new value
if (!(handledAttributeMutations.get(element) as string[]).includes(mutation.attributeName as string)) {
this.handleAttributeMutation(mutation);
// add this attribute to the list of handled attributes
handledAttributeMutations.set(
element, [
...handledAttributeMutations.get(element) as string[],
mutation.attributeName as string
])
;
}
break;
}
}
}
private handleChildListMutation(mutation: MutationRecord): void {
// note: we currently ignore removed nodes: if a node is removed
// by external code, it will be added back on re-render. Hide
// elements instead of removing them.
// TODO: at least handle removing previously-added nodes
mutation.addedNodes.forEach((node) => {
if (!(node instanceof Element)) {
return;
}
if (this.removedElements.includes(node)) {
// this node was removed then added back, so no net change
this.removedElements.splice(this.removedElements.indexOf(node), 1);
return;
}
if (this.isElementAddedByTranslation(node)) {
return;
}
this.addedElements.push(node);
});
mutation.removedNodes.forEach((node) => {
if (!(node instanceof Element)) {
return;
}
if (this.addedElements.includes(node)) {
// this node was added then removed, so no net change
this.addedElements.splice(this.addedElements.indexOf(node), 1);
return;
}
this.removedElements.push(node);
});
}
private handleAttributeMutation(mutation: MutationRecord):void {
const element = mutation.target as Element;
if (!this.changedElements.has(element)) {
this.changedElements.set(element, new ElementChanges());
this.changedElementsCount++;
}
const changedElement = this.changedElements.get(element) as ElementChanges;
switch (mutation.attributeName) {
case 'class':
this.handleClassAttributeMutation(mutation, changedElement);
break;
case 'style':
this.handleStyleAttributeMutation(mutation, changedElement);
break;
default:
this.handleGenericAttributeMutation(mutation, changedElement);
}
if (changedElement.isEmpty()) {
this.changedElements.delete(element);
this.changedElementsCount--;
}
}
private handleClassAttributeMutation(mutation: MutationRecord, elementChanges: ElementChanges) {
const element = mutation.target as Element;
const previousValue = mutation.oldValue;
const previousValues = previousValue ? previousValue.split(' ') : [];
previousValues.forEach((value, index) => {
const trimmedValue = value.trim();
if (trimmedValue !== '') {
previousValues[index] = trimmedValue;
} else {
previousValues.splice(index, 1);
}
});
const newValues: string[] = [].slice.call(element.classList);
const addedValues = newValues.filter((value) => !previousValues.includes(value));
const removedValues = previousValues.filter((value) => !newValues.includes(value));
addedValues.forEach((value) => {
elementChanges.addClass(value);
});
removedValues.forEach((value) => {
elementChanges.removeClass(value);
});
}
private handleStyleAttributeMutation(mutation: MutationRecord, elementChanges: ElementChanges) {
const element = mutation.target as Element;
const previousValue = mutation.oldValue || '';
const previousStyles = this.extractStyles(previousValue);
const newValue = element.getAttribute('style') || '';
const newStyles = this.extractStyles(newValue);
const addedOrChangedStyles = Object.keys(newStyles).filter((key) => previousStyles[key] === undefined || previousStyles[key] !== newStyles[key]);
const removedStyles = Object.keys(previousStyles).filter((key) => !newStyles[key]);
addedOrChangedStyles.forEach((style) => {
elementChanges.addStyle(
style,
newStyles[style],
previousStyles[style] === undefined ? null : previousStyles[style]
);
});
removedStyles.forEach((style) => {
elementChanges.removeStyle(
style,
previousStyles[style]
);
});
}
private handleGenericAttributeMutation(mutation: MutationRecord, elementChanges: ElementChanges) {
const attributeName = mutation.attributeName as string;
const element = mutation.target as Element;
let oldValue = mutation.oldValue;
let newValue = element.getAttribute(attributeName) as string;
// try to normalize situations like: disabled="disabled"
// we don't want the value "disabled" and *no* value to be seen as a change
if (oldValue === attributeName) {
oldValue = '';
}
if (newValue === attributeName) {
newValue = '';
}
if (!element.hasAttribute(attributeName)) {
if (oldValue === null) {
// null means the attribute was added, but since it's
// not present currently, it was likely later removed
// inside this same set of mutations. So, do nothing
return;
}
elementChanges.removeAttribute(
attributeName,
mutation.oldValue as string
);
return;
}
if (newValue === oldValue) {
// the attribute was changed, but it matches its current value
// likely the value was changed, then changed back all within
// the same set of mutations. So, do nothing
return;
}
elementChanges.addAttribute(
attributeName,
element.getAttribute(attributeName) as string,
mutation.oldValue
);
}
private extractStyles(styles: string): { [key: string]: string } {
const styleObject: { [key: string]: string } = {};
styles.split(';').forEach((style) => {
// split on the first ":" only
const parts = style.split(':');
// skip invalid styles (likely just whitespace)
if (parts.length === 1) {
return;
}
const property = parts[0].trim();
styleObject[property] = parts.slice(1).join(':').trim();
});
return styleObject;
}
/**
* Helps avoid tracking changes by Chrome's translation feature.
*
* When Chrome translates, it mutates the dom in a way that triggers MutationObserver.
* This includes adding new elements wrapped in a <font> tag. This causes live
* components to incorrectly think that these new elements should persist through
* re-renders, causing duplicate text.
*/
private isElementAddedByTranslation(element: Element): boolean {
return element.tagName === 'FONT' && element.getAttribute('style') === 'vertical-align: inherit;'
}
}