pseudo-dom 0.9.0 → 0.10.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.
@@ -1,191 +1,361 @@
1
- /**
2
- * @file Substitute for the DOM Element Class.
3
- * @author Joshua Heagle <joshuaheagle@gmail.com>
4
- * @version 1.0.0
5
- */
6
- import { PseudoNode } from '../interfaces/PseudoNode';
7
- import { PseudoElement } from '../interfaces/PseudoElement';
8
- import { NodeService } from './NodeService';
9
- import { PseudoNamedNodeMap } from '../interfaces/PseudoNamedNodeMap';
10
- import { PseudoDOMTokenList } from '../interfaces/PseudoDOMTokenList';
11
- import { PseudoHTMLCollection } from '../interfaces/PseudoHTMLCollection';
12
- type attribute = {
13
- name: string;
14
- value: any;
15
- };
16
- /**
17
- * Simulate the behaviour of the Element Class when there is no DOM available.
18
- * @author Joshua Heagle <joshuaheagle@gmail.com>
19
- * @class
20
- * @augments PseudoNode
21
- * @property {string} tagName
22
- * @property {string} className
23
- * @property {string} id
24
- * @property {string} innerHtml
25
- * @property {Array} attributes
26
- * @property {function} hasAttribute
27
- * @property {function} setAttribute
28
- * @property {function} getAttribute
29
- * @property {function} removeAttribute
30
- */
31
- export declare class ElementService extends NodeService implements Partial<PseudoElement> {
32
- id: string;
33
- innerHTML: string;
34
- type: string;
35
- private readonly tag;
36
- private readonly attributeList;
37
- private readonly propertyAttributes;
38
- private readonly tokenList;
39
- private defaultEventApplied;
40
- /**
41
- * @param {Object} [settings={}]
42
- * @param {string} [settings.tagName=''] The name of the tag this element represents
43
- * @param {Array<{name: string, value: *}>} [settings.attributes=[]] The attributes (also assigned as properties) to start with
44
- * @param {PseudoNode|null} [settings.parent=null] The node to add this element to as its last child
45
- * @param {Array<PseudoNode>} [settings.children=[]] The nodes to start as children
46
- * @constructor
47
- */
48
- constructor({ tagName, attributes, parent, children }?: {
49
- tagName?: string;
50
- attributes?: Array<attribute>;
51
- parent?: PseudoNode | null;
52
- children?: Array<any>;
53
- });
54
- /**
55
- * The attributes with the values they have now: the ones which are also properties (className, id, style, ...) can
56
- * have been changed through the property, which does not change the stored list.
57
- * @returns {Array<{name: string, value: *}>}
58
- */
59
- private currentAttributes;
60
- get nodeName(): string;
61
- /**
62
- * A copy of this element without its children: the same tag and attributes (the values which are objects, such as
63
- * style, are copied too rather than shared), but not its parent or listeners.
64
- * @returns {ElementService}
65
- */
66
- protected cloneShallow(): NodeService;
67
- /**
68
- * Elements are equal when they have the same tag and the same attributes (in any order), which is what isEqualNode
69
- * checks before it compares the children.
70
- * @param {NodeService} other The element to compare with
71
- * @returns {boolean}
72
- */
73
- protected equalsShallow(other: NodeService): boolean;
74
- get tagName(): string;
75
- get nodeType(): number;
76
- get attributes(): PseudoNamedNodeMap;
77
- get classList(): PseudoDOMTokenList;
78
- get className(): string;
79
- set className(className: string);
80
- /**
81
- * Some elements have default behaviour, this registers it when the element is added.
82
- * @returns {Function}
83
- */
84
- /**
85
- * A live view of this element's element children (text, comments and the like are not included).
86
- * @returns {PseudoHTMLCollection}
87
- */
88
- get children(): PseudoHTMLCollection;
89
- /**
90
- * How many element children this element has.
91
- * @returns {number}
92
- */
93
- get childElementCount(): number;
94
- /**
95
- * The first child of this element which is an element, or null when there is none.
96
- * @returns {PseudoElement|null}
97
- */
98
- get firstElementChild(): PseudoElement | null;
99
- /**
100
- * The last child of this element which is an element, or null when there is none.
101
- * @returns {PseudoElement|null}
102
- */
103
- get lastElementChild(): PseudoElement | null;
104
- /**
105
- * The sibling after this one which is an element, or null when there is none.
106
- * @returns {PseudoElement|null}
107
- */
108
- get nextElementSibling(): PseudoElement | null;
109
- /**
110
- * The sibling before this one which is an element, or null when there is none.
111
- * @returns {PseudoElement|null}
112
- */
113
- get previousElementSibling(): PseudoElement | null;
114
- /**
115
- * Put an element at a position relative to this one: beforebegin (before this element, as its previous sibling),
116
- * afterbegin (as this element's first child), beforeend (as this element's last child) or afterend (after this
117
- * element, as its next sibling).
118
- * @param {string} position beforebegin, afterbegin, beforeend or afterend
119
- * @param {ElementService} element The element to insert
120
- * @returns {ElementService|null} The inserted element, or null when the position needed a parent this element does not have
121
- * @throws {Error} When the position is not one of the four above
122
- */
123
- insertAdjacentElement(position: string, element: PseudoElement): PseudoElement | null;
124
- /**
125
- * Put text at a position relative to this element, the same as insertAdjacentElement but the text becomes a text node.
126
- * @param {string} position beforebegin, afterbegin, beforeend or afterend
127
- * @param {string} text The text to insert
128
- * @throws {Error} When the position is not one of the four above
129
- */
130
- insertAdjacentText(position: string, text: string): void;
131
- /**
132
- * Shared implementation for insertAdjacentElement / insertAdjacentText.
133
- * @param {string} position beforebegin, afterbegin, beforeend or afterend
134
- * @param {PseudoNode|string} node The node (or text) to insert
135
- * @returns {PseudoNode|null} The inserted node, or null when the position needed a parent this element does not have
136
- * @throws {Error} When the position is not one of the four above
137
- */
138
- private insertAdjacent;
139
- /**
140
- * Not implemented yet (HTML parsing is out of scope for now).
141
- * @param {string} position beforebegin, afterbegin, beforeend or afterend
142
- * @param {string} text The markup which would be parsed
143
- * @throws {Error}
144
- */
145
- insertAdjacentHTML(position: string, text: string): void;
146
- /**
147
- * Whether this element itself (not its descendants) matches the given CSS selector.
148
- * @param {string} selectors A CSS selector
149
- * @returns {boolean}
150
- */
151
- matches(selectors: string): boolean;
152
- /**
153
- * The nearest ancestor of this element (starting with this element itself) which matches the CSS selector, or
154
- * null when none of them do.
155
- * @param {string} selectors A CSS selector
156
- * @returns {PseudoElement|null}
157
- */
158
- closest(selectors: string): PseudoElement | null;
159
- applyDefaultEvent(): Function;
160
- /**
161
- * An element which is added as a child gets its default events (for example a submit button submits its form).
162
- * @param {NodeService} child The node which was inserted
163
- */
164
- protected childInserted(child: NodeService): void;
165
- /**
166
- * Check whether the element has an attribute by that name.
167
- * @param {string} attributeName
168
- * @returns {boolean}
169
- */
170
- hasAttribute(attributeName: string): boolean;
171
- /**
172
- * Set the value of an attribute, adding the attribute if it did not exist.
173
- * @param {string} attributeName
174
- * @param {string} attributeValue
175
- * @returns {undefined}
176
- */
177
- setAttribute(attributeName: string, attributeValue: string): void;
178
- /**
179
- * Retrieve the value of an attribute.
180
- * @param {string} attributeName
181
- * @returns {string|null} The value, or null when there is no such attribute
182
- */
183
- getAttribute(attributeName: string): string | null;
184
- /**
185
- * Remove an attribute from the element.
186
- * @param {string} attributeName
187
- * @returns {undefined}
188
- */
189
- removeAttribute(attributeName: string): void;
190
- }
191
- export {};
1
+ /**
2
+ * @file Substitute for the DOM Element Class.
3
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
4
+ * @version 1.0.0
5
+ */
6
+ import { PseudoNode } from '../interfaces/PseudoNode';
7
+ import { PseudoElement } from '../interfaces/PseudoElement';
8
+ import { NodeService } from './NodeService';
9
+ import { PseudoNamedNodeMap } from '../interfaces/PseudoNamedNodeMap';
10
+ import { PseudoDOMTokenList } from '../interfaces/PseudoDOMTokenList';
11
+ import { PseudoHTMLCollection } from '../interfaces/PseudoHTMLCollection';
12
+ import { PseudoShadowRoot } from '../interfaces/PseudoShadowRoot';
13
+ type attribute = {
14
+ name: string;
15
+ value: any;
16
+ };
17
+ type DOMRect = {
18
+ x: number;
19
+ y: number;
20
+ width: number;
21
+ height: number;
22
+ top: number;
23
+ right: number;
24
+ bottom: number;
25
+ left: number;
26
+ };
27
+ /**
28
+ * Simulate the behaviour of the Element Class when there is no DOM available.
29
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
30
+ * @class
31
+ * @augments PseudoNode
32
+ * @property {string} tagName
33
+ * @property {string} className
34
+ * @property {string} id
35
+ * @property {string} innerHtml
36
+ * @property {Array} attributes
37
+ * @property {function} hasAttribute
38
+ * @property {function} setAttribute
39
+ * @property {function} getAttribute
40
+ * @property {function} removeAttribute
41
+ */
42
+ export declare class ElementService extends NodeService implements Partial<PseudoElement> {
43
+ id: string;
44
+ innerHTML: string;
45
+ type: string;
46
+ clientHeight: number;
47
+ clientLeft: number;
48
+ clientTop: number;
49
+ clientWidth: number;
50
+ scrollHeight: number;
51
+ scrollLeft: number;
52
+ scrollTop: number;
53
+ scrollWidth: number;
54
+ /** What getBoundingClientRect() returns - not really computed (there is no layout engine), set this directly. */
55
+ boundingClientRect: DOMRect;
56
+ /** What getClientRects() returns - set this directly. */
57
+ clientRects: Array<DOMRect>;
58
+ /** What getAnimations() returns - set this directly. */
59
+ animations: Array<any>;
60
+ /** What checkVisibility() returns - set this directly. */
61
+ isVisible: boolean;
62
+ private readonly tag;
63
+ private readonly attributeList;
64
+ private readonly propertyAttributes;
65
+ private readonly tokenList;
66
+ private readonly capturedPointers;
67
+ private shadowRootInstance;
68
+ private defaultEventApplied;
69
+ /**
70
+ * @param {Object} [settings={}]
71
+ * @param {string} [settings.tagName=''] The name of the tag this element represents
72
+ * @param {Array<{name: string, value: *}>} [settings.attributes=[]] The attributes (also assigned as properties) to start with
73
+ * @param {PseudoNode|null} [settings.parent=null] The node to add this element to as its last child
74
+ * @param {Array<PseudoNode>} [settings.children=[]] The nodes to start as children
75
+ * @constructor
76
+ */
77
+ constructor({ tagName, attributes, parent, children }?: {
78
+ tagName?: string;
79
+ attributes?: Array<attribute>;
80
+ parent?: PseudoNode | null;
81
+ children?: Array<any>;
82
+ });
83
+ /**
84
+ * The attributes with the values they have now: the ones which are also properties (className, id, style, ...) can
85
+ * have been changed through the property, which does not change the stored list.
86
+ * @returns {Array<{name: string, value: *}>}
87
+ */
88
+ private currentAttributes;
89
+ get nodeName(): string;
90
+ /**
91
+ * A copy of this element without its children: the same tag and attributes (the values which are objects, such as
92
+ * style, are copied too rather than shared), but not its parent or listeners.
93
+ * @returns {ElementService}
94
+ */
95
+ protected cloneShallow(): NodeService;
96
+ /**
97
+ * Elements are equal when they have the same tag and the same attributes (in any order), which is what isEqualNode
98
+ * checks before it compares the children.
99
+ * @param {NodeService} other The element to compare with
100
+ * @returns {boolean}
101
+ */
102
+ protected equalsShallow(other: NodeService): boolean;
103
+ get tagName(): string;
104
+ /**
105
+ * The local part of the element's qualified name. There is no real namespace parsing here, so this is always the
106
+ * same as tagName.
107
+ * @returns {string}
108
+ */
109
+ get localName(): string;
110
+ /**
111
+ * The element's namespace prefix, or null when it has none. There is no real namespace parsing here, so this is
112
+ * always null.
113
+ * @returns {string|null}
114
+ */
115
+ get prefix(): string | null;
116
+ get nodeType(): number;
117
+ get attributes(): PseudoNamedNodeMap;
118
+ get classList(): PseudoDOMTokenList;
119
+ get className(): string;
120
+ set className(className: string);
121
+ /**
122
+ * Some elements have default behaviour, this registers it when the element is added.
123
+ * @returns {Function}
124
+ */
125
+ /**
126
+ * A live view of this element's element children (text, comments and the like are not included).
127
+ * @returns {PseudoHTMLCollection}
128
+ */
129
+ get children(): PseudoHTMLCollection;
130
+ /**
131
+ * How many element children this element has.
132
+ * @returns {number}
133
+ */
134
+ get childElementCount(): number;
135
+ /**
136
+ * The first child of this element which is an element, or null when there is none.
137
+ * @returns {PseudoElement|null}
138
+ */
139
+ get firstElementChild(): PseudoElement | null;
140
+ /**
141
+ * The last child of this element which is an element, or null when there is none.
142
+ * @returns {PseudoElement|null}
143
+ */
144
+ get lastElementChild(): PseudoElement | null;
145
+ /**
146
+ * The sibling after this one which is an element, or null when there is none.
147
+ * @returns {PseudoElement|null}
148
+ */
149
+ get nextElementSibling(): PseudoElement | null;
150
+ /**
151
+ * The sibling before this one which is an element, or null when there is none.
152
+ * @returns {PseudoElement|null}
153
+ */
154
+ get previousElementSibling(): PseudoElement | null;
155
+ /**
156
+ * Put an element at a position relative to this one: beforebegin (before this element, as its previous sibling),
157
+ * afterbegin (as this element's first child), beforeend (as this element's last child) or afterend (after this
158
+ * element, as its next sibling).
159
+ * @param {string} position beforebegin, afterbegin, beforeend or afterend
160
+ * @param {ElementService} element The element to insert
161
+ * @returns {ElementService|null} The inserted element, or null when the position needed a parent this element does not have
162
+ * @throws {Error} When the position is not one of the four above
163
+ */
164
+ insertAdjacentElement(position: string, element: PseudoElement): PseudoElement | null;
165
+ /**
166
+ * Put text at a position relative to this element, the same as insertAdjacentElement but the text becomes a text node.
167
+ * @param {string} position beforebegin, afterbegin, beforeend or afterend
168
+ * @param {string} text The text to insert
169
+ * @throws {Error} When the position is not one of the four above
170
+ */
171
+ insertAdjacentText(position: string, text: string): void;
172
+ /**
173
+ * Shared implementation for insertAdjacentElement / insertAdjacentText.
174
+ * @param {string} position beforebegin, afterbegin, beforeend or afterend
175
+ * @param {PseudoNode|string} node The node (or text) to insert
176
+ * @returns {PseudoNode|null} The inserted node, or null when the position needed a parent this element does not have
177
+ * @throws {Error} When the position is not one of the four above
178
+ */
179
+ private insertAdjacent;
180
+ /**
181
+ * Not implemented yet (HTML parsing is out of scope for now).
182
+ * @param {string} position beforebegin, afterbegin, beforeend or afterend
183
+ * @param {string} text The markup which would be parsed
184
+ * @throws {Error}
185
+ */
186
+ insertAdjacentHTML(position: string, text: string): void;
187
+ /**
188
+ * Whether this element itself (not its descendants) matches the given CSS selector.
189
+ * @param {string} selectors A CSS selector
190
+ * @returns {boolean}
191
+ */
192
+ matches(selectors: string): boolean;
193
+ /**
194
+ * The nearest ancestor of this element (starting with this element itself) which matches the CSS selector, or
195
+ * null when none of them do.
196
+ * @param {string} selectors A CSS selector
197
+ * @returns {PseudoElement|null}
198
+ */
199
+ closest(selectors: string): PseudoElement | null;
200
+ applyDefaultEvent(): Function;
201
+ /**
202
+ * An element which is added as a child gets its default events (for example a submit button submits its form).
203
+ * @param {NodeService} child The node which was inserted
204
+ */
205
+ protected childInserted(child: NodeService): void;
206
+ /**
207
+ * Check whether the element has an attribute by that name.
208
+ * @param {string} attributeName
209
+ * @returns {boolean}
210
+ */
211
+ hasAttribute(attributeName: string): boolean;
212
+ /**
213
+ * Set the value of an attribute, adding the attribute if it did not exist.
214
+ * @param {string} attributeName
215
+ * @param {string} attributeValue
216
+ * @returns {undefined}
217
+ */
218
+ setAttribute(attributeName: string, attributeValue: string): void;
219
+ /**
220
+ * Retrieve the value of an attribute.
221
+ * @param {string} attributeName
222
+ * @returns {string|null} The value, or null when there is no such attribute
223
+ */
224
+ getAttribute(attributeName: string): string | null;
225
+ /**
226
+ * Remove an attribute from the element.
227
+ * @param {string} attributeName
228
+ * @returns {undefined}
229
+ */
230
+ removeAttribute(attributeName: string): void;
231
+ /**
232
+ * The name of every attribute on the element, in the order they were set.
233
+ * @returns {Array<string>}
234
+ */
235
+ getAttributeNames(): Array<string>;
236
+ /**
237
+ * Whether the element has any attributes at all.
238
+ * @returns {boolean}
239
+ */
240
+ hasAttributes(): boolean;
241
+ /**
242
+ * Add the attribute (with an empty value) when it is not present, or remove it when it is - unless force says
243
+ * which of those to do instead. Returns whether the attribute is present after the call.
244
+ * @param {string} attributeName
245
+ * @param {boolean} [force]
246
+ * @returns {boolean}
247
+ */
248
+ toggleAttribute(attributeName: string, force?: boolean): boolean;
249
+ /**
250
+ * The size of the element and its position, settable directly - there is no layout engine here to compute it.
251
+ * @returns {DOMRect}
252
+ */
253
+ getBoundingClientRect(): DOMRect;
254
+ /**
255
+ * The bounding rectangles for each line of text in the element, settable directly - there is no layout engine
256
+ * here to compute it.
257
+ * @returns {Array<DOMRect>}
258
+ */
259
+ getClientRects(): Array<DOMRect>;
260
+ /**
261
+ * The Animation objects currently active on the element, settable directly - there is no animation engine here.
262
+ * @returns {Array<*>}
263
+ */
264
+ getAnimations(): Array<any>;
265
+ /**
266
+ * Whether the element is expected to be visible, settable directly - there is no rendering here to check it.
267
+ * @returns {boolean}
268
+ */
269
+ checkVisibility(): boolean;
270
+ /**
271
+ * A read-only view of the element's own inline style declarations (there is no CSS cascade here, so this is not a
272
+ * real computed style - just what the element's own style object holds).
273
+ * @returns {{get: function(string): (string|undefined)}}
274
+ */
275
+ computedStyleMap(): {
276
+ get: (property: string) => string | undefined;
277
+ };
278
+ /**
279
+ * Whether this element currently has capture of the given pointer.
280
+ * @param {number} pointerId
281
+ * @returns {boolean}
282
+ */
283
+ hasPointerCapture(pointerId: number): boolean;
284
+ /**
285
+ * Give this element capture of the given pointer.
286
+ * @param {number} pointerId
287
+ * @returns {undefined}
288
+ */
289
+ setPointerCapture(pointerId: number): void;
290
+ /**
291
+ * Release this element's capture of the given pointer, if it had it.
292
+ * @param {number} pointerId
293
+ * @returns {undefined}
294
+ */
295
+ releasePointerCapture(pointerId: number): void;
296
+ /**
297
+ * Scroll to the given position (or, given an options object, the position(s) it has). There is no real scrollable
298
+ * viewport here: this just sets scrollLeft / scrollTop.
299
+ * @param {number|{left: number, top: number}} [x=0]
300
+ * @param {number} [y=0]
301
+ * @returns {undefined}
302
+ */
303
+ scroll(x?: number | {
304
+ left?: number;
305
+ top?: number;
306
+ }, y?: number): void;
307
+ /**
308
+ * Scroll to the given position. An alias for scroll.
309
+ * @param {number|{left: number, top: number}} [x=0]
310
+ * @param {number} [y=0]
311
+ * @returns {undefined}
312
+ */
313
+ scrollTo(x?: number | {
314
+ left?: number;
315
+ top?: number;
316
+ }, y?: number): void;
317
+ /**
318
+ * Scroll by the given amount, relative to the current position.
319
+ * @param {number|{left: number, top: number}} [x=0]
320
+ * @param {number} [y=0]
321
+ * @returns {undefined}
322
+ */
323
+ scrollBy(x?: number | {
324
+ left?: number;
325
+ top?: number;
326
+ }, y?: number): void;
327
+ /**
328
+ * Scroll an ancestor until this element is in view. There is no real viewport here for that to mean anything, so
329
+ * this does nothing (override it on an instance in a test which needs to observe the call).
330
+ * @returns {undefined}
331
+ */
332
+ scrollIntoView(): void;
333
+ /**
334
+ * Asynchronously ask for the element to be shown fullscreen. There is no real fullscreen here, so this just
335
+ * resolves, like a browser granting the request would.
336
+ * @returns {Promise<void>}
337
+ */
338
+ requestFullscreen(): Promise<void>;
339
+ /**
340
+ * Asynchronously ask for the pointer to be locked to this element. There is no real pointer lock here, so this
341
+ * just resolves, like a browser granting the request would.
342
+ * @returns {Promise<void>}
343
+ */
344
+ requestPointerLock(): Promise<void>;
345
+ /**
346
+ * Attach a shadow tree to this element and return its ShadowRoot. Throws when it already hosts one.
347
+ * @param {{mode: string}} options
348
+ * @returns {PseudoShadowRoot}
349
+ * @throws {Error}
350
+ */
351
+ attachShadow(options: {
352
+ mode: string;
353
+ }): PseudoShadowRoot;
354
+ /**
355
+ * This element's shadow root, when it has one attached in 'open' mode, or null (including when the mode is
356
+ * 'closed' - it still exists, but is not reachable this way, like the DOM's).
357
+ * @returns {PseudoShadowRoot|null}
358
+ */
359
+ get shadowRoot(): PseudoShadowRoot | null;
360
+ }
361
+ export {};