@cntwg/html-helper 0.0.19 → 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,91 +1,189 @@
1
- // [v0.1.019-20230614]
1
+ // [v0.1.031-20240713]
2
2
 
3
3
  // === module init block ===
4
4
 
5
5
  const {
6
6
  readAsString,
7
7
  isArray, isObject, isPlainObject, valueToArray,
8
+ valueToIDString,
8
9
  } = require('@ygracs/bsfoc-lib-js');
9
10
 
11
+ // === module extra block (helper functions) ===
12
+
13
+ // === module main block ===
14
+
15
+ /***
16
+ * (* constant definitions *)
17
+ */
18
+
10
19
  const CSS_CLASS_CURRENT = 'current';
11
20
  const CSS_CLASS_SELECTED = 'selected';
12
21
  const CSS_CLASS_ACTIVE = 'active';
13
22
  const CSS_CLASS_DISABLED = 'disabled';
14
23
  const CSS_CLASS_HIDDEN = 'hidden';
15
24
 
16
- // === module extra block (helper functions) ===
17
-
18
- // === module main block (function definitions) ===
19
-
20
- function isHTMLElement(obj){
25
+ /***
26
+ * (* function definitions *)
27
+ */
28
+
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) {
21
36
  return obj instanceof HTMLElement;
22
37
  };
23
38
 
24
- 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) {
25
47
  return isHTMLElement(obj) && obj.classList.contains(CSS_CLASS_SELECTED);
26
48
  };
27
49
 
28
- 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) {
29
58
  return isHTMLElement(obj) && obj.classList.contains(CSS_CLASS_CURRENT);
30
59
  };
31
60
 
32
- 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) {
33
69
  return isHTMLElement(obj) && obj.classList.contains(CSS_CLASS_ACTIVE);
34
70
  };
35
71
 
36
- 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) {
37
80
  return isHTMLElement(obj) && obj.classList.contains(CSS_CLASS_HIDDEN);
38
81
  };
39
82
 
40
- 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) {
41
90
  const isSUCCEED = isHTMLElement(obj);
42
91
  if (isSUCCEED) obj.classList.add(CSS_CLASS_HIDDEN);
43
92
  return isSUCCEED;
44
93
  };
45
94
 
46
- 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) {
47
102
  const isSUCCEED = isHTMLElement(obj);
48
103
  if (isSUCCEED) obj.classList.remove(CSS_CLASS_HIDDEN);
49
104
  return isSUCCEED;
50
105
  };
51
106
 
52
- 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) {
53
114
  const isSUCCEED = isHTMLElement(obj);
54
115
  if (isSUCCEED) obj.classList.add(CSS_CLASS_SELECTED);
55
116
  return isSUCCEED;
56
117
  };
57
118
 
58
- 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) {
59
126
  const isSUCCEED = isHTMLElement(obj);
60
127
  if (isSUCCEED) obj.classList.remove(CSS_CLASS_SELECTED);
61
128
  return isSUCCEED;
62
129
  };
63
130
 
64
- 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) {
65
138
  const isSUCCEED = isHTMLElement(obj);
66
139
  if (isSUCCEED) obj.classList.add(CSS_CLASS_CURRENT);
67
140
  return isSUCCEED;
68
141
  };
69
142
 
70
- 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) {
71
150
  const isSUCCEED = isHTMLElement(obj);
72
151
  if (isSUCCEED) obj.classList.remove(CSS_CLASS_CURRENT);
73
152
  return isSUCCEED;
74
153
  };
75
154
 
76
- 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) {
77
162
  const isSUCCEED = isHTMLElement(obj);
78
163
  if (isSUCCEED) obj.classList.add(CSS_CLASS_ACTIVE);
79
164
  return isSUCCEED;
80
165
  };
81
166
 
82
- 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) {
83
174
  const isSUCCEED = isHTMLElement(obj);
84
175
  if (isSUCCEED) obj.classList.remove(CSS_CLASS_ACTIVE);
85
176
  return isSUCCEED;
86
177
  };
87
178
 
