@cntwg/html-helper 0.0.21 → 0.0.23

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,4 +1,4 @@
1
- // [v0.1.031-20240713]
1
+ // [v0.1.036-20241110]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -28,8 +28,8 @@ const CSS_CLASS_HIDDEN = 'hidden';
28
28
 
29
29
  /**
30
30
  * @function isHTMLElement
31
- * @param {any}
32
- * @return {bool}
31
+ * @param {any} obj - some element
32
+ * @return {boolean}
33
33
  * @description Checks if the given object is an instance of an `HTMLElement`.
34
34
  */
35
35
  function isHTMLElement(obj) {
@@ -38,8 +38,8 @@ function isHTMLElement(obj) {
38
38
 
39
39
  /**
40
40
  * @function isSelectedHTMLElement
41
- * @param {HTMLElement}
42
- * @return {bool}
41
+ * @param {HTMLElement} obj - some element
42
+ * @return {boolean}
43
43
  * @description Checks if the given object is an instance of a `HTMLElement`
44
44
  * and if so it's marked as "selected".
45
45
  */
@@ -49,8 +49,8 @@ function isSelectedHTMLElement(obj) {
49
49
 
50
50
  /**
51
51
  * @function isCurrentHTMLElement
52
- * @param {HTMLElement}
53
- * @return {bool}
52
+ * @param {HTMLElement} obj - some element
53
+ * @return {boolean}
54
54
  * @description Checks if the given object is an instance of a `HTMLElement`
55
55
  * and if so it's marked as "current".
56
56
  */
@@ -60,8 +60,8 @@ function isCurrentHTMLElement(obj) {
60
60
 
61
61
  /**
62
62
  * @function isActiveHTMLElement
63
- * @param {HTMLElement}
64
- * @return {bool}
63
+ * @param {HTMLElement} obj - some element
64
+ * @return {boolean}
65
65
  * @description Checks if the given object is an instance of a `HTMLElement`
66
66
  * and if so it's marked as "active".
67
67
  */
@@ -71,8 +71,8 @@ function isActiveHTMLElement(obj) {
71
71
 
72
72
  /**
73
73
  * @function isHiddenHTMLElement
74
- * @param {HTMLElement}
75
- * @return {bool}
74
+ * @param {HTMLElement} obj - some element
75
+ * @return {boolean}
76
76
  * @description Checks if the given object is an instance of a `HTMLElement`
77
77
  * and if so it's marked as "hidden".
78
78
  */
@@ -81,109 +81,157 @@ function isHiddenHTMLElement(obj) {
81
81
  };
82
82
 
83
83
  /**
84
- * @function hideHtmlElement
85
- * @param {HTMLElement}
86
- * @return {bool}
84
+ * @since v0.0.23
85
+ * @function hideHTMLElement
86
+ * @param {HTMLElement} obj - some element
87
+ * @return {boolean}
87
88
  * @description Hides a given HTML-element.
88
89
  */
89
- function hideHtmlElement(obj) {
90
+ function hideHTMLElement(obj) {
90
91
  const isSUCCEED = isHTMLElement(obj);
91
92
  if (isSUCCEED) obj.classList.add(CSS_CLASS_HIDDEN);
92
93
  return isSUCCEED;
93
94
  };
94
95
 
96
+ /** @deprecated */
97
+ function hideHtmlElement(obj) {
98
+ return hideHTMLElement(obj);
99
+ };
100
+
95
101
  /**
96
- * @function showHtmlElement
97
- * @param {HTMLElement}
98
- * @return {bool}
102
+ * @since v0.0.23
103
+ * @function showHTMLElement
104
+ * @param {HTMLElement} obj - some element
105
+ * @return {boolean}
99
106
  * @description Shows a given HTML-element.
100
107
  */
101
- function showHtmlElement(obj) {
108
+ function showHTMLElement(obj) {
102
109
  const isSUCCEED = isHTMLElement(obj);
103
110
  if (isSUCCEED) obj.classList.remove(CSS_CLASS_HIDDEN);
104
111
  return isSUCCEED;
105
112
  };
106
113
 
114
+ /** @deprecated */
115
+ function showHtmlElement(obj) {
116
+ return showHTMLElement(obj);
117
+ };
118
+
107
119
  /**
108
- * @function selectHtmlElement
109
- * @param {HTMLElement}
110
- * @return {bool}
120
+ * @since v0.0.23
121
+ * @function selectHTMLElement
122
+ * @param {HTMLElement} obj - some element
123
+ * @return {boolean}
111
124
  * @description Makes selected a given HTML-element.
112
125
  */
113
- function selectHtmlElement(obj) {
126
+ function selectHTMLElement(obj) {
114
127
  const isSUCCEED = isHTMLElement(obj);
115
128
  if (isSUCCEED) obj.classList.add(CSS_CLASS_SELECTED);
116
129
  return isSUCCEED;
117
130
  };
118
131
 
132
+ /** @deprecated */
133
+ function selectHtmlElement(obj) {
134
+ return selectHTMLElement(obj);
135
+ };
136
+
119
137
  /**
120
- * @function unselectHtmlElement
121
- * @param {HTMLElement}
122
- * @return {bool}
138
+ * @since v0.0.23
139
+ * @function unselectHTMLElement
140
+ * @param {HTMLElement} obj - some element
141
+ * @return {boolean}
123
142
  * @description Makes unselected a given HTML-element.
124
143
  */
125
- function unselectHtmlElement(obj) {
144
+ function unselectHTMLElement(obj) {
126
145
  const isSUCCEED = isHTMLElement(obj);
127
146
  if (isSUCCEED) obj.classList.remove(CSS_CLASS_SELECTED);
128
147
  return isSUCCEED;
129
148
  };
130
149
 
150
+ /** @deprecated */
151
+ function unselectHtmlElement(obj) {
152
+ return unselectHTMLElement(obj);
153
+ };
154
+
131
155
  /**
132
- * @function markHtmlElementAsCurrent
133
- * @param {HTMLElement}
134
- * @return {bool} - `true` if succeed.
156
+ * @since v0.0.23
157
+ * @function markHTMLElementAsCurrent
158
+ * @param {HTMLElement} obj - some element
159
+ * @return {boolean} - `true` if succeed.
135
160
  * @description Marks a given HTML-element as "current".
136
161
  */
137
- function markHtmlElementAsCurrent(obj) {
162
+ function markHTMLElementAsCurrent(obj) {
138
163
  const isSUCCEED = isHTMLElement(obj);
139
164
  if (isSUCCEED) obj.classList.add(CSS_CLASS_CURRENT);
140
165
  return isSUCCEED;
141
166
  };
142
167
 
168
+ /** @deprecated */
169
+ function markHtmlElementAsCurrent(obj) {
170
+ return markHTMLElementAsCurrent(obj);
171
+ };
172
+
143
173
  /**
144
- * @function unmarkCurrentHtmlElement
145
- * @param {HTMLElement}
146
- * @return {bool}
174
+ * @since v0.0.23
175
+ * @function unmarkCurrentHTMLElement
176
+ * @param {HTMLElement} obj - some element
177
+ * @return {boolean}
147
178
  * @description Unmarks a given HTML-element previous marked as "current".
148
179
  */
149
- function unmarkCurrentHtmlElement(obj) {
180
+ function unmarkCurrentHTMLElement(obj) {
150
181
  const isSUCCEED = isHTMLElement(obj);
151
182
  if (isSUCCEED) obj.classList.remove(CSS_CLASS_CURRENT);
152
183
  return isSUCCEED;
153
184
  };
154
185
 
186
+ /** @deprecated */
187
+ function unmarkCurrentHtmlElement(obj) {
188
+ return unmarkCurrentHTMLElement(obj);
189
+ };
190
+
155
191
  /**
156
- * @function markHtmlElementAsActive
157
- * @param {HTMLElement}
158
- * @return {bool}
192
+ * @since v0.0.23
193
+ * @function markHTMLElementAsActive
194
+ * @param {HTMLElement} obj - some element
195
+ * @return {boolean}
159
196
  * @description Marks a given HTML-element as "active".
160
197
  */
161
- function markHtmlElementAsActive(obj) {
198
+ function markHTMLElementAsActive(obj) {
162
199
  const isSUCCEED = isHTMLElement(obj);
163
200
  if (isSUCCEED) obj.classList.add(CSS_CLASS_ACTIVE);
164
201
  return isSUCCEED;
165
202
  };
166
203
 
204
+ /** @deprecated */
205
+ function markHtmlElementAsActive(obj) {
206
+ return markHTMLElementAsActive(obj);
207
+ };
208
+
167
209
  /**
168
- * @function unmarkActiveHtmlElement
169
- * @param {HTMLElement}
170
- * @return {bool}
210
+ * @since v0.0.23
211
+ * @function unmarkActiveHTMLElement
212
+ * @param {HTMLElement} obj - some element
213
+ * @return {boolean}
171
214
  * @description Unmarks a given HTML-element previous marked as "active".
172
215
  */
173
- function unmarkActiveHtmlElement(obj) {
216
+ function unmarkActiveHTMLElement(obj) {
174
217
  const isSUCCEED = isHTMLElement(obj);
175
218
  if (isSUCCEED) obj.classList.remove(CSS_CLASS_ACTIVE);
176
219
  return isSUCCEED;
177
220
  };
178
221
 
222
+ /** @deprecated */
223
+ function unmarkActiveHtmlElement(obj) {
224
+ return unmarkActiveHTMLElement(obj);
225
+ };
226
+
179
227
  /**
180
- * @function lockHtmlElement
181
- * @param {HTMLElement}
182
- * @return {bool}
228
+ * @since v0.0.23
229
+ * @function lockHTMLElement
230
+ * @param {HTMLElement} obj - some element
231
+ * @return {boolean}
183
232
  * @description Disables a given HTML-element.
184
- * @since v0.0.19
185
233
  */
186
- function lockHtmlElement(obj) {
234
+ function lockHTMLElement(obj) {
187
235
  let isSUCCEED = isHTMLElement(obj);
188
236
  if (isSUCCEED) {
189
237
  const { classList, tagName } = obj;
@@ -202,14 +250,19 @@ function lockHtmlElement(obj) {
202
250
  return isSUCCEED;
203
251
  };
204
252
 
253
+ /** @deprecated */
254
+ function lockHtmlElement(obj) {
255
+ return lockHTMLElement(obj);
256
+ };
257
+
205
258
  /**
206
- * @function unlockHtmlElement
207
- * @param {HTMLElement}
208
- * @return {bool}
259
+ * @since v0.0.23
260
+ * @function unlockHTMLElement
261
+ * @param {HTMLElement} obj - some element
262
+ * @return {boolean}
209
263
  * @description Enables a given HTML-element.
210
- * @since v0.0.19
211
264
  */
212
- function unlockHtmlElement(obj) {
265
+ function unlockHTMLElement(obj) {
213
266
  let isSUCCEED = isHTMLElement(obj);
214
267
  if (isSUCCEED) {
215
268
  const { classList, tagName } = obj;
@@ -228,40 +281,58 @@ function unlockHtmlElement(obj) {
228
281
  return isSUCCEED;
229
282
  };
230
283
 
284
+ /** @deprecated */
285
+ function unlockHtmlElement(obj) {
286
+ return unlockHTMLElement(obj);
287
+ };
288
+
231
289
  /**
232
- * @function inactivateHtmlElements
233
- * @param {...HTMLElement}
234
- * @return {none}
235
- * @description Disables an HTML-elements given by a list of a function params.
290
+ * @since v0.0.23
291
+ * @function activateHTMLElements
292
+ * @param {...HTMLElement} obj - some element
293
+ * @return {void}
294
+ * @description Enables an HTML-elements given by a list of a function params.
236
295
  */
237
- function inactivateHtmlElements(...args) {
296
+ function activateHTMLElements(...args) {
238
297
  for (let item of args) {
239
- lockHtmlElement(item);
298
+ unlockHTMLElement(item);
240
299
  };
241
300
  };
242
301
 
302
+ /** @deprecated */
303
+ function activateHtmlElements(...args) {
304
+ activateHTMLElements(...args);
305
+ };
306
+
243
307
  /**
244
- * @function activateHtmlElements
245
- * @param {...HTMLElement}
246
- * @return {none}
247
- * @description Enables an HTML-elements given by a list of a function params.
308
+ * @since v0.0.23
309
+ * @function inactivateHTMLElements
310
+ * @param {...HTMLElement} obj - some element
311
+ * @return {void}
312
+ * @description Disables an HTML-elements given by a list of a function params.
248
313
  */
249
- function activateHtmlElements(...args) {
314
+ function inactivateHTMLElements(...args) {
250
315
  for (let item of args) {
251
- unlockHtmlElement(item);
316
+ lockHTMLElement(item);
252
317
  };
253
318
  };
254
319
 
320
+ /** @deprecated */
321
+ function inactivateHtmlElements(...args) {
322
+ inactivateHTMLElements(...args);
323
+ };
324
+
255
325
  /**
326
+ * @since v0.0.21
256
327
  * @function readAsTagName
257
- * @param {any}
328
+ * @param {any} value - some value
258
329
  * @return {string}
259
- * @since v0.0.21
260
330
  * @description Tries to convert a given value to a valid name of an HTML-tag.
261
331
  */
262
332
  function readAsTagName(value) {
263
333
  let tagName = valueToIDString(value, { ignoreNumbers: true });
264
- if (tagName !== null && tagName !== '') {
334
+ if (tagName === null) return '';
335
+ if (tagName !== '') {
265
336
  // // TODO: do extra checks
266
337
  const template = /[^\w]/;
267
338
  const trigger = tagName.match(template);
@@ -271,20 +342,21 @@ function readAsTagName(value) {
271
342
  };
272
343
  if (tagName !== '') tagName = tagName.toLowerCase();
273
344
  };
274
- return tagName === null ? '' : tagName;
345
+ return tagName;
275
346
  };
276
347
 
277
348
  /**
349
+ * @since v0.0.21
278
350
  * @function readAsAttrName
279
- * @param {any}
351
+ * @param {any} value - some value
280
352
  * @return {string}
281
- * @since v0.0.21
282
353
  * @description Tries to convert a given value to a valid name
283
354
  * of an HTML-attribute.
284
355
  */
285
356
  function readAsAttrName(value) {
286
357
  let attrName = valueToIDString(value, { ignoreNumbers: true });
287
- if (attrName !== null && attrName !== '') {
358
+ if (attrName === null) return '';
359
+ if (attrName !== '') {
288
360
  // // TODO: do extra checks
289
361
  const template = /[\s\/\\\"\'<>=]/;
290
362
  const trigger = attrName.match(template);
@@ -293,12 +365,13 @@ function readAsAttrName(value) {
293
365
  attrName = '';
294
366
  };
295
367
  };
296
- return attrName === null ? '' : attrName;
368
+ return attrName;
297
369
  };
298
370
 
299
371
  /**
372
+ * @since v0.0.18
300
373
  * @function readAsAttrValue
301
- * @param {any}
374
+ * @param {any} value - some value
302
375
  * @return {?string}
303
376
  * @description Tries to convert a given value to a valid "attribute value".
304
377
  */
@@ -326,10 +399,11 @@ function readAsAttrValue(value) {
326
399
  };
327
400
 
328
401
  /**
402
+ * @since v0.0.13
329
403
  * @function valueToClassList
330
- * @param {any}
331
- * @param {bool} [opt]
332
- * @return {array}
404
+ * @param {any} value - some value
405
+ * @param {boolean} [opt] - flag that indicates whether a dups allowed
406
+ * @return {string[]}
333
407
  * @description Tries to convert a given value to a list of a valid
334
408
  * "class attributes".
335
409
  */
@@ -352,15 +426,43 @@ function valueToClassList(value, opt) {
352
426
  return result;
353
427
  };
354
428
 
429
+ /**
430
+ * @since v0.0.22
431
+ * @function valueToElementID
432
+ * @param {any} value - some value
433
+ * @return {string}
434
+ * @description Tries to convert a given value to a valid identifier
435
+ * suitable as a value for an "ID-attribute" of an HTML-element.
436
+ */
437
+ function valueToElementID(value) {
438
+ let id = valueToIDString(value);
439
+ if (id === null) return '';
440
+ if (id !== '') {
441
+ // // TODO: do extra checks
442
+ const template = /[\s\/\\\"\'<>=]/;
443
+ const trigger = id.match(template);
444
+ if (trigger) {
445
+ //console.log(trigger);
446
+ id = '';
447
+ };
448
+ };
449
+ return id;
450
+ };
451
+
452
+ /**
453
+ * @typedef {Object} elemDesc
454
+ * @property {string} [id] - element ID
455
+ * @property {string} [text] - some text
456
+ * @property {object} [attr] - an attributes list
457
+ * @property {string|string[]} [classNames] - a class names list (added since v0.0.19)
458
+ * @property {object} [data] - a 'dataset'-attributes list
459
+ * @description An element description.
460
+ */
461
+
355
462
  /**
356
463
  * @function createNewHtmlElement
357
- * @param {string}
358
- * @param {object} [opt]
359
- * @param {string} [opt.id]
360
- * @param {string} [opt.text]
361
- * @param {object} [opt.attr]
362
- * @param {string|array} [opt.classNames] (added since v0.0.19),
363
- * @param {object} [opt.data]
464
+ * @param {string} tagName - an element tag name
465
+ * @param {elemDesc} [opt] - an element description object
364
466
  * @return {?HTMLElement}
365
467
  * @description Creates a new HTML-element.
366
468
  */
@@ -385,7 +487,7 @@ function createNewHtmlElement(tagName, opt) {
385
487
  data: dset,
386
488
  } = opt;
387
489
  // set an element id
388
- if ((id = valueToIDString(id)) !== null) item.setAttribute('id', id);
490
+ if ((id = valueToElementID(id)) !== '') item.setAttribute('id', id);
389
491
  // set an element text context
390
492
  if (typeof text === 'string') {
391
493
  item.appendChild(document.createTextNode(text));
@@ -396,9 +498,20 @@ function createNewHtmlElement(tagName, opt) {
396
498
  for (let [ key, value ] of attr ) {
397
499
  if (
398
500
  ((key = readAsAttrName(key)) !== '')
399
- && ((value = readAsAttrValue(value)) !== null)
400
501
  ) {
401
- item.setAttribute(key.toLowerCase(), value);
502
+ key = key.toLowerCase();
503
+ if (key === 'id') {
504
+ if (
505
+ id === ''
506
+ && ((value = valueToElementID(value)) !== '')
507
+ ) {
508
+ item.setAttribute('id', value);
509
+ };
510
+ } else {
511
+ if ((value = readAsAttrValue(value)) !== null) {
512
+ item.setAttribute(key, value);
513
+ };
514
+ };
402
515
  };
403
516
  };
404
517
  };
@@ -444,6 +557,33 @@ module.exports.isCurrentHTMLElement = isCurrentHTMLElement;
444
557
  module.exports.isSelectedHTMLElement = isSelectedHTMLElement;
445
558
  module.exports.isActiveHTMLElement = isActiveHTMLElement;
446
559
  module.exports.isHiddenHTMLElement = isHiddenHTMLElement;
560
+
561
+ module.exports.showHTMLElement = showHTMLElement;
562
+ module.exports.hideHTMLElement = hideHTMLElement;
563
+ module.exports.selectHTMLElement = selectHTMLElement;
564
+ module.exports.unselectHTMLElement = unselectHTMLElement;
565
+ module.exports.markHTMLElementAsCurrent = markHTMLElementAsCurrent;
566
+ module.exports.unmarkCurrentHTMLElement = unmarkCurrentHTMLElement;
567
+ module.exports.markHTMLElementAsActive = markHTMLElementAsActive;
568
+ module.exports.unmarkActiveHTMLElement = unmarkActiveHTMLElement;
569
+ module.exports.lockHTMLElement = lockHTMLElement;
570
+ module.exports.unlockHTMLElement = unlockHTMLElement;
571
+ module.exports.activateHTMLElements = activateHTMLElements;
572
+ module.exports.inactivateHTMLElements = inactivateHTMLElements;
573
+
574
+ module.exports.valueToClassList = valueToClassList;
575
+
576
+ // experimental
577
+ module.exports.createNewHtmlElement = createNewHtmlElement;
578
+ module.exports.readAsAttrValue = readAsAttrValue;
579
+ module.exports.readAsTagName = readAsTagName;
580
+ module.exports.readAsAttrName = readAsAttrName;
581
+ module.exports.valueToElementID = valueToElementID;
582
+
583
+ // re-exported
584
+ module.exports.valueToIDString = valueToIDString;
585
+
586
+ // @deprecated
447
587
  module.exports.showHtmlElement = showHtmlElement;
448
588
  module.exports.hideHtmlElement = hideHtmlElement;
449
589
  module.exports.selectHtmlElement = selectHtmlElement;
@@ -456,14 +596,3 @@ module.exports.lockHtmlElement = lockHtmlElement;
456
596
  module.exports.unlockHtmlElement = unlockHtmlElement;
457
597
  module.exports.activateHtmlElements = activateHtmlElements;
458
598
  module.exports.inactivateHtmlElements = inactivateHtmlElements;
459
-
460
- module.exports.valueToClassList = valueToClassList;
461
-
462
- // experimental
463
- module.exports.createNewHtmlElement = createNewHtmlElement;
464
- module.exports.readAsAttrValue = readAsAttrValue;
465
- module.exports.readAsTagName = readAsTagName;
466
- module.exports.readAsAttrName = readAsAttrName;
467
-
468
- // re-exported
469
- module.exports.valueToIDString = valueToIDString;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntwg/html-helper",
3
- "version": "0.0.21",
3
+ "version": "0.0.23",
4
4
  "description": "A base HTML-helper library for js",
5
5
  "author": "ygracs <cs70th-om@rambler.ru>",
6
6
  "license": "MIT",
@@ -15,17 +15,15 @@
15
15
  ],
16
16
  "main": "./index.js",
17
17
  "files": [
18
- "doc/html-helper-lib.md",
19
- "doc/html-ctrls-list.md",
20
- "doc/html-ctrls-btn.md",
21
- "doc/html-ctrls-fields.md",
22
- "lib/html-helper-lib.js",
18
+ "doc/*.md",
23
19
  "lib/html-ctrls/lists-stubs.js",
24
20
  "lib/html-ctrls/lists-btn.js",
25
21
  "lib/html-ctrls/list.js",
26
22
  "lib/html-ctrls/buttons.js",
27
23
  "lib/html-ctrls/fields.js",
28
24
  "lib/html-ctrls/mod-hfunc.js",
25
+ "lib/html-helper-lib.js",
26
+ "lib/event-hfunc.js",
29
27
  "index.js",
30
28
  "CHANGELOG.md"
31
29
  ],
@@ -59,7 +57,7 @@
59
57
  "devDependencies": {
60
58
  "jest": "^29.7.0",
61
59
  "jest-environment-jsdom": "^29.7.0",
62
- "jsdoc-to-markdown": "^8.0.1",
60
+ "jsdoc-to-markdown": "^9.0.5",
63
61
  "minimist": "^1.2.8"
64
62
  }
65
63
  }