858f2bdf5
Boyan Georgiev
fixes
|
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
"use strict";
const DOMException = require("domexception/webidl2js-wrapper");
const reportException = require("../helpers/runtime-script-errors");
const idlUtils = require("../generated/utils");
const { nodeRoot } = require("../helpers/node");
const {
isNode, isShadowRoot, isSlotable, getEventTargetParent,
isShadowInclusiveAncestor, retarget
} = require("../helpers/shadow-dom");
const MouseEvent = require("../generated/MouseEvent");
const EVENT_PHASE = {
NONE: 0,
CAPTURING_PHASE: 1,
AT_TARGET: 2,
BUBBLING_PHASE: 3
};
class EventTargetImpl {
constructor(globalObject) {
this._globalObject = globalObject;
this._eventListeners = Object.create(null);
}
addEventListener(type, callback, options) {
// webidl2js currently can't handle neither optional arguments nor callback interfaces
if (callback === undefined || callback === null) {
callback = null;
} else if (typeof callback !== "object" && typeof callback !== "function") {
throw new TypeError("Only undefined, null, an object, or a function are allowed for the callback parameter");
}
options = normalizeEventHandlerOptions(options, ["capture", "once", "passive"]);
if (callback === null) {
return;
}
if (!this._eventListeners[type]) {
this._eventListeners[type] = [];
}
for (let i = 0; i < this._eventListeners[type].length; ++i) {
const listener = this._eventListeners[type][i];
if (listener.options.capture === options.capture && listener.callback === callback) {
return;
}
}
this._eventListeners[type].push({
callback,
options
});
}
removeEventListener(type, callback, options) {
if (callback === undefined || callback === null) {
callback = null;
} else if (typeof callback !== "object" && typeof callback !== "function") {
throw new TypeError("Only undefined, null, an object, or a function are allowed for the callback parameter");
}
options = normalizeEventHandlerOptions(options, ["capture"]);
if (callback === null) {
// Optimization, not in the spec.
return;
}
if (!this._eventListeners[type]) {
return;
}
for (let i = 0; i < this._eventListeners[type].length; ++i) {
const listener = this._eventListeners[type][i];
if (listener.callback === callback && listener.options.capture === options.capture) {
this._eventListeners[type].splice(i, 1);
break;
}
}
}
dispatchEvent(eventImpl) {
if (eventImpl._dispatchFlag || !eventImpl._initializedFlag) {
throw DOMException.create(this._globalObject, [
"Tried to dispatch an uninitialized event",
"InvalidStateError"
]);
}
if (eventImpl.eventPhase !== EVENT_PHASE.NONE) {
throw DOMException.create(this._globalObject, [
"Tried to dispatch a dispatching event",
"InvalidStateError"
]);
}
eventImpl.isTrusted = false;
return this._dispatch(eventImpl);
}
// https://dom.spec.whatwg.org/#get-the-parent
_getTheParent() {
return null;
}
// https://dom.spec.whatwg.org/#concept-event-dispatch
// legacyOutputDidListenersThrowFlag optional parameter is not necessary here since it is only used by indexDB.
_dispatch(eventImpl, targetOverride /* , legacyOutputDidListenersThrowFlag */) {
let targetImpl = this;
let clearTargets = false;
let activationTarget = null;
eventImpl._dispatchFlag = true;
targetOverride = targetOverride || targetImpl;
let relatedTarget = retarget(eventImpl.relatedTarget, targetImpl);
if (targetImpl !== relatedTarget || targetImpl === eventImpl.relatedTarget) {
const touchTargets = [];
appendToEventPath(eventImpl, targetImpl, targetOverride, relatedTarget, touchTargets, false);
const isActivationEvent = MouseEvent.isImpl(eventImpl) && eventImpl.type === "click";
if (isActivationEvent && targetImpl._hasActivationBehavior) {
activationTarget = targetImpl;
}
let slotInClosedTree = false;
let slotable = isSlotable(targetImpl) && targetImpl._assignedSlot ? targetImpl : null;
let parent = getEventTargetParent(targetImpl, eventImpl);
// Populate event path
// https://dom.spec.whatwg.org/#event-path
while (parent !== null) {
if (slotable !== null) {
if (parent.localName !== "slot") {
throw new Error(`JSDOM Internal Error: Expected parent to be a Slot`);
}
slotable = null;
const parentRoot = nodeRoot(parent);
if (isShadowRoot(parentRoot) && parentRoot.mode === "closed") {
slotInClosedTree = true;
}
}
if (isSlotable(parent) && parent._assignedSlot) {
slotable = parent;
}
relatedTarget = retarget(eventImpl.relatedTarget, parent);
if (
(isNode(parent) && isShadowInclusiveAncestor(nodeRoot(targetImpl), parent)) ||
idlUtils.wrapperForImpl(parent).constructor.name === "Window"
) {
if (isActivationEvent && eventImpl.bubbles && activationTarget === null &&
parent._hasActivationBehavior) {
activationTarget = parent;
}
appendToEventPath(eventImpl, parent, null, relatedTarget, touchTargets, slotInClosedTree);
} else if (parent === relatedTarget) {
parent = null;
} else {
targetImpl = parent;
if (isActivationEvent && activationTarget === null && targetImpl._hasActivationBehavior) {
activationTarget = targetImpl;
}
appendToEventPath(eventImpl, parent, targetImpl, relatedTarget, touchTargets, slotInClosedTree);
}
if (parent !== null) {
parent = getEventTargetParent(parent, eventImpl);
}
slotInClosedTree = false;
}
let clearTargetsStructIndex = -1;
for (let i = eventImpl._path.length - 1; i >= 0 && clearTargetsStructIndex === -1; i--) {
if (eventImpl._path[i].target !== null) {
clearTargetsStructIndex = i;
}
}
const clearTargetsStruct = eventImpl._path[clearTargetsStructIndex];
clearTargets =
(isNode(clearTargetsStruct.target) && isShadowRoot(nodeRoot(clearTargetsStruct.target))) ||
(isNode(clearTargetsStruct.relatedTarget) && isShadowRoot(nodeRoot(clearTargetsStruct.relatedTarget)));
if (activationTarget !== null && activationTarget._legacyPreActivationBehavior) {
activationTarget._legacyPreActivationBehavior();
}
for (let i = eventImpl._path.length - 1; i >= 0; --i) {
const struct = eventImpl._path[i];
if (struct.target !== null) {
eventImpl.eventPhase = EVENT_PHASE.AT_TARGET;
} else {
eventImpl.eventPhase = EVENT_PHASE.CAPTURING_PHASE;
}
invokeEventListeners(struct, eventImpl, "capturing");
}
for (let i = 0; i < eventImpl._path.length; i++) {
const struct = eventImpl._path[i];
if (struct.target !== null) {
eventImpl.eventPhase = EVENT_PHASE.AT_TARGET;
} else {
if (!eventImpl.bubbles) {
continue;
}
eventImpl.eventPhase = EVENT_PHASE.BUBBLING_PHASE;
}
invokeEventListeners(struct, eventImpl, "bubbling");
}
}
eventImpl.eventPhase = EVENT_PHASE.NONE;
eventImpl.currentTarget = null;
eventImpl._path = [];
eventImpl._dispatchFlag = false;
eventImpl._stopPropagationFlag = false;
eventImpl._stopImmediatePropagationFlag = false;
if (clearTargets) {
eventImpl.target = null;
eventImpl.relatedTarget = null;
}
if (activationTarget !== null) {
if (!eventImpl._canceledFlag) {
activationTarget._activationBehavior();
} else if (activationTarget._legacyCanceledActivationBehavior) {
activationTarget._legacyCanceledActivationBehavior();
}
}
return !eventImpl._canceledFlag;
}
}
module.exports = {
implementation: EventTargetImpl
};
// https://dom.spec.whatwg.org/#concept-event-listener-invoke
function invokeEventListeners(struct, eventImpl, phase) {
const structIndex = eventImpl._path.indexOf(struct);
for (let i = structIndex; i >= 0; i--) {
const t = eventImpl._path[i];
if (t.target) {
eventImpl.target = t.target;
break;
}
}
eventImpl.relatedTarget = idlUtils.wrapperForImpl(struct.relatedTarget);
if (eventImpl._stopPropagationFlag) {
return;
}
eventImpl.currentTarget = idlUtils.wrapperForImpl(struct.item);
const listeners = struct.item._eventListeners;
innerInvokeEventListeners(eventImpl, listeners, phase, struct);
}
// https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke
function innerInvokeEventListeners(eventImpl, listeners, phase) {
let found = false;
const { type, target } = eventImpl;
const wrapper = idlUtils.wrapperForImpl(target);
if (!listeners || !listeners[type]) {
return found;
}
// Copy event listeners before iterating since the list can be modified during the iteration.
const handlers = listeners[type].slice();
for (let i = 0; i < handlers.length; i++) {
const listener = handlers[i];
const { capture, once, passive } = listener.options;
// Check if the event listener has been removed since the listeners has been cloned.
if (!listeners[type].includes(listener)) {
continue;
}
found = true;
if (
(phase === "capturing" && !capture) ||
(phase === "bubbling" && capture)
) {
continue;
}
if (once) {
listeners[type].splice(listeners[type].indexOf(listener), 1);
}
if (passive) {
eventImpl._inPassiveListenerFlag = true;
}
try {
if (typeof listener.callback === "object") {
if (typeof listener.callback.handleEvent === "function") {
listener.callback.handleEvent(idlUtils.wrapperForImpl(eventImpl));
}
} else {
listener.callback.call(eventImpl.currentTarget, idlUtils.wrapperForImpl(eventImpl));
}
} catch (e) {
let window = null;
if (wrapper && wrapper._document) {
// Triggered by Window
window = wrapper;
} else if (target._ownerDocument) {
// Triggered by most webidl2js'ed instances
window = target._ownerDocument._defaultView;
} else if (wrapper._ownerDocument) {
// Currently triggered by some non-webidl2js things
window = wrapper._ownerDocument._defaultView;
}
if (window) {
reportException(window, e);
}
// Errors in window-less documents just get swallowed... can you think of anything better?
}
eventImpl._inPassiveListenerFlag = false;
if (eventImpl._stopImmediatePropagationFlag) {
return found;
}
}
return found;
}
/**
* Normalize the event listeners options argument in order to get always a valid options object
* @param {Object} options - user defined options
* @param {Array} defaultBoolKeys - boolean properties that should belong to the options object
* @returns {Object} object containing at least the "defaultBoolKeys"
*/
function normalizeEventHandlerOptions(options, defaultBoolKeys) {
const returnValue = {};
// no need to go further here
if (typeof options === "boolean" || options === null || typeof options === "undefined") {
returnValue.capture = Boolean(options);
return returnValue;
}
// non objects options so we typecast its value as "capture" value
if (typeof options !== "object") {
returnValue.capture = Boolean(options);
// at this point we don't need to loop the "capture" key anymore
defaultBoolKeys = defaultBoolKeys.filter(k => k !== "capture");
}
for (const key of defaultBoolKeys) {
returnValue[key] = Boolean(options[key]);
}
return returnValue;
}
// https://dom.spec.whatwg.org/#concept-event-path-append
function appendToEventPath(eventImpl, target, targetOverride, relatedTarget, touchTargets, slotInClosedTree) {
const itemInShadowTree = isNode(target) && isShadowRoot(nodeRoot(target));
const rootOfClosedTree = isShadowRoot(target) && target.mode === "closed";
eventImpl._path.push({
item: target,
itemInShadowTree,
target: targetOverride,
relatedTarget,
touchTargets,
rootOfClosedTree,
slotInClosedTree
});
}
|