@cntwg/html-helper 0.1.2 → 0.1.4

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/CHANGELOG.md CHANGED
@@ -1,3 +1,16 @@
1
+ #### *v0.1.4*
2
+
3
+ Release version.
4
+
5
+ > - some fixes.
6
+
7
+ #### *v0.1.3*
8
+
9
+ Release version.
10
+
11
+ > - `README.md` updated;
12
+ > - some fixes.
13
+
1
14
  #### *v0.1.2*
2
15
 
3
16
  Release version.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
- >|***rev.*:**|0.2.8|
1
+ >|***rev.*:**|0.2.9|
2
2
  >|:---|---:|
3
- >|date:|2025-10-23|
3
+ >|date:|2026-04-14|
4
4
 
5
5
  ## Introduction
6
6
 
@@ -36,3 +36,5 @@ This module was make for use as a library in a development of a Web-apps based o
36
36
  ### Installation
37
37
 
38
38
  `npm install @cntwg/html-helper`
39
+
40
+ > Note: It's recommended to "pin" a package versions range to its minor releases after installation. To do so use `~` range specifier (*e.g. `~0.1.0`*) in `package.json`.
package/index.d.ts ADDED
@@ -0,0 +1,54 @@
1
+ export namespace eventHelper {
2
+ let pushEventHandler: typeof ev_helper.pushEventHandler;
3
+ let removeEventHandler: typeof ev_helper.removeEventHandler;
4
+ let triggerEventHandler: typeof ev_helper.triggerEventHandler;
5
+ }
6
+ import { valueToIDString } from "@ygracs/bsfoc-lib-js";
7
+ import ev_helper = require("./lib/event-hfunc.js");
8
+
9
+ import {
10
+ CSS_CLASS_STRING,
11
+ isSelectedHTMLElement, isCurrentHTMLElement,
12
+ isActiveHTMLElement, isHiddenHTMLElement,
13
+ hideHTMLElement, showHTMLElement,
14
+ selectHTMLElement, unselectHTMLElement,
15
+ markHTMLElementAsCurrent, unmarkCurrentHTMLElement,
16
+ markHTMLElementAsActive, unmarkActiveHTMLElement,
17
+ lockHTMLElement, unlockHTMLElement,
18
+ activateHTMLElements, inactivateHTMLElements,
19
+ createNewHTMLElement,
20
+ } from "./lib/html-helper-lib";
21
+
22
+ import type {
23
+ IElementDesc,
24
+ } from "./lib/html-helper-lib";
25
+
26
+ import {
27
+ isHTMLElement, isHTMLButton,
28
+ valueToClassList,
29
+ readAsTagName, readAsAttrName, readAsAttrValue,
30
+ valueToElementID,
31
+ } from "./lib/mod-hfunc";
32
+
33
+ export {
34
+ CSS_CLASS_STRING,
35
+ isHTMLElement,
36
+ isSelectedHTMLElement, isCurrentHTMLElement,
37
+ isActiveHTMLElement, isHiddenHTMLElement,
38
+ hideHTMLElement, showHTMLElement,
39
+ selectHTMLElement, unselectHTMLElement,
40
+ markHTMLElementAsCurrent, unmarkCurrentHTMLElement,
41
+ markHTMLElementAsActive, unmarkActiveHTMLElement,
42
+ lockHTMLElement, unlockHTMLElement,
43
+ activateHTMLElements, inactivateHTMLElements,
44
+ valueToClassList,
45
+ readAsTagName, readAsAttrName, readAsAttrValue,
46
+ valueToElementID,
47
+ createNewHTMLElement,
48
+ isHTMLButton,
49
+ valueToIDString,
50
+ };
51
+
52
+ export type {
53
+ IElementDesc,
54
+ };
package/index.js CHANGED
@@ -1,9 +1,10 @@
1
- // [v0.2.034-20260309]
1
+ // [v0.2.037-20260517]
2
2
 
3
3
  // === module init block ===
4
4
 
5
- const html_helper = require('./lib/html-helper-lib.js');
6
- const ev_helper = require('./lib/event-hfunc.js');
5
+ const html_helper = require('./lib/html-helper-lib');
6
+ const mod_helper = require('./lib/mod-hfunc');
7
+ const ev_helper = require('./lib/event-hfunc');
7
8
 
