pseudo-dom 0.10.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.
@@ -1,202 +1,269 @@
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.HTMLElementService = void 0
14
- const ElementService_1 = require('./ElementService')
15
- const createEvent_1 = __importDefault(require('../factories/createEvent'))
16
- const activeElement_1 = require('../functions/activeElement')
17
- const createStyleDeclaration_1 = __importDefault(require('../factories/createStyleDeclaration'))
18
- const createDataset_1 = __importDefault(require('../factories/createDataset'))
19
- /**
20
- * Simulate the behaviour of the HTMLElement Class when there is no DOM available.
21
- * @author Joshua Heagle <joshuaheagle@gmail.com>
22
- * @class
23
- * @augments PseudoElement
24
- * @property {boolean} hidden - State of whether element is visible
25
- * @property {number} offsetHeight - The height of the element as offset by the parent element
26
- * @property {number} offsetLeft - The position of the left side of the element based on the parent element
27
- * @property {PseudoHTMLElement} offsetParent - A reference to the closest positioned parent element
28
- * @property {number} offsetTop - The position of the top side of the element based on the parent element
29
- * @property {number} offsetWidth - The width of the element as offset by the parent element
30
- * @property {CSSStyleDeclarationService} style - The element's inline styles, live and settable per property
31
- * @property {Object.<string, string>} dataset - The element's data-* attributes, live, under their camelCase names
32
- * @property {string} title - The title attribute which affects the text visible on hover
33
- */
34
- class HTMLElementService extends ElementService_1.ElementService {
35
- /**
36
- * Simulate the HTMLElement object when the Dom is not available
37
- * @param {Object} [elementOptions={}]
38
- * @param {string} [elementOptions.tagName='']
39
- * @param {PseudoNode|Object} [elementOptions.parent={}]
40
- * @param {Array} [elementOptions.children=[]]
41
- * @constructor
42
- */
43
- constructor ({
44
- tagName = '',
45
- parent = null,
46
- children = []
47
- } = {}) {
48
- super({
49
- tagName,
50
- attributes: [{
51
- name: 'hidden',
52
- value: false
53
- }, {
54
- name: 'offsetHeight',
55
- value: 0
56
- }, {
57
- name: 'offsetLeft',
58
- value: 0
59
- }, {
60
- name: 'offsetParent',
61
- value: null
62
- }, {
63
- name: 'offsetTop',
64
- value: 0
65
- }, {
66
- name: 'offsetWidth',
67
- value: 0
68
- }, {
69
- name: 'title',
70
- value: ''
71
- }],
72
- parent,
73
- children
74
- })
75
- this.styleDeclaration = (0, createStyleDeclaration_1.default)()
76
- this.datasetProxy = (0, createDataset_1.default)(this)
77
- }
78
-
79
- /**
80
- * The element's inline styles: a live CSSStyleDeclaration-like object, so both style.setProperty('color', 'red')
81
- * and style.color = 'red' work.
82
- * @returns {CSSStyleDeclarationService}
83
- */
84
- get style () {
85
- return this.styleDeclaration
86
- }
87
-
88
- /**
89
- * The element's data-* attributes, live, under their camelCase names (data-foo-bar <-> dataset.fooBar). Backed
90
- * directly by getAttribute / setAttribute, so it is never out of sync with the attributes themselves.
91
- * @returns {Object.<string, string>}
92
- */
93
- get dataset () {
94
- return this.datasetProxy
95
- }
96
-
97
- /**
98
- * Style is not attribute-backed like most properties (see the constructor), so cloneNode needs its own copy of it.
99
- * @returns {NodeService}
100
- */
101
- cloneShallow () {
102
- const copy = super.cloneShallow()
103
- copy.style.cssText = this.style.cssText
104
- return copy
105
- }
106
-
107
- /**
108
- * Style is not attribute-backed like most properties, so isEqualNode needs to compare it separately too.
109
- * @param {NodeService} other The node to compare with
110
- * @returns {boolean}
111
- */
112
- equalsShallow (other) {
113
- return super.equalsShallow(other) && this.style.cssText === other.style.cssText
114
- }
115
-
116
- /**
117
- * Whether this element can have the focus: form controls and links which are not disabled, and anything with a tabindex.
118
- * @returns {boolean}
119
- */
120
- get canFocus () {
121
- if (this.hasAttribute('disabled')) {
122
- return false
123
- }
124
- switch (this.tagName) {
125
- case 'button':
126
- case 'select':
127
- case 'textarea':
128
- return true
129
- case 'input':
130
- return String(this.getAttribute('type') || '').toLowerCase() !== 'hidden'
131
- case 'a':
132
- return this.hasAttribute('href') || this.hasAttribute('tabindex')
133
- default:
134
- return this.hasAttribute('tabindex')
135
- }
136
- }
137
-
138
- /**
139
- * Click the element: a click event is sent to it, which bubbles and can be cancelled, like one from a user but a
140
- * script made it (so it is not trusted). A disabled element does nothing.
141
- */
142
- click () {
143
- if (this.hasAttribute('disabled')) {
144
- return
145
- }
146
- this.dispatchEvent((0, createEvent_1.default)('click', {}, {
147
- browser: true
148
- }))
149
- }
150
-
151
- /**
152
- * Give the element the focus. The element which had it gets blur then focusout, and this one gets focus then
153
- * focusin (blur and focus do not bubble, focusin and focusout do). Nothing happens when the element cannot have the
154
- * focus or already has it.
155
- */
156
- focus () {
157
- const root = this.getRootNode()
158
- const previous = (0, activeElement_1.getActiveElement)(root)
159
- if (!this.canFocus || previous === this) {
160
- return
161
- }
162
- const send = (target, type, relatedTarget) => {
163
- target.dispatchEvent((0, createEvent_1.default)(type, {
164
- relatedTarget
165
- }, {
166
- browser: true,
167
- trusted: true
168
- }))
169
- }
170
- if (previous) {
171
- send(previous, 'blur', this)
172
- send(previous, 'focusout', this)
173
- }
174
- (0, activeElement_1.setActiveElement)(root, this)
175
- send(this, 'focus', previous)
176
- send(this, 'focusin', previous)
177
- }
178
-
179
- /**
180
- * Take the focus away from the element, when it has it: it gets blur then focusout.
181
- */
182
- blur () {
183
- const root = this.getRootNode()
184
- if ((0, activeElement_1.getActiveElement)(root) !== this) {
185
- return
186
- }
187
- (0, activeElement_1.setActiveElement)(root, null)
188
- this.dispatchEvent((0, createEvent_1.default)('blur', {
189
- relatedTarget: null
190
- }, {
191
- browser: true,
192
- trusted: true
193
- }))
194
- this.dispatchEvent((0, createEvent_1.default)('focusout', {
195
- relatedTarget: null
196
- }, {
197
- browser: true,
198
- trusted: true
199
- }))
200
- }
201
- }
202
- exports.HTMLElementService = HTMLElementService
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.HTMLElementService = void 0
14
+ const ElementService_1 = require('./ElementService')
15
+ const createEvent_1 = __importDefault(require('../factories/createEvent'))
16
+ const activeElement_1 = require('../functions/activeElement')
17
+ const createStyleDeclaration_1 = __importDefault(require('../factories/createStyleDeclaration'))
18
+ const createDataset_1 = __importDefault(require('../factories/createDataset'))
19
+ const parseHTML_1 = __importDefault(require('../factories/parseHTML'))
20
+ /**
21
+ * Simulate the behaviour of the HTMLElement Class when there is no DOM available.
22
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
23
+ * @class
24
+ * @augments PseudoElement
25
+ * @property {boolean} hidden - State of whether element is visible
26
+ * @property {number} offsetHeight - The height of the element as offset by the parent element
27
+ * @property {number} offsetLeft - The position of the left side of the element based on the parent element
28
+ * @property {PseudoHTMLElement} offsetParent - A reference to the closest positioned parent element
29
+ * @property {number} offsetTop - The position of the top side of the element based on the parent element
30
+ * @property {number} offsetWidth - The width of the element as offset by the parent element
31
+ * @property {CSSStyleDeclarationService} style - The element's inline styles, live and settable per property
32
+ * @property {Object.<string, string>} dataset - The element's data-* attributes, live, under their camelCase names
33
+ * @property {string} title - The title attribute which affects the text visible on hover
34
+ */
35
+ class HTMLElementService extends ElementService_1.ElementService {
36
+ /**
37
+ * Simulate the HTMLElement object when the Dom is not available
38
+ * @param {Object} [elementOptions={}]
39
+ * @param {string} [elementOptions.tagName='']
40
+ * @param {PseudoNode|Object} [elementOptions.parent={}]
41
+ * @param {Array} [elementOptions.children=[]]
42
+ * @constructor
43
+ */
44
+ constructor ({
45
+ tagName = '',
46
+ parent = null,
47
+ children = []
48
+ } = {}) {
49
+ super({
50
+ tagName,
51
+ attributes: [{
52
+ name: 'hidden',
53
+ value: false
54
+ }, {
55
+ name: 'offsetHeight',
56
+ value: 0
57
+ }, {
58
+ name: 'offsetLeft',
59
+ value: 0
60
+ }, {
61
+ name: 'offsetParent',
62
+ value: null
63
+ }, {
64
+ name: 'offsetTop',
65
+ value: 0
66
+ }, {
67
+ name: 'offsetWidth',
68
+ value: 0
69
+ }, {
70
+ name: 'title',
71
+ value: ''
72
+ }],
73
+ parent,
74
+ children
75
+ })
76
+ this.styleDeclaration = (0, createStyleDeclaration_1.default)()
77
+ this.datasetProxy = (0, createDataset_1.default)(this)
78
+ }
79
+
80
+ /**
81
+ * The element's inline styles: a live CSSStyleDeclaration-like object, so both style.setProperty('color', 'red')
82
+ * and style.color = 'red' work.
83
+ * @returns {CSSStyleDeclarationService}
84
+ */
85
+ get style () {
86
+ return this.styleDeclaration
87
+ }
88
+
89
+ /**
90
+ * The element's data-* attributes, live, under their camelCase names (data-foo-bar <-> dataset.fooBar). Backed
91
+ * directly by getAttribute / setAttribute, so it is never out of sync with the attributes themselves.
92
+ * @returns {Object.<string, string>}
93
+ */
94
+ get dataset () {
95
+ return this.datasetProxy
96
+ }
97
+
98
+ /**
99
+ * Style is not attribute-backed like most properties (see the constructor), so cloneNode needs its own copy of it.
100
+ * @returns {NodeService}
101
+ */
102
+ cloneShallow () {
103
+ const copy = super.cloneShallow()
104
+ copy.style.cssText = this.style.cssText
105
+ return copy
106
+ }
107
+
108
+ /**
109
+ * Style is not attribute-backed like most properties, so isEqualNode needs to compare it separately too.
110
+ * @param {NodeService} other The node to compare with
111
+ * @returns {boolean}
112
+ */
113
+ equalsShallow (other) {
114
+ return super.equalsShallow(other) && this.style.cssText === other.style.cssText
115
+ }
116
+
117
+ /**
118
+ * Parses html with this class building each new element (matches HTML: parsed elements behave like plain
119
+ * HTMLElements, not whatever specialized class happens to be setting innerHTML / outerHTML).
120
+ * @param {string} html
121
+ * @returns {Array<*>}
122
+ */
123
+ parse (html) {
124
+ return (0, parseHTML_1.default)(html, this.ownerDocument, HTMLElementService)
125
+ }
126
+
127
+ /**
128
+ * Replace this element's children by parsing html. innerHTML's setter is here rather than on ElementService (which
129
+ * only has the getter) because building the new elements needs a concrete element class - see parse().
130
+ * @param {string} html
131
+ * @returns {undefined}
132
+ */
133
+ set innerHTML (html) {
134
+ this.replaceChildren(...this.parse(html))
135
+ }
136
+
137
+ get innerHTML () {
138
+ return super.innerHTML
139
+ }
140
+
141
+ /**
142
+ * Replace this element itself, in its parent, by parsing html. Does nothing when it has no parent, like
143
+ * replaceWith. outerHTML's setter is here rather than on ElementService for the same reason as innerHTML's.
144
+ * @param {string} html
145
+ * @returns {undefined}
146
+ */
147
+ set outerHTML (html) {
148
+ this.replaceWith(...this.parse(html))
149
+ }
150
+
151
+ get outerHTML () {
152
+ return super.outerHTML
153
+ }
154
+
155
+ /**
156
+ * Parse html and insert the resulting nodes at the given position, like insertAdjacentElement /
157
+ * insertAdjacentText.
158
+ * @param {string} position beforebegin, afterbegin, beforeend or afterend
159
+ * @param {string} html The markup to parse
160
+ * @returns {undefined}
161
+ * @throws {Error} When the position is not one of the four above
162
+ */
163
+ insertAdjacentHTML (position, html) {
164
+ const nodes = this.parse(html)
165
+ switch (position) {
166
+ case 'beforebegin':
167
+ this.before(...nodes)
168
+ return
169
+ case 'afterbegin':
170
+ this.prepend(...nodes)
171
+ return
172
+ case 'beforeend':
173
+ this.append(...nodes)
174
+ return
175
+ case 'afterend':
176
+ this.after(...nodes)
177
+ return
178
+ default:
179
+ throw new Error(`insertAdjacentHTML: "${position}" is not one of beforebegin, afterbegin, beforeend, afterend.`)
180
+ }
181
+ }
182
+
183
+ /**
184
+ * Whether this element can have the focus: form controls and links which are not disabled, and anything with a tabindex.
185
+ * @returns {boolean}
186
+ */
187
+ get canFocus () {
188
+ if (this.hasAttribute('disabled')) {
189
+ return false
190
+ }
191
+ switch (this.tagName) {
192
+ case 'button':
193
+ case 'select':
194
+ case 'textarea':
195
+ return true
196
+ case 'input':
197
+ return String(this.getAttribute('type') || '').toLowerCase() !== 'hidden'
198
+ case 'a':
199
+ return this.hasAttribute('href') || this.hasAttribute('tabindex')
200
+ default:
201
+ return this.hasAttribute('tabindex')
202
+ }
203
+ }
204
+
205
+ /**
206
+ * Click the element: a click event is sent to it, which bubbles and can be cancelled, like one from a user but a
207
+ * script made it (so it is not trusted). A disabled element does nothing.
208
+ */
209
+ click () {
210
+ if (this.hasAttribute('disabled')) {
211
+ return
212
+ }
213
+ this.dispatchEvent((0, createEvent_1.default)('click', {}, {
214
+ browser: true
215
+ }))
216
+ }
217
+
218
+ /**
219
+ * Give the element the focus. The element which had it gets blur then focusout, and this one gets focus then
220
+ * focusin (blur and focus do not bubble, focusin and focusout do). Nothing happens when the element cannot have the
221
+ * focus or already has it.
222
+ */
223
+ focus () {
224
+ const root = this.getRootNode()
225
+ const previous = (0, activeElement_1.getActiveElement)(root)
226
+ if (!this.canFocus || previous === this) {
227
+ return
228
+ }
229
+ const send = (target, type, relatedTarget) => {
230
+ target.dispatchEvent((0, createEvent_1.default)(type, {
231
+ relatedTarget
232
+ }, {
233
+ browser: true,
234
+ trusted: true
235
+ }))
236
+ }
237
+ if (previous) {
238
+ send(previous, 'blur', this)
239
+ send(previous, 'focusout', this)
240
+ }
241
+ (0, activeElement_1.setActiveElement)(root, this)
242
+ send(this, 'focus', previous)
243
+ send(this, 'focusin', previous)
244
+ }
245
+
246
+ /**
247
+ * Take the focus away from the element, when it has it: it gets blur then focusout.
248
+ */
249
+ blur () {
250
+ const root = this.getRootNode()
251
+ if ((0, activeElement_1.getActiveElement)(root) !== this) {
252
+ return
253
+ }
254
+ (0, activeElement_1.setActiveElement)(root, null)
255
+ this.dispatchEvent((0, createEvent_1.default)('blur', {
256
+ relatedTarget: null
257
+ }, {
258
+ browser: true,
259
+ trusted: true
260
+ }))
261
+ this.dispatchEvent((0, createEvent_1.default)('focusout', {
262
+ relatedTarget: null
263
+ }, {
264
+ browser: true,
265
+ trusted: true
266
+ }))
267
+ }
268
+ }
269
+ exports.HTMLElementService = HTMLElementService
@@ -1 +1 @@
1
- "use strict";var __importDefault=function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.HTMLElementService=void 0;const ElementService_1=require("./ElementService"),createEvent_1=__importDefault(require("../factories/createEvent")),activeElement_1=require("../functions/activeElement"),createStyleDeclaration_1=__importDefault(require("../factories/createStyleDeclaration")),createDataset_1=__importDefault(require("../factories/createDataset"));class HTMLElementService extends ElementService_1.ElementService{constructor({tagName:e="",parent:t=null,children:a=[]}={}){super({tagName:e,attributes:[{name:"hidden",value:!1},{name:"offsetHeight",value:0},{name:"offsetLeft",value:0},{name:"offsetParent",value:null},{name:"offsetTop",value:0},{name:"offsetWidth",value:0},{name:"title",value:""}],parent:t,children:a}),this.styleDeclaration=(0,createStyleDeclaration_1.default)(),this.datasetProxy=(0,createDataset_1.default)(this)}get style(){return this.styleDeclaration}get dataset(){return this.datasetProxy}cloneShallow(){const e=super.cloneShallow();return e.style.cssText=this.style.cssText,e}equalsShallow(e){return super.equalsShallow(e)&&this.style.cssText===e.style.cssText}get canFocus(){if(this.hasAttribute("disabled"))return!1;switch(this.tagName){case"button":case"select":case"textarea":return!0;case"input":return"hidden"!==String(this.getAttribute("type")||"").toLowerCase();case"a":return this.hasAttribute("href")||this.hasAttribute("tabindex");default:return this.hasAttribute("tabindex")}}click(){this.hasAttribute("disabled")||this.dispatchEvent((0,createEvent_1.default)("click",{},{browser:!0}))}focus(){const e=this.getRootNode(),t=(0,activeElement_1.getActiveElement)(e);if(!this.canFocus||t===this)return;const a=(e,t,a)=>{e.dispatchEvent((0,createEvent_1.default)(t,{relatedTarget:a},{browser:!0,trusted:!0}))};t&&(a(t,"blur",this),a(t,"focusout",this)),(0,activeElement_1.setActiveElement)(e,this),a(this,"focus",t),a(this,"focusin",t)}blur(){const e=this.getRootNode();(0,activeElement_1.getActiveElement)(e)===this&&((0,activeElement_1.setActiveElement)(e,null),this.dispatchEvent((0,createEvent_1.default)("blur",{relatedTarget:null},{browser:!0,trusted:!0})),this.dispatchEvent((0,createEvent_1.default)("focusout",{relatedTarget:null},{browser:!0,trusted:!0})))}}exports.HTMLElementService=HTMLElementService;
1
+ "use strict";var __importDefault=function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.HTMLElementService=void 0;const ElementService_1=require("./ElementService"),createEvent_1=__importDefault(require("../factories/createEvent")),activeElement_1=require("../functions/activeElement"),createStyleDeclaration_1=__importDefault(require("../factories/createStyleDeclaration")),createDataset_1=__importDefault(require("../factories/createDataset")),parseHTML_1=__importDefault(require("../factories/parseHTML"));class HTMLElementService extends ElementService_1.ElementService{constructor({tagName:e="",parent:t=null,children:r=[]}={}){super({tagName:e,attributes:[{name:"hidden",value:!1},{name:"offsetHeight",value:0},{name:"offsetLeft",value:0},{name:"offsetParent",value:null},{name:"offsetTop",value:0},{name:"offsetWidth",value:0},{name:"title",value:""}],parent:t,children:r}),this.styleDeclaration=(0,createStyleDeclaration_1.default)(),this.datasetProxy=(0,createDataset_1.default)(this)}get style(){return this.styleDeclaration}get dataset(){return this.datasetProxy}cloneShallow(){const e=super.cloneShallow();return e.style.cssText=this.style.cssText,e}equalsShallow(e){return super.equalsShallow(e)&&this.style.cssText===e.style.cssText}parse(e){return(0,parseHTML_1.default)(e,this.ownerDocument,HTMLElementService)}set innerHTML(e){this.replaceChildren(...this.parse(e))}get innerHTML(){return super.innerHTML}set outerHTML(e){this.replaceWith(...this.parse(e))}get outerHTML(){return super.outerHTML}insertAdjacentHTML(e,t){const r=this.parse(t);switch(e){case"beforebegin":return void this.before(...r);case"afterbegin":return void this.prepend(...r);case"beforeend":return void this.append(...r);case"afterend":return void this.after(...r);default:throw new Error(`insertAdjacentHTML: "${e}" is not one of beforebegin, afterbegin, beforeend, afterend.`)}}get canFocus(){if(this.hasAttribute("disabled"))return!1;switch(this.tagName){case"button":case"select":case"textarea":return!0;case"input":return"hidden"!==String(this.getAttribute("type")||"").toLowerCase();case"a":return this.hasAttribute("href")||this.hasAttribute("tabindex");default:return this.hasAttribute("tabindex")}}click(){this.hasAttribute("disabled")||this.dispatchEvent((0,createEvent_1.default)("click",{},{browser:!0}))}focus(){const e=this.getRootNode(),t=(0,activeElement_1.getActiveElement)(e);if(!this.canFocus||t===this)return;const r=(e,t,r)=>{e.dispatchEvent((0,createEvent_1.default)(t,{relatedTarget:r},{browser:!0,trusted:!0}))};t&&(r(t,"blur",this),r(t,"focusout",this)),(0,activeElement_1.setActiveElement)(e,this),r(this,"focus",t),r(this,"focusin",t)}blur(){const e=this.getRootNode();(0,activeElement_1.getActiveElement)(e)===this&&((0,activeElement_1.setActiveElement)(e,null),this.dispatchEvent((0,createEvent_1.default)("blur",{relatedTarget:null},{browser:!0,trusted:!0})),this.dispatchEvent((0,createEvent_1.default)("focusout",{relatedTarget:null},{browser:!0,trusted:!0})))}}exports.HTMLElementService=HTMLElementService;