pseudo-dom 0.9.0 → 0.11.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 (33) hide show
  1. package/README.md +918 -18
  2. package/browser/pseudo-dom.js +3296 -604
  3. package/browser/pseudo-dom.min.js +1 -1
  4. package/dist/factories/createDataset.d.ts +10 -10
  5. package/dist/factories/createDataset.js +78 -78
  6. package/dist/factories/createStyleDeclaration.d.ts +30 -30
  7. package/dist/factories/createStyleDeclaration.js +64 -64
  8. package/dist/factories/parseHTML.d.ts +13 -0
  9. package/dist/factories/parseHTML.js +84 -0
  10. package/dist/factories/parseHTML.min.js +1 -0
  11. package/dist/factories/serializeHTML.d.ts +15 -0
  12. package/dist/factories/serializeHTML.js +114 -0
  13. package/dist/factories/serializeHTML.min.js +1 -0
  14. package/dist/interfaces/PseudoCSSStyleDeclaration.d.ts +14 -14
  15. package/dist/interfaces/PseudoCSSStyleDeclaration.js +5 -5
  16. package/dist/interfaces/PseudoElement.d.ts +765 -702
  17. package/dist/interfaces/PseudoHTMLElement.d.ts +33 -33
  18. package/dist/interfaces/PseudoShadowRoot.d.ts +9 -0
  19. package/dist/services/CSSStyleDeclarationService.d.ts +73 -73
  20. package/dist/services/CSSStyleDeclarationService.js +156 -156
  21. package/dist/services/ElementService.d.ts +371 -191
  22. package/dist/services/ElementService.js +732 -426
  23. package/dist/services/ElementService.min.js +1 -1
  24. package/dist/services/HTMLElementService.d.ts +32 -0
  25. package/dist/services/HTMLElementService.js +67 -0
  26. package/dist/services/HTMLElementService.min.js +1 -1
  27. package/dist/services/NodeService.d.ts +8 -0
  28. package/dist/services/NodeService.js +11 -0
  29. package/dist/services/NodeService.min.js +1 -1
  30. package/dist/services/ShadowRootService.d.ts +12 -0
  31. package/dist/services/ShadowRootService.js +16 -1
  32. package/dist/services/ShadowRootService.min.js +1 -1
  33. package/package.json +2 -1
