This repository has been archived by the owner on Sep 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
/
patch-events.js
473 lines (441 loc) · 13.9 KB
/
patch-events.js
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/**
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
import * as utils from './utils.js';
import * as nativeMethods from './native-methods.js';
/*
Make this name unique so it is unlikely to conflict with properties on objects passed to `addEventListener`
https://github.com/webcomponents/shadydom/issues/173
*/
const /** string */ eventWrappersName = `__eventWrappers${Date.now()}`;
// https://github.com/w3c/webcomponents/issues/513#issuecomment-224183937
let alwaysComposed = {
'blur': true,
'focus': true,
'focusin': true,
'focusout': true,
'click': true,
'dblclick': true,
'mousedown': true,
'mouseenter': true,
'mouseleave': true,
'mousemove': true,
'mouseout': true,
'mouseover': true,
'mouseup': true,
'wheel': true,
'beforeinput': true,
'input': true,
'keydown': true,
'keyup': true,
'compositionstart': true,
'compositionupdate': true,
'compositionend': true,
'touchstart': true,
'touchend': true,
'touchmove': true,
'touchcancel': true,
'pointerover': true,
'pointerenter': true,
'pointerdown': true,
'pointermove': true,
'pointerup': true,
'pointercancel': true,
'pointerout': true,
'pointerleave': true,
'gotpointercapture': true,
'lostpointercapture': true,
'dragstart': true,
'drag': true,
'dragenter': true,
'dragleave': true,
'dragover': true,
'drop': true,
'dragend': true,
'DOMActivate': true,
'DOMFocusIn': true,
'DOMFocusOut': true,
'keypress': true
};
function pathComposer(startNode, composed) {
let composedPath = [];
let current = startNode;
let startRoot = startNode === window ? window : startNode.getRootNode();
while (current) {
composedPath.push(current);
if (current.assignedSlot) {
current = current.assignedSlot;
} else if (current.nodeType === Node.DOCUMENT_FRAGMENT_NODE && current.host && (composed || current !== startRoot)) {
current = current.host;
} else {
current = current.parentNode;
}
}
// event composedPath includes window when startNode's ownerRoot is document
if (composedPath[composedPath.length - 1] === document) {
composedPath.push(window);
}
return composedPath;
}
function retarget(refNode, path) {
if (!utils.isShadyRoot) {
return refNode;
}
// If ANCESTOR's root is not a shadow root or ANCESTOR's root is BASE's
// shadow-including inclusive ancestor, return ANCESTOR.
let refNodePath = pathComposer(refNode, true);
let p$ = path;
for (let i=0, ancestor, lastRoot, root, rootIdx; i < p$.length; i++) {
ancestor = p$[i];
root = ancestor === window ? window : ancestor.getRootNode();
if (root !== lastRoot) {
rootIdx = refNodePath.indexOf(root);
lastRoot = root;
}
if (!utils.isShadyRoot(root) || rootIdx > -1) {
return ancestor;
}
}
}
let eventMixin = {
/**
* @this {Event}
*/
get composed() {
// isTrusted may not exist in this browser, so just check if isTrusted is explicitly false
if (this.isTrusted !== false && this.__composed === undefined) {
this.__composed = alwaysComposed[this.type];
}
return this.__composed || false;
},
/**
* @this {Event}
*/
composedPath() {
if (!this.__composedPath) {
this.__composedPath = pathComposer(this['__target'], this.composed);
}
return this.__composedPath;
},
/**
* @this {Event}
*/
get target() {
return retarget(this.currentTarget, this.composedPath());
},
// http://w3c.github.io/webcomponents/spec/shadow/#event-relatedtarget-retargeting
/**
* @this {Event}
*/
get relatedTarget() {
if (!this.__relatedTarget) {
return null;
}
if (!this.__relatedTargetComposedPath) {
this.__relatedTargetComposedPath = pathComposer(this.__relatedTarget, true);
}
// find the deepest node in relatedTarget composed path that is in the same root with the currentTarget
return retarget(this.currentTarget, this.__relatedTargetComposedPath);
},
/**
* @this {Event}
*/
stopPropagation() {
Event.prototype.stopPropagation.call(this);
this.__propagationStopped = true;
},
/**
* @this {Event}
*/
stopImmediatePropagation() {
Event.prototype.stopImmediatePropagation.call(this);
this.__immediatePropagationStopped = true;
this.__propagationStopped = true;
}
};
function mixinComposedFlag(Base) {
// NOTE: avoiding use of `class` here so that transpiled output does not
// try to do `Base.call` with a dom construtor.
let klazz = function(type, options) {
let event = new Base(type, options);
event.__composed = options && Boolean(options['composed']);
return event;
}
// put constructor properties on subclass
utils.mixin(klazz, Base);
klazz.prototype = Base.prototype;
return klazz;
}
let nonBubblingEventsToRetarget = {
'focus': true,
'blur': true
};
function fireHandlers(event, node, phase) {
let hs = node.__handlers && node.__handlers[event.type] &&
node.__handlers[event.type][phase];
if (hs) {
for (let i = 0, fn; (fn = hs[i]); i++) {
if (event.target === event.relatedTarget) {
return;
}
fn.call(node, event);
if (event.__immediatePropagationStopped) {
return;
}
}
}
}
function retargetNonBubblingEvent(e) {
let path = e.composedPath();
let node;
// override `currentTarget` to let patched `target` calculate correctly
Object.defineProperty(e, 'currentTarget', {
get: function() {
return node;
},
configurable: true
});
for (let i = path.length - 1; i >= 0; i--) {
node = path[i];
// capture phase fires all capture handlers
fireHandlers(e, node, 'capture');
if (e.__propagationStopped) {
return;
}
}
// set the event phase to `AT_TARGET` as in spec
Object.defineProperty(e, 'eventPhase', {get() { return Event.AT_TARGET }});
// the event only needs to be fired when owner roots change when iterating the event path
// keep track of the last seen owner root
let lastFiredRoot;
for (let i = 0; i < path.length; i++) {
node = path[i];
const root = node.__shady && node.__shady.root;
if (i === 0 || (root && root === lastFiredRoot)) {
fireHandlers(e, node, 'bubble');
// don't bother with window, it doesn't have `getRootNode` and will be last in the path anyway
if (node !== window) {
lastFiredRoot = node.getRootNode();
}
if (e.__propagationStopped) {
return;
}
}
}
}
function listenerSettingsEqual(savedListener, node, type, capture, once, passive) {
let {
node: savedNode,
type: savedType,
capture: savedCapture,
once: savedOnce,
passive: savedPassive
} = savedListener;
return node === savedNode &&
type === savedType &&
capture === savedCapture &&
once === savedOnce &&
passive === savedPassive;
}
export function findListener(wrappers, node, type, capture, once, passive) {
for (let i = 0; i < wrappers.length; i++) {
if (listenerSettingsEqual(wrappers[i], node, type, capture, once, passive)) {
return i;
}
}
return -1;
}
/**
* Firefox can throw on accessing eventWrappers inside of `removeEventListener` during a selenium run
* Try/Catch accessing eventWrappers to work around
* https://bugzilla.mozilla.org/show_bug.cgi?id=1353074
*/
function getEventWrappers(eventLike) {
let wrappers = null;
try {
wrappers = eventLike[eventWrappersName];
} catch (e) {} // eslint-disable-line no-empty
return wrappers;
}
/**
* @this {Event}
*/
export function addEventListener(type, fnOrObj, optionsOrCapture) {
if (!fnOrObj) {
return;
}
// The callback `fn` might be used for multiple nodes/events. Since we generate
// a wrapper function, we need to keep track of it when we remove the listener.
// It's more efficient to store the node/type/options information as Array in
// `fn` itself rather than the node (we assume that the same callback is used
// for few nodes at most, whereas a node will likely have many event listeners).
// NOTE(valdrin) invoking external functions is costly, inline has better perf.
let capture, once, passive;
if (typeof optionsOrCapture === 'object') {
capture = Boolean(optionsOrCapture.capture);
once = Boolean(optionsOrCapture.once);
passive = Boolean(optionsOrCapture.passive);
} else {
capture = Boolean(optionsOrCapture);
once = false;
passive = false;
}
// hack to let ShadyRoots have event listeners
// event listener will be on host, but `currentTarget`
// will be set to shadyroot for event listener
let target = (optionsOrCapture && optionsOrCapture.__shadyTarget) || this;
let wrappers = fnOrObj[eventWrappersName];
if (wrappers) {
// Stop if the wrapper function has already been created.
if (findListener(wrappers, target, type, capture, once, passive) > -1) {
return;
}
} else {
fnOrObj[eventWrappersName] = [];
}
/**
* @this {HTMLElement}
*/
const wrapperFn = function(e) {
// Support `once` option.
if (once) {
this.removeEventListener(type, fnOrObj, optionsOrCapture);
}
if (!e['__target']) {
patchEvent(e);
}
let lastCurrentTargetDesc;
if (target !== this) {
// replace `currentTarget` to make `target` and `relatedTarget` correct for inside the shadowroot
lastCurrentTargetDesc = Object.getOwnPropertyDescriptor(e, 'currentTarget');
Object.defineProperty(e, 'currentTarget', {get() { return target }, configurable: true});
}
// There are two critera that should stop events from firing on this node
// 1. the event is not composed and the current node is not in the same root as the target
// 2. when bubbling, if after retargeting, relatedTarget and target point to the same node
if (e.composed || e.composedPath().indexOf(target) > -1) {
if (e.target === e.relatedTarget) {
if (e.eventPhase === Event.BUBBLING_PHASE) {
e.stopImmediatePropagation();
}
return;
}
// prevent non-bubbling events from triggering bubbling handlers on shadowroot, but only if not in capture phase
if (e.eventPhase !== Event.CAPTURING_PHASE && !e.bubbles && e.target !== target) {
return;
}
let ret = (typeof fnOrObj === 'object' && fnOrObj.handleEvent) ?
fnOrObj.handleEvent(e) :
fnOrObj.call(target, e);
if (target !== this) {
// replace the "correct" `currentTarget`
if (lastCurrentTargetDesc) {
Object.defineProperty(e, 'currentTarget', lastCurrentTargetDesc);
lastCurrentTargetDesc = null;
} else {
delete e['currentTarget'];
}
}
return ret;
}
};
// Store the wrapper information.
fnOrObj[eventWrappersName].push({
node: this,
type: type,
capture: capture,
once: once,
passive: passive,
wrapperFn: wrapperFn
});
if (nonBubblingEventsToRetarget[type]) {
this.__handlers = this.__handlers || {};
this.__handlers[type] = this.__handlers[type] ||
{'capture': [], 'bubble': []};
this.__handlers[type][capture ? 'capture' : 'bubble'].push(wrapperFn);
} else {
let ael = this instanceof Window ? nativeMethods.windowAddEventListener :
nativeMethods.addEventListener;
ael.call(this, type, wrapperFn, optionsOrCapture);
}
}
/**
* @this {Event}
*/
export function removeEventListener(type, fnOrObj, optionsOrCapture) {
if (!fnOrObj) {
return;
}
// NOTE(valdrin) invoking external functions is costly, inline has better perf.
let capture, once, passive;
if (typeof optionsOrCapture === 'object') {
capture = Boolean(optionsOrCapture.capture);
once = Boolean(optionsOrCapture.once);
passive = Boolean(optionsOrCapture.passive);
} else {
capture = Boolean(optionsOrCapture);
once = false;
passive = false;
}
let target = (optionsOrCapture && optionsOrCapture.__shadyTarget) || this;
// Search the wrapped function.
let wrapperFn = undefined;
let wrappers = getEventWrappers(fnOrObj);
if (wrappers) {
let idx = findListener(wrappers, target, type, capture, once, passive);
if (idx > -1) {
wrapperFn = wrappers.splice(idx, 1)[0].wrapperFn;
// Cleanup.
if (!wrappers.length) {
fnOrObj[eventWrappersName] = undefined;
}
}
}
let rel = this instanceof Window ? nativeMethods.windowRemoveEventListener :
nativeMethods.removeEventListener;
rel.call(this, type, wrapperFn || fnOrObj, optionsOrCapture);
if (wrapperFn && nonBubblingEventsToRetarget[type] &&
this.__handlers && this.__handlers[type]) {
const arr = this.__handlers[type][capture ? 'capture' : 'bubble'];
const idx = arr.indexOf(wrapperFn);
if (idx > -1) {
arr.splice(idx, 1);
}
}
}
function activateFocusEventOverrides() {
for (let ev in nonBubblingEventsToRetarget) {
window.addEventListener(ev, function(e) {
if (!e['__target']) {
patchEvent(e);
retargetNonBubblingEvent(e);
}
}, true);
}
}
function patchEvent(event) {
event['__target'] = event.target;
event.__relatedTarget = event.relatedTarget;
// patch event prototype if we can
if (utils.settings.hasDescriptors) {
utils.patchPrototype(event, eventMixin);
// and fallback to patching instance
} else {
utils.extend(event, eventMixin);
}
}
let PatchedEvent = mixinComposedFlag(window.Event);
let PatchedCustomEvent = mixinComposedFlag(window.CustomEvent);
let PatchedMouseEvent = mixinComposedFlag(window.MouseEvent);
export function patchEvents() {
window.Event = PatchedEvent;
window.CustomEvent = PatchedCustomEvent;
window.MouseEvent = PatchedMouseEvent;
activateFocusEventOverrides();
}