@cntwg/html-helper 0.0.21 → 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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ #### *v0.0.22*
2
+
3
+ Pre-release version.
4
+
5
+ > - `html-helper-lib.md` updated;
6
+ > - add functions `valueToElementID` into `html-helper-lib.js` module;
7
+ > - improve behavior for `createNewHtmlElement` function.
8
+
1
9
  #### *v0.0.21*
2
10
 
3
11
  Pre-release version.
@@ -8,6 +16,7 @@ Pre-release version.
8
16
  > - updated dependency on `@ygracs/bsfoc-lib-js` module to v0.2.1;
9
17
  > - remove some deprecates (*see docs for details*);
10
18
  > - change behavior for `loadItems` method of `THtmlStubItemsSet` class in handling `options` parameter (*see docs for details*);
19
+ > - add functions `readAsTagName` and `readAsAttrName` into `html-helper-lib.js` module;
11
20
  > - added 'components' property for module exports;
12
21
  > - some other fixes.
13
22
 
@@ -1,6 +1,6 @@
1
- >|***rev.*:**|0.1.11|
1
+ >|***rev.*:**|0.1.12|
2
2
  >|:---|---:|
3
- >|date:|2023-07-13|
3
+ >|date:|2023-07-14|
4
4
 
5
5
  ## Introduction
6
6
 
@@ -148,3 +148,9 @@ This function tries to convert a given `value` to the value of type which is sui
148
148
  #### **readAsAttrValue(value)**
149
149
 
150
150
  This function tries to convert a given `value` to the value of type which is suitable for an HTML-element's attribute. If failed a `null` is returned.
151
+
152
+ #### **valueToElementID(value)**
153
+
154
+ > since: `v0.0.22`
155
+
156
+ This function tries to convert a given `value` to the value of a valid identifier which is suitable for an HTML-element's ID-attribute. If failed an empty string is returned.
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- // [v0.1.019-20240713]
1
+ // [v0.1.021-20240714]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -62,6 +62,7 @@ module.exports.components = components;
62
62
  module.exports.readAsTagName = html_helper.readAsTagName;
63
63
  module.exports.readAsAttrName = html_helper.readAsAttrName;
64
64
  module.exports.readAsAttrValue = html_helper.readAsAttrValue;
65
+ module.exports.valueToElementID = html_helper.valueToElementID;
65
66
  module.exports.createNewHtmlElement = html_helper.createNewHtmlElement;
66
67
 
67
68
  module.exports.THtmlInputField = html_flds.THtmlInputField;
@@ -1,4 +1,4 @@
1
- // [v0.1.062-20240705]
1
+ // [v0.1.063-20240714]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -37,6 +37,16 @@ const ILC_SMODE_CTRL = 2;
37
37
 
38
38
  // === module extra block (helper functions) ===
39
39
 
40
+ /**
41
+ * @function srchListElementByAttr
42
+ * @param {TItemsListEx}
43
+ * @param {string}
44
+ * @param {string}
45
+ * @returns {object}
46
+ * @inner
47
+ * @description Searches an element in a list by a given attributes
48
+ * of that element.
49
+ */
40
50
  function srchListElementByAttr(list, name, value = '') {
41
51
  const _value = readAsAttrValue(value);
42
52
  let item = null;
@@ -1,4 +1,4 @@
1
- // [v0.1.031-20240713]
1
+ // [v0.1.033-20240714]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -261,7 +261,8 @@ function activateHtmlElements(...args) {
261
261
  */
262
262
  function readAsTagName(value) {
263
263
  let tagName = valueToIDString(value, { ignoreNumbers: true });
264
- if (tagName !== null && tagName !== '') {
264
+ if (tagName === null) return '';
265
+ if (tagName !== '') {
265
266
  // // TODO: do extra checks
266
267
  const template = /[^\w]/;
267
268
  const trigger = tagName.match(template);
@@ -271,7 +272,7 @@ function readAsTagName(value) {
271
272
  };
272
273
  if (tagName !== '') tagName = tagName.toLowerCase();
273
274
  };
274
- return tagName === null ? '' : tagName;
275
+ return tagName;
275
276
  };
276
277
 
277
278
  /**
@@ -284,7 +285,8 @@ function readAsTagName(value) {
284
285
  */
285
286
  function readAsAttrName(value) {
286
287
  let attrName = valueToIDString(value, { ignoreNumbers: true });
287
- if (attrName !== null && attrName !== '') {
288
+ if (attrName === null) return '';
289
+ if (attrName !== '') {
288
290
  // // TODO: do extra checks
289
291
  const template = /[\s\/\\\"\'<>=]/;
290
292
  const trigger = attrName.match(template);
@@ -293,13 +295,14 @@ function readAsAttrName(value) {
293
295
  attrName = '';
294
296
  };
295
297
  };
296
- return attrName === null ? '' : attrName;
298
+ return attrName;
297
299
  };
298
300
 
299
301
  /**
300
302
  * @function readAsAttrValue
301
303
  * @param {any}
302
304
  * @return {?string}
305
+ * @since v0.0.18
303
306
  * @description Tries to convert a given value to a valid "attribute value".
304
307
  */
305
308
  function readAsAttrValue(value) {
@@ -330,6 +333,7 @@ function readAsAttrValue(value) {
330
333
  * @param {any}
331
334
  * @param {bool} [opt]
332
335
  * @return {array}
336
+ * @since v0.0.13
333
337
  * @description Tries to convert a given value to a list of a valid
334
338
  * "class attributes".
335
339
  */
@@ -352,6 +356,29 @@ function valueToClassList(value, opt) {
352
356
  return result;
353
357
  };
354
358
 
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
+
355
382
  /**
356
383
  * @function createNewHtmlElement
357
384
  * @param {string}
@@ -385,7 +412,7 @@ function createNewHtmlElement(tagName, opt) {
385
412
  data: dset,
386
413
  } = opt;
387
414
  // set an element id
388
- if ((id = valueToIDString(id)) !== null) item.setAttribute('id', id);
415
+ if ((id = valueToElementID(id)) !== '') item.setAttribute('id', id);
389
416
  // set an element text context
390
417
  if (typeof text === 'string') {
391
418
  item.appendChild(document.createTextNode(text));
@@ -396,9 +423,20 @@ function createNewHtmlElement(tagName, opt) {
396
423
  for (let [ key, value ] of attr ) {
397
424
  if (
398
425
  ((key = readAsAttrName(key)) !== '')
399
- && ((value = readAsAttrValue(value)) !== null)
400
426
  ) {
401
- 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
+ };
402
440
  };
403
441
  };
404
442
  };
@@ -464,6 +502,7 @@ module.exports.createNewHtmlElement = createNewHtmlElement;
464
502
  module.exports.readAsAttrValue = readAsAttrValue;
465
503
  module.exports.readAsTagName = readAsTagName;
466
504
  module.exports.readAsAttrName = readAsAttrName;
505
+ module.exports.valueToElementID = valueToElementID;
467
506
 
468
507
  // re-exported
469
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.21",
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",