@@ -1,426 +1,732 @@
1
- 'use strict'
2
-
3
- require('core-js/modules/esnext.iterator.constructor.js')
4
- require('core-js/modules/esnext.iterator.every.js')
5
- require('core-js/modules/esnext.iterator.find.js')
6
- require('core-js/modules/esnext.iterator.for-each.js')
7
- require('core-js/modules/esnext.iterator.map.js')
8
- require('core-js/modules/esnext.iterator.some.js')
9
- const __importDefault = void 0 && (void 0).__importDefault || function (mod) {
10
- return mod && mod.__esModule
11
- ? mod
12
- : {
13
- default: mod
14
- }
15
- }
16
- Object.defineProperty(exports, '__esModule', {
17
- value: true
18
- })
19
- exports.ElementService = void 0
20
- const createEvent_1 = __importDefault(require('../factories/createEvent'))
21
- const NodeService_1 = require('./NodeService')
22
- const AttrService_1 = require('./AttrService')
23
- const DOMTokenListService_1 = require('./DOMTokenListService')
24
- const NamedNodeMapService_1 = require('./NamedNodeMapService')
25
- const getParentNodesFromAttribute_1 = __importDefault(require('../functions/getParentNodesFromAttribute'))
26
- const HTMLCollectionService_1 = require('./HTMLCollectionService')
27
- const cloneObject_1 = __importDefault(require('si-funciona/dist/helpers/objects/cloneObject'))
28
- const isEqual_1 = __importDefault(require('si-funciona/dist/helpers/objects/isEqual'))
29
- const query_1 = require('../factories/query')
30
- /**
31
- * Simulate the behaviour of the Element Class when there is no DOM available.
32
- * @author Joshua Heagle <joshuaheagle@gmail.com>
33
- * @class
34
- * @augments PseudoNode
35
- * @property {string} tagName
36
- * @property {string} className
37
- * @property {string} id
38
- * @property {string} innerHtml
39
- * @property {Array} attributes
40
- * @property {function} hasAttribute
41
- * @property {function} setAttribute
42
- * @property {function} getAttribute
43
- * @property {function} removeAttribute
44
- */
45
- class ElementService extends NodeService_1.NodeService {
46
- /**
47
- * @param {Object} [settings={}]
48
- * @param {string} [settings.tagName=''] The name of the tag this element represents
49
- * @param {Array<{name: string, value: *}>} [settings.attributes=[]] The attributes (also assigned as properties) to start with
50
- * @param {PseudoNode|null} [settings.parent=null] The node to add this element to as its last child
51
- * @param {Array<PseudoNode>} [settings.children=[]] The nodes to start as children
52
- * @constructor
53
- */
54
- constructor ({
55
- tagName = '',
56
- attributes = [],
57
- parent = null,
58
- children = []
59
- } = {}) {
60
- super()
61
- this.defaultEventApplied = false
62
- this.tokenList = new DOMTokenListService_1.DOMTokenListService()
63
- this.tag = tagName
64
- this.attributeList = attributes.concat([{
65
- name: 'className',
66
- value: ''
67
- }, {
68
- name: 'id',
69
- value: ''
70
- }, {
71
- name: 'innerHTML',
72
- value: ''
73
- }])
74
- this.propertyAttributes = this.attributeList.map(({
75
- name
76
- }) => name)
77
- this.attributeList.forEach(({
78
- name,
79
- value
80
- }) => {
81
- this[name] = value
82
- })
83
- children.forEach(child => {
84
- if (!child || typeof child.nodeType !== 'number') {
85
- throw new TypeError('The children of an element must be nodes.')
86
- }
87
- this.appendChild(child)
88
- })
89
- if (parent) {
90
- parent.appendChild(this)
91
- }
92
- }
93
-
94
- /**
95
- * The attributes with the values they have now: the ones which are also properties (className, id, style, ...) can
96
- * have been changed through the property, which does not change the stored list.
97
- * @returns {Array<{name: string, value: *}>}
98
- */
99
- currentAttributes () {
100
- return this.attributeList.map(({
101
- name,
102
- value
103
- }) => ({
104
- name,
105
- value: this.propertyAttributes.indexOf(name) >= 0 ? this[name] : value
106
- }))
107
- }
108
-
109
- get nodeName () {
110
- return this.tag
111
- }
112
-
113
- /**
114
- * A copy of this element without its children: the same tag and attributes (the values which are objects, such as
115
- * style, are copied too rather than shared), but not its parent or listeners.
116
- * @returns {ElementService}
117
- */
118
- cloneShallow () {
119
- const copy = new this.constructor({
120
- tagName: this.tag
121
- })
122
- copy.attributeList.length = 0
123
- this.currentAttributes().forEach(({
124
- name,
125
- value
126
- }) => {
127
- const copied = typeof value === 'object' && value !== null ? (0, cloneObject_1.default)(value) : value
128
- copy.attributeList.push({
129
- name,
130
- value: copied
131
- })
132
- if (copy.propertyAttributes.indexOf(name) >= 0) {
133
- copy[name] = copied
134
- }
135
- })
136
- copy.ownerDocumentStore = this.ownerDocumentStore
137
- return copy
138
- }
139
-
140
- /**
141
- * Elements are equal when they have the same tag and the same attributes (in any order), which is what isEqualNode
142
- * checks before it compares the children.
143
- * @param {NodeService} other The element to compare with
144
- * @returns {boolean}
145
- */
146
- equalsShallow (other) {
147
- const mine = this.currentAttributes()
148
- const theirs = other.currentAttributes()
149
- return this.nodeName === other.nodeName && mine.length === theirs.length && mine.every(({
150
- name,
151
- value
152
- }) => {
153
- const match = theirs.find(attributeOfOther => attributeOfOther.name === name)
154
- return typeof match !== 'undefined' && (0, isEqual_1.default)(value, match.value)
155
- })
156
- }
157
-
158
- get tagName () {
159
- return this.tag
160
- }
161
-
162
- get nodeType () {
163
- return NodeService_1.NodeService.ELEMENT_NODE
164
- }
165
-
166
- get attributes () {
167
- return new NamedNodeMapService_1.NamedNodeMapService(this.currentAttributes().map(({
168
- name,
169
- value
170
- }) => new AttrService_1.AttrService(name, String(value), this)))
171
- }
172
-
173
- get classList () {
174
- return this.tokenList
175
- }
176
-
177
- get className () {
178
- return this.tokenList.value
179
- }
180
-
181
- set className (className) {
182
- this.tokenList.value = className
183
- }
184
-
185
- /**
186
- * Some elements have default behaviour, this registers it when the element is added.
187
- * @returns {Function}
188
- */
189
- /**
190
- * A live view of this element's element children (text, comments and the like are not included).
191
- * @returns {PseudoHTMLCollection}
192
- */
193
- get children () {
194
- return new HTMLCollectionService_1.HTMLCollectionService(this)
195
- }
196
-
197
- /**
198
- * How many element children this element has.
199
- * @returns {number}
200
- */
201
- get childElementCount () {
202
- return this.children.length
203
- }
204
-
205
- /**
206
- * The first child of this element which is an element, or null when there is none.
207
- * @returns {PseudoElement|null}
208
- */
209
- get firstElementChild () {
210
- return this.children.item(0)
211
- }
212
-
213
- /**
214
- * The last child of this element which is an element, or null when there is none.
215
- * @returns {PseudoElement|null}
216
- */
217
- get lastElementChild () {
218
- const elementChildren = this.children
219
- return elementChildren.item(elementChildren.length - 1)
220
- }
221
-
222
- /**
223
- * The sibling after this one which is an element, or null when there is none.
224
- * @returns {PseudoElement|null}
225
- */
226
- get nextElementSibling () {
227
- let sibling = this.nextSibling
228
- while (sibling && sibling.nodeType !== NodeService_1.NodeService.ELEMENT_NODE) {
229
- sibling = sibling.nextSibling
230
- }
231
- return sibling
232
- }
233
-
234
- /**
235
- * The sibling before this one which is an element, or null when there is none.
236
- * @returns {PseudoElement|null}
237
- */
238
- get previousElementSibling () {
239
- let sibling = this.previousSibling
240
- while (sibling && sibling.nodeType !== NodeService_1.NodeService.ELEMENT_NODE) {
241
- sibling = sibling.previousSibling
242
- }
243
- return sibling
244
- }
245
-
246
- /**
247
- * Put an element at a position relative to this one: beforebegin (before this element, as its previous sibling),
248
- * afterbegin (as this element's first child), beforeend (as this element's last child) or afterend (after this
249
- * element, as its next sibling).
250
- * @param {string} position beforebegin, afterbegin, beforeend or afterend
251
- * @param {ElementService} element The element to insert
252
- * @returns {ElementService|null} The inserted element, or null when the position needed a parent this element does not have
253
- * @throws {Error} When the position is not one of the four above
254
- */
255
- insertAdjacentElement (position, element) {
256
- return this.insertAdjacent(position, element)
257
- }
258
-
259
- /**
260
- * Put text at a position relative to this element, the same as insertAdjacentElement but the text becomes a text node.
261
- * @param {string} position beforebegin, afterbegin, beforeend or afterend
262
- * @param {string} text The text to insert
263
- * @throws {Error} When the position is not one of the four above
264
- */
265
- insertAdjacentText (position, text) {
266
- this.insertAdjacent(position, text)
267
- }
268
-
269
- /**
270
- * Shared implementation for insertAdjacentElement / insertAdjacentText.
271
- * @param {string} position beforebegin, afterbegin, beforeend or afterend
272
- * @param {PseudoNode|string} node The node (or text) to insert
273
- * @returns {PseudoNode|null} The inserted node, or null when the position needed a parent this element does not have
274
- * @throws {Error} When the position is not one of the four above
275
- */
276
- insertAdjacent (position, node) {
277
- switch (position) {
278
- case 'beforebegin':
279
- if (!this.parentNode) {
280
- return null
281
- }
282
- this.before(node)
283
- return node
284
- case 'afterbegin':
285
- this.prepend(node)
286
- return node
287
- case 'beforeend':
288
- this.append(node)
289
- return node
290
- case 'afterend':
291
- if (!this.parentNode) {
292
- return null
293
- }
294
- this.after(node)
295
- return node
296
- default:
297
- throw new Error(`"${position}" is not one of beforebegin, afterbegin, beforeend or afterend.`)
298
- }
299
- }
300
-
301
- /**
302
- * Not implemented yet (HTML parsing is out of scope for now).
303
- * @param {string} position beforebegin, afterbegin, beforeend or afterend
304
- * @param {string} text The markup which would be parsed
305
- * @throws {Error}
306
- */
307
- insertAdjacentHTML (position, text) {
308
- throw new Error('ElementService.insertAdjacentHTML() is not implemented yet.')
309
- }
310
-
311
- /**
312
- * Whether this element itself (not its descendants) matches the given CSS selector.
313
- * @param {string} selectors A CSS selector
314
- * @returns {boolean}
315
- */
316
- matches (selectors) {
317
- return (0, query_1.matches)(this, selectors)
318
- }
319
-
320
- /**
321
- * The nearest ancestor of this element (starting with this element itself) which matches the CSS selector, or
322
- * null when none of them do.
323
- * @param {string} selectors A CSS selector
324
- * @returns {PseudoElement|null}
325
- */
326
- closest (selectors) {
327
- return (0, query_1.closest)(this, selectors)
328
- }
329
-
330
- applyDefaultEvent () {
331
- let callback = event => undefined
332
- if (this.defaultEventApplied) {
333
- return callback
334
- }
335
- switch (this.tagName) {
336
- case 'button':
337
- case 'input':
338
- // Clicking a submit button submits the form it is in: the form gets a submit event, which can be cancelled
339
- callback = event => {
340
- const type = String(this.getAttribute('type') || this.type || '').toLowerCase()
341
- const submits = this.tagName === 'button' ? type !== 'button' && type !== 'reset' : /^(submit|image)$/.test(type)
342
- const forms = (0, getParentNodesFromAttribute_1.default)('tagName', 'form', this)
343
- if (submits && forms.length && !this.hasAttribute('disabled')) {
344
- forms[forms.length - 1].dispatchEvent((0, createEvent_1.default)('submit', {}, {
345
- browser: true,
346
- trusted: event.isTrusted
347
- }))
348
- }
349
- }
350
- super.setDefaultEvent('click', callback)
351
- this.defaultEventApplied = true
352
- }
353
- return callback
354
- }
355
-
356
- /**
357
- * An element which is added as a child gets its default events (for example a submit button submits its form).
358
- * @param {NodeService} child The node which was inserted
359
- */
360
- childInserted (child) {
361
- if (typeof child.applyDefaultEvent === 'function') {
362
- child.applyDefaultEvent()
363
- }
364
- }
365
-
366
- /**
367
- * Check whether the element has an attribute by that name.
368
- * @param {string} attributeName
369
- * @returns {boolean}
370
- */
371
- hasAttribute (attributeName) {
372
- return this.attributeList.some(({
373
- name
374
- }) => name === attributeName)
375
- }
376
-
377
- /**
378
- * Set the value of an attribute, adding the attribute if it did not exist.
379
- * @param {string} attributeName
380
- * @param {string} attributeValue
381
- * @returns {undefined}
382
- */
383
- setAttribute (attributeName, attributeValue) {
384
- const existing = this.attributeList.find(({
385
- name
386
- }) => name === attributeName)
387
- if (existing) {
388
- existing.value = attributeValue
389
- } else {
390
- this.attributeList.push({
391
- name: attributeName,
392
- value: attributeValue
393
- })
394
- }
395
- if (this.propertyAttributes.indexOf(attributeName) >= 0) {
396
- this[attributeName] = attributeValue
397
- }
398
- }
399
-
400
- /**
401
- * Retrieve the value of an attribute.
402
- * @param {string} attributeName
403
- * @returns {string|null} The value, or null when there is no such attribute
404
- */
405
- getAttribute (attributeName) {
406
- const found = this.currentAttributes().find(({
407
- name
408
- }) => name === attributeName)
409
- return found ? found.value : null
410
- }
411
-
412
- /**
413
- * Remove an attribute from the element.
414
- * @param {string} attributeName
415
- * @returns {undefined}
416
- */
417
- removeAttribute (attributeName) {
418
- const index = this.attributeList.findIndex(({
419
- name
420
- }) => name === attributeName)
421
- if (index >= 0) {
422
- this.attributeList.splice(index, 1)
423
- }
424
- }
425
- }
426
- exports.ElementService = ElementService
1
+ 'use strict'
2
+
3
+ require('core-js/modules/esnext.iterator.constructor.js')
4
+ require('core-js/modules/esnext.iterator.every.js')
5
+ require('core-js/modules/esnext.iterator.find.js')
6
+ require('core-js/modules/esnext.iterator.for-each.js')
7
+ require('core-js/modules/esnext.iterator.map.js')
8
+ require('core-js/modules/esnext.iterator.some.js')
9
+ require('core-js/modules/esnext.set.add-all.js')
10
+ require('core-js/modules/esnext.set.delete-all.js')
11
+ require('core-js/modules/esnext.set.difference.js')
12
+ require('core-js/modules/esnext.set.every.js')
13
+ require('core-js/modules/esnext.set.filter.js')
14
+ require('core-js/modules/esnext.set.find.js')
15
+ require('core-js/modules/esnext.set.intersection.js')
16
+ require('core-js/modules/esnext.set.is-disjoint-from.js')
17
+ require('core-js/modules/esnext.set.is-subset-of.js')
18
+ require('core-js/modules/esnext.set.is-superset-of.js')
19
+ require('core-js/modules/esnext.set.join.js')
20
+ require('core-js/modules/esnext.set.map.js')
21
+ require('core-js/modules/esnext.set.reduce.js')
22
+ require('core-js/modules/esnext.set.some.js')
23
+ require('core-js/modules/esnext.set.symmetric-difference.js')
24
+ require('core-js/modules/esnext.set.union.js')
25
+ const __importDefault = void 0 && (void 0).__importDefault || function (mod) {
26
+ return mod && mod.__esModule
27
+ ? mod
28
+ : {
29
+ default: mod
30
+ }
31
+ }
32
+ Object.defineProperty(exports, '__esModule', {
33
+ value: true
34
+ })
35
+ exports.ElementService = void 0
36
+ const createEvent_1 = __importDefault(require('../factories/createEvent'))
37
+ const NodeService_1 = require('./NodeService')
38
+ const AttrService_1 = require('./AttrService')
39
+ const DOMTokenListService_1 = require('./DOMTokenListService')
40
+ const NamedNodeMapService_1 = require('./NamedNodeMapService')
41
+ const getParentNodesFromAttribute_1 = __importDefault(require('../functions/getParentNodesFromAttribute'))
42
+ const HTMLCollectionService_1 = require('./HTMLCollectionService')
43
+ const cloneObject_1 = __importDefault(require('si-funciona/dist/helpers/objects/cloneObject'))
44
+ const isEqual_1 = __importDefault(require('si-funciona/dist/helpers/objects/isEqual'))
45
+ const query_1 = require('../factories/query')
46
+ const ShadowRootService_1 = require('./ShadowRootService')
47
+ const serializeHTML_1 = require('../factories/serializeHTML')
48
+ const zeroRect = () => ({
49
+ x: 0,
50
+ y: 0,
51
+ width: 0,
52
+ height: 0,
53
+ top: 0,
54
+ right: 0,
55
+ bottom: 0,
56
+ left: 0
57
+ })
58
+ /**
59
+ * Simulate the behaviour of the Element Class when there is no DOM available.
60
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
61
+ * @class
62
+ * @augments PseudoNode
63
+ * @property {string} tagName
64
+ * @property {string} className
65
+ * @property {string} id
66
+ * @property {string} innerHtml
67
+ * @property {Array} attributes
68
+ * @property {function} hasAttribute
69
+ * @property {function} setAttribute
70
+ * @property {function} getAttribute
71
+ * @property {function} removeAttribute
72
+ */
73
+ class ElementService extends NodeService_1.NodeService {
74
+ /**
75
+ * @param {Object} [settings={}]
76
+ * @param {string} [settings.tagName=''] The name of the tag this element represents
77
+ * @param {Array<{name: string, value: *}>} [settings.attributes=[]] The attributes (also assigned as properties) to start with
78
+ * @param {PseudoNode|null} [settings.parent=null] The node to add this element to as its last child
79
+ * @param {Array<PseudoNode>} [settings.children=[]] The nodes to start as children
80
+ * @constructor
81
+ */
82
+ constructor ({
83
+ tagName = '',
84
+ attributes = [],
85
+ parent = null,
86
+ children = []
87
+ } = {}) {
88
+ super()
89
+ /** What getBoundingClientRect() returns - not really computed (there is no layout engine), set this directly. */
90
+ this.boundingClientRect = zeroRect()
91
+ /** What getClientRects() returns - set this directly. */
92
+ this.clientRects = []
93
+ /** What getAnimations() returns - set this directly. */
94
+ this.animations = []
95
+ /** What checkVisibility() returns - set this directly. */
96
+ this.isVisible = true
97
+ this.capturedPointers = new Set()
98
+ this.shadowRootInstance = null
99
+ this.defaultEventApplied = false
100
+ this.tokenList = new DOMTokenListService_1.DOMTokenListService()
101
+ this.tag = tagName
102
+ this.attributeList = attributes.concat([{
103
+ name: 'className',
104
+ value: ''
105
+ }, {
106
+ name: 'id',
107
+ value: ''
108
+ },
109
+ // Layout is not really computed (there is no rendering engine here): these start at 0, like an unrendered
110
+ // element's would, but can be set directly to whatever a test needs code under test to see.
111
+ {
112
+ name: 'clientHeight',
113
+ value: 0
114
+ }, {
115
+ name: 'clientLeft',
116
+ value: 0
117
+ }, {
118
+ name: 'clientTop',
119
+ value: 0
120
+ }, {
121
+ name: 'clientWidth',
122
+ value: 0
123
+ }, {
124
+ name: 'scrollHeight',
125
+ value: 0
126
+ }, {
127
+ name: 'scrollLeft',
128
+ value: 0
129
+ }, {
130
+ name: 'scrollTop',
131
+ value: 0
132
+ }, {
133
+ name: 'scrollWidth',
134
+ value: 0
135
+ }])
136
+ this.propertyAttributes = this.attributeList.map(({
137
+ name
138
+ }) => name)
139
+ this.attributeList.forEach(({
140
+ name,
141
+ value
142
+ }) => {
143
+ this[name] = value
144
+ })
145
+ children.forEach(child => {
146
+ if (!child || typeof child.nodeType !== 'number') {
147
+ throw new TypeError('The children of an element must be nodes.')
148
+ }
149
+ this.appendChild(child)
150
+ })
151
+ if (parent) {
152
+ parent.appendChild(this)
153
+ }
154
+ }
155
+
156
+ /**
157
+ * The attributes with the values they have now: the ones which are also properties (className, id, style, ...) can
158
+ * have been changed through the property, which does not change the stored list.
159
+ * @returns {Array<{name: string, value: *}>}
160
+ */
161
+ currentAttributes () {
162
+ return this.attributeList.map(({
163
+ name,
164
+ value
165
+ }) => ({
166
+ name,
167
+ value: this.propertyAttributes.indexOf(name) >= 0 ? this[name] : value
168
+ }))
169
+ }
170
+
171
+ get nodeName () {
172
+ return this.tag
173
+ }
174
+
175
+ /**
176
+ * A copy of this element without its children: the same tag and attributes (the values which are objects, such as
177
+ * style, are copied too rather than shared), but not its parent or listeners.
178
+ * @returns {ElementService}
179
+ */
180
+ cloneShallow () {
181
+ const copy = new this.constructor({
182
+ tagName: this.tag
183
+ })
184
+ copy.attributeList.length = 0
185
+ this.currentAttributes().forEach(({
186
+ name,
187
+ value
188
+ }) => {
189
+ const copied = typeof value === 'object' && value !== null ? (0, cloneObject_1.default)(value) : value
190
+ copy.attributeList.push({
191
+ name,
192
+ value: copied
193
+ })
194
+ if (copy.propertyAttributes.indexOf(name) >= 0) {
195
+ copy[name] = copied
196
+ }
197
+ })
198
+ copy.ownerDocumentStore = this.ownerDocumentStore
199
+ return copy
200
+ }
201
+
202
+ /**
203
+ * Elements are equal when they have the same tag and the same attributes (in any order), which is what isEqualNode
204
+ * checks before it compares the children.
205
+ * @param {NodeService} other The element to compare with
206
+ * @returns {boolean}
207
+ */
208
+ equalsShallow (other) {
209
+ const mine = this.currentAttributes()
210
+ const theirs = other.currentAttributes()
211
+ return this.nodeName === other.nodeName && mine.length === theirs.length && mine.every(({
212
+ name,
213
+ value
214
+ }) => {
215
+ const match = theirs.find(attributeOfOther => attributeOfOther.name === name)
216
+ return typeof match !== 'undefined' && (0, isEqual_1.default)(value, match.value)
217
+ })
218
+ }
219
+
220
+ get tagName () {
221
+ return this.tag
222
+ }
223
+
224
+ /**
225
+ * The local part of the element's qualified name. There is no real namespace parsing here, so this is always the
226
+ * same as tagName.
227
+ * @returns {string}
228
+ */
229
+ get localName () {
230
+ return this.tag
231
+ }
232
+
233
+ /**
234
+ * The element's namespace prefix, or null when it has none. There is no real namespace parsing here, so this is
235
+ * always null.
236
+ * @returns {string|null}
237
+ */
238
+ get prefix () {
239
+ return null
240
+ }
241
+
242
+ /**
243
+ * The HTML markup of this element's children. Only the getter is here (the setter, which needs to build new
244
+ * elements from parsed HTML, is on HTMLElementService - see its class comment).
245
+ * @returns {string}
246
+ */
247
+ get innerHTML () {
248
+ return (0, serializeHTML_1.serializeChildren)(this)
249
+ }
250
+
251
+ /**
252
+ * The HTML markup of this element itself, including its children. Only the getter is here (see innerHTML).
253
+ * @returns {string}
254
+ */
255
+ get outerHTML () {
256
+ return (0, serializeHTML_1.serializeOuter)(this)
257
+ }
258
+
259
+ get nodeType () {
260
+ return NodeService_1.NodeService.ELEMENT_NODE
261
+ }
262
+
263
+ get attributes () {
264
+ return new NamedNodeMapService_1.NamedNodeMapService(this.currentAttributes().map(({
265
+ name,
266
+ value
267
+ }) => new AttrService_1.AttrService(name, String(value), this)))
268
+ }
269
+
270
+ get classList () {
271
+ return this.tokenList
272
+ }
273
+
274
+ get className () {
275
+ return this.tokenList.value
276
+ }
277
+
278
+ set className (className) {
279
+ this.tokenList.value = className
280
+ }
281
+
282
+ /**
283
+ * Some elements have default behaviour, this registers it when the element is added.
284
+ * @returns {Function}
285
+ */
286
+ /**
287
+ * A live view of this element's element children (text, comments and the like are not included).
288
+ * @returns {PseudoHTMLCollection}
289
+ */
290
+ get children () {
291
+ return new HTMLCollectionService_1.HTMLCollectionService(this)
292
+ }
293
+
294
+ /**
295
+ * How many element children this element has.
296
+ * @returns {number}
297
+ */
298
+ get childElementCount () {
299
+ return this.children.length
300
+ }
301
+
302
+ /**
303
+ * The first child of this element which is an element, or null when there is none.
304
+ * @returns {PseudoElement|null}
305
+ */
306
+ get firstElementChild () {
307
+ return this.children.item(0)
308
+ }
309
+
310
+ /**
311
+ * The last child of this element which is an element, or null when there is none.
312
+ * @returns {PseudoElement|null}
313
+ */
314
+ get lastElementChild () {
315
+ const elementChildren = this.children
316
+ return elementChildren.item(elementChildren.length - 1)
317
+ }
318
+
319
+ /**
320
+ * The sibling after this one which is an element, or null when there is none.
321
+ * @returns {PseudoElement|null}
322
+ */
323
+ get nextElementSibling () {
324
+ let sibling = this.nextSibling
325
+ while (sibling && sibling.nodeType !== NodeService_1.NodeService.ELEMENT_NODE) {
326
+ sibling = sibling.nextSibling
327
+ }
328
+ return sibling
329
+ }
330
+
331
+ /**
332
+ * The sibling before this one which is an element, or null when there is none.
333
+ * @returns {PseudoElement|null}
334
+ */
335
+ get previousElementSibling () {
336
+ let sibling = this.previousSibling
337
+ while (sibling && sibling.nodeType !== NodeService_1.NodeService.ELEMENT_NODE) {
338
+ sibling = sibling.previousSibling
339
+ }
340
+ return sibling
341
+ }
342
+
343
+ /**
344
+ * Put an element at a position relative to this one: beforebegin (before this element, as its previous sibling),
345
+ * afterbegin (as this element's first child), beforeend (as this element's last child) or afterend (after this
346
+ * element, as its next sibling).
347
+ * @param {string} position beforebegin, afterbegin, beforeend or afterend
348
+ * @param {ElementService} element The element to insert
349
+ * @returns {ElementService|null} The inserted element, or null when the position needed a parent this element does not have
350
+ * @throws {Error} When the position is not one of the four above
351
+ */
352
+ insertAdjacentElement (position, element) {
353
+ return this.insertAdjacent(position, element)
354
+ }
355
+
356
+ /**
357
+ * Put text at a position relative to this element, the same as insertAdjacentElement but the text becomes a text node.
358
+ * @param {string} position beforebegin, afterbegin, beforeend or afterend
359
+ * @param {string} text The text to insert
360
+ * @throws {Error} When the position is not one of the four above
361
+ */
362
+ insertAdjacentText (position, text) {
363
+ this.insertAdjacent(position, text)
364
+ }
365
+
366
+ /**
367
+ * Shared implementation for insertAdjacentElement / insertAdjacentText.
368
+ * @param {string} position beforebegin, afterbegin, beforeend or afterend
369
+ * @param {PseudoNode|string} node The node (or text) to insert
370
+ * @returns {PseudoNode|null} The inserted node, or null when the position needed a parent this element does not have
371
+ * @throws {Error} When the position is not one of the four above
372
+ */
373
+ insertAdjacent (position, node) {
374
+ switch (position) {
375
+ case 'beforebegin':
376
+ if (!this.parentNode) {
377
+ return null
378
+ }
379
+ this.before(node)
380
+ return node
381
+ case 'afterbegin':
382
+ this.prepend(node)
383
+ return node
384
+ case 'beforeend':
385
+ this.append(node)
386
+ return node
387
+ case 'afterend':
388
+ if (!this.parentNode) {
389
+ return null
390
+ }
391
+ this.after(node)
392
+ return node
393
+ default:
394
+ throw new Error(`"${position}" is not one of beforebegin, afterbegin, beforeend or afterend.`)
395
+ }
396
+ }
397
+
398
+ /**
399
+ * Not implemented yet (HTML parsing is out of scope for now).
400
+ * @param {string} position beforebegin, afterbegin, beforeend or afterend
401
+ * @param {string} text The markup which would be parsed
402
+ * @throws {Error}
403
+ */
404
+ insertAdjacentHTML (position, text) {
405
+ throw new Error('ElementService.insertAdjacentHTML() is not implemented yet.')
406
+ }
407
+
408
+ /**
409
+ * Whether this element itself (not its descendants) matches the given CSS selector.
410
+ * @param {string} selectors A CSS selector
411
+ * @returns {boolean}
412
+ */
413
+ matches (selectors) {
414
+ return (0, query_1.matches)(this, selectors)
415
+ }
416
+
417
+ /**
418
+ * The nearest ancestor of this element (starting with this element itself) which matches the CSS selector, or
419
+ * null when none of them do.
420
+ * @param {string} selectors A CSS selector
421
+ * @returns {PseudoElement|null}
422
+ */
423
+ closest (selectors) {
424
+ return (0, query_1.closest)(this, selectors)
425
+ }
426
+
427
+ applyDefaultEvent () {
428
+ let callback = event => undefined
429
+ if (this.defaultEventApplied) {
430
+ return callback
431
+ }
432
+ switch (this.tagName) {
433
+ case 'button':
434
+ case 'input':
435
+ // Clicking a submit button submits the form it is in: the form gets a submit event, which can be cancelled
436
+ callback = event => {
437
+ const type = String(this.getAttribute('type') || this.type || '').toLowerCase()
438
+ const submits = this.tagName === 'button' ? type !== 'button' && type !== 'reset' : /^(submit|image)$/.test(type)
439
+ const forms = (0, getParentNodesFromAttribute_1.default)('tagName', 'form', this)
440
+ if (submits && forms.length && !this.hasAttribute('disabled')) {
441
+ forms[forms.length - 1].dispatchEvent((0, createEvent_1.default)('submit', {}, {
442
+ browser: true,
443
+ trusted: event.isTrusted
444
+ }))
445
+ }
446
+ }
447
+ super.setDefaultEvent('click', callback)
448
+ this.defaultEventApplied = true
449
+ }
450
+ return callback
451
+ }
452
+
453
+ /**
454
+ * An element which is added as a child gets its default events (for example a submit button submits its form).
455
+ * @param {NodeService} child The node which was inserted
456
+ */
457
+ childInserted (child) {
458
+ if (typeof child.applyDefaultEvent === 'function') {
459
+ child.applyDefaultEvent()
460
+ }
461
+ }
462
+
463
+ /**
464
+ * Check whether the element has an attribute by that name.
465
+ * @param {string} attributeName
466
+ * @returns {boolean}
467
+ */
468
+ hasAttribute (attributeName) {
469
+ return this.attributeList.some(({
470
+ name
471
+ }) => name === attributeName)
472
+ }
473
+
474
+ /**
475
+ * Set the value of an attribute, adding the attribute if it did not exist.
476
+ * @param {string} attributeName
477
+ * @param {string} attributeValue
478
+ * @returns {undefined}
479
+ */
480
+ setAttribute (attributeName, attributeValue) {
481
+ const existing = this.attributeList.find(({
482
+ name
483
+ }) => name === attributeName)
484
+ if (existing) {
485
+ existing.value = attributeValue
486
+ } else {
487
+ this.attributeList.push({
488
+ name: attributeName,
489
+ value: attributeValue
490
+ })
491
+ }
492
+ if (this.propertyAttributes.indexOf(attributeName) >= 0) {
493
+ this[attributeName] = attributeValue
494
+ }
495
+ }
496
+
497
+ /**
498
+ * Retrieve the value of an attribute.
499
+ * @param {string} attributeName
500
+ * @returns {string|null} The value, or null when there is no such attribute
501
+ */
502
+ getAttribute (attributeName) {
503
+ const found = this.currentAttributes().find(({
504
+ name
505
+ }) => name === attributeName)
506
+ return found ? found.value : null
507
+ }
508
+
509
+ /**
510
+ * Remove an attribute from the element.
511
+ * @param {string} attributeName
512
+ * @returns {undefined}
513
+ */
514
+ removeAttribute (attributeName) {
515
+ const index = this.attributeList.findIndex(({
516
+ name
517
+ }) => name === attributeName)
518
+ if (index >= 0) {
519
+ this.attributeList.splice(index, 1)
520
+ }
521
+ }
522
+
523
+ /**
524
+ * The name of every attribute on the element, in the order they were set.
525
+ * @returns {Array<string>}
526
+ */
527
+ getAttributeNames () {
528
+ return this.currentAttributes().map(({
529
+ name
530
+ }) => name)
531
+ }
532
+
533
+ /**
534
+ * Whether the element has any attributes at all.
535
+ * @returns {boolean}
536
+ */
537
+ hasAttributes () {
538
+ return this.attributeList.length > 0
539
+ }
540
+
541
+ /**
542
+ * Add the attribute (with an empty value) when it is not present, or remove it when it is - unless force says
543
+ * which of those to do instead. Returns whether the attribute is present after the call.
544
+ * @param {string} attributeName
545
+ * @param {boolean} [force]
546
+ * @returns {boolean}
547
+ */
548
+ toggleAttribute (attributeName, force) {
549
+ const present = this.hasAttribute(attributeName)
550
+ const shouldHave = typeof force === 'boolean' ? force : !present
551
+ if (shouldHave && !present) {
552
+ this.setAttribute(attributeName, '')
553
+ } else if (!shouldHave && present) {
554
+ this.removeAttribute(attributeName)
555
+ }
556
+ return shouldHave
557
+ }
558
+
559
+ /**
560
+ * The size of the element and its position, settable directly - there is no layout engine here to compute it.
561
+ * @returns {DOMRect}
562
+ */
563
+ getBoundingClientRect () {
564
+ return this.boundingClientRect
565
+ }
566
+
567
+ /**
568
+ * The bounding rectangles for each line of text in the element, settable directly - there is no layout engine
569
+ * here to compute it.
570
+ * @returns {Array<DOMRect>}
571
+ */
572
+ getClientRects () {
573
+ return this.clientRects
574
+ }
575
+
576
+ /**
577
+ * The Animation objects currently active on the element, settable directly - there is no animation engine here.
578
+ * @returns {Array<*>}
579
+ */
580
+ getAnimations () {
581
+ return this.animations
582
+ }
583
+
584
+ /**
585
+ * Whether the element is expected to be visible, settable directly - there is no rendering here to check it.
586
+ * @returns {boolean}
587
+ */
588
+ checkVisibility () {
589
+ return this.isVisible
590
+ }
591
+
592
+ /**
593
+ * A read-only view of the element's own inline style declarations (there is no CSS cascade here, so this is not a
594
+ * real computed style - just what the element's own style object holds).
595
+ * @returns {{get: function(string): (string|undefined)}}
596
+ */
597
+ computedStyleMap () {
598
+ const style = this.style
599
+ return {
600
+ get: property => {
601
+ const value = style ? style.getPropertyValue(property) : ''
602
+ return value === '' ? undefined : value
603
+ }
604
+ }
605
+ }
606
+
607
+ /**
608
+ * Whether this element currently has capture of the given pointer.
609
+ * @param {number} pointerId
610
+ * @returns {boolean}
611
+ */
612
+ hasPointerCapture (pointerId) {
613
+ return this.capturedPointers.has(pointerId)
614
+ }
615
+
616
+ /**
617
+ * Give this element capture of the given pointer.
618
+ * @param {number} pointerId
619
+ * @returns {undefined}
620
+ */
621
+ setPointerCapture (pointerId) {
622
+ this.capturedPointers.add(pointerId)
623
+ }
624
+
625
+ /**
626
+ * Release this element's capture of the given pointer, if it had it.
627
+ * @param {number} pointerId
628
+ * @returns {undefined}
629
+ */
630
+ releasePointerCapture (pointerId) {
631
+ this.capturedPointers.delete(pointerId)
632
+ }
633
+
634
+ /**
635
+ * Scroll to the given position (or, given an options object, the position(s) it has). There is no real scrollable
636
+ * viewport here: this just sets scrollLeft / scrollTop.
637
+ * @param {number|{left: number, top: number}} [x=0]
638
+ * @param {number} [y=0]
639
+ * @returns {undefined}
640
+ */
641
+ scroll (x = 0, y = 0) {
642
+ if (typeof x === 'number') {
643
+ this.scrollLeft = x
644
+ this.scrollTop = y
645
+ return
646
+ }
647
+ if (typeof x.left === 'number') {
648
+ this.scrollLeft = x.left
649
+ }
650
+ if (typeof x.top === 'number') {
651
+ this.scrollTop = x.top
652
+ }
653
+ }
654
+
655
+ /**
656
+ * Scroll to the given position. An alias for scroll.
657
+ * @param {number|{left: number, top: number}} [x=0]
658
+ * @param {number} [y=0]
659
+ * @returns {undefined}
660
+ */
661
+ scrollTo (x = 0, y = 0) {
662
+ this.scroll(x, y)
663
+ }
664
+
665
+ /**
666
+ * Scroll by the given amount, relative to the current position.
667
+ * @param {number|{left: number, top: number}} [x=0]
668
+ * @param {number} [y=0]
669
+ * @returns {undefined}
670
+ */
671
+ scrollBy (x = 0, y = 0) {
672
+ if (typeof x === 'number') {
673
+ this.scroll(this.scrollLeft + x, this.scrollTop + y)
674
+ return
675
+ }
676
+ this.scroll({
677
+ left: this.scrollLeft + (x.left || 0),
678
+ top: this.scrollTop + (x.top || 0)
679
+ })
680
+ }
681
+
682
+ /**
683
+ * Scroll an ancestor until this element is in view. There is no real viewport here for that to mean anything, so
684
+ * this does nothing (override it on an instance in a test which needs to observe the call).
685
+ * @returns {undefined}
686
+ */
687
+ scrollIntoView () {}
688
+ /**
689
+ * Asynchronously ask for the element to be shown fullscreen. There is no real fullscreen here, so this just
690
+ * resolves, like a browser granting the request would.
691
+ * @returns {Promise<void>}
692
+ */
693
+ requestFullscreen () {
694
+ return Promise.resolve()
695
+ }
696
+
697
+ /**
698
+ * Asynchronously ask for the pointer to be locked to this element. There is no real pointer lock here, so this
699
+ * just resolves, like a browser granting the request would.
700
+ * @returns {Promise<void>}
701
+ */
702
+ requestPointerLock () {
703
+ return Promise.resolve()
704
+ }
705
+
706
+ /**
707
+ * Attach a shadow tree to this element and return its ShadowRoot. Throws when it already hosts one.
708
+ * @param {{mode: string}} options
709
+ * @returns {PseudoShadowRoot}
710
+ * @throws {Error}
711
+ */
712
+ attachShadow (options) {
713
+ if (this.shadowRootInstance) {
714
+ throw new Error('Shadow root cannot be created on a host which already hosts a shadow tree.')
715
+ }
716
+ const root = new ShadowRootService_1.ShadowRootService()
717
+ root.host = this
718
+ root.mode = options.mode
719
+ this.shadowRootInstance = root
720
+ return root
721
+ }
722
+
723
+ /**
724
+ * This element's shadow root, when it has one attached in 'open' mode, or null (including when the mode is
725
+ * 'closed' - it still exists, but is not reachable this way, like the DOM's).
726
+ * @returns {PseudoShadowRoot|null}
727
+ */
728
+ get shadowRoot () {
729
+ return this.shadowRootInstance && this.shadowRootInstance.mode === 'open' ? this.shadowRootInstance : null
730
+ }
731
+ }
732
+ exports.ElementService = ElementService