8
9
  const {
9
10
  valueToIDString,
@@ -19,11 +20,19 @@ const eventHelper = {
19
20
  removeEventHandler: ev_helper.removeEventHandler,
20
21
  triggerEventHandler: ev_helper.triggerEventHandler,
21
22
  };
23
+ module.exports.eventHelper = eventHelper;
22
24
 
23
25
  // === module exports block ===
24
26
 
27
+ module.exports.isHTMLElement = mod_helper.isHTMLElement;
28
+ module.exports.isHTMLButton = mod_helper.isHTMLButton;
29
+ module.exports.readAsTagName = mod_helper.readAsTagName;
30
+ module.exports.readAsAttrName = mod_helper.readAsAttrName;
31
+ module.exports.readAsAttrValue = mod_helper.readAsAttrValue;
32
+ module.exports.valueToElementID = mod_helper.valueToElementID;
33
+ module.exports.valueToClassList = mod_helper.valueToClassList;
34
+
25
35
  module.exports.CSS_CLASS_STRING = html_helper.CSS_CLASS_STRING;
26
- module.exports.isHTMLElement = html_helper.isHTMLElement;
27
36
  module.exports.isSelectedHTMLElement = html_helper.isSelectedHTMLElement;
28
37
  module.exports.isCurrentHTMLElement = html_helper.isCurrentHTMLElement;
29
38
  module.exports.isActiveHTMLElement = html_helper.isActiveHTMLElement;
@@ -40,17 +49,10 @@ module.exports.lockHTMLElement = html_helper.lockHTMLElement;
40
49
  module.exports.unlockHTMLElement = html_helper.unlockHTMLElement;
41
50
  module.exports.activateHTMLElements = html_helper.activateHTMLElements;
42
51
  module.exports.inactivateHTMLElements = html_helper.inactivateHTMLElements;
43
- module.exports.valueToClassList = html_helper.valueToClassList;
44
52
 
45
- // * experimental *
46
- module.exports.readAsTagName = html_helper.readAsTagName;
47
- module.exports.readAsAttrName = html_helper.readAsAttrName;
48
- module.exports.readAsAttrValue = html_helper.readAsAttrValue;
49
- module.exports.valueToElementID = html_helper.valueToElementID;
50
53
  module.exports.createNewHTMLElement = html_helper.createNewHTMLElement;
51
- module.exports.isHTMLButton = html_helper.isHTMLButton;
52
-
53
- module.exports.eventHelper = eventHelper;
54
+ // * exports types definitions *
55
+ module.exports.IElementDesc = html_helper.IElementDesc;
54
56
 
55
57
  // * re-exported *
56
58
  module.exports.valueToIDString = valueToIDString;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @inner
3
+ */
4
+ export function pushEventHandler(pool: Map<string, Function>, name: string, evnt: Function): void;
5
+ /**
6
+ * @inner
7
+ */
8
+ export function removeEventHandler(pool: Map<string, Function>, name: string): void;
9
+ /**
10
+ * @inner
11
+ */
12
+ export function triggerEventHandler(pool: Map<string, Function>, name: string, ...args: any[]): void;
@@ -1,4 +1,4 @@
1
- // [v0.1.064-20260309]
1
+ // [v0.1.065-20260413]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -50,14 +50,15 @@ module.exports.removeEventHandler = removeEventHandler;
50
50
  * @function triggerEventHandler
51
51
  * @param {Map<string, Function>} pool - pool object
52
52
  * @param {string} name - event name
53
- * @param {...any} [args]
53
+ * @param {...any} args - optional params send to a target event
54
54
  * @returns {void}
55
55
  * @inner
56
56
  */
57
57
  function triggerEventHandler(pool, name, ...args) {
58
- const _name = typeof name === 'string' ? name.trim() : '';
59
- if (_name !== '') {
60
- if (pool.has(_name)) pool.get(_name)(...args);
58
+ const target = typeof name === 'string' ? name.trim() : '';
59
+ if (target !== '' && pool.has(target)) {
60
+ const e = pool.get(target);
61
+ if (typeof e === 'function') e(...args);
61
62
  };
62
63
  };
63
64
  module.exports.triggerEventHandler = triggerEventHandler;
@@ -0,0 +1,131 @@
1
+ /**
2
+ * An element descriptor.
3
+ */
4
+ export type IElementDesc = {
5
+ /**
6
+ * - element ID
7
+ */
8
+ id?: string;
9
+ /**
10
+ * - some text
11
+ */
12
+ text?: string;
13
+ /**
14
+ * - an attributes list
15
+ */
16
+ attr?: {
17
+ [x: string]: any;
18
+ };
19
+ /**
20
+ * - a class names list
21
+ * @since 0.0.19
22
+ */
23
+ classNames?: string | string[];
24
+ /**
25
+ * - a 'dataset'-attributes list
26
+ */
27
+ data?: {
28
+ [x: string]: any;
29
+ };
30
+ };
31
+ export const IElementDesc: IElementDesc;
32
+ export namespace CSS_CLASS_STRING {
33
+ export { CSS_CLASS_CURRENT };
34
+ export { CSS_CLASS_SELECTED };
35
+ export { CSS_CLASS_ACTIVE };
36
+ export { CSS_CLASS_DISABLED };
37
+ export { CSS_CLASS_HIDDEN };
38
+ }
39
+
40
+ /**
41
+ * Checks if the given object is an instance of a `HTMLElement`
42
+ * and if so it's marked as "selected".
43
+ */
44
+ export function isSelectedHTMLElement(obj: HTMLElement): boolean;
45
+ /**
46
+ * Checks if the given object is an instance of a `HTMLElement`
47
+ * and if so it's marked as "current".
48
+ */
49
+ export function isCurrentHTMLElement(obj: HTMLElement): boolean;
50
+ /**
51
+ * Checks if the given object is an instance of a `HTMLElement`
52
+ * and if so it's marked as "active".
53
+ */
54
+ export function isActiveHTMLElement(obj: HTMLElement): boolean;
55
+ /**
56
+ * Checks if the given object is an instance of a `HTMLElement`
57
+ * and if so it's marked as "hidden".
58
+ */
59
+ export function isHiddenHTMLElement(obj: HTMLElement): boolean;
60
+ /**
61
+ * Hides a given HTML-element.
62
+ * @since v0.0.23
63
+ */
64
+ export function hideHTMLElement(obj: HTMLElement): boolean;
65
+ /**
66
+ * Shows a given HTML-element.
67
+ * @since v0.0.23
68
+ */
69
+ export function showHTMLElement(obj: HTMLElement): boolean;
70
+ /**
71
+ * Makes selected a given HTML-element.
72
+ * @since v0.0.23
73
+ */
74
+ export function selectHTMLElement(obj: HTMLElement): boolean;
75
+ /**
76
+ * Makes unselected a given HTML-element.
77
+ * @since v0.0.23
78
+ */
79
+ export function unselectHTMLElement(obj: HTMLElement): boolean;
80
+ /**
81
+ * Marks a given HTML-element as "current".
82
+ * @since v0.0.23
83
+ */
84
+ export function markHTMLElementAsCurrent(obj: HTMLElement): boolean;
85
+ /**
86
+ * Unmarks a given HTML-element previous marked as "current".
87
+ * @since v0.0.23
88
+ */
89
+ export function unmarkCurrentHTMLElement(obj: HTMLElement): boolean;
90
+ /**
91
+ * Marks a given HTML-element as "active".
92
+ * @since v0.0.23
93
+ */
94
+ export function markHTMLElementAsActive(obj: HTMLElement): boolean;
95
+ /**
96
+ * Unmarks a given HTML-element previous marked as "active".
97
+ * @since v0.0.23
98
+ */
99
+ export function unmarkActiveHTMLElement(obj: HTMLElement): boolean;
100
+ /**
101
+ * Disables a given HTML-element.
102
+ * @since v0.0.23
103
+ */
104
+ export function lockHTMLElement(obj: HTMLElement): boolean;
105
+ /**
106
+ * Enables a given HTML-element.
107
+ * @since v0.0.23
108
+ */
109
+ export function unlockHTMLElement(obj: HTMLElement): boolean;
110
+ /**
111
+ * Enables an HTML-elements given by a list of a function params.
112
+ * @since v0.0.23
113
+ */
114
+ export function activateHTMLElements(...args: HTMLElement[]): void;
115
+ /**
116
+ * Disables an HTML-elements given by a list of a function params.
117
+ * @since v0.0.23
118
+ */
119
+ export function inactivateHTMLElements(...args: HTMLElement[]): void;
120
+ /**
121
+ * Creates a new HTML-element.
122
+ * @since 0.0.25
123
+ * @experimental
124
+ */
125
+ export function createNewHTMLElement(tagName: string, opt?: IElementDesc): HTMLElement | null;
126
+ declare const CSS_CLASS_CURRENT: "current";
127
+ declare const CSS_CLASS_SELECTED: "selected";
128
+ declare const CSS_CLASS_ACTIVE: "active";
129
+ declare const CSS_CLASS_DISABLED: "disabled";
130
+ declare const CSS_CLASS_HIDDEN: "hidden";
131
+ export {};
@@ -1,17 +1,19 @@
1
- // [v0.1.046-20260309]
1
+ // [v0.1.050-20260517]
2
2
 
3
3
  // === module init block ===
4
4
 
5
5
  const {
6
- isArray, isObject, isPlainObject, valueToArray,
7
- valueToIDString,
6
+ isArray, isObject, isPlainObject,
8
7
  } = require('@ygracs/bsfoc-lib-js');
9
8
 
10
- // === module inner block ===
9
+ const {
10
+ isHTMLElement,
11
+ valueToElementID, valueToClassList,
12
+ readAsTagName,
13
+ readAsAttrName, readAsAttrValue,
14
+ } = require('./mod-hfunc');
11
15
 
12
- const etlHTagInputForBtn = new Set([
13
- 'button', 'submit', 'reset', 'image',
14
- ]);
16
+ // === module inner block ===
15
17
 
16
18
  // === module main block ===
17
19
 
@@ -21,40 +23,6 @@ const CSS_CLASS_ACTIVE = 'active';
21
23
  const CSS_CLASS_DISABLED = 'disabled';
22
24
  const CSS_CLASS_HIDDEN = 'hidden';
23
25
 
24
- /**
25
- * Checks if the given object is an instance of an `HTMLElement`.
26
- * @function isHTMLElement
27
- * @param {any} obj - some element
28
- * @return {boolean}
29
- */
30
- function isHTMLElement(obj) {
31
- return obj instanceof HTMLElement;
32
- };
33
-
34
- /**
35
- * Checks if the given object is an HTML-button.
36
- * @function isHTMLButton
37
- * @param {any} obj - element to be verified
38
- * @returns {boolean}
39
- */
40
- function isHTMLButton(obj) {
41
- let result = false;
42
- if (isHTMLElement(obj)) {
43
- switch (obj.tagName.toLowerCase()) {
44
- case 'button': {
45
- result = true;
46
- break;
47
- }
48
- case 'input': {
49
- result = etlHTagInputForBtn.has(obj.type.toLowerCase());
50
- break;
51
- }
52
- default: {}
53
- };
54
- };
55
- return result;
56
- }
57
-
58
26
  /**
59
27
  * Checks if the given object is an instance of a `HTMLElement`
60
28
  * and if so it's marked as "selected".
@@ -65,6 +33,7 @@ function isHTMLButton(obj) {
65
33
  function isSelectedHTMLElement(obj) {
66
34
  return isHTMLElement(obj) && obj.classList.contains(CSS_CLASS_SELECTED);
67
35
  };
36
+ module.exports.isSelectedHTMLElement = isSelectedHTMLElement;
68
37
 
69
38
  /**
70
39
  * Checks if the given object is an instance of a `HTMLElement`
@@ -76,6 +45,7 @@ function isSelectedHTMLElement(obj) {
76
45
  function isCurrentHTMLElement(obj) {
77
46
  return isHTMLElement(obj) && obj.classList.contains(CSS_CLASS_CURRENT);
78
47
  };
48
+ module.exports.isCurrentHTMLElement = isCurrentHTMLElement;
79
49
 
80
50
  /**
81
51
  * Checks if the given object is an instance of a `HTMLElement`
@@ -87,6 +57,7 @@ function isCurrentHTMLElement(obj) {
87
57
  function isActiveHTMLElement(obj) {
88
58
  return isHTMLElement(obj) && obj.classList.contains(CSS_CLASS_ACTIVE);
89
59
  };
60
+ module.exports.isActiveHTMLElement = isActiveHTMLElement;
90
61
 
91
62
  /**
92
63
  * Checks if the given object is an instance of a `HTMLElement`
@@ -98,6 +69,7 @@ function isActiveHTMLElement(obj) {
98
69
  function isHiddenHTMLElement(obj) {
99
70
  return isHTMLElement(obj) && obj.classList.contains(CSS_CLASS_HIDDEN);
100
71
  };
72
+ module.exports.isHiddenHTMLElement = isHiddenHTMLElement;
101
73
 
102
74
  /**
103
75
  * Hides a given HTML-element.
@@ -111,6 +83,7 @@ function hideHTMLElement(obj) {
111
83
  if (isSUCCEED) obj.classList.add(CSS_CLASS_HIDDEN);
112
84
  return isSUCCEED;
113
85
  };
86
+ module.exports.hideHTMLElement = hideHTMLElement;
114
87
 
115
88
  /**
116
89
  * Shows a given HTML-element.
@@ -124,6 +97,7 @@ function showHTMLElement(obj) {
124
97
  if (isSUCCEED) obj.classList.remove(CSS_CLASS_HIDDEN);
125
98
  return isSUCCEED;
126
99
  };
100
+ module.exports.showHTMLElement = showHTMLElement;
127
101
 
128
102
  /**
129
103
  * Makes selected a given HTML-element.
@@ -137,6 +111,7 @@ function selectHTMLElement(obj) {
137
111
  if (isSUCCEED) obj.classList.add(CSS_CLASS_SELECTED);
138
112
  return isSUCCEED;
139
113
  };
114
+ module.exports.selectHTMLElement = selectHTMLElement;
140
115
 
141
116
  /**
142
117
  * Makes unselected a given HTML-element.
@@ -150,6 +125,7 @@ function unselectHTMLElement(obj) {
150
125
  if (isSUCCEED) obj.classList.remove(CSS_CLASS_SELECTED);
151
126
  return isSUCCEED;
152
127
  };
128
+ module.exports.unselectHTMLElement = unselectHTMLElement;
153
129
 
154
130
  /**
155
131
  * Marks a given HTML-element as "current".
@@ -163,6 +139,7 @@ function markHTMLElementAsCurrent(obj) {
163
139
  if (isSUCCEED) obj.classList.add(CSS_CLASS_CURRENT);
164
140
  return isSUCCEED;
165
141
  };
142
+ module.exports.markHTMLElementAsCurrent = markHTMLElementAsCurrent;
166
143
 
167
144
  /**
168
145
  * Unmarks a given HTML-element previous marked as "current".
@@ -176,6 +153,7 @@ function unmarkCurrentHTMLElement(obj) {
176
153
  if (isSUCCEED) obj.classList.remove(CSS_CLASS_CURRENT);
177
154
  return isSUCCEED;
178
155
  };
156
+ module.exports.unmarkCurrentHTMLElement = unmarkCurrentHTMLElement;
179
157
 
180
158
  /**
181
159
  * Marks a given HTML-element as "active".
@@ -189,6 +167,7 @@ function markHTMLElementAsActive(obj) {
189
167
  if (isSUCCEED) obj.classList.add(CSS_CLASS_ACTIVE);
190
168
  return isSUCCEED;
191
169
  };
170
+ module.exports.markHTMLElementAsActive = markHTMLElementAsActive;
192
171
 
193
172
  /**
194
173
  * Unmarks a given HTML-element previous marked as "active".
@@ -202,6 +181,7 @@ function unmarkActiveHTMLElement(obj) {
202
181
  if (isSUCCEED) obj.classList.remove(CSS_CLASS_ACTIVE);
203
182
  return isSUCCEED;
204
183
  };
184
+ module.exports.unmarkActiveHTMLElement = unmarkActiveHTMLElement;
205
185
 
206
186
  /**
207
187
  * Disables a given HTML-element.
@@ -226,6 +206,7 @@ function lockHTMLElement(obj) {
226
206
  };
227
207
  return isSUCCEED;
228
208
  };
209
+ module.exports.lockHTMLElement = lockHTMLElement;
229
210
 
230
211
  /**
231
212
  * Enables a given HTML-element.
@@ -250,12 +231,13 @@ function unlockHTMLElement(obj) {
250
231
  };
251
232
  return isSUCCEED;
252
233
  };
234
+ module.exports.unlockHTMLElement = unlockHTMLElement;
253
235
 
254
236
  /**
255
237
  * Enables an HTML-elements given by a list of a function params.
256
238
  * @since v0.0.23
257
239
  * @function activateHTMLElements
258
- * @param {...HTMLElement} obj - some element
240
+ * @param {...HTMLElement} args - some element
259
241
  * @return {void}
260
242
  */
261
243
  function activateHTMLElements(...args) {
@@ -263,12 +245,13 @@ function activateHTMLElements(...args) {
263
245
  unlockHTMLElement(item);
264
246
  };
265
247
  };
248
+ module.exports.activateHTMLElements = activateHTMLElements;
266
249
 
267
250
  /**
268
251
  * Disables an HTML-elements given by a list of a function params.
269
252
  * @since v0.0.23
270
253
  * @function inactivateHTMLElements
271
- * @param {...HTMLElement} obj - some element
254
+ * @param {...HTMLElement} args - some element
272
255
  * @return {void}
273
256
  */
274
257
  function inactivateHTMLElements(...args) {
@@ -276,147 +259,31 @@ function inactivateHTMLElements(...args) {
276
259
  lockHTMLElement(item);
277
260
  };
278
261
  };
279
-
280
- /**
281
- * Tries to convert a given value to a valid name of an HTML-tag.
282
- * @since v0.0.21
283
- * @function readAsTagName
284
- * @param {any} value - some value
285
- * @return {string}
286
- */
287
- function readAsTagName(value) {
288
- let tagName = valueToIDString(value, { ignoreNumbers: true });
289
- if (tagName === null) return '';
290
- if (tagName !== '') {
291
- // // TODO: do extra checks
292
- const template = /[^\w]/;
293
- const trigger = tagName.match(template);
294
- if (trigger) {
295
- //console.log(trigger);
296
- tagName = '';
297
- };
298
- if (tagName !== '') tagName = tagName.toLowerCase();
299
- };
300
- return tagName;
301
- };
302
-
303
- /**
304
- * Tries to convert a given value to a valid name of an HTML-attribute.
305
- * @since v0.0.21
306
- * @function readAsAttrName
307
- * @param {any} value - some value
308
- * @return {string}
309
- */
310
- function readAsAttrName(value) {
311
- let attrName = valueToIDString(value, { ignoreNumbers: true });
312
- if (attrName === null) return '';
313
- if (attrName !== '') {
314
- // // TODO: do extra checks
315
- const template = /[\s\/\\\"\'<>=]/;
316
- const trigger = attrName.match(template);
317
- if (trigger) {
318
- //console.log(trigger);
319
- attrName = '';
320
- };
321
- };
322
- return attrName;
323
- };
324
-
325
- /**
326
- * Tries to convert a given value to a valid "attribute value".
327
- * @since v0.0.18
328
- * @function readAsAttrValue
329
- * @param {any} value - some value
330
- * @return {?string}
331
- */
332
- function readAsAttrValue(value) {
333
- let result = null;
334
- switch (typeof value) {
335
- case 'boolean': {
336
- result = value.toString();
337
- break;
338
- }
339
- case 'number': {
340
- if (Number.isNaN(value)) break;
341
- result = value.toString();
342
- break;
343
- }
344
- case 'string': {
345
- result = value.trim();
346
- break;
347
- }
348
- default: {}
349
- };
350
- return result;
351
- };
352
-
353
- /**
354
- * Tries to convert a given value to a list of a valid "class attributes".
355
- * @since v0.0.13
356
- * @function valueToClassList
357
- * @param {any} value - some value
358
- * @param {boolean} [opt] - flag that indicates whether a dups allowed
359
- * @return {string[]}
360
- */
361
- function valueToClassList(value, opt) {
362
- let result = [];
363
- let list = valueToArray(value);
364
- list.forEach((elem) => {
365
- let value = '';
366
- if (
367
- typeof elem === 'string'
368
- && ((value = elem.trim()) !== '')
369
- ) {
370
- value = value.split(/\s+/);
371
- if (value.length > 0) result.push(...value);
372
- };
373
- });
374
- if (result.length > 0 && typeof opt === 'boolean' && opt) {
375
- result = [ ...(new Set(result)) ];
376
- };
377
- return result;
378
- };
379
-
380
- /**
381
- * Tries to convert a given value to a valid identifier suitable as a value
382
- * for an "ID-attribute" of an HTML-element.
383
- * @since v0.0.22
384
- * @function valueToElementID
385
- * @param {any} value - some value
386
- * @return {string}
387
- */
388
- function valueToElementID(value) {
389
- let id = valueToIDString(value);
390
- if (id === null) return '';
391
- if (id !== '') {
392
- // // TODO: do extra checks
393
- const template = /[\s\/\\\"\'<>=]/;
394
- const trigger = id.match(template);
395
- if (trigger) {
396
- //console.log(trigger);
397
- id = '';
398
- };
399
- };
400
- return id;
401
- };
262
+ module.exports.inactivateHTMLElements = inactivateHTMLElements;
402
263
 
403
264
  /**
404
265
  * An element descriptor.
405
- * @typedef {Object} elemDesc
266
+ * @typedef {Object} IElementDesc
406
267
  * @property {string} [id] - element ID
407
268
  * @property {string} [text] - some text
408
269
  * @property {Object<string, any>} [attr] - an attributes list
409
270
  * @property {string|string[]} [classNames] - a class names list (added since v0.0.19)
410
271
  * @property {Object<string, any>} [data] - a 'dataset'-attributes list
411
272
  */
273
+ /**
274
+ * A virtual constant meant for support jsdoc notation:
275
+ * @type {IElementDesc}
276
+ */
277
+ module.exports.IElementDesc = {};
412
278
 
413
279
  /**
414
280
  * Creates a new HTML-element.
415
281
  * @since 0.0.25
416
282
  * @function createNewHTMLElement
417
283
  * @param {string} tagName - an element tag name
418
- * @param {elemDesc} [opt] - an element descriptor object
284
+ * @param {IElementDesc} [opt] - an element descriptor object
419
285
  * @return {?HTMLElement}
286
+ * @experimental
420
287
  */
421
288
  function createNewHTMLElement(tagName, opt) {
422
289
  let item = null;
@@ -446,8 +313,8 @@ function createNewHTMLElement(tagName, opt) {
446
313
  };
447
314
  // set an attributes of the element
448
315
  if (isObject(attr)) {
449
- attr = isArray(attr) ? attr : Object.entries(attr);
450
- for (let [ key, value ] of attr ) {
316
+ const data = isArray(attr) ? attr : Object.entries(attr);
317
+ for (let [ key, value ] of data ) {
451
318
  if (
452
319
  ((key = readAsAttrName(key)) !== '')
453
320
  ) {
@@ -474,8 +341,8 @@ function createNewHTMLElement(tagName, opt) {
474
341
  };
475
342
  // set a data-attributes of the element
476
343
  if (isObject(dset)) {
477
- dset = isArray(dset) ? dset : Object.entries(dset);
478
- for (let [ key, value ] of dset ) {
344
+ const data = isArray(dset) ? dset : Object.entries(dset);
345
+ for (let [ key, value ] of data ) {
479
346
  if (
480
347
  ((key = readAsAttrName(key)) !== '')
481
348
  && ((value = readAsAttrValue(value)) !== null)
@@ -489,6 +356,7 @@ function createNewHTMLElement(tagName, opt) {
489
356
  };
490
357
  return item;
491
358
  };
359
+ module.exports.createNewHTMLElement = createNewHTMLElement;
492
360
 
493
361
  // === module exports block ===
494
362
 
@@ -499,32 +367,3 @@ module.exports.CSS_CLASS_STRING = /*function(){ return*/ {
499
367
  CSS_CLASS_DISABLED: CSS_CLASS_DISABLED,
500
368
  CSS_CLASS_HIDDEN: CSS_CLASS_HIDDEN
501
369
  }/*}*/;
502
-
503
- module.exports.isHTMLElement = isHTMLElement;
504
- module.exports.isHTMLButton = isHTMLButton;
505
- module.exports.isCurrentHTMLElement = isCurrentHTMLElement;
506
- module.exports.isSelectedHTMLElement = isSelectedHTMLElement;
507
- module.exports.isActiveHTMLElement = isActiveHTMLElement;
508
- module.exports.isHiddenHTMLElement = isHiddenHTMLElement;
509
-
510
- module.exports.showHTMLElement = showHTMLElement;
511
- module.exports.hideHTMLElement = hideHTMLElement;
512
- module.exports.selectHTMLElement = selectHTMLElement;
513
- module.exports.unselectHTMLElement = unselectHTMLElement;
514
- module.exports.markHTMLElementAsCurrent = markHTMLElementAsCurrent;
515
- module.exports.unmarkCurrentHTMLElement = unmarkCurrentHTMLElement;
516
- module.exports.markHTMLElementAsActive = markHTMLElementAsActive;
517
- module.exports.unmarkActiveHTMLElement = unmarkActiveHTMLElement;
518
- module.exports.lockHTMLElement = lockHTMLElement;
519
- module.exports.unlockHTMLElement = unlockHTMLElement;
520
- module.exports.activateHTMLElements = activateHTMLElements;
521
- module.exports.inactivateHTMLElements = inactivateHTMLElements;
522
-
523
- module.exports.valueToClassList = valueToClassList;
524
-
525
- // * experimental *
526
- module.exports.createNewHTMLElement = createNewHTMLElement;
527
- module.exports.readAsAttrValue = readAsAttrValue;
528
- module.exports.readAsTagName = readAsTagName;
529
- module.exports.readAsAttrName = readAsAttrName;
530
- module.exports.valueToElementID = valueToElementID;
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Checks if the given object is an instance of an `HTMLElement`.
3
+ */
4
+ export function isHTMLElement(obj: any): obj is HTMLElement;
5
+ /**
6
+ * Checks if the given object is an HTML-button.
7
+ */
8
+ export function isHTMLButton(obj: any): boolean;
9
+ /**
10
+ * Tries to convert a given value to a valid name of an HTML-tag.
11
+ * @since v0.0.21
12
+ * @experimental
13
+ */
14
+ export function readAsTagName(value: any): string;
15
+ /**
16
+ * Tries to convert a given value to a valid name of an HTML-attribute.
17
+ * @since v0.0.21
18
+ * @experimental
19
+ */
20
+ export function readAsAttrName(value: any): string;
21
+ /**
22
+ * Tries to convert a given value to a valid "attribute value".
23
+ * @since v0.0.18
24
+ * @experimental
25
+ */
26
+ export function readAsAttrValue(value: any): string | null;
27
+ /**
28
+ * Tries to convert a given value to a valid identifier suitable as a value
29
+ * for an "ID-attribute" of an HTML-element.
30
+ * @since v0.0.22
31
+ * @experimental
32
+ */
33
+ export function valueToElementID(value: any): string;
34
+ /**
35
+ * Tries to convert a given value to a list of a valid "class attributes".
36
+ * @since v0.0.13
37
+ */
38
+ export function valueToClassList(value: any, opt?: boolean): string[];
@@ -0,0 +1,184 @@
1
+ // [v0.1.048-20260517]
2
+
3
+ // === module init block ===
4
+
5
+ const {
6
+ valueToArray,
7
+ valueToIDString,
8
+ } = require('@ygracs/bsfoc-lib-js');
9
+
10
+ // === module inner block ===
11
+
12
+ const etlHTagInputForBtn = new Set([
13
+ 'button', 'submit', 'reset', 'image',
14
+ ]);
15
+
16
+ // === module main block ===
17
+
18
+ /**
19
+ * Checks if the given object is an instance of an `HTMLElement`.
20
+ * @function isHTMLElement
21
+ * @param {any} obj - some element
22
+ * @return {boolean}
23
+ */
24
+ function isHTMLElement(obj) {
25
+ return obj instanceof HTMLElement;
26
+ };
27
+ module.exports.isHTMLElement = isHTMLElement;
28
+
29
+ /**
30
+ * Checks if the given object is an HTML-button.
31
+ * @function isHTMLButton
32
+ * @param {any} obj - element to be verified
33
+ * @returns {boolean}
34
+ */
35
+ function isHTMLButton(obj) {
36
+ let result = false;
37
+ if (isHTMLElement(obj)) {
38
+ switch (obj.tagName.toLowerCase()) {
39
+ case 'button': {
40
+ result = true;
41
+ break;
42
+ }
43
+ case 'input': {
44
+ result = etlHTagInputForBtn.has(obj.type.toLowerCase());
45
+ break;
46
+ }
47
+ default: {}
48
+ };
49
+ };
50
+ return result;
51
+ }
52
+ module.exports.isHTMLButton = isHTMLButton;
53
+
54
+ /**
55
+ * Tries to convert a given value to a valid name of an HTML-tag.
56
+ * @since v0.0.21
57
+ * @function readAsTagName
58
+ * @param {any} value - some value
59
+ * @return {string}
60
+ * @experimental
61
+ */
62
+ function readAsTagName(value) {
63
+ let tagName = valueToIDString(value, { ignoreNumbers: true });
64
+ if (tagName === null) return '';
65
+ if (tagName !== '') {
66
+ // // TODO: do extra checks
67
+ const template = /[^\w]/;
68
+ const trigger = tagName.match(template);
69
+ if (trigger) {
70
+ //console.log(trigger);
71
+ tagName = '';
72
+ };
73
+ if (tagName !== '') tagName = tagName.toLowerCase();
74
+ };
75
+ return tagName;
76
+ };
77
+ module.exports.readAsTagName = readAsTagName;
78
+
79
+ /**
80
+ * Tries to convert a given value to a valid name of an HTML-attribute.
81
+ * @since v0.0.21
82
+ * @function readAsAttrName
83
+ * @param {any} value - some value
84
+ * @return {string}
85
+ * @experimental
86
+ */
87
+ function readAsAttrName(value) {
88
+ let attrName = valueToIDString(value, { ignoreNumbers: true });
89
+ if (attrName === null) return '';
90
+ if (attrName !== '') {
91
+ // // TODO: do extra checks
92
+ const template = /[\s\/\\\"\'<>=]/;
93
+ const trigger = attrName.match(template);
94
+ if (trigger) {
95
+ //console.log(trigger);
96
+ attrName = '';
97
+ };
98
+ };
99
+ return attrName;
100
+ };
101
+ module.exports.readAsAttrName = readAsAttrName;
102
+
103
+ /**
104
+ * Tries to convert a given value to a valid "attribute value".
105
+ * @since v0.0.18
106
+ * @function readAsAttrValue
107
+ * @param {any} value - some value
108
+ * @return {?string}
109
+ * @experimental
110
+ */
111
+ function readAsAttrValue(value) {
112
+ let result = null;
113
+ switch (typeof value) {
114
+ case 'boolean': {
115
+ result = value.toString();
116
+ break;
117
+ }
118
+ case 'number': {
119
+ if (Number.isNaN(value)) break;
120
+ result = value.toString();
121
+ break;
122
+ }
123
+ case 'string': {
124
+ result = value.trim();
125
+ break;
126
+ }
127
+ default: {}
128
+ };
129
+ return result;
130
+ };
131
+ module.exports.readAsAttrValue = readAsAttrValue;
132
+
133
+ /**
134
+ * Tries to convert a given value to a valid identifier suitable as a value
135
+ * for an "ID-attribute" of an HTML-element.
136
+ * @since v0.0.22
137
+ * @function valueToElementID
138
+ * @param {any} value - some value
139
+ * @return {string}
140
+ * @experimental
141
+ */
142
+ function valueToElementID(value) {
143
+ let id = valueToIDString(value);
144
+ if (id === null) return '';
145
+ if (id !== '') {
146
+ // // TODO: do extra checks
147
+ const template = /[\s\/\\\"\'<>=]/;
148
+ const trigger = id.match(template);
149
+ if (trigger) {
150
+ //console.log(trigger);
151
+ id = '';
152
+ };
153
+ };
154
+ return id;
155
+ };
156
+ module.exports.valueToElementID = valueToElementID;
157
+
158
+ /**
159
+ * Tries to convert a given value to a list of a valid "class attributes".
160
+ * @since v0.0.13
161
+ * @function valueToClassList
162
+ * @param {any} value - some value
163
+ * @param {boolean} [opt] - flag that indicates whether a dups allowed
164
+ * @return {string[]}
165
+ */
166
+ function valueToClassList(value, opt) {
167
+ /** @type {string[]} */
168
+ let result = [];
169
+ valueToArray(value).forEach((elem) => {
170
+ let value = '';
171
+ if (
172
+ typeof elem === 'string'
173
+ && ((value = elem.trim()) !== '')
174
+ ) {
175
+ const a = value.split(/\s+/);
176
+ if (a.length > 0) result.push(...a);
177
+ };
178
+ });
179
+ if (result.length > 0 && typeof opt === 'boolean' && opt) {
180
+ result = [ ...(new Set(result)) ];
181
+ };
182
+ return result;
183
+ };
184
+ module.exports.valueToClassList = valueToClassList;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntwg/html-helper",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "A base HTML-helper library for js",
5
5
  "author": "ygracs <cs70th-om@rambler.ru>",
6
6
  "license": "MIT",
@@ -13,11 +13,15 @@
13
13
  "html"
14
14
  ],
15
15
  "main": "./index.js",
16
+ "types": "./index.d.ts",
16
17
  "files": [
17
18
  "doc/*.md",
18
19
  "lib/html-helper-lib.js",
19
20
  "lib/event-hfunc.js",
21
+ "lib/mod-hfunc.js",
22
+ "lib/*.d.ts",
20
23
  "index.js",
24
+ "index.d.ts",
21
25
  "CHANGELOG.md"
22
26
  ],
23
27
  "scripts": {
@@ -26,7 +30,8 @@
26
30
  "test-bs:g1": "jest base/fg-1",
27
31
  "test-bs:g2": "jest base/fg-2",
28
32
  "build-doc-md": "jsdoc2md",
29
- "build-doc-html": "jsdoc"
33
+ "build-doc-html": "jsdoc",
34
+ "gen-dts": "npx -p typescript tsc"
30
35
  },
31
36
  "imports": {
32
37
  "#lib/*": "./lib/*",
@@ -37,9 +42,10 @@
37
42
  },
38
43
  "devDependencies": {
39
44
  "@ygracs/test-helper": "~0.0.2-b",
40
- "jest": "^30.2.0",
41
- "jest-environment-jsdom": "^30.2.0",
45
+ "jest": "^30.4.2",
46
+ "jest-environment-jsdom": "^30.4.1",
42
47
  "jsdoc-to-markdown": "^9.1.3",
43
- "minimist": "^1.2.8"
48
+ "minimist": "^1.2.8",
49
+ "typescript": "~5.9.3"
44
50
  }
45
51
  }