@htmltools/hdom 0.1.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/lib/hdom.d.ts +138 -0
- package/lib/hdom.js +314 -0
- package/package.json +33 -0
package/lib/hdom.d.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
export class hdom {
|
|
2
|
+
/**
|
|
3
|
+
* @param {string|Element} e
|
|
4
|
+
* @param {string} className
|
|
5
|
+
*/
|
|
6
|
+
static addClass(e: string | Element, className: string): void;
|
|
7
|
+
/**
|
|
8
|
+
* @param {string|Element} e
|
|
9
|
+
* @param {string} type
|
|
10
|
+
* @param {function(Event):void} fn
|
|
11
|
+
* @returns {Element}
|
|
12
|
+
*/
|
|
13
|
+
static addEventListener(e: string | Element, type: string, fn: (arg0: Event) => void): Element;
|
|
14
|
+
/**
|
|
15
|
+
* @param {string|HTMLDialogElement} e
|
|
16
|
+
*/
|
|
17
|
+
static closeDialog(e: string | HTMLDialogElement): void;
|
|
18
|
+
/**
|
|
19
|
+
* @param {string} id
|
|
20
|
+
* @param {boolean} checked
|
|
21
|
+
* @returns {HTMLInputElement}
|
|
22
|
+
*/
|
|
23
|
+
static createCheckBox(id: string, checked: boolean): HTMLInputElement;
|
|
24
|
+
/**
|
|
25
|
+
* @param {string} name
|
|
26
|
+
* @param {Object<string,string|number>|string} [attributes]
|
|
27
|
+
* @returns {HTMLElement}
|
|
28
|
+
*/
|
|
29
|
+
static createElement(name: string, attributes?: {
|
|
30
|
+
[x: string]: string | number;
|
|
31
|
+
} | string): HTMLElement;
|
|
32
|
+
/**
|
|
33
|
+
* @param {Object.<string,string|number>|string} attributes
|
|
34
|
+
* @returns {HTMLInputElement}
|
|
35
|
+
*/
|
|
36
|
+
static createInputElement(attributes: {
|
|
37
|
+
[x: string]: string | number;
|
|
38
|
+
} | string): HTMLInputElement;
|
|
39
|
+
/**
|
|
40
|
+
* @param {string} name
|
|
41
|
+
* @param {Object<string,string>} attributes
|
|
42
|
+
* @param {string|number} text
|
|
43
|
+
* @returns {Element}
|
|
44
|
+
*/
|
|
45
|
+
static createTextElement(name: string, attributes: {
|
|
46
|
+
[x: string]: string;
|
|
47
|
+
}, text: string | number): Element;
|
|
48
|
+
/**
|
|
49
|
+
* @param {string|HTMLElement} e
|
|
50
|
+
* @param {boolean} [enable=true]
|
|
51
|
+
*/
|
|
52
|
+
static enableElement(e: string | HTMLElement, enable?: boolean): void;
|
|
53
|
+
/**
|
|
54
|
+
* @template {HTMLElement} T
|
|
55
|
+
* @param {string|Element} e
|
|
56
|
+
* @returns {T}
|
|
57
|
+
*/
|
|
58
|
+
static getElement<T extends HTMLElement>(e: string | Element): T;
|
|
59
|
+
/**
|
|
60
|
+
* @param {string|Element} form
|
|
61
|
+
* @param {string} elName
|
|
62
|
+
* @returns {HTMLFormElement|RadioNodeList}
|
|
63
|
+
*/
|
|
64
|
+
static getFormElement(form: string | Element, elName: string): HTMLFormElement | RadioNodeList;
|
|
65
|
+
/**
|
|
66
|
+
* @param {string|HTMLElement|RadioNodeList} e
|
|
67
|
+
* @returns {string}
|
|
68
|
+
*/
|
|
69
|
+
static getFormElementValue(e: string | HTMLElement | RadioNodeList): string;
|
|
70
|
+
/**
|
|
71
|
+
* @param {string|Element} e
|
|
72
|
+
* @returns {boolean}
|
|
73
|
+
*/
|
|
74
|
+
static isChecked(e: string | Element): boolean;
|
|
75
|
+
/**
|
|
76
|
+
* @param {string|Element} e
|
|
77
|
+
* @returns {boolean}
|
|
78
|
+
*/
|
|
79
|
+
static isElement(e: string | Element): boolean;
|
|
80
|
+
/**
|
|
81
|
+
* @param {string|HTMLElement} e
|
|
82
|
+
* @returns {boolean}
|
|
83
|
+
*/
|
|
84
|
+
static isHidden(e: string | HTMLElement): boolean;
|
|
85
|
+
/**
|
|
86
|
+
* @param {string|Element} e
|
|
87
|
+
* @returns {Element}
|
|
88
|
+
*/
|
|
89
|
+
static removeChildren(e: string | Element): Element;
|
|
90
|
+
/**
|
|
91
|
+
* @param {string|Element} e
|
|
92
|
+
* @param {string} className
|
|
93
|
+
*/
|
|
94
|
+
static removeClass(e: string | Element, className: string): void;
|
|
95
|
+
/**
|
|
96
|
+
* @param {string|HTMLElement} e
|
|
97
|
+
*/
|
|
98
|
+
static selectAllText(e: string | HTMLElement): void;
|
|
99
|
+
/**
|
|
100
|
+
* @param {string|HTMLElement} e
|
|
101
|
+
* @param {string} attName
|
|
102
|
+
* @param {string} value
|
|
103
|
+
*/
|
|
104
|
+
static setAttribute(e: string | HTMLElement, attName: string, value: string): void;
|
|
105
|
+
/**
|
|
106
|
+
* @param {string|Element} e
|
|
107
|
+
* @param {string|number} text
|
|
108
|
+
*/
|
|
109
|
+
static setElementText(e: string | Element, text: string | number): void;
|
|
110
|
+
/**
|
|
111
|
+
* @param {string|HTMLElement} e
|
|
112
|
+
*/
|
|
113
|
+
static setFocusTo(e: string | HTMLElement): void;
|
|
114
|
+
/**
|
|
115
|
+
* @param {string|HTMLElement} e
|
|
116
|
+
* @param {string|number} value
|
|
117
|
+
*/
|
|
118
|
+
static setFormElementValue(e: string | HTMLElement, value: string | number): void;
|
|
119
|
+
/**
|
|
120
|
+
* @param {string|Element} e
|
|
121
|
+
* @param {string} text
|
|
122
|
+
*/
|
|
123
|
+
static setHTMLValue(e: string | Element, text: string): void;
|
|
124
|
+
/**
|
|
125
|
+
* @param {string|Element} e
|
|
126
|
+
* @param {string} text
|
|
127
|
+
*/
|
|
128
|
+
static setTextValue(e: string | Element, text: string): void;
|
|
129
|
+
/**
|
|
130
|
+
* @param {string|HTMLDialogElement} e
|
|
131
|
+
*/
|
|
132
|
+
static showDialog(e: string | HTMLDialogElement): void;
|
|
133
|
+
/**
|
|
134
|
+
* @param {string|Element} e
|
|
135
|
+
* @param {boolean} [show=true]
|
|
136
|
+
*/
|
|
137
|
+
static showElement(e: string | Element, show?: boolean): void;
|
|
138
|
+
}
|
package/lib/hdom.js
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
export class hdom {
|
|
2
|
+
/**
|
|
3
|
+
* @param {string|Element} e
|
|
4
|
+
* @param {string} className
|
|
5
|
+
*/
|
|
6
|
+
static addClass(e, className) {
|
|
7
|
+
const elem = this.getElement(e);
|
|
8
|
+
elem.classList.add(className);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {string|Element} e
|
|
13
|
+
* @param {string} type
|
|
14
|
+
* @param {function(Event):void} fn
|
|
15
|
+
* @returns {Element}
|
|
16
|
+
*/
|
|
17
|
+
static addEventListener(e, type, fn) {
|
|
18
|
+
const elem = this.getElement(e);
|
|
19
|
+
if (elem instanceof Element) {
|
|
20
|
+
elem.addEventListener(type, fn);
|
|
21
|
+
}
|
|
22
|
+
return elem;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {string|HTMLDialogElement} e
|
|
27
|
+
*/
|
|
28
|
+
static closeDialog(e) {
|
|
29
|
+
const elem = this.getElement(e);
|
|
30
|
+
if (elem instanceof HTMLDialogElement) {
|
|
31
|
+
elem.close();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @param {string} id
|
|
37
|
+
* @param {boolean} checked
|
|
38
|
+
* @returns {HTMLInputElement}
|
|
39
|
+
*/
|
|
40
|
+
static createCheckBox(id, checked) {
|
|
41
|
+
/** @type {Object<string,string>} */
|
|
42
|
+
const atts = {};
|
|
43
|
+
atts.type = "checkbox";
|
|
44
|
+
atts.id = id;
|
|
45
|
+
if (checked) {
|
|
46
|
+
atts.checked = "";
|
|
47
|
+
}
|
|
48
|
+
return this.createInputElement(atts);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @param {string} name
|
|
53
|
+
* @param {Object<string,string|number>|string} [attributes]
|
|
54
|
+
* @returns {HTMLElement}
|
|
55
|
+
*/
|
|
56
|
+
static createElement(name, attributes) {
|
|
57
|
+
const e = document.createElement(name);
|
|
58
|
+
switch (typeof attributes) {
|
|
59
|
+
case "string":
|
|
60
|
+
// Assume it's a class name.
|
|
61
|
+
e.className = attributes;
|
|
62
|
+
break;
|
|
63
|
+
case "object":
|
|
64
|
+
for (const [k, v] of Object.entries(attributes)) {
|
|
65
|
+
e.setAttribute(k, v.toString());
|
|
66
|
+
}
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
return e;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @param {Object.<string,string|number>|string} attributes
|
|
74
|
+
* @returns {HTMLInputElement}
|
|
75
|
+
*/
|
|
76
|
+
static createInputElement(attributes) {
|
|
77
|
+
const e = this.createElement("input", attributes);
|
|
78
|
+
if (!(e instanceof HTMLInputElement)) {
|
|
79
|
+
throw new Error();
|
|
80
|
+
}
|
|
81
|
+
return e;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @param {string} name
|
|
86
|
+
* @param {Object<string,string>} attributes
|
|
87
|
+
* @param {string|number} text
|
|
88
|
+
* @returns {Element}
|
|
89
|
+
*/
|
|
90
|
+
static createTextElement(name, attributes, text) {
|
|
91
|
+
const elem = this.createElement(name, attributes);
|
|
92
|
+
this.setTextValue(elem, text.toString());
|
|
93
|
+
return elem;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @param {string|HTMLElement} e
|
|
98
|
+
* @param {boolean} [enable=true]
|
|
99
|
+
*/
|
|
100
|
+
static enableElement(e, enable = true) {
|
|
101
|
+
const elem = this.getElement(e);
|
|
102
|
+
if (elem instanceof HTMLInputElement || elem instanceof HTMLButtonElement) {
|
|
103
|
+
elem.disabled = !enable;
|
|
104
|
+
} else if (elem instanceof HTMLElement) {
|
|
105
|
+
elem.inert = !enable;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* @template {HTMLElement} T
|
|
111
|
+
* @param {string|Element} e
|
|
112
|
+
* @returns {T}
|
|
113
|
+
*/
|
|
114
|
+
static getElement(e) {
|
|
115
|
+
if (typeof e === "string") {
|
|
116
|
+
const el = document.getElementById(e);
|
|
117
|
+
if (el === null) {
|
|
118
|
+
throw new Error();
|
|
119
|
+
}
|
|
120
|
+
// @ts-ignore
|
|
121
|
+
return el;
|
|
122
|
+
}
|
|
123
|
+
// @ts-ignore
|
|
124
|
+
return e;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* @param {string|Element} form
|
|
129
|
+
* @param {string} elName
|
|
130
|
+
* @returns {HTMLFormElement|RadioNodeList}
|
|
131
|
+
*/
|
|
132
|
+
static getFormElement(form, elName) {
|
|
133
|
+
form = this.getElement(form);
|
|
134
|
+
if (!(form instanceof HTMLFormElement)) {
|
|
135
|
+
throw new Error();
|
|
136
|
+
}
|
|
137
|
+
const elem = form.elements.namedItem(elName);
|
|
138
|
+
if (!(elem instanceof HTMLFormElement)) {
|
|
139
|
+
if (!(elem instanceof RadioNodeList)) {
|
|
140
|
+
throw new Error();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return elem;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* @param {string|HTMLElement|RadioNodeList} e
|
|
148
|
+
* @returns {string}
|
|
149
|
+
*/
|
|
150
|
+
static getFormElementValue(e) {
|
|
151
|
+
const elem = typeof e === "string" ? this.getElement(e) : e;
|
|
152
|
+
if (
|
|
153
|
+
elem instanceof HTMLInputElement ||
|
|
154
|
+
elem instanceof HTMLSelectElement ||
|
|
155
|
+
elem instanceof HTMLTextAreaElement ||
|
|
156
|
+
elem instanceof RadioNodeList
|
|
157
|
+
) {
|
|
158
|
+
return elem.value;
|
|
159
|
+
}
|
|
160
|
+
throw new Error();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* @param {string|Element} e
|
|
165
|
+
* @returns {boolean}
|
|
166
|
+
*/
|
|
167
|
+
static isChecked(e) {
|
|
168
|
+
const elem = this.getElement(e);
|
|
169
|
+
if (!(elem instanceof HTMLInputElement)) {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
return elem.checked;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* @param {string|Element} e
|
|
177
|
+
* @returns {boolean}
|
|
178
|
+
*/
|
|
179
|
+
static isElement(e) {
|
|
180
|
+
if (e instanceof Element) {
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
return document.getElementById(e) !== null;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* @param {string|HTMLElement} e
|
|
188
|
+
* @returns {boolean}
|
|
189
|
+
*/
|
|
190
|
+
static isHidden(e) {
|
|
191
|
+
const elem = this.getElement(e);
|
|
192
|
+
return elem instanceof HTMLElement && elem.hidden;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* @param {string|Element} e
|
|
197
|
+
* @returns {Element}
|
|
198
|
+
*/
|
|
199
|
+
static removeChildren(e) {
|
|
200
|
+
const elem = this.getElement(e);
|
|
201
|
+
while (elem.firstChild) {
|
|
202
|
+
elem.firstChild.remove();
|
|
203
|
+
}
|
|
204
|
+
return elem;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* @param {string|Element} e
|
|
209
|
+
* @param {string} className
|
|
210
|
+
*/
|
|
211
|
+
static removeClass(e, className) {
|
|
212
|
+
const elem = this.getElement(e);
|
|
213
|
+
elem.classList.remove(className);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* @param {string|HTMLElement} e
|
|
218
|
+
*/
|
|
219
|
+
static selectAllText(e) {
|
|
220
|
+
/** @type {HTMLInputElement} */
|
|
221
|
+
const elem = this.getElement(e);
|
|
222
|
+
elem.select();
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* @param {string|HTMLElement} e
|
|
227
|
+
* @param {string} attName
|
|
228
|
+
* @param {string} value
|
|
229
|
+
*/
|
|
230
|
+
static setAttribute(e, attName, value) {
|
|
231
|
+
const elem = this.getElement(e);
|
|
232
|
+
if (!(elem instanceof HTMLElement)) {
|
|
233
|
+
throw new Error();
|
|
234
|
+
}
|
|
235
|
+
elem.setAttribute(attName, value);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* @param {string|Element} e
|
|
240
|
+
* @param {string|number} text
|
|
241
|
+
*/
|
|
242
|
+
static setElementText(e, text) {
|
|
243
|
+
const elem = this.getElement(e);
|
|
244
|
+
if (!(elem instanceof HTMLElement)) {
|
|
245
|
+
throw new Error();
|
|
246
|
+
}
|
|
247
|
+
this.removeChildren(elem);
|
|
248
|
+
elem.appendChild(document.createTextNode(text.toString()));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* @param {string|HTMLElement} e
|
|
253
|
+
*/
|
|
254
|
+
static setFocusTo(e) {
|
|
255
|
+
const elem = this.getElement(e);
|
|
256
|
+
elem.focus();
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* @param {string|HTMLElement} e
|
|
261
|
+
* @param {string|number} value
|
|
262
|
+
*/
|
|
263
|
+
static setFormElementValue(e, value) {
|
|
264
|
+
const elem = this.getElement(e);
|
|
265
|
+
if (
|
|
266
|
+
elem instanceof HTMLInputElement ||
|
|
267
|
+
elem instanceof HTMLTextAreaElement ||
|
|
268
|
+
elem instanceof HTMLSelectElement
|
|
269
|
+
) {
|
|
270
|
+
elem.value = value.toString();
|
|
271
|
+
} else if (elem instanceof HTMLElement) {
|
|
272
|
+
elem.setAttribute("value", value.toString());
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* @param {string|Element} e
|
|
278
|
+
* @param {string} text
|
|
279
|
+
*/
|
|
280
|
+
static setHTMLValue(e, text) {
|
|
281
|
+
const elem = this.getElement(e);
|
|
282
|
+
elem.innerHTML = text;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* @param {string|Element} e
|
|
287
|
+
* @param {string} text
|
|
288
|
+
*/
|
|
289
|
+
static setTextValue(e, text) {
|
|
290
|
+
const elem = this.getElement(e);
|
|
291
|
+
elem.textContent = text;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* @param {string|HTMLDialogElement} e
|
|
296
|
+
*/
|
|
297
|
+
static showDialog(e) {
|
|
298
|
+
const elem = this.getElement(e);
|
|
299
|
+
if (elem instanceof HTMLDialogElement) {
|
|
300
|
+
elem.show();
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* @param {string|Element} e
|
|
306
|
+
* @param {boolean} [show=true]
|
|
307
|
+
*/
|
|
308
|
+
static showElement(e, show = true) {
|
|
309
|
+
const elem = this.getElement(e);
|
|
310
|
+
if (elem instanceof HTMLElement) {
|
|
311
|
+
elem.hidden = !show;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@htmltools/hdom",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Tools to interact with the HTML DOM.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/html-tools/hdom.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/html-tools/hdom",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"files": [
|
|
13
|
+
"lib"
|
|
14
|
+
],
|
|
15
|
+
"exports": {
|
|
16
|
+
".": "./lib/hdom.js"
|
|
17
|
+
},
|
|
18
|
+
"types": "./lib/hdom.d.ts",
|
|
19
|
+
"scripts": {
|
|
20
|
+
"check": "npm run eslint && npm run tsc",
|
|
21
|
+
"eslint": "eslint",
|
|
22
|
+
"make-tar": "npm pack --pack-destination ignored/dist",
|
|
23
|
+
"prepack": "tsc lib/hdom.js --declaration --allowJs --emitDeclarationOnly",
|
|
24
|
+
"prettier": "prettier -l .",
|
|
25
|
+
"tsc": "tsc"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^24.0.10",
|
|
29
|
+
"eslint": "^9.38.0",
|
|
30
|
+
"prettier": "^3.6.2",
|
|
31
|
+
"typescript": "^5.9.3"
|
|
32
|
+
}
|
|
33
|
+
}
|