@cntwg/xml-lib-js 0.0.27 → 0.0.28
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 +8 -0
- package/README.md +8 -2
- package/doc/xmldoc-lib.md +27 -3
- package/index.js +5 -1
- package/lib/xmldoc-lib.js +88 -11
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|***rev.*:**|0.1.
|
|
1
|
+
|***rev.*:**|0.1.9|
|
|
2
2
|
|:---|---:|
|
|
3
|
-
|***date***:|2024-
|
|
3
|
+
|***date***:|2024-08-01|
|
|
4
4
|
|
|
5
5
|
## Introduction
|
|
6
6
|
|
|
@@ -21,6 +21,12 @@ xObj-manipulator is a set of functions (see docs for [`@ygracs/xobj-lib-js` modu
|
|
|
21
21
|
+ `XML_DEF_ROOT_ETAG_NAME`;
|
|
22
22
|
+ `XML_DEF_PARSE_OPTIONS`;
|
|
23
23
|
|
|
24
|
+
- functions:
|
|
25
|
+
|
|
26
|
+
+ `readAsTagName` (*experimental*);
|
|
27
|
+
+ `readAsAttrName` (*experimental*);
|
|
28
|
+
+ `valueToElementID` (*experimental*);
|
|
29
|
+
|
|
24
30
|
- classes:
|
|
25
31
|
|
|
26
32
|
+ `TXmlAttributesMapper`;
|
package/doc/xmldoc-lib.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
>|***rev.*:**|0.1.
|
|
1
|
+
>|***rev.*:**|0.1.27|
|
|
2
2
|
>|:---|---:|
|
|
3
|
-
>|date:|2024-
|
|
3
|
+
>|date:|2024-08-01|
|
|
4
4
|
|
|
5
5
|
## Introduction
|
|
6
6
|
|
|
7
|
-
This paper describes a constants and an object classes provided by `xmldoc-lib.js` module.
|
|
7
|
+
This paper describes a constants, functions and an object classes provided by `xmldoc-lib.js` module.
|
|
8
8
|
|
|
9
9
|
## Module constants
|
|
10
10
|
|
|
@@ -45,6 +45,30 @@ This constant defines a default value for naming the root element for XML-docume
|
|
|
45
45
|
|
|
46
46
|
This constant defines a default value for naming the language attribute for XML-element. Its value is `lang`.
|
|
47
47
|
|
|
48
|
+
## Module functions
|
|
49
|
+
|
|
50
|
+
### Experimental functions
|
|
51
|
+
|
|
52
|
+
> Note: Purpose of those functions will be discussed and some may be deprecate and make obsolete or functionality may be altered. So use it with cautions in mind.
|
|
53
|
+
|
|
54
|
+
#### **readAsTagName(value)**
|
|
55
|
+
|
|
56
|
+
> since: `v0.0.28`
|
|
57
|
+
|
|
58
|
+
This function tries to convert a given `value` to the value of type which is suitable for an XML-element's tag name. If failed an empty string is returned.
|
|
59
|
+
|
|
60
|
+
#### **readAsAttrName(value)**
|
|
61
|
+
|
|
62
|
+
> since: `v0.0.28`
|
|
63
|
+
|
|
64
|
+
This function tries to convert a given `value` to the value of type which is suitable for an XML-element's attribute name. If failed an empty string is returned.
|
|
65
|
+
|
|
66
|
+
#### **valueToElementID(value)**
|
|
67
|
+
|
|
68
|
+
> since: `v0.0.28`
|
|
69
|
+
|
|
70
|
+
This function tries to convert a given `value` to the value of a valid identifier which is suitable for an XML-element's ID-attribute. If failed an empty string is returned.
|
|
71
|
+
|
|
48
72
|
## Module classes
|
|
49
73
|
|
|
50
74
|
### **TXmlContentParseOptions**
|
package/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// [v0.1.
|
|
1
|
+
// [v0.1.016-20240801]
|
|
2
2
|
|
|
3
3
|
// === module init block ===
|
|
4
4
|
|
|
@@ -20,6 +20,10 @@ module.exports.XML_DEF_PARSE_OPTIONS = xmldoc.XML_DEF_PARSE_OPTIONS;
|
|
|
20
20
|
module.exports.XML_DEF_ROOT_ETAG_NAME = xmldoc.XML_DEF_ROOT_ETAG_NAME;
|
|
21
21
|
module.exports.XML_LANG_ATTR_TNAME = xmldoc.XML_LANG_ATTR_TNAME;
|
|
22
22
|
|
|
23
|
+
module.exports.readAsTagName = xmldoc.readAsTagName;
|
|
24
|
+
module.exports.readAsAttrName = xmldoc.readAsAttrName;
|
|
25
|
+
module.exports.valueToElementID = xmldoc.valueToElementID;
|
|
26
|
+
|
|
23
27
|
module.exports.TXmlContentParseOptions = xmldoc.TXmlContentParseOptions;
|
|
24
28
|
|
|
25
29
|
module.exports.TXmlAttributesMapper = xmldoc.TXmlAttributesMapper;
|
package/lib/xmldoc-lib.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// [v0.1.
|
|
1
|
+
// [v0.1.085-20240801]
|
|
2
2
|
|
|
3
3
|
// === module init block ===
|
|
4
4
|
|
|
@@ -10,6 +10,7 @@ const {
|
|
|
10
10
|
valueToIndex, readAsBool, readAsString, isNotEmptyString,
|
|
11
11
|
isArray, isObject, isPlainObject,
|
|
12
12
|
valueToArray, valueToEntry,
|
|
13
|
+
valueToIDString,
|
|
13
14
|
} = require('@ygracs/bsfoc-lib-js');
|
|
14
15
|
|
|
15
16
|
const xObj = require('@ygracs/xobj-lib-js');
|
|
@@ -66,6 +67,75 @@ const XML_TE_NROOT_EMSG = 'root element not found';
|
|
|
66
67
|
* (* function definitions *)
|
|
67
68
|
*/
|
|
68
69
|
|
|
70
|
+
/**
|
|
71
|
+
* @function readAsTagName
|
|
72
|
+
* @param {any}
|
|
73
|
+
* @return {string}
|
|
74
|
+
* @since v0.0.28
|
|
75
|
+
* @description Tries to convert a given value to a valid tag name
|
|
76
|
+
* of an XML-element.
|
|
77
|
+
*/
|
|
78
|
+
function readAsTagName(value) {
|
|
79
|
+
let tagName = valueToIDString(value, { ignoreNumbers: true });
|
|
80
|
+
if (tagName === null) return '';
|
|
81
|
+
if (tagName !== '') {
|
|
82
|
+
// // TODO: do extra checks
|
|
83
|
+
const template = /[\s\/\\\"\'<>=]/;
|
|
84
|
+
const trigger = tagName.match(template);
|
|
85
|
+
if (trigger) {
|
|
86
|
+
//console.log(trigger);
|
|
87
|
+
tagName = '';
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
return tagName;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @function readAsAttrName
|
|
95
|
+
* @param {any}
|
|
96
|
+
* @return {string}
|
|
97
|
+
* @since v0.0.28
|
|
98
|
+
* @description Tries to convert a given value to a valid name
|
|
99
|
+
* of an XML-attribute.
|
|
100
|
+
*/
|
|
101
|
+
function readAsAttrName(value) {
|
|
102
|
+
let attrName = valueToIDString(value, { ignoreNumbers: true });
|
|
103
|
+
if (attrName === null) return '';
|
|
104
|
+
if (attrName !== '') {
|
|
105
|
+
// // TODO: do extra checks
|
|
106
|
+
const template = /[\s\/\\\"\'<>=]/;
|
|
107
|
+
const trigger = attrName.match(template);
|
|
108
|
+
if (trigger) {
|
|
109
|
+
//console.log(trigger);
|
|
110
|
+
attrName = '';
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
return attrName;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @function valueToElementID
|
|
118
|
+
* @param {any}
|
|
119
|
+
* @return {string}
|
|
120
|
+
* @since v0.0.28
|
|
121
|
+
* @description Tries to convert a given value to a valid identifier
|
|
122
|
+
* suitable as a value for an "ID-attribute" of an XML-element.
|
|
123
|
+
*/
|
|
124
|
+
function valueToElementID(value) {
|
|
125
|
+
let id = valueToIDString(value);
|
|
126
|
+
if (id === null) return '';
|
|
127
|
+
if (id !== '') {
|
|
128
|
+
// // TODO: do extra checks
|
|
129
|
+
const template = /[\s\/\\\"\'<>=]/;
|
|
130
|
+
const trigger = id.match(template);
|
|
131
|
+
if (trigger) {
|
|
132
|
+
//console.log(trigger);
|
|
133
|
+
id = '';
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
return id;
|
|
137
|
+
};
|
|
138
|
+
|
|
69
139
|
/***
|
|
70
140
|
* (* class definitions *)
|
|
71
141
|
*/
|
|
@@ -166,17 +236,20 @@ class TXmlAttributesMapper {
|
|
|
166
236
|
* @description Sets an attribute value.
|
|
167
237
|
*/
|
|
168
238
|
setAttribute(name, value){
|
|
239
|
+
const attrName = readAsAttrName(name);
|
|
169
240
|
let result = false;
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
241
|
+
if (attrName !== '') {
|
|
242
|
+
try {
|
|
243
|
+
result = xObj.writeXObjAttr(
|
|
244
|
+
this.#_host,
|
|
245
|
+
name,
|
|
246
|
+
value,
|
|
247
|
+
this.#_options.settings.attributesKey,
|
|
248
|
+
);
|
|
249
|
+
} catch (err) {
|
|
250
|
+
// // TODO: verify what error is thrown
|
|
251
|
+
result = false;
|
|
252
|
+
};
|
|
180
253
|
};
|
|
181
254
|
return result;
|
|
182
255
|
}
|
|
@@ -1443,6 +1516,10 @@ module.exports.XML_DEF_PARSE_OPTIONS = XML_DEF_PARSE_OPTIONS;
|
|
|
1443
1516
|
module.exports.XML_DEF_ROOT_ETAG_NAME = XML_DEF_ROOT_ETAG_NAME;
|
|
1444
1517
|
module.exports.XML_LANG_ATTR_TNAME = XML_LANG_ATTR_TNAME;
|
|
1445
1518
|
|
|
1519
|
+
module.exports.readAsTagName = readAsTagName;
|
|
1520
|
+
module.exports.readAsAttrName = readAsAttrName;
|
|
1521
|
+
module.exports.valueToElementID = valueToElementID;
|
|
1522
|
+
|
|
1446
1523
|
module.exports.TXmlAttributesMapper = TXmlAttributesMapper;
|
|
1447
1524
|
module.exports.TXmlElementController = TXmlElementController;
|
|
1448
1525
|
module.exports.TXmlElementsListController = TXmlElementsListController;
|