@cntwg/html-helper 0.1.3 → 0.1.4
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 +6 -0
- package/index.d.ts +14 -4
- package/index.js +12 -9
- package/lib/html-helper-lib.d.ts +23 -57
- package/lib/html-helper-lib.js +34 -195
- package/lib/mod-hfunc.d.ts +38 -0
- package/lib/mod-hfunc.js +184 -0
- package/package.json +4 -3
package/CHANGELOG.md
CHANGED
package/index.d.ts
CHANGED
|
@@ -8,7 +8,6 @@ import ev_helper = require("./lib/event-hfunc.js");
|
|
|
8
8
|
|
|
9
9
|
import {
|
|
10
10
|
CSS_CLASS_STRING,
|
|
11
|
-
isHTMLElement,
|
|
12
11
|
isSelectedHTMLElement, isCurrentHTMLElement,
|
|
13
12
|
isActiveHTMLElement, isHiddenHTMLElement,
|
|
14
13
|
hideHTMLElement, showHTMLElement,
|
|
@@ -17,12 +16,19 @@ import {
|
|
|
17
16
|
markHTMLElementAsActive, unmarkActiveHTMLElement,
|
|
18
17
|
lockHTMLElement, unlockHTMLElement,
|
|
19
18
|
activateHTMLElements, inactivateHTMLElements,
|
|
19
|
+
createNewHTMLElement,
|
|
20
|
+
} from "./lib/html-helper-lib";
|
|
21
|
+
|
|
22
|
+
import type {
|
|
23
|
+
IElementDesc,
|
|
24
|
+
} from "./lib/html-helper-lib";
|
|
25
|
+
|
|
26
|
+
import {
|
|
27
|
+
isHTMLElement, isHTMLButton,
|
|
20
28
|
valueToClassList,
|
|
21
29
|
readAsTagName, readAsAttrName, readAsAttrValue,
|
|
22
30
|
valueToElementID,
|
|
23
|
-
|
|
24
|
-
isHTMLButton,
|
|
25
|
-
} from "./lib/html-helper-lib.js";
|
|
31
|
+
} from "./lib/mod-hfunc";
|
|
26
32
|
|
|
27
33
|
export {
|
|
28
34
|
CSS_CLASS_STRING,
|
|
@@ -42,3 +48,7 @@ export {
|
|
|
42
48
|
isHTMLButton,
|
|
43
49
|
valueToIDString,
|
|
44
50
|
};
|
|
51
|
+
|
|
52
|
+
export type {
|
|
53
|
+
IElementDesc,
|
|
54
|
+
};
|
package/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
// [v0.2.
|
|
1
|
+
// [v0.2.037-20260517]
|
|
2
2
|
|
|
3
3
|
// === module init block ===
|
|
4
4
|
|
|
5
5
|
const html_helper = require('./lib/html-helper-lib');
|
|
6
|
+
const mod_helper = require('./lib/mod-hfunc');
|
|
6
7
|
const ev_helper = require('./lib/event-hfunc');
|
|
7
8
|
|
|
8
9
|
const {
|
|
@@ -23,8 +24,15 @@ module.exports.eventHelper = eventHelper;
|
|
|
23
24
|
|
|
24
25
|
// === module exports block ===
|
|
25
26
|
|
|
27
|
+
module.exports.isHTMLElement = mod_helper.isHTMLElement;
|
|
28
|
+
module.exports.isHTMLButton = mod_helper.isHTMLButton;
|
|
29
|
+
module.exports.readAsTagName = mod_helper.readAsTagName;
|
|
30
|
+
module.exports.readAsAttrName = mod_helper.readAsAttrName;
|
|
31
|
+
module.exports.readAsAttrValue = mod_helper.readAsAttrValue;
|
|
32
|
+
module.exports.valueToElementID = mod_helper.valueToElementID;
|
|
33
|
+
module.exports.valueToClassList = mod_helper.valueToClassList;
|
|
34
|
+
|
|
26
35
|
module.exports.CSS_CLASS_STRING = html_helper.CSS_CLASS_STRING;
|
|
27
|
-
module.exports.isHTMLElement = html_helper.isHTMLElement;
|
|
28
36
|
module.exports.isSelectedHTMLElement = html_helper.isSelectedHTMLElement;
|
|
29
37
|
module.exports.isCurrentHTMLElement = html_helper.isCurrentHTMLElement;
|
|
30
38
|
module.exports.isActiveHTMLElement = html_helper.isActiveHTMLElement;
|
|
@@ -41,15 +49,10 @@ module.exports.lockHTMLElement = html_helper.lockHTMLElement;
|
|
|
41
49
|
module.exports.unlockHTMLElement = html_helper.unlockHTMLElement;
|
|
42
50
|
module.exports.activateHTMLElements = html_helper.activateHTMLElements;
|
|
43
51
|
module.exports.inactivateHTMLElements = html_helper.inactivateHTMLElements;
|
|
44
|
-
module.exports.valueToClassList = html_helper.valueToClassList;
|
|
45
52
|
|
|
46
|
-
// * experimental *
|
|
47
|
-
module.exports.readAsTagName = html_helper.readAsTagName;
|
|
48
|
-
module.exports.readAsAttrName = html_helper.readAsAttrName;
|
|
49
|
-
module.exports.readAsAttrValue = html_helper.readAsAttrValue;
|
|
50
|
-
module.exports.valueToElementID = html_helper.valueToElementID;
|
|
51
53
|
module.exports.createNewHTMLElement = html_helper.createNewHTMLElement;
|
|
52
|
-
|
|
54
|
+
// * exports types definitions *
|
|
55
|
+
module.exports.IElementDesc = html_helper.IElementDesc;
|
|
53
56
|
|
|
54
57
|
// * re-exported *
|
|
55
58
|
module.exports.valueToIDString = valueToIDString;
|
package/lib/html-helper-lib.d.ts
CHANGED
|
@@ -1,15 +1,7 @@
|
|
|
1
|
-
export namespace CSS_CLASS_STRING {
|
|
2
|
-
export { CSS_CLASS_CURRENT };
|
|
3
|
-
export { CSS_CLASS_SELECTED };
|
|
4
|
-
export { CSS_CLASS_ACTIVE };
|
|
5
|
-
export { CSS_CLASS_DISABLED };
|
|
6
|
-
export { CSS_CLASS_HIDDEN };
|
|
7
|
-
}
|
|
8
|
-
|
|
9
1
|
/**
|
|
10
2
|
* An element descriptor.
|
|
11
3
|
*/
|
|
12
|
-
export type
|
|
4
|
+
export type IElementDesc = {
|
|
13
5
|
/**
|
|
14
6
|
* - element ID
|
|
15
7
|
*/
|
|
@@ -36,31 +28,25 @@ export type elemDesc = {
|
|
|
36
28
|
[x: string]: any;
|
|
37
29
|
};
|
|
38
30
|
};
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
31
|
+
export const IElementDesc: IElementDesc;
|
|
32
|
+
export namespace CSS_CLASS_STRING {
|
|
33
|
+
export { CSS_CLASS_CURRENT };
|
|
34
|
+
export { CSS_CLASS_SELECTED };
|
|
35
|
+
export { CSS_CLASS_ACTIVE };
|
|
36
|
+
export { CSS_CLASS_DISABLED };
|
|
37
|
+
export { CSS_CLASS_HIDDEN };
|
|
38
|
+
}
|
|
45
39
|
|
|
46
40
|
/**
|
|
47
|
-
* Checks if the given object is an instance of
|
|
48
|
-
|
|
49
|
-
export function isHTMLElement(obj: any): obj is HTMLElement;
|
|
50
|
-
/**
|
|
51
|
-
* Checks if the given object is an HTML-button.
|
|
41
|
+
* Checks if the given object is an instance of a `HTMLElement`
|
|
42
|
+
* and if so it's marked as "selected".
|
|
52
43
|
*/
|
|
53
|
-
export function
|
|
44
|
+
export function isSelectedHTMLElement(obj: HTMLElement): boolean;
|
|
54
45
|
/**
|
|
55
46
|
* Checks if the given object is an instance of a `HTMLElement`
|
|
56
47
|
* and if so it's marked as "current".
|
|
57
48
|
*/
|
|
58
49
|
export function isCurrentHTMLElement(obj: HTMLElement): boolean;
|
|
59
|
-
/**
|
|
60
|
-
* Checks if the given object is an instance of a `HTMLElement`
|
|
61
|
-
* and if so it's marked as "selected".
|
|
62
|
-
*/
|
|
63
|
-
export function isSelectedHTMLElement(obj: HTMLElement): boolean;
|
|
64
50
|
/**
|
|
65
51
|
* Checks if the given object is an instance of a `HTMLElement`
|
|
66
52
|
* and if so it's marked as "active".
|
|
@@ -72,15 +58,15 @@ export function isActiveHTMLElement(obj: HTMLElement): boolean;
|
|
|
72
58
|
*/
|
|
73
59
|
export function isHiddenHTMLElement(obj: HTMLElement): boolean;
|
|
74
60
|
/**
|
|
75
|
-
*
|
|
61
|
+
* Hides a given HTML-element.
|
|
76
62
|
* @since v0.0.23
|
|
77
63
|
*/
|
|
78
|
-
export function
|
|
64
|
+
export function hideHTMLElement(obj: HTMLElement): boolean;
|
|
79
65
|
/**
|
|
80
|
-
*
|
|
66
|
+
* Shows a given HTML-element.
|
|
81
67
|
* @since v0.0.23
|
|
82
68
|
*/
|
|
83
|
-
export function
|
|
69
|
+
export function showHTMLElement(obj: HTMLElement): boolean;
|
|
84
70
|
/**
|
|
85
71
|
* Makes selected a given HTML-element.
|
|
86
72
|
* @since v0.0.23
|
|
@@ -131,35 +117,15 @@ export function activateHTMLElements(...args: HTMLElement[]): void;
|
|
|
131
117
|
* @since v0.0.23
|
|
132
118
|
*/
|
|
133
119
|
export function inactivateHTMLElements(...args: HTMLElement[]): void;
|
|
134
|
-
/**
|
|
135
|
-
* Tries to convert a given value to a list of a valid "class attributes".
|
|
136
|
-
* @since v0.0.13
|
|
137
|
-
*/
|
|
138
|
-
export function valueToClassList(value: any, opt?: boolean): string[];
|
|
139
120
|
/**
|
|
140
121
|
* Creates a new HTML-element.
|
|
141
122
|
* @since 0.0.25
|
|
123
|
+
* @experimental
|
|
142
124
|
*/
|
|
143
|
-
export function createNewHTMLElement(tagName: string, opt?:
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Tries to convert a given value to a valid name of an HTML-tag.
|
|
151
|
-
* @since v0.0.21
|
|
152
|
-
*/
|
|
153
|
-
export function readAsTagName(value: any): string;
|
|
154
|
-
/**
|
|
155
|
-
* Tries to convert a given value to a valid name of an HTML-attribute.
|
|
156
|
-
* @since v0.0.21
|
|
157
|
-
*/
|
|
158
|
-
export function readAsAttrName(value: any): string;
|
|
159
|
-
/**
|
|
160
|
-
* Tries to convert a given value to a valid identifier suitable as a value
|
|
161
|
-
* for an "ID-attribute" of an HTML-element.
|
|
162
|
-
* @since v0.0.22
|
|
163
|
-
*/
|
|
164
|
-
export function valueToElementID(value: any): string;
|
|
125
|
+
export function createNewHTMLElement(tagName: string, opt?: IElementDesc): HTMLElement | null;
|
|
126
|
+
declare const CSS_CLASS_CURRENT: "current";
|
|
127
|
+
declare const CSS_CLASS_SELECTED: "selected";
|
|
128
|
+
declare const CSS_CLASS_ACTIVE: "active";
|
|
129
|
+
declare const CSS_CLASS_DISABLED: "disabled";
|
|
130
|
+
declare const CSS_CLASS_HIDDEN: "hidden";
|
|
165
131
|
export {};
|
package/lib/html-helper-lib.js
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
// [v0.1.
|
|
1
|
+
// [v0.1.050-20260517]
|
|
2
2
|
|
|
3
3
|
// === module init block ===
|
|
4
4
|
|
|
5
5
|
const {
|
|
6
|
-
isArray, isObject, isPlainObject,
|
|
7
|
-
valueToIDString,
|
|
6
|
+
isArray, isObject, isPlainObject,
|
|
8
7
|
} = require('@ygracs/bsfoc-lib-js');
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
const {
|
|
10
|
+
isHTMLElement,
|
|
11
|
+
valueToElementID, valueToClassList,
|
|
12
|
+
readAsTagName,
|
|
13
|
+
readAsAttrName, readAsAttrValue,
|
|
14
|
+
} = require('./mod-hfunc');
|
|
11
15
|
|
|
12
|
-
|
|
13
|
-
'button', 'submit', 'reset', 'image',
|
|
14
|
-
]);
|
|
16
|
+
// === module inner block ===
|
|
15
17
|
|
|
16
18
|
// === module main block ===
|
|
17
19
|
|
|
@@ -21,40 +23,6 @@ const CSS_CLASS_ACTIVE = 'active';
|
|
|
21
23
|
const CSS_CLASS_DISABLED = 'disabled';
|
|
22
24
|
const CSS_CLASS_HIDDEN = 'hidden';
|
|
23
25
|
|
|
24
|
-
/**
|
|
25
|
-
* Checks if the given object is an instance of an `HTMLElement`.
|
|
26
|
-
* @function isHTMLElement
|
|
27
|
-
* @param {any} obj - some element
|
|
28
|
-
* @return {boolean}
|
|
29
|
-
*/
|
|
30
|
-
function isHTMLElement(obj) {
|
|
31
|
-
return obj instanceof HTMLElement;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Checks if the given object is an HTML-button.
|
|
36
|
-
* @function isHTMLButton
|
|
37
|
-
* @param {any} obj - element to be verified
|
|
38
|
-
* @returns {boolean}
|
|
39
|
-
*/
|
|
40
|
-
function isHTMLButton(obj) {
|
|
41
|
-
let result = false;
|
|
42
|
-
if (isHTMLElement(obj)) {
|
|
43
|
-
switch (obj.tagName.toLowerCase()) {
|
|
44
|
-
case 'button': {
|
|
45
|
-
result = true;
|
|
46
|
-
break;
|
|
47
|
-
}
|
|
48
|
-
case 'input': {
|
|
49
|
-
result = etlHTagInputForBtn.has(obj.type.toLowerCase());
|
|
50
|
-
break;
|
|
51
|
-
}
|
|
52
|
-
default: {}
|
|
53
|
-
};
|
|
54
|
-
};
|
|
55
|
-
return result;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
26
|
/**
|
|
59
27
|
* Checks if the given object is an instance of a `HTMLElement`
|
|
60
28
|
* and if so it's marked as "selected".
|
|
@@ -65,6 +33,7 @@ function isHTMLButton(obj) {
|
|
|
65
33
|
function isSelectedHTMLElement(obj) {
|
|
66
34
|
return isHTMLElement(obj) && obj.classList.contains(CSS_CLASS_SELECTED);
|
|
67
35
|
};
|
|
36
|
+
module.exports.isSelectedHTMLElement = isSelectedHTMLElement;
|
|
68
37
|
|
|
69
38
|
/**
|
|
70
39
|
* Checks if the given object is an instance of a `HTMLElement`
|
|
@@ -76,6 +45,7 @@ function isSelectedHTMLElement(obj) {
|
|
|
76
45
|
function isCurrentHTMLElement(obj) {
|
|
77
46
|
return isHTMLElement(obj) && obj.classList.contains(CSS_CLASS_CURRENT);
|
|
78
47
|
};
|
|
48
|
+
module.exports.isCurrentHTMLElement = isCurrentHTMLElement;
|
|
79
49
|
|
|
80
50
|
/**
|
|
81
51
|
* Checks if the given object is an instance of a `HTMLElement`
|
|
@@ -87,6 +57,7 @@ function isCurrentHTMLElement(obj) {
|
|
|
87
57
|
function isActiveHTMLElement(obj) {
|
|
88
58
|
return isHTMLElement(obj) && obj.classList.contains(CSS_CLASS_ACTIVE);
|
|
89
59
|
};
|
|
60
|
+
module.exports.isActiveHTMLElement = isActiveHTMLElement;
|
|
90
61
|
|
|
91
62
|
/**
|
|
92
63
|
* Checks if the given object is an instance of a `HTMLElement`
|
|
@@ -98,6 +69,7 @@ function isActiveHTMLElement(obj) {
|
|
|
98
69
|
function isHiddenHTMLElement(obj) {
|
|
99
70
|
return isHTMLElement(obj) && obj.classList.contains(CSS_CLASS_HIDDEN);
|
|
100
71
|
};
|
|
72
|
+
module.exports.isHiddenHTMLElement = isHiddenHTMLElement;
|
|
101
73
|
|
|
102
74
|
/**
|
|
103
75
|
* Hides a given HTML-element.
|
|
@@ -111,6 +83,7 @@ function hideHTMLElement(obj) {
|
|
|
111
83
|
if (isSUCCEED) obj.classList.add(CSS_CLASS_HIDDEN);
|
|
112
84
|
return isSUCCEED;
|
|
113
85
|
};
|
|
86
|
+
module.exports.hideHTMLElement = hideHTMLElement;
|
|
114
87
|
|
|
115
88
|
/**
|
|
116
89
|
* Shows a given HTML-element.
|
|
@@ -124,6 +97,7 @@ function showHTMLElement(obj) {
|
|
|
124
97
|
if (isSUCCEED) obj.classList.remove(CSS_CLASS_HIDDEN);
|
|
125
98
|
return isSUCCEED;
|
|
126
99
|
};
|
|
100
|
+
module.exports.showHTMLElement = showHTMLElement;
|
|
127
101
|
|
|
128
102
|
/**
|
|
129
103
|
* Makes selected a given HTML-element.
|
|
@@ -137,6 +111,7 @@ function selectHTMLElement(obj) {
|
|
|
137
111
|
if (isSUCCEED) obj.classList.add(CSS_CLASS_SELECTED);
|
|
138
112
|
return isSUCCEED;
|
|
139
113
|
};
|
|
114
|
+
module.exports.selectHTMLElement = selectHTMLElement;
|
|
140
115
|
|
|
141
116
|
/**
|
|
142
117
|
* Makes unselected a given HTML-element.
|
|
@@ -150,6 +125,7 @@ function unselectHTMLElement(obj) {
|
|
|
150
125
|
if (isSUCCEED) obj.classList.remove(CSS_CLASS_SELECTED);
|
|
151
126
|
return isSUCCEED;
|
|
152
127
|
};
|
|
128
|
+
module.exports.unselectHTMLElement = unselectHTMLElement;
|
|
153
129
|
|
|
154
130
|
/**
|
|
155
131
|
* Marks a given HTML-element as "current".
|
|
@@ -163,6 +139,7 @@ function markHTMLElementAsCurrent(obj) {
|
|
|
163
139
|
if (isSUCCEED) obj.classList.add(CSS_CLASS_CURRENT);
|
|
164
140
|
return isSUCCEED;
|
|
165
141
|
};
|
|
142
|
+
module.exports.markHTMLElementAsCurrent = markHTMLElementAsCurrent;
|
|
166
143
|
|
|
167
144
|
/**
|
|
168
145
|
* Unmarks a given HTML-element previous marked as "current".
|
|
@@ -176,6 +153,7 @@ function unmarkCurrentHTMLElement(obj) {
|
|
|
176
153
|
if (isSUCCEED) obj.classList.remove(CSS_CLASS_CURRENT);
|
|
177
154
|
return isSUCCEED;
|
|
178
155
|
};
|
|
156
|
+
module.exports.unmarkCurrentHTMLElement = unmarkCurrentHTMLElement;
|
|
179
157
|
|
|
180
158
|
/**
|
|
181
159
|
* Marks a given HTML-element as "active".
|
|
@@ -189,6 +167,7 @@ function markHTMLElementAsActive(obj) {
|
|
|
189
167
|
if (isSUCCEED) obj.classList.add(CSS_CLASS_ACTIVE);
|
|
190
168
|
return isSUCCEED;
|
|
191
169
|
};
|
|
170
|
+
module.exports.markHTMLElementAsActive = markHTMLElementAsActive;
|
|
192
171
|
|
|
193
172
|
/**
|
|
194
173
|
* Unmarks a given HTML-element previous marked as "active".
|
|
@@ -202,6 +181,7 @@ function unmarkActiveHTMLElement(obj) {
|
|
|
202
181
|
if (isSUCCEED) obj.classList.remove(CSS_CLASS_ACTIVE);
|
|
203
182
|
return isSUCCEED;
|
|
204
183
|
};
|
|
184
|
+
module.exports.unmarkActiveHTMLElement = unmarkActiveHTMLElement;
|
|
205
185
|
|
|
206
186
|
/**
|
|
207
187
|
* Disables a given HTML-element.
|
|
@@ -226,6 +206,7 @@ function lockHTMLElement(obj) {
|
|
|
226
206
|
};
|
|
227
207
|
return isSUCCEED;
|
|
228
208
|
};
|
|
209
|
+
module.exports.lockHTMLElement = lockHTMLElement;
|
|
229
210
|
|
|
230
211
|
/**
|
|
231
212
|
* Enables a given HTML-element.
|
|
@@ -250,6 +231,7 @@ function unlockHTMLElement(obj) {
|
|
|
250
231
|
};
|
|
251
232
|
return isSUCCEED;
|
|
252
233
|
};
|
|
234
|
+
module.exports.unlockHTMLElement = unlockHTMLElement;
|
|
253
235
|
|
|
254
236
|
/**
|
|
255
237
|
* Enables an HTML-elements given by a list of a function params.
|
|
@@ -263,6 +245,7 @@ function activateHTMLElements(...args) {
|
|
|
263
245
|
unlockHTMLElement(item);
|
|
264
246
|
};
|
|
265
247
|
};
|
|
248
|
+
module.exports.activateHTMLElements = activateHTMLElements;
|
|
266
249
|
|
|
267
250
|
/**
|
|
268
251
|
* Disables an HTML-elements given by a list of a function params.
|
|
@@ -276,147 +259,31 @@ function inactivateHTMLElements(...args) {
|
|
|
276
259
|
lockHTMLElement(item);
|
|
277
260
|
};
|
|
278
261
|
};
|
|
279
|
-
|
|
280
|
-
/**
|
|
281
|
-
* Tries to convert a given value to a valid name of an HTML-tag.
|
|
282
|
-
* @since v0.0.21
|
|
283
|
-
* @function readAsTagName
|
|
284
|
-
* @param {any} value - some value
|
|
285
|
-
* @return {string}
|
|
286
|
-
*/
|
|
287
|
-
function readAsTagName(value) {
|
|
288
|
-
let tagName = valueToIDString(value, { ignoreNumbers: true });
|
|
289
|
-
if (tagName === null) return '';
|
|
290
|
-
if (tagName !== '') {
|
|
291
|
-
// // TODO: do extra checks
|
|
292
|
-
const template = /[^\w]/;
|
|
293
|
-
const trigger = tagName.match(template);
|
|
294
|
-
if (trigger) {
|
|
295
|
-
//console.log(trigger);
|
|
296
|
-
tagName = '';
|
|
297
|
-
};
|
|
298
|
-
if (tagName !== '') tagName = tagName.toLowerCase();
|
|
299
|
-
};
|
|
300
|
-
return tagName;
|
|
301
|
-
};
|
|
302
|
-
|
|
303
|
-
/**
|
|
304
|
-
* Tries to convert a given value to a valid name of an HTML-attribute.
|
|
305
|
-
* @since v0.0.21
|
|
306
|
-
* @function readAsAttrName
|
|
307
|
-
* @param {any} value - some value
|
|
308
|
-
* @return {string}
|
|
309
|
-
*/
|
|
310
|
-
function readAsAttrName(value) {
|
|
311
|
-
let attrName = valueToIDString(value, { ignoreNumbers: true });
|
|
312
|
-
if (attrName === null) return '';
|
|
313
|
-
if (attrName !== '') {
|
|
314
|
-
// // TODO: do extra checks
|
|
315
|
-
const template = /[\s\/\\\"\'<>=]/;
|
|
316
|
-
const trigger = attrName.match(template);
|
|
317
|
-
if (trigger) {
|
|
318
|
-
//console.log(trigger);
|
|
319
|
-
attrName = '';
|
|
320
|
-
};
|
|
321
|
-
};
|
|
322
|
-
return attrName;
|
|
323
|
-
};
|
|
324
|
-
|
|
325
|
-
/**
|
|
326
|
-
* Tries to convert a given value to a valid "attribute value".
|
|
327
|
-
* @since v0.0.18
|
|
328
|
-
* @function readAsAttrValue
|
|
329
|
-
* @param {any} value - some value
|
|
330
|
-
* @return {?string}
|
|
331
|
-
*/
|
|
332
|
-
function readAsAttrValue(value) {
|
|
333
|
-
let result = null;
|
|
334
|
-
switch (typeof value) {
|
|
335
|
-
case 'boolean': {
|
|
336
|
-
result = value.toString();
|
|
337
|
-
break;
|
|
338
|
-
}
|
|
339
|
-
case 'number': {
|
|
340
|
-
if (Number.isNaN(value)) break;
|
|
341
|
-
result = value.toString();
|
|
342
|
-
break;
|
|
343
|
-
}
|
|
344
|
-
case 'string': {
|
|
345
|
-
result = value.trim();
|
|
346
|
-
break;
|
|
347
|
-
}
|
|
348
|
-
default: {}
|
|
349
|
-
};
|
|
350
|
-
return result;
|
|
351
|
-
};
|
|
352
|
-
|
|
353
|
-
/**
|
|
354
|
-
* Tries to convert a given value to a list of a valid "class attributes".
|
|
355
|
-
* @since v0.0.13
|
|
356
|
-
* @function valueToClassList
|
|
357
|
-
* @param {any} value - some value
|
|
358
|
-
* @param {boolean} [opt] - flag that indicates whether a dups allowed
|
|
359
|
-
* @return {string[]}
|
|
360
|
-
*/
|
|
361
|
-
function valueToClassList(value, opt) {
|
|
362
|
-
/** @type {string[]} */
|
|
363
|
-
let result = [];
|
|
364
|
-
valueToArray(value).forEach((elem) => {
|
|
365
|
-
let value = '';
|
|
366
|
-
if (
|
|
367
|
-
typeof elem === 'string'
|
|
368
|
-
&& ((value = elem.trim()) !== '')
|
|
369
|
-
) {
|
|
370
|
-
const a = value.split(/\s+/);
|
|
371
|
-
if (a.length > 0) result.push(...a);
|
|
372
|
-
};
|
|
373
|
-
});
|
|
374
|
-
if (result.length > 0 && typeof opt === 'boolean' && opt) {
|
|
375
|
-
result = [ ...(new Set(result)) ];
|
|
376
|
-
};
|
|
377
|
-
return result;
|
|
378
|
-
};
|
|
379
|
-
|
|
380
|
-
/**
|
|
381
|
-
* Tries to convert a given value to a valid identifier suitable as a value
|
|
382
|
-
* for an "ID-attribute" of an HTML-element.
|
|
383
|
-
* @since v0.0.22
|
|
384
|
-
* @function valueToElementID
|
|
385
|
-
* @param {any} value - some value
|
|
386
|
-
* @return {string}
|
|
387
|
-
*/
|
|
388
|
-
function valueToElementID(value) {
|
|
389
|
-
let id = valueToIDString(value);
|
|
390
|
-
if (id === null) return '';
|
|
391
|
-
if (id !== '') {
|
|
392
|
-
// // TODO: do extra checks
|
|
393
|
-
const template = /[\s\/\\\"\'<>=]/;
|
|
394
|
-
const trigger = id.match(template);
|
|
395
|
-
if (trigger) {
|
|
396
|
-
//console.log(trigger);
|
|
397
|
-
id = '';
|
|
398
|
-
};
|
|
399
|
-
};
|
|
400
|
-
return id;
|
|
401
|
-
};
|
|
262
|
+
module.exports.inactivateHTMLElements = inactivateHTMLElements;
|
|
402
263
|
|
|
403
264
|
/**
|
|
404
265
|
* An element descriptor.
|
|
405
|
-
* @typedef {Object}
|
|
266
|
+
* @typedef {Object} IElementDesc
|
|
406
267
|
* @property {string} [id] - element ID
|
|
407
268
|
* @property {string} [text] - some text
|
|
408
269
|
* @property {Object<string, any>} [attr] - an attributes list
|
|
409
270
|
* @property {string|string[]} [classNames] - a class names list (added since v0.0.19)
|
|
410
271
|
* @property {Object<string, any>} [data] - a 'dataset'-attributes list
|
|
411
272
|
*/
|
|
273
|
+
/**
|
|
274
|
+
* A virtual constant meant for support jsdoc notation:
|
|
275
|
+
* @type {IElementDesc}
|
|
276
|
+
*/
|
|
277
|
+
module.exports.IElementDesc = {};
|
|
412
278
|
|
|
413
279
|
/**
|
|
414
280
|
* Creates a new HTML-element.
|
|
415
281
|
* @since 0.0.25
|
|
416
282
|
* @function createNewHTMLElement
|
|
417
283
|
* @param {string} tagName - an element tag name
|
|
418
|
-
* @param {
|
|
284
|
+
* @param {IElementDesc} [opt] - an element descriptor object
|
|
419
285
|
* @return {?HTMLElement}
|
|
286
|
+
* @experimental
|
|
420
287
|
*/
|
|
421
288
|
function createNewHTMLElement(tagName, opt) {
|
|
422
289
|
let item = null;
|
|
@@ -489,6 +356,7 @@ function createNewHTMLElement(tagName, opt) {
|
|
|
489
356
|
};
|
|
490
357
|
return item;
|
|
491
358
|
};
|
|
359
|
+
module.exports.createNewHTMLElement = createNewHTMLElement;
|
|
492
360
|
|
|
493
361
|
// === module exports block ===
|
|
494
362
|
|
|
@@ -499,32 +367,3 @@ module.exports.CSS_CLASS_STRING = /*function(){ return*/ {
|
|
|
499
367
|
CSS_CLASS_DISABLED: CSS_CLASS_DISABLED,
|
|
500
368
|
CSS_CLASS_HIDDEN: CSS_CLASS_HIDDEN
|
|
501
369
|
}/*}*/;
|
|
502
|
-
|
|
503
|
-
module.exports.isHTMLElement = isHTMLElement;
|
|
504
|
-
module.exports.isHTMLButton = isHTMLButton;
|
|
505
|
-
module.exports.isCurrentHTMLElement = isCurrentHTMLElement;
|
|
506
|
-
module.exports.isSelectedHTMLElement = isSelectedHTMLElement;
|
|
507
|
-
module.exports.isActiveHTMLElement = isActiveHTMLElement;
|
|
508
|
-
module.exports.isHiddenHTMLElement = isHiddenHTMLElement;
|
|
509
|
-
|
|
510
|
-
module.exports.showHTMLElement = showHTMLElement;
|
|
511
|
-
module.exports.hideHTMLElement = hideHTMLElement;
|
|
512
|
-
module.exports.selectHTMLElement = selectHTMLElement;
|
|
513
|
-
module.exports.unselectHTMLElement = unselectHTMLElement;
|
|
514
|
-
module.exports.markHTMLElementAsCurrent = markHTMLElementAsCurrent;
|
|
515
|
-
module.exports.unmarkCurrentHTMLElement = unmarkCurrentHTMLElement;
|
|
516
|
-
module.exports.markHTMLElementAsActive = markHTMLElementAsActive;
|
|
517
|
-
module.exports.unmarkActiveHTMLElement = unmarkActiveHTMLElement;
|
|
518
|
-
module.exports.lockHTMLElement = lockHTMLElement;
|
|
519
|
-
module.exports.unlockHTMLElement = unlockHTMLElement;
|
|
520
|
-
module.exports.activateHTMLElements = activateHTMLElements;
|
|
521
|
-
module.exports.inactivateHTMLElements = inactivateHTMLElements;
|
|
522
|
-
|
|
523
|
-
module.exports.valueToClassList = valueToClassList;
|
|
524
|
-
|
|
525
|
-
// * experimental *
|
|
526
|
-
module.exports.createNewHTMLElement = createNewHTMLElement;
|
|
527
|
-
module.exports.readAsAttrValue = readAsAttrValue;
|
|
528
|
-
module.exports.readAsTagName = readAsTagName;
|
|
529
|
-
module.exports.readAsAttrName = readAsAttrName;
|
|
530
|
-
module.exports.valueToElementID = valueToElementID;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the given object is an instance of an `HTMLElement`.
|
|
3
|
+
*/
|
|
4
|
+
export function isHTMLElement(obj: any): obj is HTMLElement;
|
|
5
|
+
/**
|
|
6
|
+
* Checks if the given object is an HTML-button.
|
|
7
|
+
*/
|
|
8
|
+
export function isHTMLButton(obj: any): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Tries to convert a given value to a valid name of an HTML-tag.
|
|
11
|
+
* @since v0.0.21
|
|
12
|
+
* @experimental
|
|
13
|
+
*/
|
|
14
|
+
export function readAsTagName(value: any): string;
|
|
15
|
+
/**
|
|
16
|
+
* Tries to convert a given value to a valid name of an HTML-attribute.
|
|
17
|
+
* @since v0.0.21
|
|
18
|
+
* @experimental
|
|
19
|
+
*/
|
|
20
|
+
export function readAsAttrName(value: any): string;
|
|
21
|
+
/**
|
|
22
|
+
* Tries to convert a given value to a valid "attribute value".
|
|
23
|
+
* @since v0.0.18
|
|
24
|
+
* @experimental
|
|
25
|
+
*/
|
|
26
|
+
export function readAsAttrValue(value: any): string | null;
|
|
27
|
+
/**
|
|
28
|
+
* Tries to convert a given value to a valid identifier suitable as a value
|
|
29
|
+
* for an "ID-attribute" of an HTML-element.
|
|
30
|
+
* @since v0.0.22
|
|
31
|
+
* @experimental
|
|
32
|
+
*/
|
|
33
|
+
export function valueToElementID(value: any): string;
|
|
34
|
+
/**
|
|
35
|
+
* Tries to convert a given value to a list of a valid "class attributes".
|
|
36
|
+
* @since v0.0.13
|
|
37
|
+
*/
|
|
38
|
+
export function valueToClassList(value: any, opt?: boolean): string[];
|
package/lib/mod-hfunc.js
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// [v0.1.048-20260517]
|
|
2
|
+
|
|
3
|
+
// === module init block ===
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
valueToArray,
|
|
7
|
+
valueToIDString,
|
|
8
|
+
} = require('@ygracs/bsfoc-lib-js');
|
|
9
|
+
|
|
10
|
+
// === module inner block ===
|
|
11
|
+
|
|
12
|
+
const etlHTagInputForBtn = new Set([
|
|
13
|
+
'button', 'submit', 'reset', 'image',
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
// === module main block ===
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Checks if the given object is an instance of an `HTMLElement`.
|
|
20
|
+
* @function isHTMLElement
|
|
21
|
+
* @param {any} obj - some element
|
|
22
|
+
* @return {boolean}
|
|
23
|
+
*/
|
|
24
|
+
function isHTMLElement(obj) {
|
|
25
|
+
return obj instanceof HTMLElement;
|
|
26
|
+
};
|
|
27
|
+
module.exports.isHTMLElement = isHTMLElement;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Checks if the given object is an HTML-button.
|
|
31
|
+
* @function isHTMLButton
|
|
32
|
+
* @param {any} obj - element to be verified
|
|
33
|
+
* @returns {boolean}
|
|
34
|
+
*/
|
|
35
|
+
function isHTMLButton(obj) {
|
|
36
|
+
let result = false;
|
|
37
|
+
if (isHTMLElement(obj)) {
|
|
38
|
+
switch (obj.tagName.toLowerCase()) {
|
|
39
|
+
case 'button': {
|
|
40
|
+
result = true;
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
case 'input': {
|
|
44
|
+
result = etlHTagInputForBtn.has(obj.type.toLowerCase());
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
default: {}
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
module.exports.isHTMLButton = isHTMLButton;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Tries to convert a given value to a valid name of an HTML-tag.
|
|
56
|
+
* @since v0.0.21
|
|
57
|
+
* @function readAsTagName
|
|
58
|
+
* @param {any} value - some value
|
|
59
|
+
* @return {string}
|
|
60
|
+
* @experimental
|
|
61
|
+
*/
|
|
62
|
+
function readAsTagName(value) {
|
|
63
|
+
let tagName = valueToIDString(value, { ignoreNumbers: true });
|
|
64
|
+
if (tagName === null) return '';
|
|
65
|
+
if (tagName !== '') {
|
|
66
|
+
// // TODO: do extra checks
|
|
67
|
+
const template = /[^\w]/;
|
|
68
|
+
const trigger = tagName.match(template);
|
|
69
|
+
if (trigger) {
|
|
70
|
+
//console.log(trigger);
|
|
71
|
+
tagName = '';
|
|
72
|
+
};
|
|
73
|
+
if (tagName !== '') tagName = tagName.toLowerCase();
|
|
74
|
+
};
|
|
75
|
+
return tagName;
|
|
76
|
+
};
|
|
77
|
+
module.exports.readAsTagName = readAsTagName;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Tries to convert a given value to a valid name of an HTML-attribute.
|
|
81
|
+
* @since v0.0.21
|
|
82
|
+
* @function readAsAttrName
|
|
83
|
+
* @param {any} value - some value
|
|
84
|
+
* @return {string}
|
|
85
|
+
* @experimental
|
|
86
|
+
*/
|
|
87
|
+
function readAsAttrName(value) {
|
|
88
|
+
let attrName = valueToIDString(value, { ignoreNumbers: true });
|
|
89
|
+
if (attrName === null) return '';
|
|
90
|
+
if (attrName !== '') {
|
|
91
|
+
// // TODO: do extra checks
|
|
92
|
+
const template = /[\s\/\\\"\'<>=]/;
|
|
93
|
+
const trigger = attrName.match(template);
|
|
94
|
+
if (trigger) {
|
|
95
|
+
//console.log(trigger);
|
|
96
|
+
attrName = '';
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
return attrName;
|
|
100
|
+
};
|
|
101
|
+
module.exports.readAsAttrName = readAsAttrName;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Tries to convert a given value to a valid "attribute value".
|
|
105
|
+
* @since v0.0.18
|
|
106
|
+
* @function readAsAttrValue
|
|
107
|
+
* @param {any} value - some value
|
|
108
|
+
* @return {?string}
|
|
109
|
+
* @experimental
|
|
110
|
+
*/
|
|
111
|
+
function readAsAttrValue(value) {
|
|
112
|
+
let result = null;
|
|
113
|
+
switch (typeof value) {
|
|
114
|
+
case 'boolean': {
|
|
115
|
+
result = value.toString();
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
case 'number': {
|
|
119
|
+
if (Number.isNaN(value)) break;
|
|
120
|
+
result = value.toString();
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
case 'string': {
|
|
124
|
+
result = value.trim();
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
default: {}
|
|
128
|
+
};
|
|
129
|
+
return result;
|
|
130
|
+
};
|
|
131
|
+
module.exports.readAsAttrValue = readAsAttrValue;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Tries to convert a given value to a valid identifier suitable as a value
|
|
135
|
+
* for an "ID-attribute" of an HTML-element.
|
|
136
|
+
* @since v0.0.22
|
|
137
|
+
* @function valueToElementID
|
|
138
|
+
* @param {any} value - some value
|
|
139
|
+
* @return {string}
|
|
140
|
+
* @experimental
|
|
141
|
+
*/
|
|
142
|
+
function valueToElementID(value) {
|
|
143
|
+
let id = valueToIDString(value);
|
|
144
|
+
if (id === null) return '';
|
|
145
|
+
if (id !== '') {
|
|
146
|
+
// // TODO: do extra checks
|
|
147
|
+
const template = /[\s\/\\\"\'<>=]/;
|
|
148
|
+
const trigger = id.match(template);
|
|
149
|
+
if (trigger) {
|
|
150
|
+
//console.log(trigger);
|
|
151
|
+
id = '';
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
return id;
|
|
155
|
+
};
|
|
156
|
+
module.exports.valueToElementID = valueToElementID;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Tries to convert a given value to a list of a valid "class attributes".
|
|
160
|
+
* @since v0.0.13
|
|
161
|
+
* @function valueToClassList
|
|
162
|
+
* @param {any} value - some value
|
|
163
|
+
* @param {boolean} [opt] - flag that indicates whether a dups allowed
|
|
164
|
+
* @return {string[]}
|
|
165
|
+
*/
|
|
166
|
+
function valueToClassList(value, opt) {
|
|
167
|
+
/** @type {string[]} */
|
|
168
|
+
let result = [];
|
|
169
|
+
valueToArray(value).forEach((elem) => {
|
|
170
|
+
let value = '';
|
|
171
|
+
if (
|
|
172
|
+
typeof elem === 'string'
|
|
173
|
+
&& ((value = elem.trim()) !== '')
|
|
174
|
+
) {
|
|
175
|
+
const a = value.split(/\s+/);
|
|
176
|
+
if (a.length > 0) result.push(...a);
|
|
177
|
+
};
|
|
178
|
+
});
|
|
179
|
+
if (result.length > 0 && typeof opt === 'boolean' && opt) {
|
|
180
|
+
result = [ ...(new Set(result)) ];
|
|
181
|
+
};
|
|
182
|
+
return result;
|
|
183
|
+
};
|
|
184
|
+
module.exports.valueToClassList = valueToClassList;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cntwg/html-helper",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "A base HTML-helper library for js",
|
|
5
5
|
"author": "ygracs <cs70th-om@rambler.ru>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"doc/*.md",
|
|
19
19
|
"lib/html-helper-lib.js",
|
|
20
20
|
"lib/event-hfunc.js",
|
|
21
|
+
"lib/mod-hfunc.js",
|
|
21
22
|
"lib/*.d.ts",
|
|
22
23
|
"index.js",
|
|
23
24
|
"index.d.ts",
|
|
@@ -41,8 +42,8 @@
|
|
|
41
42
|
},
|
|
42
43
|
"devDependencies": {
|
|
43
44
|
"@ygracs/test-helper": "~0.0.2-b",
|
|
44
|
-
"jest": "^30.
|
|
45
|
-
"jest-environment-jsdom": "^30.
|
|
45
|
+
"jest": "^30.4.2",
|
|
46
|
+
"jest-environment-jsdom": "^30.4.1",
|
|
46
47
|
"jsdoc-to-markdown": "^9.1.3",
|
|
47
48
|
"minimist": "^1.2.8",
|
|
48
49
|
"typescript": "~5.9.3"
|