@cntwg/html-helper 0.0.20 → 0.0.21

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.031-20240713]
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) ===
25
+ /***
26
+ * (* function definitions *)
27
+ */
18
28
 
19
- // === module main block (function definitions) ===
20
-
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,81 @@ 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 && tagName !== '') {
265
+ // // TODO: do extra checks
266
+ const template = /[^\w]/;
267
+ const trigger = tagName.match(template);
268
+ if (trigger) {
269
+ //console.log(trigger);
270
+ tagName = '';
271
+ };
272
+ if (tagName !== '') tagName = tagName.toLowerCase();
273
+ };
274
+ return tagName === null ? '' : tagName;
275
+ };
276
+
277
+ /**
278
+ * @function readAsAttrName
279
+ * @param {any}
280
+ * @return {string}
281
+ * @since v0.0.21
282
+ * @description Tries to convert a given value to a valid name
283
+ * of an HTML-attribute.
284
+ */
285
+ function readAsAttrName(value) {
286
+ let attrName = valueToIDString(value, { ignoreNumbers: true });
287
+ if (attrName !== null && attrName !== '') {
288
+ // // TODO: do extra checks
289
+ const template = /[\s\/\\\"\'<>=]/;
290
+ const trigger = attrName.match(template);
291
+ if (trigger) {
292
+ //console.log(trigger);
293
+ attrName = '';
294
+ };
295
+ };
296
+ return attrName === null ? '' : attrName;
297
+ };
298
+
299
+ /**
300
+ * @function readAsAttrValue
301
+ * @param {any}
302
+ * @return {?string}
303
+ * @description Tries to convert a given value to a valid "attribute value".
304
+ */
305
+ function readAsAttrValue(value) {
140
306
  let result = null;
141
307
  switch (typeof value) {
142
308
  case 'boolean': {
@@ -159,7 +325,15 @@ function readAsAttrValue(value){
159
325
  return result;
160
326
  };
161
327
 
162
- function valueToClassList(value, opt){
328
+ /**
329
+ * @function valueToClassList
330
+ * @param {any}
331
+ * @param {bool} [opt]
332
+ * @return {array}
333
+ * @description Tries to convert a given value to a list of a valid
334
+ * "class attributes".
335
+ */
336
+ function valueToClassList(value, opt) {
163
337
  let result = [];
164
338
  let list = valueToArray(value);
165
339
  list.forEach((elem) => {
@@ -178,17 +352,36 @@ function valueToClassList(value, opt){
178
352
  return result;
179
353
  };
180
354
 
181
- function createNewHtmlElement(tagName, opt){
182
- let _tag = typeof tagName === 'string' ? tagName.trim() : '';
355
+ /**
356
+ * @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]
364
+ * @return {?HTMLElement}
365
+ * @description Creates a new HTML-element.
366
+ */
367
+ function createNewHtmlElement(tagName, opt) {
368
+ let item = null;
369
+ let _tag = readAsTagName(tagName);
183
370
  if (_tag === '') return null;
184
- let item = document.createElement(_tag.toLowerCase());
185
- if (isPlainObject(opt)) {
371
+ try {
372
+ item = document.createElement(_tag);
373
+ } catch (err) {
374
+ //console.log(`TEST IS_ERR: => ${err}`);
375
+ item = null;
376
+ } finally {
377
+ // // TODO:
378
+ };
379
+ if (item && isPlainObject(opt)) {
186
380
  let {
187
381
  id,
188
382
  text,
189
383
  attr,
190
- classNames,
191
- class: _class,
384
+ classNames: cl,
192
385
  data: dset,
193
386
  } = opt;
194
387
  // set an element id
@@ -202,7 +395,7 @@ function createNewHtmlElement(tagName, opt){
202
395
  attr = isArray(attr) ? attr : Object.entries(attr);
203
396
  for (let [ key, value ] of attr ) {
204
397
  if (
205
- ((key = valueToIDString(key)) !== null)
398
+ ((key = readAsAttrName(key)) !== '')
206
399
  && ((value = readAsAttrValue(value)) !== null)
207
400
  ) {
208
401
  item.setAttribute(key.toLowerCase(), value);
@@ -210,9 +403,8 @@ function createNewHtmlElement(tagName, opt){
210
403
  };
211
404
  };
212
405
  // set a class-attributes of the element
213
- if (_class === undefined) _class = classNames;
214
- if (_class !== undefined) {
215
- const cl = valueToClassList(_class, true);
406
+ if (cl !== undefined) {
407
+ cl = valueToClassList(cl, true);
216
408
  if (cl.length > 0) item.classList.add(...cl);
217
409
  };
218
410
  // set a data-attributes of the element
@@ -220,7 +412,7 @@ function createNewHtmlElement(tagName, opt){
220
412
  dset = isArray(dset) ? dset : Object.entries(dset);
221
413
  for (let [ key, value ] of dset ) {
222
414
  if (
223
- ((key = valueToIDString(key)) !== null)
415
+ ((key = readAsAttrName(key)) !== '')
224
416
  && ((value = readAsAttrValue(value)) !== null)
225
417
  ) {
226
418
  item.dataset[key] = value;
@@ -233,11 +425,13 @@ function createNewHtmlElement(tagName, opt){
233
425
  return item;
234
426
  };
235
427
 
236
- // === module main block (class definitions) ===
428
+ /***
429
+ * (* class definitions *)
430
+ */
237
431
 
238
432
  // === module exports block ===
239
433
 
240
- exports.CSS_CLASS_STRING = /*function(){ return*/ {
434
+ module.exports.CSS_CLASS_STRING = /*function(){ return*/ {
241
435
  CSS_CLASS_CURRENT: CSS_CLASS_CURRENT,
242
436
  CSS_CLASS_SELECTED: CSS_CLASS_SELECTED,
243
437
  CSS_CLASS_ACTIVE: CSS_CLASS_ACTIVE,
@@ -245,27 +439,31 @@ exports.CSS_CLASS_STRING = /*function(){ return*/ {
245
439
  CSS_CLASS_HIDDEN: CSS_CLASS_HIDDEN
246
440
  }/*}*/;
247
441
 
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;
442
+ module.exports.isHTMLElement = isHTMLElement;
443
+ module.exports.isCurrentHTMLElement = isCurrentHTMLElement;
444
+ module.exports.isSelectedHTMLElement = isSelectedHTMLElement;
445
+ module.exports.isActiveHTMLElement = isActiveHTMLElement;
446
+ module.exports.isHiddenHTMLElement = isHiddenHTMLElement;
447
+ module.exports.showHtmlElement = showHtmlElement;
448
+ module.exports.hideHtmlElement = hideHtmlElement;
449
+ module.exports.selectHtmlElement = selectHtmlElement;
450
+ module.exports.unselectHtmlElement = unselectHtmlElement;
451
+ module.exports.markHtmlElementAsCurrent = markHtmlElementAsCurrent;
452
+ module.exports.unmarkCurrentHtmlElement = unmarkCurrentHtmlElement;
453
+ module.exports.markHtmlElementAsActive = markHtmlElementAsActive;
454
+ module.exports.unmarkActiveHtmlElement = unmarkActiveHtmlElement;
455
+ module.exports.lockHtmlElement = lockHtmlElement;
456
+ module.exports.unlockHtmlElement = unlockHtmlElement;
457
+ module.exports.activateHtmlElements = activateHtmlElements;
458
+ module.exports.inactivateHtmlElements = inactivateHtmlElements;
459
+
460
+ module.exports.valueToClassList = valueToClassList;
267
461
 
268
462
  // experimental
269
- exports.createNewHtmlElement = createNewHtmlElement;
270
- exports.valueToIDString = valueToIDString;
271
- exports.readAsAttrValue = readAsAttrValue;
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.20",
3
+ "version": "0.0.21",
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
  }