@cntwg/html-helper 0.0.19 → 0.0.20
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 +7 -0
- package/lib/html-helper-lib.js +15 -40
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
package/lib/html-helper-lib.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
// [v0.1.
|
|
1
|
+
// [v0.1.020-20230801]
|
|
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
|
|
|
10
11
|
const CSS_CLASS_CURRENT = 'current';
|
|
@@ -177,26 +178,6 @@ function valueToClassList(value, opt){
|
|
|
177
178
|
return result;
|
|
178
179
|
};
|
|
179
180
|
|
|
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
181
|
function createNewHtmlElement(tagName, opt){
|
|
201
182
|
let _tag = typeof tagName === 'string' ? tagName.trim() : '';
|
|
202
183
|
if (_tag === '') return null;
|
|
@@ -211,7 +192,7 @@ function createNewHtmlElement(tagName, opt){
|
|
|
211
192
|
data: dset,
|
|
212
193
|
} = opt;
|
|
213
194
|
// set an element id
|
|
214
|
-
if ((id = valueToIDString(id)) !==
|
|
195
|
+
if ((id = valueToIDString(id)) !== null) item.setAttribute('id', id);
|
|
215
196
|
// set an element text context
|
|
216
197
|
if (typeof text === 'string') {
|
|
217
198
|
item.appendChild(document.createTextNode(text));
|
|
@@ -220,15 +201,12 @@ function createNewHtmlElement(tagName, opt){
|
|
|
220
201
|
if (isObject(attr)) {
|
|
221
202
|
attr = isArray(attr) ? attr : Object.entries(attr);
|
|
222
203
|
for (let [ key, value ] of attr ) {
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
value =
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
});
|
|
230
|
-
if (value !== '') item.setAttribute(key.toLowerCase(), value);
|
|
231
|
-
}
|
|
204
|
+
if (
|
|
205
|
+
((key = valueToIDString(key)) !== null)
|
|
206
|
+
&& ((value = readAsAttrValue(value)) !== null)
|
|
207
|
+
) {
|
|
208
|
+
item.setAttribute(key.toLowerCase(), value);
|
|
209
|
+
};
|
|
232
210
|
};
|
|
233
211
|
};
|
|
234
212
|
// set a class-attributes of the element
|
|
@@ -241,15 +219,12 @@ function createNewHtmlElement(tagName, opt){
|
|
|
241
219
|
if (isObject(dset)) {
|
|
242
220
|
dset = isArray(dset) ? dset : Object.entries(dset);
|
|
243
221
|
for (let [ key, value ] of dset ) {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
value =
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
});
|
|
251
|
-
if (value !== '') item.dataset[key] = value;
|
|
252
|
-
}
|
|
222
|
+
if (
|
|
223
|
+
((key = valueToIDString(key)) !== null)
|
|
224
|
+
&& ((value = readAsAttrValue(value)) !== null)
|
|
225
|
+
) {
|
|
226
|
+
item.dataset[key] = value;
|
|
227
|
+
};
|
|
253
228
|
};
|
|
254
229
|
};
|
|
255
230
|
// set an event handlers for the element
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cntwg/html-helper",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.20",
|
|
4
4
|
"description": "A base HTML-helper library for js",
|
|
5
5
|
"author": "ygracs <cs70th-om@rambler.ru>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -46,11 +46,11 @@
|
|
|
46
46
|
"#lib/*": "./lib/*"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@ygracs/bsfoc-lib-js": "^0.1.
|
|
49
|
+
"@ygracs/bsfoc-lib-js": "^0.1.4"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"jest": "^29.
|
|
53
|
-
"jest-environment-jsdom": "^29.
|
|
52
|
+
"jest": "^29.6.2",
|
|
53
|
+
"jest-environment-jsdom": "^29.6.2",
|
|
54
54
|
"minimist": "^1.2.8"
|
|
55
55
|
}
|
|
56
56
|
}
|