pseudo-dom 0.2.0 → 0.4.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.
Files changed (62) hide show
  1. package/README.md +903 -43
  2. package/browser/pseudo-dom.js +1489 -376
  3. package/browser/pseudo-dom.min.js +1 -1
  4. package/dist/classes/PseudoEventListener.d.ts +25 -25
  5. package/dist/classes/PseudoEventListener.js +37 -34
  6. package/dist/classes/PseudoEventListener.min.js +1 -1
  7. package/dist/factories/createEvent.d.ts +28 -0
  8. package/dist/factories/createEvent.js +56 -0
  9. package/dist/factories/createEvent.min.js +1 -0
  10. package/dist/factories/eventDefaults.d.ts +31 -0
  11. package/dist/factories/eventDefaults.js +105 -0
  12. package/dist/factories/eventDefaults.min.js +1 -0
  13. package/dist/factories.d.ts +6 -0
  14. package/dist/factories.js +5 -1
  15. package/dist/factories.min.js +1 -1
  16. package/dist/functions/activeElement.d.ts +14 -0
  17. package/dist/functions/activeElement.js +33 -0
  18. package/dist/functions/activeElement.min.js +1 -0
  19. package/dist/functions/modifierState.d.ts +29 -0
  20. package/dist/functions/modifierState.js +41 -0
  21. package/dist/functions/modifierState.min.js +1 -0
  22. package/dist/interfaces/PseudoEventTarget.d.ts +2 -2
  23. package/dist/main.d.ts +32 -1
  24. package/dist/main.js +66 -1
  25. package/dist/main.min.js +1 -1
  26. package/dist/services/CustomEventService.d.ts +37 -0
  27. package/dist/services/CustomEventService.js +35 -0
  28. package/dist/services/CustomEventService.min.js +1 -0
  29. package/dist/services/ElementService.d.ts +1 -0
  30. package/dist/services/ElementService.js +17 -10
  31. package/dist/services/ElementService.min.js +1 -1
  32. package/dist/services/EventService.d.ts +18 -4
  33. package/dist/services/EventService.js +65 -31
  34. package/dist/services/EventService.min.js +1 -1
  35. package/dist/services/EventTargetService.d.ts +41 -9
  36. package/dist/services/EventTargetService.js +121 -52
  37. package/dist/services/EventTargetService.min.js +1 -1
  38. package/dist/services/FocusEventService.d.ts +32 -0
  39. package/dist/services/FocusEventService.js +35 -0
  40. package/dist/services/FocusEventService.min.js +1 -0
  41. package/dist/services/HTMLElementService.d.ts +20 -0
  42. package/dist/services/HTMLElementService.js +95 -0
  43. package/dist/services/HTMLElementService.min.js +1 -1
  44. package/dist/services/InputEventService.d.ts +41 -0
  45. package/dist/services/InputEventService.js +47 -0
  46. package/dist/services/InputEventService.min.js +1 -0
  47. package/dist/services/KeyboardEventService.d.ts +70 -0
  48. package/dist/services/KeyboardEventService.js +86 -0
  49. package/dist/services/KeyboardEventService.min.js +1 -0
  50. package/dist/services/MouseEventService.d.ts +80 -0
  51. package/dist/services/MouseEventService.js +108 -0
  52. package/dist/services/MouseEventService.min.js +1 -0
  53. package/dist/services/PointerEventService.d.ts +51 -0
  54. package/dist/services/PointerEventService.js +67 -0
  55. package/dist/services/PointerEventService.min.js +1 -0
  56. package/dist/services/UIEventService.d.ts +43 -0
  57. package/dist/services/UIEventService.js +42 -0
  58. package/dist/services/UIEventService.min.js +1 -0
  59. package/dist/simulate.d.ts +32 -0
  60. package/dist/simulate.js +115 -0
  61. package/dist/simulate.min.js +1 -0
  62. package/package.json +1 -1