88
- 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) {
89
187
  let isSUCCEED = isHTMLElement(obj);
90
188
  if (isSUCCEED) {
91
189
  const { classList, tagName } = obj;
@@ -104,7 +202,14 @@ function lockHtmlElement(obj){
104
202
  return isSUCCEED;
105
203
  };
106
204
 
107
- 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) {
108
213
  let isSUCCEED = isHTMLElement(obj);
109
214
  if (isSUCCEED) {
110
215
  const { classList, tagName } = obj;
@@ -123,19 +228,81 @@ function unlockHtmlElement(obj){
123
228
  return isSUCCEED;
124
229
  };
125
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
+ */
126
237
  function inactivateHtmlElements(...args) {
127
238
  for (let item of args) {
128
239
  lockHtmlElement(item);
129
240
  };
130
241
  };
131
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
+ */
132
249
  function activateHtmlElements(...args) {
133
250
  for (let item of args) {
134
251
  unlockHtmlElement(item);
135
252
  };
136
253
  };
137
254
 
138
- 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) {
139
306
  let result = null;
140
307
  switch (typeof value) {
141
308
  case 'boolean': {
@@ -158,7 +325,15 @@ function readAsAttrValue(value){
158
325
  return result;
159
326
  };
160
327
 
161
- 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) {
162
337
  let result = [];
163
338
  let list = valueToArray(value);
164
339
  list.forEach((elem) => {
@@ -177,41 +352,40 @@ function valueToClassList(value, opt){
177
352
  return result;
178
353
  };
179
354
 
180
- function valueToIDString(value){
181
- let result = '';
182
- switch (typeof value) {
183
- case 'number': {
184
- if (Number.isNaN(value) || value < 0) break;
185
- if (Number.isInteger(value)) result = value.toString();
186
- break;
187
- }
188
- case 'string': {
189
- result = value.trim();
190
- // // TODO: check if a valid character sequencies
191
- break;
192
- }
193
- default: {
194
- break;
195
- }
196
- };
197
- return result;
198
- };
199
-
200
- function createNewHtmlElement(tagName, opt){
201
- 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);
202
370
  if (_tag === '') return null;
203
- let item = document.createElement(_tag.toLowerCase());
204
- 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)) {
205
380
  let {
206
381
  id,
207
382
  text,
208
383
  attr,
209
- classNames,
210
- class: _class,
384
+ classNames: cl,
211
385
  data: dset,
212
386
  } = opt;
213
387
  // set an element id
214
- if ((id = valueToIDString(id)) !== '') item.setAttribute('id', id);
388
+ if ((id = valueToIDString(id)) !== null) item.setAttribute('id', id);
215
389
  // set an element text context
216
390
  if (typeof text === 'string') {
217
391
  item.appendChild(document.createTextNode(text));
@@ -220,36 +394,29 @@ function createNewHtmlElement(tagName, opt){
220
394
  if (isObject(attr)) {
221
395
  attr = isArray(attr) ? attr : Object.entries(attr);
222
396
  for (let [ key, value ] of attr ) {
223
- key = (typeof key === 'string') ? key.trim() : '';
224
- if (key !== '') {
225
- value = readAsString(value, '', {
226
- useTrim: true,
227
- numberToString: true,
228
- boolToString: true,
229
- });
230
- if (value !== '') item.setAttribute(key.toLowerCase(), value);
231
- }
397
+ if (
398
+ ((key = readAsAttrName(key)) !== '')
399
+ && ((value = readAsAttrValue(value)) !== null)
400
+ ) {
401
+ item.setAttribute(key.toLowerCase(), value);
402
+ };
232
403
  };
233
404
  };
234
405
  // set a class-attributes of the element
235
- if (_class === undefined) _class = classNames;
236
- if (_class !== undefined) {
237
- const cl = valueToClassList(_class, true);
406
+ if (cl !== undefined) {
407
+ cl = valueToClassList(cl, true);
238
408
  if (cl.length > 0) item.classList.add(...cl);
239
409
  };
240
410
  // set a data-attributes of the element
241
411
  if (isObject(dset)) {
242
412
  dset = isArray(dset) ? dset : Object.entries(dset);
243
413
  for (let [ key, value ] of dset ) {
244
- key = (typeof key === 'string') ? key.trim() : '';
245
- if (key !== '') {
246
- value = readAsString(value, '', {
247
- useTrim: true,
248
- numberToString: true,
249
- boolToString: true,
250
- });
251
- if (value !== '') item.dataset[key] = value;
252
- }
414
+ if (
415
+ ((key = readAsAttrName(key)) !== '')
416
+ && ((value = readAsAttrValue(value)) !== null)
417
+ ) {
418
+ item.dataset[key] = value;
419
+ };
253
420
  };
254
421
  };
255
422
  // set an event handlers for the element
@@ -258,11 +425,13 @@ function createNewHtmlElement(tagName, opt){
258
425
  return item;
259
426
  };
260
427
 
261
- // === module main block (class definitions) ===
428
+ /***
429
+ * (* class definitions *)
430
+ */
262
431
 
263
432
  // === module exports block ===
264
433
 
265
- exports.CSS_CLASS_STRING = /*function(){ return*/ {
434
+ module.exports.CSS_CLASS_STRING = /*function(){ return*/ {
266
435
  CSS_CLASS_CURRENT: CSS_CLASS_CURRENT,
267
436
  CSS_CLASS_SELECTED: CSS_CLASS_SELECTED,
268
437
  CSS_CLASS_ACTIVE: CSS_CLASS_ACTIVE,
@@ -270,27 +439,31 @@ exports.CSS_CLASS_STRING = /*function(){ return*/ {
270
439
  CSS_CLASS_HIDDEN: CSS_CLASS_HIDDEN
271
440
  }/*}*/;
272
441
 
273
- exports.isHTMLElement = isHTMLElement;
274
- exports.isCurrentHTMLElement = isCurrentHTMLElement;
275
- exports.isSelectedHTMLElement = isSelectedHTMLElement;
276
- exports.isActiveHTMLElement = isActiveHTMLElement;
277
- exports.isHiddenHTMLElement = isHiddenHTMLElement;
278
- exports.showHtmlElement = showHtmlElement;
279
- exports.hideHtmlElement = hideHtmlElement;
280
- exports.selectHtmlElement = selectHtmlElement;
281
- exports.unselectHtmlElement = unselectHtmlElement;
282
- exports.markHtmlElementAsCurrent = markHtmlElementAsCurrent;
283
- exports.unmarkCurrentHtmlElement = unmarkCurrentHtmlElement;
284
- exports.markHtmlElementAsActive = markHtmlElementAsActive;
285
- exports.unmarkActiveHtmlElement = unmarkActiveHtmlElement;
286
- exports.lockHtmlElement = lockHtmlElement;
287
- exports.unlockHtmlElement = unlockHtmlElement;
288
- exports.activateHtmlElements = activateHtmlElements;
289
- exports.inactivateHtmlElements = inactivateHtmlElements;
290
-
291
- 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;
292
461
 
293
462
  // experimental
294
- exports.createNewHtmlElement = createNewHtmlElement;
295
- exports.valueToIDString = valueToIDString;
296
- 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.19",
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.3"
57
+ "@ygracs/bsfoc-lib-js": "^0.2.1"
50
58
  },
51
59
  "devDependencies": {
52
- "jest": "^29.5.0",
53
- "jest-environment-jsdom": "^29.5.0",
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
  }