@cntwg/html-helper 0.0.20 → 0.0.22

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.020-20230801]
1
+ // [v0.1.033-20240714]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -8,85 +8,182 @@ const {
8
8
  valueToIDString,
9
9
  } = require('@ygracs/bsfoc-lib-js');
10
10
 
11
+ // === module extra block (helper functions) ===
12
+
13
+ // === module main block ===
14
+
15
+ /***
16
+ * (* constant definitions *)
17
+ */
18
+
11
19
  const CSS_CLASS_CURRENT = 'current';
12
20
  const CSS_CLASS_SELECTED = 'selected';
13
21
  const CSS_CLASS_ACTIVE = 'active';
14
22
  const CSS_CLASS_DISABLED = 'disabled';
15
23
  const CSS_CLASS_HIDDEN = 'hidden';
16
24
 
17
- // === module extra block (helper functions) ===
18
-
19
- // === module main block (function definitions) ===
25
+ /***
26
+ * (* function definitions *)
27
+ */
20
28
 
21
- function isHTMLElement(obj){
29
+ /**
30
+ * @function isHTMLElement
31
+ * @param {any}
32
+ * @return {bool}
33
+ * @description Checks if the given object is an instance of an `HTMLElement`.
34
+ */
35
+ function isHTMLElement(obj) {
22
36
  return obj instanceof HTMLElement;
23
37
  };
24
38
 
25
- function isSelectedHTMLElement(obj){
39
+ /**
40
+ * @function isSelectedHTMLElement
41
+ * @param {HTMLElement}
42
+ * @return {bool}
43
+ * @description Checks if the given object is an instance of a `HTMLElement`
44
+ * and if so it's marked as "selected".
45
+ */
46
+ function isSelectedHTMLElement(obj) {
26
47
  return isHTMLElement(obj) && obj.classList.contains(CSS_CLASS_SELECTED);
27
48
  };
28
49
 
29
- function isCurrentHTMLElement(obj){
50
+ /**
51
+ * @function isCurrentHTMLElement
52
+ * @param {HTMLElement}
53
+ * @return {bool}
54
+ * @description Checks if the given object is an instance of a `HTMLElement`
55
+ * and if so it's marked as "current".
56
+ */
57
+ function isCurrentHTMLElement(obj) {
30
58
  return isHTMLElement(obj) && obj.classList.contains(CSS_CLASS_CURRENT);
31
59
  };
32
60
 
33
- function isActiveHTMLElement(obj){
61
+ /**
62
+ * @function isActiveHTMLElement
63
+ * @param {HTMLElement}
64
+ * @return {bool}
65
+ * @description Checks if the given object is an instance of a `HTMLElement`
66
+ * and if so it's marked as "active".
67
+ */
68
+ function isActiveHTMLElement(obj) {
34
69
  return isHTMLElement(obj) && obj.classList.contains(CSS_CLASS_ACTIVE);
35
70
  };
36
71
 
37
- function isHiddenHTMLElement(obj){
72
+ /**
73
+ * @function isHiddenHTMLElement
74
+ * @param {HTMLElement}
75
+ * @return {bool}
76
+ * @description Checks if the given object is an instance of a `HTMLElement`
77
+ * and if so it's marked as "hidden".
78
+ */
79
+ function isHiddenHTMLElement(obj) {
38
80
  return isHTMLElement(obj) && obj.classList.contains(CSS_CLASS_HIDDEN);
39
81
  };
40
82
 
41
- function hideHtmlElement(obj){
83
+ /**
84
+ * @function hideHtmlElement
85
+ * @param {HTMLElement}
86
+ * @return {bool}
87
+ * @description Hides a given HTML-element.
88
+ */
89
+ function hideHtmlElement(obj) {
42
90
  const isSUCCEED = isHTMLElement(obj);
43
91
  if (isSUCCEED) obj.classList.add(CSS_CLASS_HIDDEN);
44
92
  return isSUCCEED;
45
93
  };
46
94
 
47
- function showHtmlElement(obj){
95
+ /**
96
+ * @function showHtmlElement
97
+ * @param {HTMLElement}
98
+ * @return {bool}
99
+ * @description Shows a given HTML-element.
100
+ */
101
+ function showHtmlElement(obj) {
48
102
  const isSUCCEED = isHTMLElement(obj);
49
103
  if (isSUCCEED) obj.classList.remove(CSS_CLASS_HIDDEN);
50
104
  return isSUCCEED;
51
105
  };
52
106
 
53
- function selectHtmlElement(obj){
107
+ /**
108
+ * @function selectHtmlElement
109
+ * @param {HTMLElement}
110
+ * @return {bool}
111
+ * @description Makes selected a given HTML-element.
112
+ */
113
+ function selectHtmlElement(obj) {
54
114
  const isSUCCEED = isHTMLElement(obj);
55
115
  if (isSUCCEED) obj.classList.add(CSS_CLASS_SELECTED);
56
116
  return isSUCCEED;
57
117
  };
58
118
 
59
- function unselectHtmlElement(obj){
119
+ /**
120
+ * @function unselectHtmlElement
121
+ * @param {HTMLElement}
122
+ * @return {bool}
123
+ * @description Makes unselected a given HTML-element.
124
+ */
125
+ function unselectHtmlElement(obj) {
60
126
  const isSUCCEED = isHTMLElement(obj);
61
127
  if (isSUCCEED) obj.classList.remove(CSS_CLASS_SELECTED);
62
128
  return isSUCCEED;
63
129
  };
64
130
 
65
- function markHtmlElementAsCurrent(obj){
131
+ /**
132
+ * @function markHtmlElementAsCurrent
133
+ * @param {HTMLElement}
134
+ * @return {bool} - `true` if succeed.
135
+ * @description Marks a given HTML-element as "current".
136
+ */
137
+ function markHtmlElementAsCurrent(obj) {
66
138
  const isSUCCEED = isHTMLElement(obj);
67
139
  if (isSUCCEED) obj.classList.add(CSS_CLASS_CURRENT);
68
140
  return isSUCCEED;
69
141
  };
70
142
 
71
- function unmarkCurrentHtmlElement(obj){
143
+ /**
144
+ * @function unmarkCurrentHtmlElement
145
+ * @param {HTMLElement}
146
+ * @return {bool}
147
+ * @description Unmarks a given HTML-element previous marked as "current".
148
+ */
149
+ function unmarkCurrentHtmlElement(obj) {
72
150
  const isSUCCEED = isHTMLElement(obj);
73
151
  if (isSUCCEED) obj.classList.remove(CSS_CLASS_CURRENT);
74
152
  return isSUCCEED;
75
153
  };
76
154
 
77
- function markHtmlElementAsActive(obj){
155
+ /**
156
+ * @function markHtmlElementAsActive
157
+ * @param {HTMLElement}
158
+ * @return {bool}
159
+ * @description Marks a given HTML-element as "active".
160
+ */
161
+ function markHtmlElementAsActive(obj) {
78
162
  const isSUCCEED = isHTMLElement(obj);
79
163
  if (isSUCCEED) obj.classList.add(CSS_CLASS_ACTIVE);
80
164
  return isSUCCEED;
81
165
  };
82
166
 
83
- function unmarkActiveHtmlElement(obj){
167
+ /**
168
+ * @function unmarkActiveHtmlElement
169
+ * @param {HTMLElement}
170
+ * @return {bool}
171
+ * @description Unmarks a given HTML-element previous marked as "active".
172
+ */
173
+ function unmarkActiveHtmlElement(obj) {
84
174
  const isSUCCEED = isHTMLElement(obj);
85
175
  if (isSUCCEED) obj.classList.remove(CSS_CLASS_ACTIVE);
86
176
  return isSUCCEED;
87
177
  };
88
178
 
89
- function lockHtmlElement(obj){
179
+ /**
180
+ * @function lockHtmlElement
181
+ * @param {HTMLElement}
182
+ * @return {bool}
183
+ * @description Disables a given HTML-element.
184
+ * @since v0.0.19
185
+ */
186
+ function lockHtmlElement(obj) {
90
187
  let isSUCCEED = isHTMLElement(obj);
91
188
  if (isSUCCEED) {
92
189
  const { classList, tagName } = obj;
@@ -105,7 +202,14 @@ function lockHtmlElement(obj){
105
202
  return isSUCCEED;
106
203
  };
107
204
 
108
- function unlockHtmlElement(obj){
205
+ /**
206
+ * @function unlockHtmlElement
207
+ * @param {HTMLElement}
208
+ * @return {bool}
209
+ * @description Enables a given HTML-element.
210
+ * @since v0.0.19
211
+ */
212
+ function unlockHtmlElement(obj) {
109
213
  let isSUCCEED = isHTMLElement(obj);
110
214
  if (isSUCCEED) {
111
215
  const { classList, tagName } = obj;
@@ -124,19 +228,84 @@ function unlockHtmlElement(obj){
124
228
  return isSUCCEED;
125
229
  };
126
230
 
231
+ /**
232
+ * @function inactivateHtmlElements
233
+ * @param {...HTMLElement}
234
+ * @return {none}
235
+ * @description Disables an HTML-elements given by a list of a function params.
236
+ */
127
237
  function inactivateHtmlElements(...args) {
128
238
  for (let item of args) {
129
239
  lockHtmlElement(item);
130
240
  };
131
241
  };
132
242
 
243
+ /**
244
+ * @function activateHtmlElements
245
+ * @param {...HTMLElement}
246
+ * @return {none}
247
+ * @description Enables an HTML-elements given by a list of a function params.
248
+ */
133
249
  function activateHtmlElements(...args) {
134
250
  for (let item of args) {
135
251
  unlockHtmlElement(item);
136
252
  };
137
253
  };
138
254
 
139
- function readAsAttrValue(value){
255
+ /**
256
+ * @function readAsTagName
257
+ * @param {any}
258
+ * @return {string}
259
+ * @since v0.0.21
260
+ * @description Tries to convert a given value to a valid name of an HTML-tag.
261
+ */
262
+ function readAsTagName(value) {
263
+ let tagName = valueToIDString(value, { ignoreNumbers: true });
264
+ if (tagName === null) return '';
265
+ if (tagName !== '') {
266
+ // // TODO: do extra checks
267
+ const template = /[^\w]/;
268
+ const trigger = tagName.match(template);
269
+ if (trigger) {
270
+ //console.log(trigger);
271
+ tagName = '';
272
+ };
273
+ if (tagName !== '') tagName = tagName.toLowerCase();
274
+ };
275
+ return tagName;
276
+ };
277
+
278
+ /**
279
+ * @function readAsAttrName
280
+ * @param {any}
281
+ * @return {string}
282
+ * @since v0.0.21
283
+ * @description Tries to convert a given value to a valid name
284
+ * of an HTML-attribute.
285
+ */
286
+ function readAsAttrName(value) {
287
+ let attrName = valueToIDString(value, { ignoreNumbers: true });
288
+ if (attrName === null) return '';
289
+ if (attrName !== '') {
290
+ // // TODO: do extra checks
291
+ const template = /[\s\/\\\"\'<>=]/;
292
+ const trigger = attrName.match(template);
293
+ if (trigger) {
294
+ //console.log(trigger);
295
+ attrName = '';
296
+ };
297
+ };
298
+ return attrName;
299
+ };
300
+
301
+ /**
302
+ * @function readAsAttrValue
303
+ * @param {any}
304
+ * @return {?string}
305
+ * @since v0.0.18
306
+ * @description Tries to convert a given value to a valid "attribute value".
307
+ */
308
+ function readAsAttrValue(value) {
140
309
  let result = null;
141
310
  switch (typeof value) {
142
311
  case 'boolean': {
@@ -159,7 +328,16 @@ function readAsAttrValue(value){
159
328
  return result;
160
329
  };
161
330
 
162
- function valueToClassList(value, opt){
331
+ /**
332
+ * @function valueToClassList
333
+ * @param {any}
334
+ * @param {bool} [opt]
335
+ * @return {array}
336
+ * @since v0.0.13
337
+ * @description Tries to convert a given value to a list of a valid
338
+ * "class attributes".
339
+ */
340
+ function valueToClassList(value, opt) {
163
341
  let result = [];
164
342
  let list = valueToArray(value);
165
343
  list.forEach((elem) => {
@@ -178,21 +356,63 @@ function valueToClassList(value, opt){
178
356
  return result;
179
357
  };
180
358
 
181
- function createNewHtmlElement(tagName, opt){
182
- let _tag = typeof tagName === 'string' ? tagName.trim() : '';
359
+ /**
360
+ * @function valueToElementID
361
+ * @param {any}
362
+ * @return {string}
363
+ * @since v0.0.22
364
+ * @description Tries to convert a given value to a valid identifier
365
+ * suitable as a value for an "ID-attribute" of an HTML-element.
366
+ */
367
+ function valueToElementID(value) {
368
+ let id = valueToIDString(value);
369
+ if (id === null) return '';
370
+ if (id !== '') {
371
+ // // TODO: do extra checks
372
+ const template = /[\s\/\\\"\'<>=]/;
373
+ const trigger = id.match(template);
374
+ if (trigger) {
375
+ //console.log(trigger);
376
+ id = '';
377
+ };
378
+ };
379
+ return id;
380
+ };
381
+
382
+ /**
383
+ * @function createNewHtmlElement
384
+ * @param {string}
385
+ * @param {object} [opt]
386
+ * @param {string} [opt.id]
387
+ * @param {string} [opt.text]
388
+ * @param {object} [opt.attr]
389
+ * @param {string|array} [opt.classNames] (added since v0.0.19),
390
+ * @param {object} [opt.data]
391
+ * @return {?HTMLElement}
392
+ * @description Creates a new HTML-element.
393
+ */
394
+ function createNewHtmlElement(tagName, opt) {
395
+ let item = null;
396
+ let _tag = readAsTagName(tagName);
183
397
  if (_tag === '') return null;
184
- let item = document.createElement(_tag.toLowerCase());
185
- if (isPlainObject(opt)) {
398
+ try {
399
+ item = document.createElement(_tag);
400
+ } catch (err) {
401
+ //console.log(`TEST IS_ERR: => ${err}`);
402
+ item = null;
403
+ } finally {
404
+ // // TODO:
405
+ };
406
+ if (item && isPlainObject(opt)) {
186
407
  let {
187
408
  id,
188
409
  text,
189
410
  attr,
190
- classNames,
191
- class: _class,
411
+ classNames: cl,
192
412
  data: dset,
193
413
  } = opt;
194
414
  // set an element id
195
- if ((id = valueToIDString(id)) !== null) item.setAttribute('id', id);
415
+ if ((id = valueToElementID(id)) !== '') item.setAttribute('id', id);
196
416
  // set an element text context
197
417
  if (typeof text === 'string') {
198
418
  item.appendChild(document.createTextNode(text));
@@ -202,17 +422,27 @@ function createNewHtmlElement(tagName, opt){
202
422
  attr = isArray(attr) ? attr : Object.entries(attr);
203
423
  for (let [ key, value ] of attr ) {
204
424
  if (
205
- ((key = valueToIDString(key)) !== null)
206
- && ((value = readAsAttrValue(value)) !== null)
425
+ ((key = readAsAttrName(key)) !== '')
207
426
  ) {
208
- item.setAttribute(key.toLowerCase(), value);
427
+ key = key.toLowerCase();
428
+ if (key === 'id') {
429
+ if (
430
+ id === ''
431
+ && ((value = valueToElementID(value)) !== '')
432
+ ) {
433
+ item.setAttribute('id', value);
434
+ };
435
+ } else {
436
+ if ((value = readAsAttrValue(value)) !== null) {
437
+ item.setAttribute(key, value);
438
+ };
439
+ };
209
440
  };
210
441
  };
211
442
  };
212
443
  // set a class-attributes of the element
213
- if (_class === undefined) _class = classNames;
214
- if (_class !== undefined) {
215
- const cl = valueToClassList(_class, true);
444
+ if (cl !== undefined) {
445
+ cl = valueToClassList(cl, true);
216
446
  if (cl.length > 0) item.classList.add(...cl);
217
447
  };
218
448
  // set a data-attributes of the element
@@ -220,7 +450,7 @@ function createNewHtmlElement(tagName, opt){
220
450
  dset = isArray(dset) ? dset : Object.entries(dset);
221
451
  for (let [ key, value ] of dset ) {
222
452
  if (
223
- ((key = valueToIDString(key)) !== null)
453
+ ((key = readAsAttrName(key)) !== '')
224
454
  && ((value = readAsAttrValue(value)) !== null)
225
455
  ) {
226
456
  item.dataset[key] = value;
@@ -233,11 +463,13 @@ function createNewHtmlElement(tagName, opt){
233
463
  return item;
234
464
  };
235
465
 
236
- // === module main block (class definitions) ===
466
+ /***
467
+ * (* class definitions *)
468
+ */
237
469
 
238
470
  // === module exports block ===
239
471
 
240
- exports.CSS_CLASS_STRING = /*function(){ return*/ {
472
+ module.exports.CSS_CLASS_STRING = /*function(){ return*/ {
241
473
  CSS_CLASS_CURRENT: CSS_CLASS_CURRENT,
242
474
  CSS_CLASS_SELECTED: CSS_CLASS_SELECTED,
243
475
  CSS_CLASS_ACTIVE: CSS_CLASS_ACTIVE,
@@ -245,27 +477,32 @@ exports.CSS_CLASS_STRING = /*function(){ return*/ {
245
477
  CSS_CLASS_HIDDEN: CSS_CLASS_HIDDEN
246
478
  }/*}*/;
247
479
 
248
- exports.isHTMLElement = isHTMLElement;
249
- exports.isCurrentHTMLElement = isCurrentHTMLElement;
250
- exports.isSelectedHTMLElement = isSelectedHTMLElement;
251
- exports.isActiveHTMLElement = isActiveHTMLElement;
252
- exports.isHiddenHTMLElement = isHiddenHTMLElement;
253
- exports.showHtmlElement = showHtmlElement;
254
- exports.hideHtmlElement = hideHtmlElement;
255
- exports.selectHtmlElement = selectHtmlElement;
256
- exports.unselectHtmlElement = unselectHtmlElement;
257
- exports.markHtmlElementAsCurrent = markHtmlElementAsCurrent;
258
- exports.unmarkCurrentHtmlElement = unmarkCurrentHtmlElement;
259
- exports.markHtmlElementAsActive = markHtmlElementAsActive;
260
- exports.unmarkActiveHtmlElement = unmarkActiveHtmlElement;
261
- exports.lockHtmlElement = lockHtmlElement;
262
- exports.unlockHtmlElement = unlockHtmlElement;
263
- exports.activateHtmlElements = activateHtmlElements;
264
- exports.inactivateHtmlElements = inactivateHtmlElements;
265
-
266
- exports.valueToClassList = valueToClassList;
480
+ module.exports.isHTMLElement = isHTMLElement;
481
+ module.exports.isCurrentHTMLElement = isCurrentHTMLElement;
482
+ module.exports.isSelectedHTMLElement = isSelectedHTMLElement;
483
+ module.exports.isActiveHTMLElement = isActiveHTMLElement;
484
+ module.exports.isHiddenHTMLElement = isHiddenHTMLElement;
485
+ module.exports.showHtmlElement = showHtmlElement;
486
+ module.exports.hideHtmlElement = hideHtmlElement;
487
+ module.exports.selectHtmlElement = selectHtmlElement;
488
+ module.exports.unselectHtmlElement = unselectHtmlElement;
489
+ module.exports.markHtmlElementAsCurrent = markHtmlElementAsCurrent;
490
+ module.exports.unmarkCurrentHtmlElement = unmarkCurrentHtmlElement;
491
+ module.exports.markHtmlElementAsActive = markHtmlElementAsActive;
492
+ module.exports.unmarkActiveHtmlElement = unmarkActiveHtmlElement;
493
+ module.exports.lockHtmlElement = lockHtmlElement;
494
+ module.exports.unlockHtmlElement = unlockHtmlElement;
495
+ module.exports.activateHtmlElements = activateHtmlElements;
496
+ module.exports.inactivateHtmlElements = inactivateHtmlElements;
497
+
498
+ module.exports.valueToClassList = valueToClassList;
267
499
 
268
500
  // experimental
269
- exports.createNewHtmlElement = createNewHtmlElement;
270
- exports.valueToIDString = valueToIDString;
271
- exports.readAsAttrValue = readAsAttrValue;
501
+ module.exports.createNewHtmlElement = createNewHtmlElement;
502
+ module.exports.readAsAttrValue = readAsAttrValue;
503
+ module.exports.readAsTagName = readAsTagName;
504
+ module.exports.readAsAttrName = readAsAttrName;
505
+ module.exports.valueToElementID = valueToElementID;
506
+
507
+ // re-exported
508
+ module.exports.valueToIDString = valueToIDString;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntwg/html-helper",
3
- "version": "0.0.20",
3
+ "version": "0.0.22",
4
4
  "description": "A base HTML-helper library for js",
5
5
  "author": "ygracs <cs70th-om@rambler.ru>",
6
6
  "license": "MIT",
@@ -8,6 +8,11 @@
8
8
  "type": "git",
9
9
  "url": "git+https://gitlab.com/cntwg/html-helper.git"
10
10
  },
11
+ "keywords": [
12
+ "electron",
13
+ "html",
14
+ "component"
15
+ ],
11
16
  "main": "./index.js",
12
17
  "files": [
13
18
  "doc/html-helper-lib.md",
@@ -40,17 +45,21 @@
40
45
  "test-lbc:ec": "jest THtmlListButtonsController/events",
41
46
  "test-bc1": "jest THtmlButtonsControllerARCSet",
42
47
  "test-bc1:bs": "jest THtmlButtonsControllerARCSet/base",
43
- "test-bc1:ec": "jest THtmlButtonsControllerARCSet/events"
48
+ "test-bc1:ec": "jest THtmlButtonsControllerARCSet/events",
49
+ "build-doc-md": "jsdoc2md",
50
+ "build-doc-html": "jsdoc"
44
51
  },
45
52
  "imports": {
46
- "#lib/*": "./lib/*"
53
+ "#lib/*": "./lib/*",
54
+ "#test-dir/*": "./__test__/*"
47
55
  },
48
56
  "dependencies": {
49
- "@ygracs/bsfoc-lib-js": "^0.1.4"
57
+ "@ygracs/bsfoc-lib-js": "^0.2.1"
50
58
  },
51
59
  "devDependencies": {
52
- "jest": "^29.6.2",
53
- "jest-environment-jsdom": "^29.6.2",
60
+ "jest": "^29.7.0",
61
+ "jest-environment-jsdom": "^29.7.0",
62
+ "jsdoc-to-markdown": "^8.0.1",
54
63
  "minimist": "^1.2.8"
55
64
  }
56
65
  }