-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
events.js
454 lines (405 loc) · 21.9 KB
/
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
"use strict";
const { assert } = require("chai");
const { beforeEach, afterEach, describe, specify } = require("mocha-sugar-free");
const { JSDOM } = require("../../..");
class EventMonitor {
constructor() {
this.atEvents = [];
this.bubbledEvents = [];
this.capturedEvents = [];
this.allEvents = [];
this.handleEvent = function (event) {
this.allEvents.push(event);
switch (event.eventPhase) {
case event.CAPTURING_PHASE:
this.capturedEvents.push(event);
break;
case event.AT_TARGET:
this.atEvents.push(event);
break;
case event.BUBBLING_PHASE:
this.bubbledEvents.push(event);
break;
default:
throw new Error("Unspecified event phase");
}
}.bind(this);
}
}
var _setUp = function() {
var doc = require('../level1/core/files/hc_staff.xml').hc_staff();
var monitor = this.monitor = new EventMonitor();
this.win = doc.defaultView;
this.title = doc.getElementsByTagName("title").item(0);
this.body = doc.getElementsByTagName("body").item(0);
this.plist = doc.getElementsByTagName("p").item(0);
this.event = doc.createEvent("Events");
this._handleEvent = function(type) { return(function(event) { event[type](); monitor.handleEvent(event) }); }
}
var _tearDown = function(xs) {
xs = ['monitor', 'title', 'body', 'plist', 'event', '_handleEvent'].concat(xs ? xs : []);
var self = this;
xs.forEach(function(x){
self[x] = undefined;
delete(self[x]);
})
}
describe("level2/events", () => {
// A document is created using implementation.createDocument and cast to a DocumentEvent interface.
// @author Curt Arnold
// @see http://www.w3.org/TR/DOM-Level-2-Events/events#Events-DocumentEvent
specify('DocumentEvent interface', () => {
var doc = require('../level1/core/files/hc_staff.xml').hc_staff();
assert.ok((doc.createEvent instanceof Function), "should have createEvent function");
});
// A document is created using implementation.createDocument and cast to a EventTarget interface.
// @author Curt Arnold
// @see http://www.w3.org/TR/DOM-Level-2-Events/events#Events-EventTarget
specify('EventTarget interface', () => {
var doc = require('../level1/core/files/hc_staff.xml').hc_staff();
assert.ok((doc instanceof doc.defaultView.EventTarget), 'should be an instance of EventTarget');
});
// An object implementing the Event interface is created by using DocumentEvent.createEvent method with an eventType
// @author Curt Arnold
// @see http://www.w3.org/TR/DOM-Level-2-Events/events#Events-DocumentEvent-createEvent
specify('create event with each event type', () => {
var doc = require('../level1/core/files/hc_staff.xml').hc_staff(),
event_types = {'Events': doc.defaultView.Event,
'UIEvents': doc.defaultView.UIEvent,
'MouseEvents': doc.defaultView.MouseEvent ,
'HTMLEvents': doc.defaultView.Event};
for (var type in event_types) {
var event = doc.createEvent(type);
assert.notEqual(event, null, "should not be null for " + type);
assert.ok((event instanceof event_types[type]),"should be instanceof " + type);
}
});
// @author Curt Arnold
// @see http://www.w3.org/TR/DOM-Level-2-Events/events#Events-EventTarget-dispatchEvent
// @see http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-17189187
describe('dispatch event', () => {
beforeEach(() => {
this.doc = require('../level1/core/files/hc_staff.xml').hc_staff();
});
afterEach(() => {
_tearDown.call(this, 'doc');
});
specify('a null reference passed to dispatchEvent', () => {
var doc = this.doc;
assert.throws(function(){ doc.dispatchEvent(null) }, TypeError);
// TODO: figure out the best way to test (exception.code == 0) and (exception.message == 'Null event')
});
specify('a created but not initialized event passed to dispatchEvent', () => {
var doc = this.doc,
event_types = ['Events', 'UIEvents', 'MouseEvents', 'HTMLEvents'];
event_types.forEach(function(type){
assert.throwsDomException(function(){ doc.dispatchEvent(doc.createEvent(type)) }, doc, 'InvalidStateError');
})
});
// An EventListener registered on the target node with capture false, should receive any event fired on that node.
specify('EventListener with capture false', () => {
var monitor = new EventMonitor();
this.doc.addEventListener("foo", monitor.handleEvent, false);
var event = this.doc.createEvent("Events");
event.initEvent("foo",true,false);
assert.strictEqual(event.eventPhase, 0);
assert.strictEqual(event.currentTarget, null);
this.doc.dispatchEvent(event);
assert.equal(monitor.atEvents.length, 1, 'should receive atEvent');
assert.equal(monitor.bubbledEvents.length, 0, 'should not receive at bubble phase');
assert.equal(monitor.capturedEvents.length, 0, 'should not receive at capture phase');
assert.strictEqual(event.eventPhase, 0);
assert.strictEqual(event.currentTarget, null);
});
// An EventListener registered on the target node with capture true, should receive any event fired on that node.
specify('EventListener with capture true', () => {
var monitor = new EventMonitor();
this.doc.addEventListener("foo", monitor.handleEvent, true);
var event = this.doc.createEvent("Events");
event.initEvent("foo",true,false);
assert.strictEqual(event.eventPhase, 0);
assert.strictEqual(event.currentTarget, null);
this.doc.dispatchEvent(event);
assert.equal(monitor.atEvents.length, 1, 'should receive atEvent');
assert.equal(monitor.bubbledEvents.length, 0, 'should not receive at bubble phase');
assert.equal(monitor.capturedEvents.length, 0, 'should not receive at capture phase');
assert.strictEqual(event.eventPhase, 0);
assert.strictEqual(event.currentTarget, null);
});
// The same monitor is registered twice and an event is dispatched. The monitor should receive only one handleEvent call.
specify('EventListener is registered twice', () => {
var monitor = new EventMonitor();
this.doc.addEventListener("foo", monitor.handleEvent, false);
this.doc.addEventListener("foo", monitor.handleEvent, false);
var event = this.doc.createEvent("Events");
event.initEvent("foo",true,false);
this.doc.dispatchEvent(event);
assert.equal(monitor.atEvents.length, 1, 'should receive atEvent only once');
assert.equal(monitor.bubbledEvents.length, 0, 'should not receive at bubble phase');
assert.equal(monitor.capturedEvents.length, 0, 'should not receive at capture phase');
});
// The same monitor is registered twice, removed once, and an event is dispatched. The monitor should receive only no handleEvent calls.
specify('EventListener is registered twice, removed once', () => {
var monitor = new EventMonitor();
this.doc.addEventListener("foo", monitor.handleEvent, false);
this.doc.addEventListener("foo", monitor.handleEvent, false);
this.doc.removeEventListener("foo", monitor.handleEvent, false);
var event = this.doc.createEvent("Events");
event.initEvent("foo",true,false);
this.doc.dispatchEvent(event);
assert.equal(monitor.allEvents.length, 0, 'should not receive any handleEvent calls');
});
// A monitor is added, multiple calls to removeEventListener are made with similar but not identical arguments, and an event is dispatched.
// The monitor should receive handleEvent calls.
specify('EventListener is registered, other listeners (similar but not identical) are removed', () => {
var monitor = new EventMonitor();
var other = {handleEvent: function(){}}
this.doc.addEventListener("foo", monitor.handleEvent, false);
this.doc.removeEventListener("foo", monitor.handleEvent, true);
this.doc.removeEventListener("food", monitor.handleEvent, false);
this.doc.removeEventListener("foo", other.handleEvent, false);
var event = this.doc.createEvent("Events");
event.initEvent("foo",true,false);
this.doc.dispatchEvent(event);
assert.equal(monitor.allEvents.length, 1, 'should still receive the handleEvent call');
});
// Two listeners are registered on the same target, each of which will remove both itself and the other on the first event. Only one should see the event since event listeners can never be invoked after being removed.
specify('two EventListeners which both handle by unregistering itself and the other', () => {
// setup
var es = [];
var ls = [];
var EventListener1 = function() { ls.push(this); }
var EventListener2 = function() { ls.push(this); }
EventListener1.prototype.handleEvent = function(event) { _handleEvent(event); }
EventListener2.prototype.handleEvent = function(event) { _handleEvent(event); }
var _handleEvent = function(event) {
es.push(event);
ls.forEach(function(l){
event.currentTarget.removeEventListener("foo", l.handleEvent, false);
})
}
// test
var listener1 = new EventListener1();
var listener2 = new EventListener2();
this.doc.addEventListener("foo", listener1.handleEvent, false);
this.doc.addEventListener("foo", listener2.handleEvent, false);
var event = this.doc.createEvent("Events");
event.initEvent("foo",true,false);
this.doc.dispatchEvent(event);
assert.equal(es.length, 1, 'should only be handled by one EventListener');
});
})
// The Event.initEvent method is called for event returned by DocumentEvent.createEvent("Events")
// The state is checked to see if it reflects the parameters.
// @author Curt Arnold
// @see http://www.w3.org/TR/DOM-Level-2-Events/events#Events-Event-initEvent
describe('init event', () => {
beforeEach(() => {
var doc = require('../level1/core/files/hc_staff.xml').hc_staff();
this._events = ['Events'].map(function(t){ return(doc.createEvent(t)); })
});
afterEach(() => {
_tearDown.call(this, '_events');
});
specify('set state from params, bubble no cancel', () => {
this._events.forEach(function(event){
assert.notEqual(event, null, 'event should not be null for ' + event.eventType);
event.initEvent('rotate', true, false);
assert.equal(event.type, 'rotate', 'event type should be \"rotate\" for ' + event.eventType);
assert.equal(event.bubbles, true, 'event should bubble for ' + event.eventType);
assert.equal(event.cancelable, false, 'event should not be cancelable for ' + event.eventType);
})
});
specify('set state from params, cancel no bubble', () => {
this._events.forEach(function(event){
assert.notEqual(event, null, 'event should not be null for' + event.eventType);
event.initEvent('rotate', false, true);
assert.equal(event.type, 'rotate', 'event type should be \"rotate\" for ' + event.eventType);
assert.equal(event.bubbles, false, 'event should not bubble for ' + event.eventType);
assert.equal(event.cancelable, true, 'event should be cancelable for ' + event.eventType);
})
});
specify('initEvent called multiple times, final time is definitive', () => {
this._events.forEach(function(event){
assert.notEqual(event, null, 'event should not be null for ' + event.eventType);
// rotate
event.initEvent("rotate", true, true);
assert.equal(event.type, 'rotate', 'event type should be \"rotate\" for ' + event.eventType);
assert.equal(event.bubbles, true, 'event should bubble for ' + event.eventType);
assert.equal(event.cancelable, true, 'event should be cancelable for ' + event.eventType);
// shear
event.initEvent("shear", false, false);
assert.equal(event.type, 'shear', 'event type should be \"shear\" for ' + event.eventType);
assert.equal(event.bubbles, false, 'event should not bubble for ' + event.eventType);
assert.equal(event.cancelable, false, 'event should not be cancelable for ' + event.eventType);
})
});
})
describe('capture event', () => {
beforeEach(() => {
_setUp.call(this);
this.event.initEvent("foo",true,false);
});
afterEach(() => {
_tearDown.call(this);
});
specify('all capturing listeners in a direct line from dispatched node will receive the event', () => {
this.plist.addEventListener("foo", this.monitor.handleEvent, true);
this.plist.firstChild.addEventListener("foo", this.monitor.handleEvent, false);
this.plist.firstChild.dispatchEvent(this.event);
assert.equal(this.monitor.atEvents.length, 1, 'should be at 1 event');
assert.equal(this.monitor.bubbledEvents.length, 0, 'should not have any bubbled events');
assert.equal(this.monitor.capturedEvents.length, 1, 'should have captured 1 event');
});
specify('only capture listeners in a direct line from target to the document node should receive the event', () => {
var self = this;
this.title.addEventListener("foo", this.monitor.handleEvent, true);
this.plist.addEventListener("foo", function(event) { event.preventDefault(); self.monitor.handleEvent(event) }, false);
this.plist.firstChild.addEventListener("foo", this.monitor.handleEvent, false);
var return_val = this.plist.firstChild.dispatchEvent(this.event);
assert.equal(return_val, true, 'dispatchEvent should return *true*');
assert.equal(this.monitor.atEvents.length, 1, 'should be at 1 event');
assert.equal(this.monitor.bubbledEvents.length, 1, 'should have bubbled 1 event');
assert.equal(this.monitor.capturedEvents.length, 0, 'should not have captured any events');
});
})
describe('bubble event', () => {
beforeEach(() => {
_setUp.call(this);
this.event.initEvent("foo",true,false);
});
afterEach(() => {
_tearDown.call(this);
});
specify('all non-capturing listeners in a direct line from dispatched node will receive a bubbling event', () => {
this.win.addEventListener("foo", this.monitor.handleEvent, false);
this.plist.addEventListener("foo", this.monitor.handleEvent, false);
this.plist.firstChild.addEventListener("foo", this.monitor.handleEvent, false);
this.plist.firstChild.dispatchEvent(this.event);
assert.equal(this.monitor.atEvents.length, 1, 'should be at 1 event');
assert.equal(this.monitor.bubbledEvents.length, 2, 'should have 2 bubbled events');
assert.equal(this.monitor.capturedEvents.length, 0, 'should not have any captured events');
});
specify('only bubble listeners in a direct line from target to the document node should receive the event', () => {
this.win.addEventListener("foo", this.monitor.handleEvent, false);
this.title.addEventListener("foo", this.monitor.handleEvent, false);
this.plist.addEventListener("foo", this._handleEvent('preventDefault'), true);
this.plist.firstChild.addEventListener("foo", this.monitor.handleEvent, false);
var return_val = this.plist.firstChild.dispatchEvent(this.event);
assert.equal(return_val, true, 'dispatchEvent should return *true*');
assert.equal(this.monitor.atEvents.length, 1, 'should be at 1 event');
assert.equal(this.monitor.bubbledEvents.length, 1, 'should have 1 bubbled event');
assert.equal(this.monitor.capturedEvents.length, 1, 'should have captured 1 event');
});
specify('if an event does not bubble, bubble listeners should not receive the event', () => {
this.win.addEventListener("foo", this.monitor.handleEvent, false);
this.body.addEventListener("foo", this.monitor.handleEvent, true);
this.plist.addEventListener("foo", this._handleEvent('preventDefault'), false);
this.plist.firstChild.addEventListener("foo", this.monitor.handleEvent, false);
this.event.initEvent("foo",false,false);
var return_val = this.plist.firstChild.dispatchEvent(this.event);
assert.equal(return_val, true, 'dispatchEvent should return *true*');
assert.equal(this.monitor.atEvents.length, 1, 'should be at 1 event');
assert.equal(this.monitor.bubbledEvents.length, 0, 'should not have any bubbled events');
assert.equal(this.monitor.capturedEvents.length, 1, 'should have captured 1 event');
});
})
describe('stop propagation', () => {
beforeEach(() => {
_setUp.call(this);
this.event.initEvent("foo",true,false);
});
afterEach(() => {
_tearDown.call(this);
});
specify('should prevent the target from receiving the event', () => {
this.win.addEventListener("foo", this.monitor.handleEvent, false);
this.plist.addEventListener("foo", this._handleEvent('stopPropagation'), true);
this.plist.firstChild.addEventListener("foo", this.monitor.handleEvent, false);
this.plist.firstChild.dispatchEvent(this.event);
assert.equal(this.monitor.atEvents.length, 0, 'should be at 0 events');
assert.equal(this.monitor.bubbledEvents.length, 0, 'should have no bubbled events');
assert.equal(this.monitor.capturedEvents.length, 1, 'should have 1 captured event');
});
specify('should prevent all listeners from receiving the event', () => {
this.win.addEventListener("foo", this.monitor.handleEvent, false);
this.body.addEventListener("foo", this.monitor.handleEvent, false);
this.plist.addEventListener("foo", this._handleEvent('stopPropagation'), false);
this.plist.firstChild.addEventListener("foo", this.monitor.handleEvent, false);
this.plist.firstChild.dispatchEvent(this.event);
assert.equal(this.monitor.atEvents.length, 1, 'should be at 1 event');
assert.equal(this.monitor.bubbledEvents.length, 1, 'should have 1 bubbled event');
assert.equal(this.monitor.capturedEvents.length, 0, 'should have no captured events');
});
specify('stopPropagation should not prevent listeners on the same element from receiving the event', () => {
this.win.addEventListener("foo", this.monitor.handleEvent, false);
this.body.addEventListener("foo", this.monitor.handleEvent, false);
this.plist.addEventListener("foo", this._handleEvent('stopPropagation'), true);
this.plist.addEventListener("foo", this._handleEvent('stopPropagation'), false);
this.plist.addEventListener("foo", this.monitor.handleEvent, true);
this.plist.addEventListener("foo", this.monitor.handleEvent, false);
this.plist.dispatchEvent(this.event);
assert.equal(this.monitor.atEvents.length, 2, 'should be at 2 events'); // Changed from 4 to 2 after https://github.com/whatwg/dom/commit/98564fc5284439d2555f545fa04288e230a37a03
assert.equal(this.monitor.bubbledEvents.length, 0, 'should have no bubbled events');
assert.equal(this.monitor.capturedEvents.length, 0, 'should have no captured events');
});
})
describe('prevent default', () => {
beforeEach(() => {
_setUp.call(this);
this.event.initEvent("foo",true,true);
});
afterEach(() => {
_tearDown.call(this);
});
specify('the defaultPrevented flag is set when the event is prevented', () => {
this.title.addEventListener("foo", function(event) { event.preventDefault();}, false);
var return_val = this.title.dispatchEvent(this.event);
assert.equal(this.event.defaultPrevented, true, 'the defaultPrevented flag should be true when the event is prevented');
});
specify('the defaultPrevented flag is not set when the event is not prevented', () => {
this.title.addEventListener("foo", this.monitor.handleEvent, false);
var return_val = this.title.dispatchEvent(this.event);
assert.equal(this.event.defaultPrevented, false, 'the defaultPrevented flag should be false when the event is not prevented');
});
specify('a cancelable event can have its default event disabled', () => {
this.body.addEventListener("foo", this.monitor.handleEvent, true);
this.plist.addEventListener("foo", this._handleEvent('preventDefault'), false);
this.plist.firstChild.addEventListener("foo", this.monitor.handleEvent, false);
var return_val = this.plist.firstChild.dispatchEvent(this.event);
assert.equal(return_val, false, 'dispatchEvent should return *false*');
assert.equal(this.monitor.atEvents.length, 1, 'should be at 1 event');
assert.equal(this.monitor.bubbledEvents.length, 1, 'should have bubbled 1 event');
assert.equal(this.monitor.capturedEvents.length, 1, 'should have captured 1 event');
});
specify('a non-cancelable event cannot have its default event disabled', () => {
this.body.addEventListener("foo", this.monitor.handleEvent, true);
this.plist.addEventListener("foo", this._handleEvent('preventDefault'), false);
this.plist.firstChild.addEventListener("foo", this.monitor.handleEvent, false);
this.event.initEvent("foo",true,false);
var return_val = this.plist.firstChild.dispatchEvent(this.event);
assert.equal(return_val, true, 'dispatchEvent should return *true*');
assert.equal(this.monitor.atEvents.length, 1, 'should be at 1 event');
assert.equal(this.monitor.bubbledEvents.length, 1, 'should have bubbled 1 event');
assert.equal(this.monitor.capturedEvents.length, 1, 'should have captured 1 event');
});
});
specify('remove listener in handler', () => {
const { document } = (new JSDOM()).window;
let h1 = 0, h2 = 0;
document.addEventListener("click", function handler1() {
// Event handler that removes itself
h1++;
document.removeEventListener("click", handler1);
});
document.addEventListener("click", () => h2++);
const ev = document.createEvent("MouseEvents");
ev.initEvent("click", true, true);
document.dispatchEvent(ev);
assert.strictEqual(h1, 1, "handler1 must be called once");
assert.strictEqual(h2, 1, "handler2 must be called once");
document.dispatchEvent(ev);
assert.strictEqual(h1, 1, "handler1 must be called once");
assert.strictEqual(h2, 2, "handler2 must be called twice");
});
});