@@ -0,0 +1,86 @@
1
+ 'use strict'
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true
5
+ })
6
+ exports.KeyboardEventService = void 0
7
+ /**
8
+ * @file Substitute for the DOM KeyboardEvent Class.
9
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
10
+ * @version 1.0.0
11
+ */
12
+ const UIEventService_1 = require('./UIEventService')
13
+ const modifierState_1 = require('../functions/modifierState')
14
+ /**
15
+ * Simulate the behaviour of the KeyboardEvent Class when there is no DOM available.
16
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
17
+ * @class
18
+ * @augments UIEventService
19
+ * @property {string} key
20
+ * @property {string} code
21
+ * @property {number} location
22
+ * @property {boolean} repeat
23
+ * @property {boolean} isComposing
24
+ */
25
+ class KeyboardEventService extends UIEventService_1.UIEventService {
26
+ /**
27
+ * @param {string} [typeArg=''] The type of the event
28
+ * @param {KeyboardEventInit} [init={}] The options for the event
29
+ * @constructor
30
+ */
31
+ constructor (typeArg = '', init = {}) {
32
+ super(typeArg, init)
33
+ this.keyValue = init.key || ''
34
+ this.keyCode = init.code || ''
35
+ this.keyLocation = init.location || 0
36
+ this.held = !!init.repeat
37
+ this.composing = !!init.isComposing
38
+ this.modifiers = (0, modifierState_1.modifierKeys)(init)
39
+ }
40
+
41
+ get key () {
42
+ return this.keyValue
43
+ }
44
+
45
+ get code () {
46
+ return this.keyCode
47
+ }
48
+
49
+ get location () {
50
+ return this.keyLocation
51
+ }
52
+
53
+ get repeat () {
54
+ return this.held
55
+ }
56
+
57
+ get isComposing () {
58
+ return this.composing
59
+ }
60
+
61
+ get ctrlKey () {
62
+ return this.modifiers.ctrlKey
63
+ }
64
+
65
+ get shiftKey () {
66
+ return this.modifiers.shiftKey
67
+ }
68
+
69
+ get altKey () {
70
+ return this.modifiers.altKey
71
+ }
72
+
73
+ get metaKey () {
74
+ return this.modifiers.metaKey
75
+ }
76
+
77
+ /**
78
+ * Whether a modifier key was held down when the event happened.
79
+ * @param {string} key Control, Shift, Alt or Meta
80
+ * @returns {boolean}
81
+ */
82
+ getModifierState (key) {
83
+ return (0, modifierState_1.modifierState)(this.modifiers, key)
84
+ }
85
+ }
86
+ exports.KeyboardEventService = KeyboardEventService
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.KeyboardEventService=void 0;const UIEventService_1=require("./UIEventService"),modifierState_1=require("../functions/modifierState");class KeyboardEventService extends UIEventService_1.UIEventService{constructor(e="",t={}){super(e,t),this.keyValue=t.key||"",this.keyCode=t.code||"",this.keyLocation=t.location||0,this.held=!!t.repeat,this.composing=!!t.isComposing,this.modifiers=(0,modifierState_1.modifierKeys)(t)}get key(){return this.keyValue}get code(){return this.keyCode}get location(){return this.keyLocation}get repeat(){return this.held}get isComposing(){return this.composing}get ctrlKey(){return this.modifiers.ctrlKey}get shiftKey(){return this.modifiers.shiftKey}get altKey(){return this.modifiers.altKey}get metaKey(){return this.modifiers.metaKey}getModifierState(e){return(0,modifierState_1.modifierState)(this.modifiers,e)}}exports.KeyboardEventService=KeyboardEventService;
@@ -0,0 +1,80 @@
1
+ /**
2
+ * @file Substitute for the DOM MouseEvent Class.
3
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
4
+ * @version 1.0.0
5
+ */
6
+ import { UIEventInit, UIEventService } from './UIEventService';
7
+ import { PseudoEventTarget } from '../interfaces/PseudoEventTarget';
8
+ /**
9
+ * The options for creating a mouse event, on top of the ones every UI event has.
10
+ * @typedef {Object} MouseEventInit
11
+ * @property {number} [screenX=0]
12
+ * @property {number} [screenY=0]
13
+ * @property {number} [clientX=0]
14
+ * @property {number} [clientY=0]
15
+ * @property {boolean} [ctrlKey=false]
16
+ * @property {boolean} [shiftKey=false]
17
+ * @property {boolean} [altKey=false]
18
+ * @property {boolean} [metaKey=false]
19
+ * @property {number} [button=0] The button which changed (0 is the main button)
20
+ * @property {number} [buttons=0] The buttons which are down
21
+ * @property {PseudoEventTarget|null} [relatedTarget=null] The other target involved (the one the mouse came from or went to)
22
+ */
23
+ export type MouseEventInit = UIEventInit & {
24
+ screenX?: number;
25
+ screenY?: number;
26
+ clientX?: number;
27
+ clientY?: number;
28
+ ctrlKey?: boolean;
29
+ shiftKey?: boolean;
30
+ altKey?: boolean;
31
+ metaKey?: boolean;
32
+ button?: number;
33
+ buttons?: number;
34
+ relatedTarget?: PseudoEventTarget | null;
35
+ };
36
+ /**
37
+ * Simulate the behaviour of the MouseEvent Class when there is no DOM available.
38
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
39
+ * @class
40
+ * @augments UIEventService
41
+ * @property {number} screenX
42
+ * @property {number} screenY
43
+ * @property {number} clientX
44
+ * @property {number} clientY
45
+ * @property {number} button
46
+ * @property {number} buttons
47
+ * @property {PseudoEventTarget|null} relatedTarget
48
+ */
49
+ export declare class MouseEventService extends UIEventService {
50
+ private readonly position;
51
+ private readonly modifiers;
52
+ private readonly buttonPressed;
53
+ private readonly buttonsDown;
54
+ private readonly related;
55
+ /**
56
+ * @param {string} [typeArg=''] The type of the event
57
+ * @param {MouseEventInit} [init={}] The options for the event
58
+ * @constructor
59
+ */
60
+ constructor(typeArg?: string, init?: MouseEventInit);
61
+ get screenX(): number;
62
+ get screenY(): number;
63
+ get clientX(): number;
64
+ get clientY(): number;
65
+ get x(): number;
66
+ get y(): number;
67
+ get ctrlKey(): boolean;
68
+ get shiftKey(): boolean;
69
+ get altKey(): boolean;
70
+ get metaKey(): boolean;
71
+ get button(): number;
72
+ get buttons(): number;
73
+ get relatedTarget(): PseudoEventTarget | null;
74
+ /**
75
+ * Whether a modifier key was held down when the event happened.
76
+ * @param {string} key Control, Shift, Alt or Meta
77
+ * @returns {boolean}
78
+ */
79
+ getModifierState(key: string): boolean;
80
+ }
@@ -0,0 +1,108 @@
1
+ 'use strict'
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true
5
+ })
6
+ exports.MouseEventService = void 0
7
+ /**
8
+ * @file Substitute for the DOM MouseEvent Class.
9
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
10
+ * @version 1.0.0
11
+ */
12
+ const UIEventService_1 = require('./UIEventService')
13
+ const modifierState_1 = require('../functions/modifierState')
14
+ /**
15
+ * Simulate the behaviour of the MouseEvent Class when there is no DOM available.
16
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
17
+ * @class
18
+ * @augments UIEventService
19
+ * @property {number} screenX
20
+ * @property {number} screenY
21
+ * @property {number} clientX
22
+ * @property {number} clientY
23
+ * @property {number} button
24
+ * @property {number} buttons
25
+ * @property {PseudoEventTarget|null} relatedTarget
26
+ */
27
+ class MouseEventService extends UIEventService_1.UIEventService {
28
+ /**
29
+ * @param {string} [typeArg=''] The type of the event
30
+ * @param {MouseEventInit} [init={}] The options for the event
31
+ * @constructor
32
+ */
33
+ constructor (typeArg = '', init = {}) {
34
+ super(typeArg, init)
35
+ this.position = {
36
+ screenX: init.screenX || 0,
37
+ screenY: init.screenY || 0,
38
+ clientX: init.clientX || 0,
39
+ clientY: init.clientY || 0
40
+ }
41
+ this.modifiers = (0, modifierState_1.modifierKeys)(init)
42
+ this.buttonPressed = init.button || 0
43
+ this.buttonsDown = init.buttons || 0
44
+ this.related = init.relatedTarget || null
45
+ }
46
+
47
+ get screenX () {
48
+ return this.position.screenX
49
+ }
50
+
51
+ get screenY () {
52
+ return this.position.screenY
53
+ }
54
+
55
+ get clientX () {
56
+ return this.position.clientX
57
+ }
58
+
59
+ get clientY () {
60
+ return this.position.clientY
61
+ }
62
+
63
+ get x () {
64
+ return this.position.clientX
65
+ }
66
+
67
+ get y () {
68
+ return this.position.clientY
69
+ }
70
+
71
+ get ctrlKey () {
72
+ return this.modifiers.ctrlKey
73
+ }
74
+
75
+ get shiftKey () {
76
+ return this.modifiers.shiftKey
77
+ }
78
+
79
+ get altKey () {
80
+ return this.modifiers.altKey
81
+ }
82
+
83
+ get metaKey () {
84
+ return this.modifiers.metaKey
85
+ }
86
+
87
+ get button () {
88
+ return this.buttonPressed
89
+ }
90
+
91
+ get buttons () {
92
+ return this.buttonsDown
93
+ }
94
+
95
+ get relatedTarget () {
96
+ return this.related
97
+ }
98
+
99
+ /**
100
+ * Whether a modifier key was held down when the event happened.
101
+ * @param {string} key Control, Shift, Alt or Meta
102
+ * @returns {boolean}
103
+ */
104
+ getModifierState (key) {
105
+ return (0, modifierState_1.modifierState)(this.modifiers, key)
106
+ }
107
+ }
108
+ exports.MouseEventService = MouseEventService
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.MouseEventService=void 0;const UIEventService_1=require("./UIEventService"),modifierState_1=require("../functions/modifierState");class MouseEventService extends UIEventService_1.UIEventService{constructor(e="",t={}){super(e,t),this.position={screenX:t.screenX||0,screenY:t.screenY||0,clientX:t.clientX||0,clientY:t.clientY||0},this.modifiers=(0,modifierState_1.modifierKeys)(t),this.buttonPressed=t.button||0,this.buttonsDown=t.buttons||0,this.related=t.relatedTarget||null}get screenX(){return this.position.screenX}get screenY(){return this.position.screenY}get clientX(){return this.position.clientX}get clientY(){return this.position.clientY}get x(){return this.position.clientX}get y(){return this.position.clientY}get ctrlKey(){return this.modifiers.ctrlKey}get shiftKey(){return this.modifiers.shiftKey}get altKey(){return this.modifiers.altKey}get metaKey(){return this.modifiers.metaKey}get button(){return this.buttonPressed}get buttons(){return this.buttonsDown}get relatedTarget(){return this.related}getModifierState(e){return(0,modifierState_1.modifierState)(this.modifiers,e)}}exports.MouseEventService=MouseEventService;
@@ -0,0 +1,51 @@
1
+ /**
2
+ * @file Substitute for the DOM PointerEvent Class.
3
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
4
+ * @version 1.0.0
5
+ */
6
+ import { MouseEventInit, MouseEventService } from './MouseEventService';
7
+ /**
8
+ * The options for creating a pointer event, on top of the ones every mouse event has.
9
+ * @typedef {Object} PointerEventInit
10
+ * @property {number} [pointerId=0]
11
+ * @property {number} [width=1]
12
+ * @property {number} [height=1]
13
+ * @property {number} [pressure=0]
14
+ * @property {string} [pointerType=''] mouse, pen or touch
15
+ * @property {boolean} [isPrimary=false]
16
+ */
17
+ export type PointerEventInit = MouseEventInit & {
18
+ pointerId?: number;
19
+ width?: number;
20
+ height?: number;
21
+ pressure?: number;
22
+ pointerType?: string;
23
+ isPrimary?: boolean;
24
+ };
25
+ /**
26
+ * Simulate the behaviour of the PointerEvent Class when there is no DOM available.
27
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
28
+ * @class
29
+ * @augments MouseEventService
30
+ * @property {number} pointerId
31
+ * @property {number} width
32
+ * @property {number} height
33
+ * @property {number} pressure
34
+ * @property {string} pointerType
35
+ * @property {boolean} isPrimary
36
+ */
37
+ export declare class PointerEventService extends MouseEventService {
38
+ private readonly pointer;
39
+ /**
40
+ * @param {string} [typeArg=''] The type of the event
41
+ * @param {PointerEventInit} [init={}] The options for the event
42
+ * @constructor
43
+ */
44
+ constructor(typeArg?: string, init?: PointerEventInit);
45
+ get pointerId(): number;
46
+ get width(): number;
47
+ get height(): number;
48
+ get pressure(): number;
49
+ get pointerType(): string;
50
+ get isPrimary(): boolean;
51
+ }
@@ -0,0 +1,67 @@
1
+ 'use strict'
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true
5
+ })
6
+ exports.PointerEventService = void 0
7
+ /**
8
+ * @file Substitute for the DOM PointerEvent Class.
9
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
10
+ * @version 1.0.0
11
+ */
12
+ const MouseEventService_1 = require('./MouseEventService')
13
+ /**
14
+ * Simulate the behaviour of the PointerEvent Class when there is no DOM available.
15
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
16
+ * @class
17
+ * @augments MouseEventService
18
+ * @property {number} pointerId
19
+ * @property {number} width
20
+ * @property {number} height
21
+ * @property {number} pressure
22
+ * @property {string} pointerType
23
+ * @property {boolean} isPrimary
24
+ */
25
+ class PointerEventService extends MouseEventService_1.MouseEventService {
26
+ /**
27
+ * @param {string} [typeArg=''] The type of the event
28
+ * @param {PointerEventInit} [init={}] The options for the event
29
+ * @constructor
30
+ */
31
+ constructor (typeArg = '', init = {}) {
32
+ super(typeArg, init)
33
+ this.pointer = {
34
+ pointerId: init.pointerId || 0,
35
+ width: typeof init.width === 'number' ? init.width : 1,
36
+ height: typeof init.height === 'number' ? init.height : 1,
37
+ pressure: init.pressure || 0,
38
+ pointerType: init.pointerType || '',
39
+ isPrimary: !!init.isPrimary
40
+ }
41
+ }
42
+
43
+ get pointerId () {
44
+ return this.pointer.pointerId
45
+ }
46
+
47
+ get width () {
48
+ return this.pointer.width
49
+ }
50
+
51
+ get height () {
52
+ return this.pointer.height
53
+ }
54
+
55
+ get pressure () {
56
+ return this.pointer.pressure
57
+ }
58
+
59
+ get pointerType () {
60
+ return this.pointer.pointerType
61
+ }
62
+
63
+ get isPrimary () {
64
+ return this.pointer.isPrimary
65
+ }
66
+ }
67
+ exports.PointerEventService = PointerEventService
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.PointerEventService=void 0;const MouseEventService_1=require("./MouseEventService");class PointerEventService extends MouseEventService_1.MouseEventService{constructor(e="",t={}){super(e,t),this.pointer={pointerId:t.pointerId||0,width:"number"==typeof t.width?t.width:1,height:"number"==typeof t.height?t.height:1,pressure:t.pressure||0,pointerType:t.pointerType||"",isPrimary:!!t.isPrimary}}get pointerId(){return this.pointer.pointerId}get width(){return this.pointer.width}get height(){return this.pointer.height}get pressure(){return this.pointer.pressure}get pointerType(){return this.pointer.pointerType}get isPrimary(){return this.pointer.isPrimary}}exports.PointerEventService=PointerEventService;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * @file Substitute for the DOM UIEvent Class.
3
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
4
+ * @version 1.0.0
5
+ */
6
+ import { EventService } from './EventService';
7
+ /**
8
+ * The options for creating an event, on top of the ones every event has.
9
+ * @typedef {Object} UIEventInit
10
+ * @property {boolean} [bubbles=false]
11
+ * @property {boolean} [cancelable=false]
12
+ * @property {boolean} [composed=false]
13
+ * @property {number} [detail=0] Details about the event, such as how many times the mouse was clicked
14
+ * @property {*} [view=null] The window the event happened in
15
+ */
16
+ export type UIEventInit = {
17
+ bubbles?: boolean;
18
+ cancelable?: boolean;
19
+ composed?: boolean;
20
+ detail?: number;
21
+ view?: any;
22
+ };
23
+ /**
24
+ * Simulate the behaviour of the UIEvent Class when there is no DOM available: the events which come from a user
25
+ * interface (the mouse, the keyboard, focus and input).
26
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
27
+ * @class
28
+ * @augments EventService
29
+ * @property {number} detail
30
+ * @property {*} view
31
+ */
32
+ export declare class UIEventService extends EventService {
33
+ private readonly uiDetail;
34
+ private readonly uiView;
35
+ /**
36
+ * @param {string} [typeArg=''] The type of the event
37
+ * @param {UIEventInit} [init={}] The options for the event
38
+ * @constructor
39
+ */
40
+ constructor(typeArg?: string, init?: UIEventInit);
41
+ get detail(): number;
42
+ get view(): any;
43
+ }
@@ -0,0 +1,42 @@
1
+ 'use strict'
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true
5
+ })
6
+ exports.UIEventService = void 0
7
+ /**
8
+ * @file Substitute for the DOM UIEvent Class.
9
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
10
+ * @version 1.0.0
11
+ */
12
+ const EventService_1 = require('./EventService')
13
+ /**
14
+ * Simulate the behaviour of the UIEvent Class when there is no DOM available: the events which come from a user
15
+ * interface (the mouse, the keyboard, focus and input).
16
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
17
+ * @class
18
+ * @augments EventService
19
+ * @property {number} detail
20
+ * @property {*} view
21
+ */
22
+ class UIEventService extends EventService_1.EventService {
23
+ /**
24
+ * @param {string} [typeArg=''] The type of the event
25
+ * @param {UIEventInit} [init={}] The options for the event
26
+ * @constructor
27
+ */
28
+ constructor (typeArg = '', init = {}) {
29
+ super(typeArg, init)
30
+ this.uiDetail = init.detail || 0
31
+ this.uiView = init.view || null
32
+ }
33
+
34
+ get detail () {
35
+ return this.uiDetail
36
+ }
37
+
38
+ get view () {
39
+ return this.uiView
40
+ }
41
+ }
42
+ exports.UIEventService = UIEventService
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.UIEventService=void 0;const EventService_1=require("./EventService");class UIEventService extends EventService_1.EventService{constructor(e="",t={}){super(e,t),this.uiDetail=t.detail||0,this.uiView=t.view||null}get detail(){return this.uiDetail}get view(){return this.uiView}}exports.UIEventService=UIEventService;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Click an element the way a user does: pointerdown and mousedown, then the focus moves to the nearest element which
3
+ * can have it (or is taken away from the one which had it) unless mousedown was cancelled, then pointerup, mouseup
4
+ * and finally click. Every event is trusted and has the options the browser gives it. A disabled element gets nothing.
5
+ * @function click
6
+ * @param {*} element The element to click
7
+ * @param {Object} [init={}] Options for the events (for example clientX, clientY, shiftKey)
8
+ * @returns {boolean} False when the click was cancelled (or the element is disabled), so its default action did not happen
9
+ */
10
+ export declare const click: (element: any, init?: {
11
+ [option: string]: any;
12
+ }) => boolean;
13
+ /**
14
+ * Press and release a key on an element (the element which has the focus, or one given): keydown and then keyup.
15
+ * @function keyPress
16
+ * @param {*} element The element which gets the key
17
+ * @param {string} key The value of the key, such as a or Enter
18
+ * @param {Object} [init={}] Options for the events (for example code, shiftKey)
19
+ * @returns {boolean} False when keydown was cancelled, so its default action did not happen
20
+ */
21
+ export declare const keyPress: (element: any, key: string, init?: {
22
+ [option: string]: any;
23
+ }) => boolean;
24
+ declare const _default: {
25
+ click: (element: any, init?: {
26
+ [option: string]: any;
27
+ }) => boolean;
28
+ keyPress: (element: any, key: string, init?: {
29
+ [option: string]: any;
30
+ }) => boolean;
31
+ };
32
+ export default _default;
@@ -0,0 +1,115 @@
1
+ 'use strict'
2
+
3
+ const __importDefault = void 0 && (void 0).__importDefault || function (mod) {
4
+ return mod && mod.__esModule
5
+ ? mod
6
+ : {
7
+ default: mod
8
+ }
9
+ }
10
+ Object.defineProperty(exports, '__esModule', {
11
+ value: true
12
+ })
13
+ exports.keyPress = exports.click = void 0
14
+ /**
15
+ * @file Simulate what a user does, with the events the browser sends for it.
16
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
17
+ * @version 1.0.0
18
+ * @module pseudoDom/simulate
19
+ */
20
+ const createEvent_1 = __importDefault(require('./factories/createEvent'))
21
+ const activeElement_1 = require('./functions/activeElement')
22
+ const send = (target, type, init) => target.dispatchEvent((0, createEvent_1.default)(type, init, {
23
+ browser: true,
24
+ trusted: true
25
+ }))
26
+ /**
27
+ * The nearest element (starting with the element itself) which can have the focus.
28
+ * @param {*} element Where to start
29
+ * @returns {*|null}
30
+ */
31
+ const focusableFrom = element => {
32
+ let current = element
33
+ while (current) {
34
+ if (current.canFocus) {
35
+ return current
36
+ }
37
+ current = current.parentNode
38
+ }
39
+ return null
40
+ }
41
+ /**
42
+ * Click an element the way a user does: pointerdown and mousedown, then the focus moves to the nearest element which
43
+ * can have it (or is taken away from the one which had it) unless mousedown was cancelled, then pointerup, mouseup
44
+ * and finally click. Every event is trusted and has the options the browser gives it. A disabled element gets nothing.
45
+ * @function click
46
+ * @param {*} element The element to click
47
+ * @param {Object} [init={}] Options for the events (for example clientX, clientY, shiftKey)
48
+ * @returns {boolean} False when the click was cancelled (or the element is disabled), so its default action did not happen
49
+ */
50
+ const click = (element, init = {}) => {
51
+ if (element.hasAttribute && element.hasAttribute('disabled')) {
52
+ return false
53
+ }
54
+ const pointer = Object.assign({
55
+ pointerId: 1,
56
+ pointerType: 'mouse',
57
+ isPrimary: true,
58
+ button: 0
59
+ }, init)
60
+ send(element, 'pointerdown', Object.assign({
61
+ buttons: 1
62
+ }, pointer))
63
+ if (send(element, 'mousedown', Object.assign({
64
+ buttons: 1,
65
+ detail: 1
66
+ }, init, {
67
+ button: 0
68
+ }))) {
69
+ const focusable = focusableFrom(element)
70
+ if (focusable) {
71
+ focusable.focus()
72
+ } else {
73
+ const active = (0, activeElement_1.getActiveElement)(element.getRootNode())
74
+ if (active) {
75
+ active.blur()
76
+ }
77
+ }
78
+ }
79
+ send(element, 'pointerup', Object.assign({
80
+ buttons: 0
81
+ }, pointer))
82
+ send(element, 'mouseup', Object.assign({
83
+ buttons: 0,
84
+ detail: 1
85
+ }, init, {
86
+ button: 0
87
+ }))
88
+ return send(element, 'click', Object.assign({
89
+ detail: 1
90
+ }, init, {
91
+ button: 0
92
+ }))
93
+ }
94
+ exports.click = click
95
+ /**
96
+ * Press and release a key on an element (the element which has the focus, or one given): keydown and then keyup.
97
+ * @function keyPress
98
+ * @param {*} element The element which gets the key
99
+ * @param {string} key The value of the key, such as a or Enter
100
+ * @param {Object} [init={}] Options for the events (for example code, shiftKey)
101
+ * @returns {boolean} False when keydown was cancelled, so its default action did not happen
102
+ */
103
+ const keyPress = (element, key, init = {}) => {
104
+ const options = Object.assign({
105
+ key
106
+ }, init)
107
+ const proceeded = send(element, 'keydown', options)
108
+ send(element, 'keyup', options)
109
+ return proceeded
110
+ }
111
+ exports.keyPress = keyPress
112
+ exports.default = {
113
+ click: exports.click,
114
+ keyPress: exports.keyPress
115
+ }
@@ -0,0 +1 @@
1
+ "use strict";var __importDefault=function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.keyPress=exports.click=void 0;const createEvent_1=__importDefault(require("./factories/createEvent")),activeElement_1=require("./functions/activeElement"),send=(e,t,s)=>e.dispatchEvent((0,createEvent_1.default)(t,s,{browser:!0,trusted:!0})),focusableFrom=e=>{let t=e;for(;t;){if(t.canFocus)return t;t=t.parentNode}return null},click=(e,t={})=>{if(e.hasAttribute&&e.hasAttribute("disabled"))return!1;const s=Object.assign({pointerId:1,pointerType:"mouse",isPrimary:!0,button:0},t);if(send(e,"pointerdown",Object.assign({buttons:1},s)),send(e,"mousedown",Object.assign({buttons:1,detail:1},t,{button:0}))){const t=focusableFrom(e);if(t)t.focus();else{const t=(0,activeElement_1.getActiveElement)(e.getRootNode());t&&t.blur()}}return send(e,"pointerup",Object.assign({buttons:0},s)),send(e,"mouseup",Object.assign({buttons:0,detail:1},t,{button:0})),send(e,"click",Object.assign({detail:1},t,{button:0}))};exports.click=click;const keyPress=(e,t,s={})=>{const n=Object.assign({key:t},s),r=send(e,"keydown",n);return send(e,"keyup",n),r};exports.keyPress=keyPress,exports.default={click:exports.click,keyPress:exports.keyPress};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pseudo-dom",
3
3
  "description": "Mock the DOM on the server side for managing state and testing",
4
- "version": "0.2.0",
4
+ "version": "0.4.0",
5
5
  "main": "dist/main.js",
6
6
  "types": "dist/main.d.ts",
7
7
  "author": "Joshua Heagle",