@danielgindi/dom-utils 1.0.10 → 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.
- package/README.md +13 -7
- package/dist/lib.cjs.js +318 -434
- package/dist/lib.cjs.js.map +1 -1
- package/dist/lib.cjs.min.js +2 -2
- package/dist/lib.cjs.min.js.map +1 -1
- package/dist/lib.es6.js +977 -1093
- package/dist/lib.es6.js.map +1 -1
- package/dist/lib.es6.min.js +2 -2
- package/dist/lib.es6.min.js.map +1 -1
- package/dist/lib.umd.js +318 -434
- package/dist/lib.umd.js.map +1 -1
- package/dist/lib.umd.min.js +2 -2
- package/dist/lib.umd.min.js.map +1 -1
- package/dist/types/Css.d.ts +130 -0
- package/dist/types/Dom.d.ts +33 -0
- package/dist/types/DomCompat.d.ts +55 -0
- package/dist/types/DomEventsSink.d.ts +20 -0
- package/dist/types/ScrollHelper.d.ts +39 -0
- package/dist/types/Touches.d.ts +14 -0
- package/dist/types/index.d.ts +7 -0
- package/eslint.config.mjs +47 -111
- package/lib/{Css.js → Css.ts} +448 -434
- package/lib/{Dom.js → Dom.ts} +132 -132
- package/lib/{DomCompat.js → DomCompat.ts} +276 -276
- package/lib/{DomEventsSink.js → DomEventsSink.ts} +70 -68
- package/lib/{ScrollHelper.js → ScrollHelper.ts} +158 -158
- package/lib/{Touches.js → Touches.ts} +84 -84
- package/package.json +28 -18
- package/tsconfig.json +17 -0
- package/tsconfig.scripts.json +10 -0
- package/tsconfig.tests.json +10 -0
- /package/lib/{index.js → index.ts} +0 -0
package/lib/{Dom.js → Dom.ts}
RENAMED
|
@@ -1,132 +1,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, 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
|
+
};
|