mutation-observers.js
5.27 KB
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
"use strict";
const { domSymbolTree } = require("./internal-constants");
const reportException = require("./runtime-script-errors");
const Event = require("../generated/Event");
const idlUtils = require("../generated/utils");
const MutationRecord = require("../generated/MutationRecord");
const MUTATION_TYPE = {
ATTRIBUTES: "attributes",
CHARACTER_DATA: "characterData",
CHILD_LIST: "childList"
};
// Note:
// Since jsdom doesn't currently implement the concept of "unit of related similar-origin browsing contexts"
// (https://html.spec.whatwg.org/multipage/browsers.html#unit-of-related-similar-origin-browsing-contexts)
// we will approximate that the following properties are global for now.
// https://dom.spec.whatwg.org/#mutation-observer-compound-microtask-queued-flag
let mutationObserverMicrotaskQueueFlag = false;
// Non-spec compliant: List of all the mutation observers with mutation records enqueued. It's a replacement for
// mutation observer list (https://dom.spec.whatwg.org/#mutation-observer-list) but without leaking since it's empty
// before notifying the mutation observers.
const activeMutationObservers = new Set();
// https://dom.spec.whatwg.org/#signal-slot-list
const signalSlotList = [];
// https://dom.spec.whatwg.org/#queue-a-mutation-record
function queueMutationRecord(
type,
target,
name,
namespace,
oldValue,
addedNodes,
removedNodes,
previousSibling,
nextSibling
) {
const interestedObservers = new Map();
const nodes = domSymbolTree.ancestorsToArray(target);
for (const node of nodes) {
for (const registered of node._registeredObserverList) {
const { options, observer: mo } = registered;
if (
!(node !== target && options.subtree === false) &&
!(type === MUTATION_TYPE.ATTRIBUTES && options.attributes !== true) &&
!(type === MUTATION_TYPE.ATTRIBUTES && options.attributeFilter &&
!options.attributeFilter.some(value => value === name || value === namespace)) &&
!(type === MUTATION_TYPE.CHARACTER_DATA && options.characterData !== true) &&
!(type === MUTATION_TYPE.CHILD_LIST && options.childList === false)
) {
if (!interestedObservers.has(mo)) {
interestedObservers.set(mo, null);
}
if (
(type === MUTATION_TYPE.ATTRIBUTES && options.attributeOldValue === true) ||
(type === MUTATION_TYPE.CHARACTER_DATA && options.characterDataOldValue === true)
) {
interestedObservers.set(mo, oldValue);
}
}
}
}
for (const [observer, mappedOldValue] of interestedObservers.entries()) {
const record = MutationRecord.createImpl(target._globalObject, [], {
type,
target,
attributeName: name,
attributeNamespace: namespace,
oldValue: mappedOldValue,
addedNodes,
removedNodes,
previousSibling,
nextSibling
});
observer._recordQueue.push(record);
activeMutationObservers.add(observer);
}
queueMutationObserverMicrotask();
}
// https://dom.spec.whatwg.org/#queue-a-tree-mutation-record
function queueTreeMutationRecord(target, addedNodes, removedNodes, previousSibling, nextSibling) {
queueMutationRecord(
MUTATION_TYPE.CHILD_LIST,
target,
null,
null,
null,
addedNodes,
removedNodes,
previousSibling,
nextSibling
);
}
// https://dom.spec.whatwg.org/#queue-an-attribute-mutation-record
function queueAttributeMutationRecord(target, name, namespace, oldValue) {
queueMutationRecord(
MUTATION_TYPE.ATTRIBUTES,
target,
name,
namespace,
oldValue,
[],
[],
null,
null
);
}
// https://dom.spec.whatwg.org/#queue-a-mutation-observer-compound-microtask
function queueMutationObserverMicrotask() {
if (mutationObserverMicrotaskQueueFlag) {
return;
}
mutationObserverMicrotaskQueueFlag = true;
Promise.resolve().then(() => {
notifyMutationObservers();
});
}
// https://dom.spec.whatwg.org/#notify-mutation-observers
function notifyMutationObservers() {
mutationObserverMicrotaskQueueFlag = false;
const notifyList = [...activeMutationObservers].sort((a, b) => a._id - b._id);
activeMutationObservers.clear();
const signalList = [...signalSlotList];
signalSlotList.splice(0, signalSlotList.length);
for (const mo of notifyList) {
const records = [...mo._recordQueue];
mo._recordQueue = [];
for (const node of mo._nodeList) {
node._registeredObserverList = node._registeredObserverList.filter(registeredObserver => {
return registeredObserver.source !== mo;
});
if (records.length) {
try {
mo._callback(
records.map(idlUtils.wrapperForImpl),
idlUtils.wrapperForImpl(mo)
);
} catch (e) {
const { target } = records[0];
const window = target._ownerDocument._defaultView;
reportException(window, e);
}
}
}
}
for (const slot of signalList) {
const slotChangeEvent = Event.createImpl(
slot._globalObject,
[
"slotchange",
{ bubbles: true }
],
{ isTrusted: true }
);
slot._dispatch(slotChangeEvent);
}
}
module.exports = {
MUTATION_TYPE,
queueMutationRecord,
queueTreeMutationRecord,
queueAttributeMutationRecord,
queueMutationObserverMicrotask,
signalSlotList
};