@danielgindi/dom-utils 1.0.11 → 2.0.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,132 +1,132 @@
1
- import { setCssProps } from './Css.js';
2
-
3
- /**
4
- * @param {string} tag
5
- * @param {Object<string, string>} [attrs]
6
- * @param {Node|Node[]} [children]
7
- * @returns {Element}
8
- */
9
- const createElement = function (tag, attrs, children) {
10
- let el = document.createElement(tag);
11
-
12
- if (attrs && typeof attrs === 'object')
13
- setElementAttrs(el, attrs);
14
-
15
- if (children) {
16
- if (children instanceof Node) {
17
- el.appendChild(children);
18
- }
19
- else if (Array.isArray(children)) {
20
- for (let child of children)
21
- el.appendChild(child);
22
- }
23
- }
24
-
25
- return el;
26
- };
27
-
28
- /**
29
- * @param {Element} el
30
- * @param {Object<string, string>} attrs
31
- */
32
- const setElementAttrs = function (el, attrs) {
33
- for (let [key, value] of Object.entries(attrs)) {
34
- if (value === null || value === undefined)
35
- continue;
36
-
37
- if (key === 'innerHTML')
38
- el.innerHTML = String(value);
39
- else if (key === 'css' && 'style' in el)
40
- setCssProps(el, value);
41
- else if (key in el && key !== 'style')
42
- el[key] = value;
43
- else el.setAttribute(key, String(value));
44
- }
45
- };
46
-
47
- /**
48
- * @param {Element} el
49
- * @param {string} selector
50
- * @param {Node} [until]
51
- * @returns {Node|null}
52
- */
53
- const closestUntil = function (el, selector, until) {
54
- if ('closest' in el && !until)
55
- return el.closest(selector);
56
-
57
- const matches = Element.prototype.matches ||
58
- Element.prototype.msMatchesSelector ||
59
- Element.prototype.webkitMatchesSelector;
60
-
61
- do {
62
- if (matches.call(el, selector))
63
- return el;
64
-
65
- if (until && el === until)
66
- return null;
67
-
68
- el = el.parentElement || el.parentNode;
69
- } while (el !== null && el.nodeType === 1);
70
-
71
- return null;
72
- };
73
-
74
- /**
75
- * @param {Element} el
76
- * @param {string} selector
77
- * @returns {Element|null}
78
- */
79
- const prev = function (el, selector) {
80
- if (!el) return null;
81
-
82
- const matches = Element.prototype.matches ||
83
- Element.prototype.msMatchesSelector ||
84
- Element.prototype.webkitMatchesSelector;
85
-
86
- do {
87
- if (matches.call(el, selector))
88
- return el;
89
-
90
- el = el.previousElementSibling || el.previousSibling;
91
- } while (el !== null && el.nodeType === 1);
92
-
93
- return null;
94
- };
95
-
96
- /**
97
- * @param {Element} el
98
- * @param {string} selector
99
- * @returns {null|Element}
100
- */
101
- const next = function (el, selector) {
102
- if (!el) return null;
103
-
104
- const matches = Element.prototype.matches ||
105
- Element.prototype.msMatchesSelector ||
106
- Element.prototype.webkitMatchesSelector;
107
-
108
- do {
109
- if (matches.call(el, selector))
110
- return el;
111
-
112
- el = el.nextElementSibling || el.nextSibling;
113
- } while (el !== null && el.nodeType === 1);
114
-
115
- return null;
116
- };
117
-
118
- const unwrapChildren = function (el) {
119
- let parent = el.parentNode;
120
- while (el.firstChild)
121
- parent.insertBefore(el.firstChild, el);
122
- parent.removeChild(el);
123
- };
124
-
125
- export {
126
- createElement,
127
- setElementAttrs,
128
- closestUntil,
129
- prev,
130
- next,
131
- unwrapChildren,
132
- };
1
+ import { setCssProps } from './Css';
2
+
3
+ /**
4
+ * @param {string} tag
5
+ * @param {Object<string, string>} [attrs]
6
+ * @param {Node|Node[]} [children]
7
+ * @returns {Element}
8
+ */
9
+ const createElement = function (tag: string, attrs?: Record<string, unknown>, children?: Node | Node[]): Element {
10
+ let el = document.createElement(tag);
11
+
12
+ if (attrs && typeof attrs === 'object')
13
+ setElementAttrs(el, attrs);
14
+
15
+ if (children) {
16
+ if (children instanceof Node) {
17
+ el.appendChild(children);
18
+ }
19
+ else if (Array.isArray(children)) {
20
+ for (let child of children)
21
+ el.appendChild(child);
22
+ }
23
+ }
24
+
25
+ return el;
26
+ };
27
+
28
+ /**
29
+ * @param {Element} el
30
+ * @param {Object<string, string>} attrs
31
+ */
32
+ const setElementAttrs = function (el: Element, attrs: Record<string, unknown>): void {
33
+ for (let [key, value] of Object.entries(attrs)) {
34
+ if (value === null || value === undefined)
35
+ continue;
36
+
37
+ if (key === 'innerHTML')
38
+ el.innerHTML = String(value);
39
+ else if (key === 'css' && 'style' in el)
40
+ setCssProps(el as HTMLElement, value as Record<string, unknown>);
41
+ else if (key in el && key !== 'style')
42
+ (el as unknown as Record<string, unknown>)[key] = value;
43
+ else el.setAttribute(key, String(value));
44
+ }
45
+ };
46
+
47
+ /**
48
+ * @param {Element} el
49
+ * @param {string} selector
50
+ * @param {Node} [until]
51
+ * @returns {Node|null}
52
+ */
53
+ const closestUntil = function (el: Element, selector: string, until?: Node): Element | null {
54
+ if ('closest' in el && !until)
55
+ return el.closest(selector);
56
+
57
+ const matches = Element.prototype.matches ||
58
+ (Element.prototype as Element & { msMatchesSelector?: typeof Element.prototype.matches }).msMatchesSelector ||
59
+ (Element.prototype as Element & { webkitMatchesSelector?: typeof Element.prototype.matches }).webkitMatchesSelector;
60
+
61
+ do {
62
+ if (matches.call(el, selector))
63
+ return el;
64
+
65
+ if (until && el === until)
66
+ return null;
67
+
68
+ el = (el.parentElement || el.parentNode) as Element;
69
+ } while (el !== null && el.nodeType === 1);
70
+
71
+ return null;
72
+ };
73
+
74
+ /**
75
+ * @param {Element} el
76
+ * @param {string} selector
77
+ * @returns {Element|null}
78
+ */
79
+ const prev = function (el: Element, selector: string): Element | null {
80
+ if (!el) return null;
81
+
82
+ const matches = Element.prototype.matches ||
83
+ (Element.prototype as Element & { msMatchesSelector?: typeof Element.prototype.matches }).msMatchesSelector ||
84
+ (Element.prototype as Element & { webkitMatchesSelector?: typeof Element.prototype.matches }).webkitMatchesSelector;
85
+
86
+ do {
87
+ if (matches.call(el, selector))
88
+ return el;
89
+
90
+ el = (el.previousElementSibling || el.previousSibling) as Element;
91
+ } while (el !== null && el.nodeType === 1);
92
+
93
+ return null;
94
+ };
95
+
96
+ /**
97
+ * @param {Element} el
98
+ * @param {string} selector
99
+ * @returns {null|Element}
100
+ */
101
+ const next = function (el: Element, selector: string): Element | null {
102
+ if (!el) return null;
103
+
104
+ const matches = Element.prototype.matches ||
105
+ (Element.prototype as Element & { msMatchesSelector?: typeof Element.prototype.matches }).msMatchesSelector ||
106
+ (Element.prototype as Element & { webkitMatchesSelector?: typeof Element.prototype.matches }).webkitMatchesSelector;
107
+
108
+ do {
109
+ if (matches.call(el, selector))
110
+ return el;
111
+
112
+ el = (el.nextElementSibling || el.nextSibling) as Element;
113
+ } while (el !== null && el.nodeType === 1);
114
+
115
+ return null;
116
+ };
117
+
118
+ const unwrapChildren = function (el: Element): void {
119
+ let parent = el.parentNode;
120
+ while (el.firstChild)
121
+ parent.insertBefore(el.firstChild, el);
122
+ parent.removeChild(el);
123
+ };
124
+
125
+ export {
126
+ createElement,
127
+ setElementAttrs,
128
+ closestUntil,
129
+ prev,
130
+ next,
131
+ unwrapChildren,
132
+ };