pseudo-dom 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +134 -48
- package/browser/pseudo-dom.js +233 -126
- package/browser/pseudo-dom.min.js +1 -1
- package/dist/classes/PseudoEventListener.d.ts +25 -25
- package/dist/classes/PseudoEventListener.js +37 -34
- package/dist/classes/PseudoEventListener.min.js +1 -1
- package/dist/interfaces/PseudoEventTarget.d.ts +2 -2
- package/dist/services/ElementService.d.ts +1 -0
- package/dist/services/ElementService.js +11 -4
- package/dist/services/ElementService.min.js +1 -1
- package/dist/services/EventService.d.ts +16 -4
- package/dist/services/EventService.js +59 -31
- package/dist/services/EventService.min.js +1 -1
- package/dist/services/EventTargetService.d.ts +41 -9
- package/dist/services/EventTargetService.js +121 -52
- package/dist/services/EventTargetService.min.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,7 +7,10 @@ Mock the DOM for server side-side DOM state and in tests.
|
|
|
7
7
|
Pseudo DOM recreates the browser DOM API (following the MDN documentation) so DOM-dependent code can run in Node, for
|
|
8
8
|
example in tests, without a real or headless browser. It is written in TypeScript and ships type definitions.
|
|
9
9
|
|
|
10
|
-
Working today: `EventService` (Event)
|
|
10
|
+
Working today: `EventService` (Event) and `EventTargetService` (EventTarget), which dispatch an event through the tree the
|
|
11
|
+
way the DOM does (capture down from the root, the target, then bubbling back up, with `stopPropagation`,
|
|
12
|
+
`stopImmediatePropagation`, `once`, `passive`, `preventDefault` and the default action of the target; clicking a submit
|
|
13
|
+
button sends a `submit` event to its form), `NodeService`
|
|
11
14
|
(Node: a real tree with `parentNode`, `previousSibling` / `nextSibling`, `firstChild` / `lastChild`, `appendChild`,
|
|
12
15
|
`insertBefore`, `removeChild`, `replaceChild`, `contains` and `getRootNode`; nodes move when they are added somewhere
|
|
13
16
|
else, document fragments insert their children, and a node cannot be put inside itself), `ElementService` /
|
|
@@ -15,8 +18,7 @@ else, document fragments insert their children, and a node cannot be put inside
|
|
|
15
18
|
`DOMTokenListService`, `NamedNodeMapService`, `DocumentService` / `DocumentFragmentService`, `PseudoNodeList`, and
|
|
16
19
|
`generateDocument` for creating a document.
|
|
17
20
|
|
|
18
|
-
Not implemented yet (these throw a "not implemented" error or are missing):
|
|
19
|
-
target and bubble phases), `cloneNode`, `compareDocumentPosition`, `isEqualNode`, `querySelector` /
|
|
21
|
+
Not implemented yet (these throw a "not implemented" error or are missing): `cloneNode`, `compareDocumentPosition`, `isEqualNode`, `querySelector` /
|
|
20
22
|
`querySelectorAll`, `innerHTML` / `outerHTML` parsing, and most of the rest of the Element and Document APIs. The API
|
|
21
23
|
will change before 1.0.
|
|
22
24
|
## Modules
|
|
@@ -40,7 +42,10 @@ will change before 1.0.
|
|
|
40
42
|
<dd><p>Simulate the behaviour of the HTMLElement Class when there is no DOM available.</p>
|
|
41
43
|
</dd>
|
|
42
44
|
<dt><a href="#EventTargetService">EventTargetService</a></dt>
|
|
43
|
-
<dd><p>Simulate the behaviour of the EventTarget Class when there is no DOM available
|
|
45
|
+
<dd><p>Simulate the behaviour of the EventTarget Class when there is no DOM available.
|
|
46
|
+
Dispatching an event sends it through the tree the way the DOM does: down from the root to the target (capture
|
|
47
|
+
listeners), to the target itself, then back up to the root (the listeners which are not capture listeners, when the
|
|
48
|
+
event bubbles).</p>
|
|
44
49
|
</dd>
|
|
45
50
|
<dt><a href="#EventService">EventService</a></dt>
|
|
46
51
|
<dd><p>Simulate the behaviour of the Event Class when there is no DOM available.</p>
|
|
@@ -312,6 +317,9 @@ Simulate the HTMLElement object when the Dom is not available
|
|
|
312
317
|
|
|
313
318
|
## EventTargetService
|
|
314
319
|
Simulate the behaviour of the EventTarget Class when there is no DOM available.
|
|
320
|
+
Dispatching an event sends it through the tree the way the DOM does: down from the root to the target (capture
|
|
321
|
+
listeners), to the target itself, then back up to the root (the listeners which are not capture listeners, when the
|
|
322
|
+
event bubbles).
|
|
315
323
|
|
|
316
324
|
**Kind**: global class
|
|
317
325
|
**Author**: Joshua Heagle <joshuaheagle@gmail.com>
|
|
@@ -327,8 +335,12 @@ Simulate the behaviour of the EventTarget Class when there is no DOM available.
|
|
|
327
335
|
|
|
328
336
|
* [EventTargetService](#EventTargetService)
|
|
329
337
|
* [.listenersFor(type)](#EventTargetService+listenersFor) ⇒ <code>LinkedList</code>
|
|
330
|
-
* [.runEvents(event)](#EventTargetService+runEvents) ⇒ <code
|
|
338
|
+
* [.runEvents(event)](#EventTargetService+runEvents) ⇒ <code>Array.<\*></code>
|
|
339
|
+
* [.removeListener(type, listener)](#EventTargetService+removeListener)
|
|
331
340
|
* [.setDefaultEvent(type, callback)](#EventTargetService+setDefaultEvent)
|
|
341
|
+
* [.addEventListener(type, callback, [useCapture])](#EventTargetService+addEventListener)
|
|
342
|
+
* [.removeEventListener(type, callback, [options])](#EventTargetService+removeEventListener)
|
|
343
|
+
* [.dispatchEvent(event)](#EventTargetService+dispatchEvent) ⇒ <code>boolean</code>
|
|
332
344
|
|
|
333
345
|
<a name="EventTargetService+listenersFor"></a>
|
|
334
346
|
|
|
@@ -343,17 +355,30 @@ The listeners registered for a type of event, creating the (empty) list of them
|
|
|
343
355
|
|
|
344
356
|
<a name="EventTargetService+runEvents"></a>
|
|
345
357
|
|
|
346
|
-
### eventTargetService.runEvents(event) ⇒ <code
|
|
347
|
-
Run
|
|
348
|
-
|
|
349
|
-
and listeners
|
|
358
|
+
### eventTargetService.runEvents(event) ⇒ <code>Array.<\*></code>
|
|
359
|
+
Run the listeners registered on this target for the type of the event which apply to the phase the event is in
|
|
360
|
+
(at the target, the capture listeners run before the others). Listeners which are added while this runs do not run
|
|
361
|
+
for this event, and listeners which are removed while it runs no longer do. Running stops as soon as immediate
|
|
362
|
+
propagation is stopped. A listener which throws does not stop the others.
|
|
363
|
+
|
|
364
|
+
**Kind**: instance method of [<code>EventTargetService</code>](#EventTargetService)
|
|
365
|
+
**Returns**: <code>Array.<\*></code> - The errors which the listeners threw
|
|
366
|
+
|
|
367
|
+
| Param | Type | Description |
|
|
368
|
+
| --- | --- | --- |
|
|
369
|
+
| event | [<code>EventService</code>](#EventService) | The event, which is at a phase and has a current target |
|
|
370
|
+
|
|
371
|
+
<a name="EventTargetService+removeListener"></a>
|
|
372
|
+
|
|
373
|
+
### eventTargetService.removeListener(type, listener)
|
|
374
|
+
Take a listener out of the registered listeners, so that it does not run again.
|
|
350
375
|
|
|
351
376
|
**Kind**: instance method of [<code>EventTargetService</code>](#EventTargetService)
|
|
352
|
-
**Returns**: <code>\*</code> - true when there was nothing registered, otherwise the last value returned from a handler (null when none ran)
|
|
353
377
|
|
|
354
378
|
| Param | Type |
|
|
355
379
|
| --- | --- |
|
|
356
|
-
|
|
|
380
|
+
| type | <code>string</code> |
|
|
381
|
+
| listener | [<code>PseudoEventListener</code>](#PseudoEventListener) |
|
|
357
382
|
|
|
358
383
|
<a name="EventTargetService+setDefaultEvent"></a>
|
|
359
384
|
|
|
@@ -367,6 +392,54 @@ Register the function to run when nothing else has prevented the default for thi
|
|
|
367
392
|
| type | <code>string</code> |
|
|
368
393
|
| callback | <code>function</code> |
|
|
369
394
|
|
|
395
|
+
<a name="EventTargetService+addEventListener"></a>
|
|
396
|
+
|
|
397
|
+
### eventTargetService.addEventListener(type, callback, [useCapture])
|
|
398
|
+
Registers an event handler of a specific event type. Adding the same handler again for the same type and phase does
|
|
399
|
+
nothing, like the DOM.
|
|
400
|
+
|
|
401
|
+
**Kind**: instance method of [<code>EventTargetService</code>](#EventTargetService)
|
|
402
|
+
|
|
403
|
+
| Param | Type | Default | Description |
|
|
404
|
+
| --- | --- | --- | --- |
|
|
405
|
+
| type | <code>string</code> | | The type of event to listen for |
|
|
406
|
+
| callback | <code>function</code> \| <code>Object</code> | | The function to call (or an object with a handleEvent function) |
|
|
407
|
+
| [useCapture] | <code>Object</code> \| <code>boolean</code> | <code>false</code> | Listen while the event travels down to the target (true), or an object with capture, once and passive |
|
|
408
|
+
|
|
409
|
+
<a name="EventTargetService+removeEventListener"></a>
|
|
410
|
+
|
|
411
|
+
### eventTargetService.removeEventListener(type, callback, [options])
|
|
412
|
+
Removes an event listener, the one which was added with the same type, handler and phase.
|
|
413
|
+
|
|
414
|
+
**Kind**: instance method of [<code>EventTargetService</code>](#EventTargetService)
|
|
415
|
+
|
|
416
|
+
| Param | Type | Default | Description |
|
|
417
|
+
| --- | --- | --- | --- |
|
|
418
|
+
| type | <code>string</code> | | The type of event |
|
|
419
|
+
| callback | <code>function</code> \| <code>Object</code> | | The handler which was added |
|
|
420
|
+
| [options] | <code>Object</code> \| <code>boolean</code> | <code>false</code> | Whether the listener was a capture listener (true), or an object with capture |
|
|
421
|
+
|
|
422
|
+
<a name="EventTargetService+dispatchEvent"></a>
|
|
423
|
+
|
|
424
|
+
### eventTargetService.dispatchEvent(event) ⇒ <code>boolean</code>
|
|
425
|
+
Dispatches an event to this target and through the tree: capture listeners of the ancestors from the root down,
|
|
426
|
+
then the listeners of this target, then (when the event bubbles) the other listeners of the ancestors from the
|
|
427
|
+
parent up to the root. stopPropagation() stops it reaching further targets, stopImmediatePropagation() also stops
|
|
428
|
+
the remaining listeners of the current target. Afterwards, unless the default was prevented, the default action
|
|
429
|
+
of this target (see setDefaultEvent) runs. The event can be dispatched again afterwards.
|
|
430
|
+
|
|
431
|
+
**Kind**: instance method of [<code>EventTargetService</code>](#EventTargetService)
|
|
432
|
+
**Returns**: <code>boolean</code> - False when the event was cancelable and a listener prevented the default, otherwise true
|
|
433
|
+
**Throws**:
|
|
434
|
+
|
|
435
|
+
- <code>Error</code> When the event is already being dispatched, or (after the whole dispatch has finished) the error
|
|
436
|
+
which a listener threw (an error with all of them in its errors property when several did)
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
| Param | Type | Description |
|
|
440
|
+
| --- | --- | --- |
|
|
441
|
+
| event | [<code>EventService</code>](#EventService) | The event to dispatch |
|
|
442
|
+
|
|
370
443
|
<a name="EventService"></a>
|
|
371
444
|
|
|
372
445
|
## EventService
|
|
@@ -412,9 +485,9 @@ Simulate the behaviour of the Event Class when there is no DOM available.
|
|
|
412
485
|
| --- | --- | --- |
|
|
413
486
|
| typeArg | <code>string</code> | |
|
|
414
487
|
| [eventOptions] | <code>Object</code> | <code>{}</code> |
|
|
415
|
-
| [eventOptions.bubbles] | <code>boolean</code> | <code>
|
|
416
|
-
| [eventOptions.cancelable] | <code>boolean</code> | <code>
|
|
417
|
-
| [eventOptions.composed] | <code>boolean</code> | <code>
|
|
488
|
+
| [eventOptions.bubbles] | <code>boolean</code> | <code>false</code> |
|
|
489
|
+
| [eventOptions.cancelable] | <code>boolean</code> | <code>false</code> |
|
|
490
|
+
| [eventOptions.composed] | <code>boolean</code> | <code>false</code> |
|
|
418
491
|
|
|
419
492
|
<a name="EventService+inner"></a>
|
|
420
493
|
|
|
@@ -1258,22 +1331,52 @@ Handle events as they are stored and implemented.
|
|
|
1258
1331
|
|
|
1259
1332
|
|
|
1260
1333
|
* [PseudoEventListener](#PseudoEventListener)
|
|
1334
|
+
* [new PseudoEventListener(eventType, [options], handleEvent, [originalCallback])](#new_PseudoEventListener_new)
|
|
1261
1335
|
* [.callback](#PseudoEventListener+callback)
|
|
1336
|
+
* [.capture](#PseudoEventListener+capture)
|
|
1337
|
+
* [.passive](#PseudoEventListener+passive)
|
|
1338
|
+
* [.removed](#PseudoEventListener+removed)
|
|
1262
1339
|
* [.handleEvent(event)](#PseudoEventListener+handleEvent) ⇒ <code>\*</code>
|
|
1263
1340
|
* [.doCapturePhase(event)](#PseudoEventListener+doCapturePhase) ⇒ <code>boolean</code>
|
|
1264
1341
|
* [.doTargetPhase(event)](#PseudoEventListener+doTargetPhase) ⇒ <code>boolean</code>
|
|
1265
|
-
* [.doBubblePhase(event)](#PseudoEventListener+doBubblePhase) ⇒ <code>boolean</code>
|
|
1342
|
+
* [.doBubblePhase(event)](#PseudoEventListener+doBubblePhase) ⇒ <code>boolean</code>
|
|
1266
1343
|
* [.skipPhase(event)](#PseudoEventListener+skipPhase) ⇒ <code>boolean</code>
|
|
1267
|
-
* [.
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1344
|
+
* [.rejectEvent(event)](#PseudoEventListener+rejectEvent) ⇒ <code>boolean</code>
|
|
1345
|
+
|
|
1346
|
+
<a name="new_PseudoEventListener_new"></a>
|
|
1347
|
+
|
|
1348
|
+
### new PseudoEventListener(eventType, [options], handleEvent, [originalCallback])
|
|
1349
|
+
|
|
1350
|
+
| Param | Type | Default | Description |
|
|
1351
|
+
| --- | --- | --- | --- |
|
|
1352
|
+
| eventType | <code>string</code> | | The type of event this listens for |
|
|
1353
|
+
| [options] | <code>Object</code> | | The capture, once and passive options |
|
|
1354
|
+
| handleEvent | <code>function</code> | | The function which is called with the event, already bound to what it should run as |
|
|
1355
|
+
| [originalCallback] | <code>function</code> | <code>handleEvent</code> | The function (or object) which was given when registering, used to find this listener again |
|
|
1271
1356
|
|
|
1272
1357
|
<a name="PseudoEventListener+callback"></a>
|
|
1273
1358
|
|
|
1274
1359
|
### pseudoEventListener.callback
|
|
1275
1360
|
The function (or object with handleEvent) which was originally given when registering, used to find this listener again for removal.
|
|
1276
1361
|
|
|
1362
|
+
**Kind**: instance property of [<code>PseudoEventListener</code>](#PseudoEventListener)
|
|
1363
|
+
<a name="PseudoEventListener+capture"></a>
|
|
1364
|
+
|
|
1365
|
+
### pseudoEventListener.capture
|
|
1366
|
+
Whether this listener listens in the capture phase (and at the target) rather than in the bubble phase.
|
|
1367
|
+
|
|
1368
|
+
**Kind**: instance property of [<code>PseudoEventListener</code>](#PseudoEventListener)
|
|
1369
|
+
<a name="PseudoEventListener+passive"></a>
|
|
1370
|
+
|
|
1371
|
+
### pseudoEventListener.passive
|
|
1372
|
+
Whether the listener promises not to prevent the default (preventDefault does nothing while it runs).
|
|
1373
|
+
|
|
1374
|
+
**Kind**: instance property of [<code>PseudoEventListener</code>](#PseudoEventListener)
|
|
1375
|
+
<a name="PseudoEventListener+removed"></a>
|
|
1376
|
+
|
|
1377
|
+
### pseudoEventListener.removed
|
|
1378
|
+
Whether this listener has been removed, a removed listener does not run even if the event already started.
|
|
1379
|
+
|
|
1277
1380
|
**Kind**: instance property of [<code>PseudoEventListener</code>](#PseudoEventListener)
|
|
1278
1381
|
<a name="PseudoEventListener+handleEvent"></a>
|
|
1279
1382
|
|
|
@@ -1287,6 +1390,8 @@ The function (or object with handleEvent) which was originally given when regist
|
|
|
1287
1390
|
<a name="PseudoEventListener+doCapturePhase"></a>
|
|
1288
1391
|
|
|
1289
1392
|
### pseudoEventListener.doCapturePhase(event) ⇒ <code>boolean</code>
|
|
1393
|
+
A capture listener runs while the event travels down to the target.
|
|
1394
|
+
|
|
1290
1395
|
**Kind**: instance method of [<code>PseudoEventListener</code>](#PseudoEventListener)
|
|
1291
1396
|
|
|
1292
1397
|
| Param | Type |
|
|
@@ -1296,6 +1401,8 @@ The function (or object with handleEvent) which was originally given when regist
|
|
|
1296
1401
|
<a name="PseudoEventListener+doTargetPhase"></a>
|
|
1297
1402
|
|
|
1298
1403
|
### pseudoEventListener.doTargetPhase(event) ⇒ <code>boolean</code>
|
|
1404
|
+
Every listener of the target itself runs, capture listeners first.
|
|
1405
|
+
|
|
1299
1406
|
**Kind**: instance method of [<code>PseudoEventListener</code>](#PseudoEventListener)
|
|
1300
1407
|
|
|
1301
1408
|
| Param | Type |
|
|
@@ -1304,7 +1411,9 @@ The function (or object with handleEvent) which was originally given when regist
|
|
|
1304
1411
|
|
|
1305
1412
|
<a name="PseudoEventListener+doBubblePhase"></a>
|
|
1306
1413
|
|
|
1307
|
-
### pseudoEventListener.doBubblePhase(event) ⇒ <code>boolean</code>
|
|
1414
|
+
### pseudoEventListener.doBubblePhase(event) ⇒ <code>boolean</code>
|
|
1415
|
+
A listener which is not a capture listener runs while the event travels back up (when it bubbles).
|
|
1416
|
+
|
|
1308
1417
|
**Kind**: instance method of [<code>PseudoEventListener</code>](#PseudoEventListener)
|
|
1309
1418
|
|
|
1310
1419
|
| Param | Type |
|
|
@@ -1320,36 +1429,13 @@ The function (or object with handleEvent) which was originally given when regist
|
|
|
1320
1429
|
| --- | --- |
|
|
1321
1430
|
| event | <code>PseudoEvent</code> |
|
|
1322
1431
|
|
|
1323
|
-
<a name="PseudoEventListener+skipDefault"></a>
|
|
1324
|
-
|
|
1325
|
-
### pseudoEventListener.skipDefault(event) ⇒ <code>boolean</code> \| <code>\*</code>
|
|
1326
|
-
**Kind**: instance method of [<code>PseudoEventListener</code>](#PseudoEventListener)
|
|
1327
|
-
|
|
1328
|
-
| Param | Type |
|
|
1329
|
-
| --- | --- |
|
|
1330
|
-
| event | <code>PseudoEvent</code> |
|
|
1331
|
-
|
|
1332
|
-
<a name="PseudoEventListener+stopPropagation"></a>
|
|
1333
|
-
|
|
1334
|
-
### pseudoEventListener.stopPropagation(event) ⇒ <code>boolean</code>
|
|
1335
|
-
**Kind**: instance method of [<code>PseudoEventListener</code>](#PseudoEventListener)
|
|
1336
|
-
|
|
1337
|
-
| Param | Type |
|
|
1338
|
-
| --- | --- |
|
|
1339
|
-
| event | <code>PseudoEvent</code> |
|
|
1340
|
-
|
|
1341
|
-
<a name="PseudoEventListener+nonPassiveHalt"></a>
|
|
1342
|
-
|
|
1343
|
-
### pseudoEventListener.nonPassiveHalt(event) ⇒ <code>boolean</code> \| <code>\*</code>
|
|
1344
|
-
**Kind**: instance method of [<code>PseudoEventListener</code>](#PseudoEventListener)
|
|
1345
|
-
|
|
1346
|
-
| Param | Type |
|
|
1347
|
-
| --- | --- |
|
|
1348
|
-
| event | <code>PseudoEvent</code> |
|
|
1349
|
-
|
|
1350
1432
|
<a name="PseudoEventListener+rejectEvent"></a>
|
|
1351
1433
|
|
|
1352
|
-
### pseudoEventListener.rejectEvent(event) ⇒ <code
|
|
1434
|
+
### pseudoEventListener.rejectEvent(event) ⇒ <code>boolean</code>
|
|
1435
|
+
Whether this listener should not run for the event as it is now (it was removed, or it is for another phase).
|
|
1436
|
+
Stopping propagation is handled by the dispatching, since it stops other targets and not the listeners of the
|
|
1437
|
+
current one.
|
|
1438
|
+
|
|
1353
1439
|
**Kind**: instance method of [<code>PseudoEventListener</code>](#PseudoEventListener)
|
|
1354
1440
|
|
|
1355
1441
|
| Param | Type |
|