forked from dmonad/lib0
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdom.js
More file actions
291 lines (256 loc) · 6.9 KB
/
Copy pathdom.js
File metadata and controls
291 lines (256 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* eslint-env browser */
/**
* Utility module to work with the DOM.
*
* @module dom
*/
import * as pair from './pair.js'
import * as map from './map.js'
/* istanbul ignore next */
/**
* @type {Document}
*/
export const doc = /** @type {Document} */ (typeof document !== 'undefined' ? document : {})
/**
* @param {string} name
* @return {HTMLElement}
*/
/* istanbul ignore next */
export const createElement = name => doc.createElement(name)
/**
* @return {DocumentFragment}
*/
/* istanbul ignore next */
export const createDocumentFragment = () => doc.createDocumentFragment()
/**
* @param {string} text
* @return {Text}
*/
/* istanbul ignore next */
export const createTextNode = text => doc.createTextNode(text)
/* istanbul ignore next */
export const domParser = /** @type {DOMParser} */ (typeof DOMParser !== 'undefined' ? new DOMParser() : null)
/**
* @param {HTMLElement} el
* @param {string} name
* @param {Object} opts
*/
/* istanbul ignore next */
export const emitCustomEvent = (el, name, opts) => el.dispatchEvent(new CustomEvent(name, opts))
/**
* @param {Element} el
* @param {Array<pair.Pair<string,string|boolean>>} attrs Array of key-value pairs
* @return {Element}
*/
/* istanbul ignore next */
export const setAttributes = (el, attrs) => {
pair.forEach(attrs, (key, value) => {
if (value === false) {
el.removeAttribute(key)
} else if (value === true) {
el.setAttribute(key, '')
} else {
// @ts-ignore
el.setAttribute(key, value)
}
})
return el
}
/**
* @param {Element} el
* @param {Map<string, string>} attrs Array of key-value pairs
* @return {Element}
*/
/* istanbul ignore next */
export const setAttributesMap = (el, attrs) => {
attrs.forEach((value, key) => { el.setAttribute(key, value) })
return el
}
/**
* @param {Array<Node>|HTMLCollection} children
* @return {DocumentFragment}
*/
/* istanbul ignore next */
export const fragment = children => {
const fragment = createDocumentFragment()
for (let i = 0; i < children.length; i++) {
appendChild(fragment, children[i])
}
return fragment
}
/**
* @param {Element} parent
* @param {Array<Node>} nodes
* @return {Element}
*/
/* istanbul ignore next */
export const append = (parent, nodes) => {
appendChild(parent, fragment(nodes))
return parent
}
/**
* @param {HTMLElement} el
*/
/* istanbul ignore next */
export const remove = el => el.remove()
/**
* @param {EventTarget} el
* @param {string} name
* @param {EventListener} f
*/
/* istanbul ignore next */
export const addEventListener = (el, name, f) => el.addEventListener(name, f)
/**
* @param {EventTarget} el
* @param {string} name
* @param {EventListener} f
*/
/* istanbul ignore next */
export const removeEventListener = (el, name, f) => el.removeEventListener(name, f)
/**
* @param {Node} node
* @param {Array<pair.Pair<string,EventListener>>} listeners
* @return {Node}
*/
/* istanbul ignore next */
export const addEventListeners = (node, listeners) => {
pair.forEach(listeners, (name, f) => addEventListener(node, name, f))
return node
}
/**
* @param {Node} node
* @param {Array<pair.Pair<string,EventListener>>} listeners
* @return {Node}
*/
/* istanbul ignore next */
export const removeEventListeners = (node, listeners) => {
pair.forEach(listeners, (name, f) => removeEventListener(node, name, f))
return node
}
/**
* @param {string} name
* @param {Array<pair.Pair<string,string>|pair.Pair<string,boolean>>} attrs Array of key-value pairs
* @param {Array<Node>} children
* @return {Element}
*/
/* istanbul ignore next */
export const element = (name, attrs = [], children = []) =>
append(setAttributes(createElement(name), attrs), children)
/**
* @param {number} width
* @param {number} height
*/
/* istanbul ignore next */
export const canvas = (width, height) => {
const c = /** @type {HTMLCanvasElement} */ (createElement('canvas'))
c.height = height
c.width = width
return c
}
/**
* @param {string} t
* @return {Text}
*/
/* istanbul ignore next */
export const text = createTextNode
/**
* @param {pair.Pair<string,string>} pair
*/
/* istanbul ignore next */
export const pairToStyleString = pair => `${pair.left}:${pair.right};`
/**
* @param {Array<pair.Pair<string,string>>} pairs
* @return {string}
*/
/* istanbul ignore next */
export const pairsToStyleString = pairs => pairs.map(pairToStyleString).join('')
/**
* @param {Map<string,string>} m
* @return {string}
*/
/* istanbul ignore next */
export const mapToStyleString = m => map.map(m, (value, key) => `${key}:${value};`).join('')
/**
* @todo should always query on a dom element
*
* @param {HTMLElement|ShadowRoot} el
* @param {string} query
* @return {HTMLElement | null}
*/
/* istanbul ignore next */
export const querySelector = (el, query) => el.querySelector(query)
/**
* @param {HTMLElement|ShadowRoot} el
* @param {string} query
* @return {NodeListOf<HTMLElement>}
*/
/* istanbul ignore next */
export const querySelectorAll = (el, query) => el.querySelectorAll(query)
/**
* @param {string} id
* @return {HTMLElement}
*/
/* istanbul ignore next */
export const getElementById = id => /** @type {HTMLElement} */ (doc.getElementById(id))
/**
* @param {string} html
* @return {HTMLElement}
*/
/* istanbul ignore next */
const _parse = html => domParser.parseFromString(`<html><body>${html}</body></html>`, 'text/html').body
/**
* @param {string} html
* @return {DocumentFragment}
*/
/* istanbul ignore next */
export const parseFragment = html => fragment(/** @type {any} */ (_parse(html).childNodes))
/**
* @param {string} html
* @return {HTMLElement}
*/
/* istanbul ignore next */
export const parseElement = html => /** @type HTMLElement */ (_parse(html).firstElementChild)
/**
* @param {HTMLElement} oldEl
* @param {HTMLElement|DocumentFragment} newEl
*/
/* istanbul ignore next */
export const replaceWith = (oldEl, newEl) => oldEl.replaceWith(newEl)
/**
* @param {HTMLElement} parent
* @param {HTMLElement} el
* @param {Node|null} ref
* @return {HTMLElement}
*/
/* istanbul ignore next */
export const insertBefore = (parent, el, ref) => parent.insertBefore(el, ref)
/**
* @param {Node} parent
* @param {Node} child
* @return {Node}
*/
/* istanbul ignore next */
export const appendChild = (parent, child) => parent.appendChild(child)
export const ELEMENT_NODE = doc.ELEMENT_NODE
export const TEXT_NODE = doc.TEXT_NODE
export const CDATA_SECTION_NODE = doc.CDATA_SECTION_NODE
export const COMMENT_NODE = doc.COMMENT_NODE
export const DOCUMENT_NODE = doc.DOCUMENT_NODE
export const DOCUMENT_TYPE_NODE = doc.DOCUMENT_TYPE_NODE
export const DOCUMENT_FRAGMENT_NODE = doc.DOCUMENT_FRAGMENT_NODE
/**
* @param {any} node
* @param {number} type
*/
export const checkNodeType = (node, type) => node.nodeType === type
/**
* @param {Node} parent
* @param {HTMLElement} child
*/
export const isParentOf = (parent, child) => {
let p = child.parentNode
while (p && p !== parent) {
p = p.parentNode
}
return p === parent
}