pseudo-dom 0.3.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 (54) hide show
  1. package/README.md +774 -0
  2. package/browser/pseudo-dom.js +1266 -260
  3. package/browser/pseudo-dom.min.js +1 -1
  4. package/dist/factories/createEvent.d.ts +28 -0
  5. package/dist/factories/createEvent.js +56 -0
  6. package/dist/factories/createEvent.min.js +1 -0
  7. package/dist/factories/eventDefaults.d.ts +31 -0
  8. package/dist/factories/eventDefaults.js +105 -0
  9. package/dist/factories/eventDefaults.min.js +1 -0
  10. package/dist/factories.d.ts +6 -0
  11. package/dist/factories.js +5 -1
  12. package/dist/factories.min.js +1 -1
  13. package/dist/functions/activeElement.d.ts +14 -0
  14. package/dist/functions/activeElement.js +33 -0
  15. package/dist/functions/activeElement.min.js +1 -0
  16. package/dist/functions/modifierState.d.ts +29 -0
  17. package/dist/functions/modifierState.js +41 -0
  18. package/dist/functions/modifierState.min.js +1 -0
  19. package/dist/main.d.ts +32 -1
  20. package/dist/main.js +66 -1
  21. package/dist/main.min.js +1 -1
  22. package/dist/services/CustomEventService.d.ts +37 -0
  23. package/dist/services/CustomEventService.js +35 -0
  24. package/dist/services/CustomEventService.min.js +1 -0
  25. package/dist/services/ElementService.js +13 -13
  26. package/dist/services/ElementService.min.js +1 -1
  27. package/dist/services/EventService.d.ts +2 -0
  28. package/dist/services/EventService.js +7 -1
  29. package/dist/services/EventService.min.js +1 -1
  30. package/dist/services/FocusEventService.d.ts +32 -0
  31. package/dist/services/FocusEventService.js +35 -0
  32. package/dist/services/FocusEventService.min.js +1 -0
  33. package/dist/services/HTMLElementService.d.ts +20 -0
  34. package/dist/services/HTMLElementService.js +95 -0
  35. package/dist/services/HTMLElementService.min.js +1 -1
  36. package/dist/services/InputEventService.d.ts +41 -0
  37. package/dist/services/InputEventService.js +47 -0
  38. package/dist/services/InputEventService.min.js +1 -0
  39. package/dist/services/KeyboardEventService.d.ts +70 -0
  40. package/dist/services/KeyboardEventService.js +86 -0
  41. package/dist/services/KeyboardEventService.min.js +1 -0
  42. package/dist/services/MouseEventService.d.ts +80 -0
  43. package/dist/services/MouseEventService.js +108 -0
  44. package/dist/services/MouseEventService.min.js +1 -0
  45. package/dist/services/PointerEventService.d.ts +51 -0
  46. package/dist/services/PointerEventService.js +67 -0
  47. package/dist/services/PointerEventService.min.js +1 -0
  48. package/dist/services/UIEventService.d.ts +43 -0
  49. package/dist/services/UIEventService.js +42 -0
  50. package/dist/services/UIEventService.min.js +1 -0
  51. package/dist/simulate.d.ts +32 -0
  52. package/dist/simulate.js +115 -0
  53. package/dist/simulate.min.js +1 -0
  54. package/package.json +1 -1
@@ -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.3.0",
4
+ "version": "0.4.0",
5
5
  "main": "dist/main.js",
6
6
  "types": "dist/main.d.ts",
7
7
  "author": "Joshua Heagle",