@carlsebastian/jsu 1.0.34
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/LICENSE +201 -0
- package/package.json +58 -0
- package/src/global.js +7 -0
- package/src/types/api/api.d.ts +147 -0
- package/src/types/api/element.d.ts +55 -0
- package/src/types/custom/custom.d.ts +8 -0
- package/src/types/custom/error/builder/error.builder.d.ts +78 -0
- package/src/types/custom/error/constructor/error.base.d.ts +4 -0
- package/src/types/custom/error/constructor/error.custom.d.ts +34 -0
- package/src/types/custom/error/constructor/error.meta.d.ts +18 -0
- package/src/types/custom/error/error.d.ts +5 -0
- package/src/types/custom/utils/custom.utils.d.ts +42 -0
- package/src/types/custom/utils/generator/generator.d.ts +92 -0
- package/src/types/dom/attr/attr.class.d.ts +81 -0
- package/src/types/dom/attr/attr.id.d.ts +23 -0
- package/src/types/dom/attr/attr.style.d.ts +32 -0
- package/src/types/dom/dom.d.ts +8 -0
- package/src/types/dom/element/create/element.create.d.ts +67 -0
- package/src/types/dom/element/getElementBy/dom.getElementBy.d.ts +71 -0
- package/src/types/dom/element/tag-verifier/verifier.d.ts +16 -0
- package/src/types/global.d.ts +13 -0
- package/src/types/guards/data-types/data-types.d.ts +5 -0
- package/src/types/guards/formats/formats.d.ts +5 -0
- package/src/types/guards/guards.d.ts +8 -0
- package/src/types/primitives/obj/obj.accessor.d.ts +5 -0
- package/src/types/primitives/obj/obj.iterator.d.ts +5 -0
- package/src/types/primitives/primitives.d.ts +8 -0
- package/src/types/primitives/str/str.d.ts +26 -0
- package/src/types/storage/local/storage.local.d.ts +86 -0
- package/src/types/storage/session/storage.session.d.ts +86 -0
- package/src/types/storage/storage.d.ts +8 -0
- package/src/utils/custom/custom.js +20 -0
- package/src/utils/custom/error/builder/error.builder.js +181 -0
- package/src/utils/custom/error/constructor/error.base.js +71 -0
- package/src/utils/custom/error/constructor/error.custom.js +107 -0
- package/src/utils/custom/error/error.js +23 -0
- package/src/utils/custom/utils/custom.utils.js +150 -0
- package/src/utils/custom/utils/generator/generator.js +222 -0
- package/src/utils/dom/attr/attr.class.js +186 -0
- package/src/utils/dom/attr/attr.id.js +64 -0
- package/src/utils/dom/attr/attr.style.js +128 -0
- package/src/utils/dom/dom.js +29 -0
- package/src/utils/dom/element/create/element.create.js +312 -0
- package/src/utils/dom/element/getElementBy/dom.getElementBy.js +171 -0
- package/src/utils/dom/element/query/dom.query.js +75 -0
- package/src/utils/dom/element/tag-verifier/verifier.js +60 -0
- package/src/utils/dom/lifecycle/mount.js +48 -0
- package/src/utils/dom/lifecycle/unmount.js +43 -0
- package/src/utils/guards/data-types/data-types.js +201 -0
- package/src/utils/guards/formats/formats.js +274 -0
- package/src/utils/guards/guards.js +21 -0
- package/src/utils/primitives/obj/obj.accessor.js +242 -0
- package/src/utils/primitives/obj/obj.iterator.js +148 -0
- package/src/utils/primitives/primitives.js +23 -0
- package/src/utils/primitives/str/str.js +52 -0
- package/src/utils/storage/local/storage.local.js +236 -0
- package/src/utils/storage/session/storage.session.js +236 -0
- package/src/utils/storage/storage.js +59 -0
- package/src/utils/variables.js +78 -0
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import Emit from '../../../custom/error/builder/error.builder.js';
|
|
2
|
+
import { IsArrEmpty, IsPlainObjEmpty, IsStrEmpty } from '../../../guards/formats/formats.js';
|
|
3
|
+
import { IsArr, IsElement, IsNum, IsPlainObj, IsStr } from '../../../guards/data-types/data-types.js';
|
|
4
|
+
import { CountOf } from '../../../primitives/obj/obj.accessor.js';
|
|
5
|
+
import ClassOf from '../../attr/attr.class.js';
|
|
6
|
+
import IdOf from '../../attr/attr.id.js';
|
|
7
|
+
import StyleOf from '../../attr/attr.style.js';
|
|
8
|
+
import VerifyTag from '../tag-verifier/verifier.js';
|
|
9
|
+
|
|
10
|
+
/* -- Helpers -- */
|
|
11
|
+
/**
|
|
12
|
+
* Handles the parameter validation of tag and configuration for element creation.
|
|
13
|
+
*
|
|
14
|
+
* @param { string } caller - The caller id of this validation.
|
|
15
|
+
* @param { string } tag - The tag to validate.
|
|
16
|
+
* @param { Record<string, unknown> } conf - The configuration to validate.
|
|
17
|
+
* @returns { void } Does not return any as it only handles the validation process.
|
|
18
|
+
*/
|
|
19
|
+
function ValidateTagAndConfiguration(caller, tag, conf) {
|
|
20
|
+
const Caller = IsStr(caller) ? caller : "(Anonymous)";
|
|
21
|
+
|
|
22
|
+
if (!IsStr(tag))
|
|
23
|
+
Emit._ArgumentError(Caller, "tag", tag, "String");
|
|
24
|
+
|
|
25
|
+
if (!IsPlainObj(conf))
|
|
26
|
+
Emit._ArgumentError(Caller, "conf", conf, "Plain Object ({})");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Formats the configuration of element's properties.
|
|
31
|
+
*
|
|
32
|
+
* @template T
|
|
33
|
+
* @param { T } conf - the configuration to format.
|
|
34
|
+
* @param { Array<string> } definedKeys - The default or expected property id of the configuration.
|
|
35
|
+
* @returns { T extends Record<string, infer V> ? Record<string, V> : {} }
|
|
36
|
+
*/
|
|
37
|
+
function FormatConfig(conf, definedKeys) {
|
|
38
|
+
if (!IsPlainObj(conf))
|
|
39
|
+
return {};
|
|
40
|
+
|
|
41
|
+
if (!IsArr(definedKeys) || IsArrEmpty(definedKeys))
|
|
42
|
+
return conf;
|
|
43
|
+
|
|
44
|
+
if (IsPlainObjEmpty(conf))
|
|
45
|
+
return conf;
|
|
46
|
+
|
|
47
|
+
const DKLower = definedKeys.map(K => String(K).trim().toLowerCase()), ConfKeys = Object.keys(conf);
|
|
48
|
+
|
|
49
|
+
return ConfKeys.reduce((Acc, Key) => {
|
|
50
|
+
const Pos = DKLower.indexOf(Key.trim().toLowerCase());
|
|
51
|
+
if (Pos >= 0)
|
|
52
|
+
Acc[definedKeys[Pos]] = conf[Key];
|
|
53
|
+
else {
|
|
54
|
+
if (!Object.hasOwn(Acc, "Others"))
|
|
55
|
+
Acc["Others"] = {};
|
|
56
|
+
|
|
57
|
+
Acc["Others"][Key] = conf[Key];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return Acc;
|
|
61
|
+
}, {});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Contains methods for `DOM` creation objects.
|
|
66
|
+
*/
|
|
67
|
+
const Create = {
|
|
68
|
+
/**
|
|
69
|
+
* The name of this object.
|
|
70
|
+
*/
|
|
71
|
+
Name: "Create",
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Generates an instance of `HTMLElement` with the given qualified `tag`.
|
|
75
|
+
*
|
|
76
|
+
* @template { HTMLElementTags["Key"] } T
|
|
77
|
+
* @param { T } tag - The tag of element to generate.
|
|
78
|
+
* @param { HTMLElementConfig } [conf] - A configuration properties of element.
|
|
79
|
+
* @returns { T extends HTMLElementTags["Key"] ? HTMLElementTags["Map"][T] : null }
|
|
80
|
+
*
|
|
81
|
+
* @throws {ArgumentError} - When parameters are invalid.
|
|
82
|
+
* @throws {NoSuchElementTagError} - When the given element tag is not qualified.
|
|
83
|
+
*/
|
|
84
|
+
HTMLElement(tag, conf = {}) {
|
|
85
|
+
const Method = `${this.Name}.HTMLElement`;
|
|
86
|
+
let [Tag, Conf] = [tag, conf];
|
|
87
|
+
|
|
88
|
+
ValidateTagAndConfiguration(Method, Tag, Conf);
|
|
89
|
+
|
|
90
|
+
const ThisElement = VerifyTag(Tag).HTMLElement();
|
|
91
|
+
if (!IsElement(ThisElement)) {
|
|
92
|
+
console.warn(`${Method}(): There's no such element tag of '${Tag}'! (Exited with null)`);
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const ConfigKeys = ["ClassNames", "Id", "Text", "Styles"];
|
|
97
|
+
Conf = FormatConfig(Conf, ConfigKeys);
|
|
98
|
+
if (CountOf(Conf) > 0) {
|
|
99
|
+
if (Conf.ClassNames)
|
|
100
|
+
ClassOf(ThisElement).Set(Conf.ClassNames);
|
|
101
|
+
|
|
102
|
+
if (Conf.Id)
|
|
103
|
+
IdOf(ThisElement).Set(Conf.Id);
|
|
104
|
+
|
|
105
|
+
if (Conf.Text) {
|
|
106
|
+
if (!IsStr(Conf.Text))
|
|
107
|
+
Conf.Text = String(Conf.Text);
|
|
108
|
+
|
|
109
|
+
ThisElement.textContent = Conf.Text;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (Conf.Styles)
|
|
113
|
+
StyleOf(ThisElement).Set(Conf.Styles);
|
|
114
|
+
|
|
115
|
+
if (Conf?.Others && IsPlainObj(Conf.Others) && CountOf(Conf.Others) > 0) {
|
|
116
|
+
for (const [OK, OV] of Object.entries(Conf.Others))
|
|
117
|
+
ThisElement.setAttribute(OK, OV);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return ThisElement;
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Generates an instance of `SVGElement` with the given qualified tag.
|
|
126
|
+
*
|
|
127
|
+
* @template { SVGElementTags["Key"] } T
|
|
128
|
+
* @param { T } tag - The tag of svg element to generate.
|
|
129
|
+
* @param { SVGElementConfig } [conf] - A configuration properties of svg element.
|
|
130
|
+
* @returns { T extends SVGElementTags["Key"] ? SVGElementTags["Map"][T] : null }
|
|
131
|
+
*
|
|
132
|
+
* @throws {ArgumentError} - When parameters are invalid.
|
|
133
|
+
* @throws {NoSuchElementTagError} - When the given svg tag is not qualified.
|
|
134
|
+
*/
|
|
135
|
+
SVGElement(tag, conf = {}) {
|
|
136
|
+
const Method = `${this.Name}.SVGElement`;
|
|
137
|
+
let [Tag, Conf] = [tag, conf];
|
|
138
|
+
|
|
139
|
+
ValidateTagAndConfiguration(Method, Tag, Conf);
|
|
140
|
+
|
|
141
|
+
const ThisElement = VerifyTag(Tag).SVGElement();
|
|
142
|
+
if (!IsElement(ThisElement)) {
|
|
143
|
+
console.warn(`${Method}(): There's no such svg element tag of '${Tag}'! (Exited with null)`);
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const ConfKeys = ["ClassNames", "Id", "Styles", "TabIndex", "XML_Language", "XML_Space"];
|
|
148
|
+
Conf = FormatConfig(Conf);
|
|
149
|
+
if (CountOf(Conf) > 0) {
|
|
150
|
+
if (Conf.ClassNames)
|
|
151
|
+
ClassOf(ThisElement).Set(Conf.ClassNames);
|
|
152
|
+
|
|
153
|
+
if (Conf.Id)
|
|
154
|
+
IdOf(ThisElement).Set(Conf.Id);
|
|
155
|
+
|
|
156
|
+
if (Conf.Styles)
|
|
157
|
+
StyleOf(ThisElement).Set(Conf.Styles);
|
|
158
|
+
|
|
159
|
+
if (Conf.TabIndex) {
|
|
160
|
+
if (!IsNum(Conf.Tabindex)) {
|
|
161
|
+
const ParsedIndex = parseInt(Conf.TabIndex, 10);
|
|
162
|
+
if (!IsNum(ParsedIndex))
|
|
163
|
+
Emit._ArgumentError(Method, "conf.TabIndex", Conf.TabIndex, "Number");
|
|
164
|
+
|
|
165
|
+
Conf.TabIndex = ParsedIndex;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
ThisElement.setAttribute("tabindex", Conf.TabIndex);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (Conf.XML_Language) {
|
|
172
|
+
if (!IsStr(Conf.XML_Language))
|
|
173
|
+
Emit._ArgumentError(Method, "conf.XML_Language", Conf.XML_Language, "String");
|
|
174
|
+
|
|
175
|
+
if (!IsStrEmpty(Conf.XML_Language))
|
|
176
|
+
ThisElement.setAttribute("xml:lang", Conf.XML_Language);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (Conf.XML_Space) {
|
|
180
|
+
if (!IsStr(Conf.XML_Space))
|
|
181
|
+
Emit._ArgumentError(Method, "conf.XML_Space", Conf.XML_Space, "String");
|
|
182
|
+
|
|
183
|
+
if (IsStrEmpty(Conf.XML_Space) || Conf.XML_Space !== "default" && Conf.XML_Space !== "preserve")
|
|
184
|
+
Conf.XML_Space = "default";
|
|
185
|
+
|
|
186
|
+
ThisElement.setAttribute("xml:space", Conf.XML_Space);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (Conf?.Others && IsPlainObj(Conf.Others) && CountOf(Conf.Others) > 0) {
|
|
190
|
+
for (const [OK, OV] of Object.entries(Conf.Others))
|
|
191
|
+
ThisElement.setAttribute(OK, OV);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return ThisElement;
|
|
196
|
+
},
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Generates an instance of `MathMLElement` with the given qualified tag.
|
|
200
|
+
*
|
|
201
|
+
* @template { MathMLElementTags["Key"] } T
|
|
202
|
+
* @param { T } tag - The tag of math element to generate.
|
|
203
|
+
* @param { MathElementConfig } [conf] - A configuration properties of math element.
|
|
204
|
+
* @returns { T extends MathMLElementTags["Key"] ? MathMLElementTags["Map"][T] : null }
|
|
205
|
+
*
|
|
206
|
+
* @throws {ArgumentError} - When parameters are invalid.
|
|
207
|
+
* @throws {NoSuchElementTagError} - When the given math tag is not qualified.
|
|
208
|
+
*/
|
|
209
|
+
MathElement(tag, conf = {}) {
|
|
210
|
+
const Method = `${this.Name}.MathElement`;
|
|
211
|
+
let [Tag, Conf] = [tag, conf];
|
|
212
|
+
|
|
213
|
+
ValidateTagAndConfiguration(Method, Tag, Conf);
|
|
214
|
+
|
|
215
|
+
const ThisElement = VerifyTag(Tag).MathElement();
|
|
216
|
+
if (!IsElement(ThisElement)) {
|
|
217
|
+
console.warn(`${Method}(): There's no such math element tag of '${Tag}'! (Exited with null)`);
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const ConfKeys = ["Data", "Dir", "DisplayStyle", "Href", "Id", "MathBackground", "MathColor", "MathSize", "ScriptLevel"];
|
|
222
|
+
Conf = FormatConfig(Conf, ConfKeys);
|
|
223
|
+
if (CountOf(Conf) > 0) {
|
|
224
|
+
if (Conf.Data) {
|
|
225
|
+
if (!IsPlainObj(Conf.Data))
|
|
226
|
+
Emit._ArgumentError(Method, "conf.Data", Conf.Data, "Plain Object ({})");
|
|
227
|
+
|
|
228
|
+
if (CountOf(Conf.Data) > 0)
|
|
229
|
+
for (const [DK, DV] of Object.entries(Conf.Data)) {
|
|
230
|
+
if (!IsStr(DV)) {
|
|
231
|
+
console.warn(`${Method}(@conf: { ${DK}: ${DV} }): Expects a string format value! (Skipped)`);
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (IsStrEmpty(DV))
|
|
236
|
+
continue;
|
|
237
|
+
|
|
238
|
+
ThisElement.setAttribute(`data-${DK.trim().toLowerCase()}`, DV);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (Conf.Dir) {
|
|
243
|
+
if (!IsStr(Conf.Dir))
|
|
244
|
+
Emit._ArgumentError(Method, "conf.Dir", Conf.Dir, "String");
|
|
245
|
+
|
|
246
|
+
if (IsStrEmpty(Conf.Dir) || (Conf.Dir !== "ltr" && Conf.Dir !== "rtl"))
|
|
247
|
+
Conf.Dir = "ltr";
|
|
248
|
+
|
|
249
|
+
ThisElement.setAttribute("dir", Conf.Dir);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (Conf.DisplayStyle && typeof Conf.DisplayStyle === "boolean")
|
|
253
|
+
ThisElement.setAttribute("displaystyle", Conf.DisplayStyle);
|
|
254
|
+
|
|
255
|
+
if (Conf.Href) {
|
|
256
|
+
if (!IsStr(Conf.Href))
|
|
257
|
+
Emit._ArgumentError(Method, "conf.Href", Conf.Href, "String");
|
|
258
|
+
|
|
259
|
+
if (!IsStrEmpty(Conf.Href))
|
|
260
|
+
ThisElement.setAttribute("href", Conf.Href);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (Conf.Id) {
|
|
264
|
+
if (!IsStr(Conf.Id))
|
|
265
|
+
Emit._ArgumentError(Method, "conf.Id", Conf.Id, "String");
|
|
266
|
+
|
|
267
|
+
if (!IsStrEmpty(Conf.Id))
|
|
268
|
+
ThisElement.id = Conf.Id;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (Conf.ScriptLevel) {
|
|
272
|
+
if (!IsStr(Conf.ScriptLevel))
|
|
273
|
+
Emit._ArgumentError(Method, "conf.ScriptLevel", Conf.ScriptLevel, "String");
|
|
274
|
+
|
|
275
|
+
if (!IsStrEmpty(Conf.ScriptLevel))
|
|
276
|
+
ThisElement.setAttribute("scriptlevel", Conf.ScriptLevel);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (Conf.MathBackground) {
|
|
280
|
+
if (!IsStr(Conf.MathBackground))
|
|
281
|
+
Emit._ArgumentError(Method, "conf.MathBackground", Conf.MathBackground, "String");
|
|
282
|
+
|
|
283
|
+
if (!IsStrEmpty(Conf.MathBackground))
|
|
284
|
+
ThisElement.setAttribute("mathbackground", Conf.MathBackground);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (Conf.MathColor) {
|
|
288
|
+
if (!IsStr(Conf.MathColor))
|
|
289
|
+
Emit._ArgumentError(Method, "conf.MathColor", Conf.MathColor, "String");
|
|
290
|
+
|
|
291
|
+
if (!IsStrEmpty(Conf.MathColor))
|
|
292
|
+
ThisElement.setAttribute("mathcolor", Conf.MathColor);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (Conf.MathSize) {
|
|
296
|
+
if (!IsStr(Conf.MathSize))
|
|
297
|
+
Emit._ArgumentError(Method, "conf.MathSize", Conf.MathSize, "String");
|
|
298
|
+
|
|
299
|
+
if (!IsStrEmpty(Conf.MathSize))
|
|
300
|
+
ThisElement.setAttribute("mathsize", Conf.MathSize);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (Conf?.Others && IsPlainObj(Conf.Others) && CountOf(Conf.Others) > 0) {
|
|
304
|
+
for (const [OK, OV] of Object.entries(Conf.Others))
|
|
305
|
+
ThisElement.setAttribute(OK, OV);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
return ThisElement;
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
export default Create;
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import Emit from '../../../custom/error/builder/error.builder.js';
|
|
2
|
+
import { IsNullOrUndefined, IsParentNode, IsStr } from '../../../guards/data-types/data-types.js';
|
|
3
|
+
import { IsStrEmpty } from '../../../guards/formats/formats.js';
|
|
4
|
+
import { XMLNameSpace } from '../../../variables.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Contains `DOMElement` retrieval methods.
|
|
8
|
+
*/
|
|
9
|
+
const GetElementBy = {
|
|
10
|
+
/**
|
|
11
|
+
* The name of this object.
|
|
12
|
+
*/
|
|
13
|
+
name: "GetElementBy",
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Search and retrieves the element that matches the given unique `id`.
|
|
17
|
+
*
|
|
18
|
+
* @overload
|
|
19
|
+
* @param { string } id - The unique `id` of element to search.
|
|
20
|
+
* @returns { HTMLElement | "<NO_SUCH_ELEMENT_ID>" | null }
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Search and retrieve the element that matches the given unique `id`.
|
|
24
|
+
*
|
|
25
|
+
* @overload
|
|
26
|
+
* @param { string } id - The unique `id` of element to search.
|
|
27
|
+
* @param { ParentNode } [root] - The parent element of where to search and retrieve element with the given unique `id`. (Default: Document)
|
|
28
|
+
* @returns { HTMLElement | "<NO_SUCH_ELEMENT_ID>" | null }
|
|
29
|
+
*/
|
|
30
|
+
ID(id, root = document) {
|
|
31
|
+
const Method = `${this.name}.ID`;
|
|
32
|
+
|
|
33
|
+
if (!IsStr(id))
|
|
34
|
+
Emit._ArgumentError(Method, "id", id, "String");
|
|
35
|
+
|
|
36
|
+
if (IsStrEmpty(id)) {
|
|
37
|
+
console.warn(`${Method}(@id: \'\'): Expects a non-empty-string! (Exited with null)`);
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (typeof root.getElementById !== "function") {
|
|
42
|
+
console.warn(`${Method}(@root: NOT_SUPPORTED_ROOT): Expects a root element that supports 'getElementById'! (Exited with null)`);
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return root.getElementById(id) ?? "<NO_SUCH_ELEMENT_ID>";
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Search and retrieves the elements that matches the given class name.
|
|
51
|
+
*
|
|
52
|
+
* @overload
|
|
53
|
+
* @param { string } cls - The class name of elements to search.
|
|
54
|
+
* @returns { HTMLCollectionOf<Element> }
|
|
55
|
+
*/
|
|
56
|
+
/**
|
|
57
|
+
* Search and retrieves the elements that matches the given class name.
|
|
58
|
+
*
|
|
59
|
+
* @overload
|
|
60
|
+
* @param { string } cls - The class name of elements to search.
|
|
61
|
+
* @param { ParentNode } [root] - The parent element of where to search and retrieve elements with the given class name. (Default: Document)
|
|
62
|
+
* @returns { HTMLCollectionOf<Element> }
|
|
63
|
+
*/
|
|
64
|
+
ClassName(cls, root = document) {
|
|
65
|
+
const Method = `${this.name}.ClassName`;
|
|
66
|
+
|
|
67
|
+
if (!IsStr(cls))
|
|
68
|
+
Emit._ArgumentError(Method, "cls", cls, "String");
|
|
69
|
+
|
|
70
|
+
if (IsStrEmpty(cls)) {
|
|
71
|
+
console.warn(`${Method}(@cls: \'\'): Expects a non-empty-string! (Exited with [])`);
|
|
72
|
+
return [];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!IsParentNode(root)) {
|
|
76
|
+
console.warn(`${Method}(@root: NOT_SUPPORTED_ROOT): Expects a root element that supports 'getElementsByClassName'! (Exited with [])`);
|
|
77
|
+
return [];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return root.getElementsByClassName(cls);
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Search and retrieves the elements that matches the given tag name.
|
|
85
|
+
*
|
|
86
|
+
* @template { keyof ElementTags } T
|
|
87
|
+
* @overload
|
|
88
|
+
* @param { T } tag - The tag of element to retrieve.
|
|
89
|
+
* @returns { HTMLCollectionOf<ResolveTag<T>> }
|
|
90
|
+
*/
|
|
91
|
+
/**
|
|
92
|
+
* Search and retrieves the elements that matches the given tag name.
|
|
93
|
+
*
|
|
94
|
+
* @template { keyof ElementTags } T
|
|
95
|
+
* @overload
|
|
96
|
+
* @param { T } tag - The tag of element to retrieve.
|
|
97
|
+
* @param { ParentNode } [root] - The parent element of where to search and retrieve the elements that matches the given tag. (Default: Document)
|
|
98
|
+
* @returns { HTMLCollectionOf<ResolveTag<T>> }
|
|
99
|
+
*/
|
|
100
|
+
TagName(tag, root = document) {
|
|
101
|
+
const Method = `${this.name}.TagName`;
|
|
102
|
+
|
|
103
|
+
if (!IsStr(tag))
|
|
104
|
+
Emit._ArgumentError(Method, "tag", tag, "String");
|
|
105
|
+
|
|
106
|
+
if (IsStrEmpty(tag)) {
|
|
107
|
+
console.warn(`${Method}(@tag: \'\'): Expects a non-empty-string! (Exited with [])`);
|
|
108
|
+
return [];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (!IsParentNode(root)) {
|
|
112
|
+
console.warn(`${Method}(@root: NOT_SUPPORTED_ROOT): Expects a root element that supports 'getElementsByTagName'! (Exited with [])`)
|
|
113
|
+
return [];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return root.getElementsByTagName(tag);
|
|
117
|
+
},
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Search and retrieves the elements that matches the given namespace and tag name.
|
|
121
|
+
*
|
|
122
|
+
* @template { keyof ElementTags } T
|
|
123
|
+
* @template { XMLNameSpace } NS
|
|
124
|
+
* @overload
|
|
125
|
+
* @param { NS } namespace - The namespace of element to retrieve.
|
|
126
|
+
* @param { T } tag - The tag of element with the given namespace to retrieve.
|
|
127
|
+
* @returns { HTMLCollectionOf<ResolveTagNS<NS, T>> }
|
|
128
|
+
*/
|
|
129
|
+
/**
|
|
130
|
+
* Search and retrieves the elements that matches the given namespace and tag name.
|
|
131
|
+
*
|
|
132
|
+
* @template { keyof ElementTags } T
|
|
133
|
+
* @template { XMLNameSpace } NS
|
|
134
|
+
* @overload
|
|
135
|
+
* @param { NS } namespace - The namespace of element to retrieve.
|
|
136
|
+
* @param { T } tag - The tag of element with the given namespace to retrieve.
|
|
137
|
+
* @param { ParentNode } [root] - The parent element of where to search and retrieve the elements with the given namespace and tag. (Default: Document)
|
|
138
|
+
* @returns { HTMLCollectionOf<ResolveTagNS<NS, T>> }
|
|
139
|
+
*/
|
|
140
|
+
TagNameNS(namespace, tag, root = document) {
|
|
141
|
+
const Method = `${this.name}.TagNameNS`;
|
|
142
|
+
|
|
143
|
+
if (!IsStr(namespace))
|
|
144
|
+
Emit._ArgumentError(Method, "namespace", namespace, "String");
|
|
145
|
+
|
|
146
|
+
if (!IsStr(tag))
|
|
147
|
+
Emit._ArgumentError(Method, "tag", tag, "String");
|
|
148
|
+
|
|
149
|
+
if (IsStrEmpty(namespace)) {
|
|
150
|
+
console.warn(`${Method}(@namespace: \'\'): Expects a non-empty-string! (Exited with [])`);
|
|
151
|
+
return [];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (IsStrEmpty(tag)) {
|
|
155
|
+
console.warn(`${Method}(@tag: \'\'): Expects a non-empty-string! (Exited with [])`);
|
|
156
|
+
return [];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (!XMLNameSpace.includes(namespace))
|
|
160
|
+
console.warn(`${Method}(@namespace: ${namespace}): Received a unknown XML namespace of element! It will be treated as custom element but it might cause to throw an error when it violates the format.`);
|
|
161
|
+
|
|
162
|
+
if (!IsParentNode(root)) {
|
|
163
|
+
console.warn(`${Method}(@root: NOT_SUPPORTED_ROOT): Expects a root element that supports 'getElementsByTagNameNS'! (Exited with [])`);
|
|
164
|
+
return [];
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return root.getElementsByTagNameNS(namespace, tag);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export default GetElementBy;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import Emit from '../../../custom/error/builder/error.builder.js';
|
|
2
|
+
import { IsStr, IsParentNode } from '../../../guards/data-types/data-types.js';
|
|
3
|
+
import { IsStrEmpty } from '../../../guards/formats/formats.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Search and retrieves the first `DOMElement` that matches the given selector.
|
|
7
|
+
*
|
|
8
|
+
* @template { keyof ElementTags } T
|
|
9
|
+
* @overload
|
|
10
|
+
* @param { T } selector - The selector of element to search.
|
|
11
|
+
* @returns { ResolveTag<T> } The first element that matches the given selector.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Search and retrieves the first `DOMElement` that matches the given selector.
|
|
15
|
+
*
|
|
16
|
+
* @template { keyof ElementTags } T
|
|
17
|
+
* @overload
|
|
18
|
+
* @param { T } selector - The selector of element to search.
|
|
19
|
+
* @param { ParentNode } [root] - The parent node of where to look the given selector at. (Default: Document)
|
|
20
|
+
* @returns { ResolveTag<T> } The first element that matches the given selector.
|
|
21
|
+
*/
|
|
22
|
+
export function Select(selector, root = document) {
|
|
23
|
+
const Method = "Select";
|
|
24
|
+
|
|
25
|
+
if (!IsStr(selector))
|
|
26
|
+
Emit._ArgumentError(Method, "selector", selector, "String");
|
|
27
|
+
|
|
28
|
+
if (IsStrEmpty(selector)) {
|
|
29
|
+
console.warn(`${Method}(@selector: \'\'): Expects a non-empty-string! (Exited with null)`);
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!IsParentNode(root)) {
|
|
34
|
+
console.warn(`${Method}(@root: ${root}): Expects a root element that supports 'querySelector'! (Exited with null)`);
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return root.querySelector(selector);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Search and retrieves a collection of elements that matches the given selector.
|
|
43
|
+
*
|
|
44
|
+
* @template { keyof ElementTags } T
|
|
45
|
+
* @overload
|
|
46
|
+
* @param { T } selector - The selector of elements to search.
|
|
47
|
+
* @returns { ResolveTag<T>[] } The collection of elements that matches the given selector.
|
|
48
|
+
*/
|
|
49
|
+
/**
|
|
50
|
+
* Search and retrieves a collection of elements that matches the given selector.
|
|
51
|
+
*
|
|
52
|
+
* @template { keyof ElementTags } T
|
|
53
|
+
* @overload
|
|
54
|
+
* @param { T } selector - The selector of elements to search.
|
|
55
|
+
* @param { ParentNode } [root] - The parent node of where to look the given selector at. (Default: Document)
|
|
56
|
+
* @returns { ResolveTag<T>[] } The collection of elements that matches the given selector.
|
|
57
|
+
*/
|
|
58
|
+
export function SelectAll(selector, root = document) {
|
|
59
|
+
const Method = "SelectAll";
|
|
60
|
+
|
|
61
|
+
if (!IsStr(selector))
|
|
62
|
+
Emit._ArgumentError(Method, "selector", selector, "String");
|
|
63
|
+
|
|
64
|
+
if (IsStrEmpty(selector)) {
|
|
65
|
+
console.warn(`${Method}(@selector: \'\'): Expects a non-empty-string! (Exited with null)`);
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (!IsParentNode(root)) {
|
|
70
|
+
console.warn(`${Method}(@root: ${root}): Expects a root element that supports 'querySelectorAll'! (Exited with null)`);
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return Array.from(root.querySelectorAll(selector));
|
|
75
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import Emit from '../../../custom/error/builder/error.builder.js';
|
|
2
|
+
import { IsStrEmpty } from '../../../guards/formats/formats.js';
|
|
3
|
+
import { IsStr } from '../../../guards/data-types/data-types.js';
|
|
4
|
+
import * as VAR from '../../../variables.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Verifies the specified tag of `Element` and returns its representative `Element`.
|
|
8
|
+
*
|
|
9
|
+
* @template { keyof ElementTags } KTag
|
|
10
|
+
* @param { KTag } tag - The tag of Element to verify.
|
|
11
|
+
* @returns The response object methods for element instance verification type.
|
|
12
|
+
*/
|
|
13
|
+
export default function VerifyTag(tag) {
|
|
14
|
+
const Method = "VerifyTag";
|
|
15
|
+
|
|
16
|
+
if (!IsStr(tag) || IsStrEmpty(tag))
|
|
17
|
+
Emit._ArgumentError(Method, "tag", tag, "String");
|
|
18
|
+
|
|
19
|
+
tag = tag.trim().toLowerCase()
|
|
20
|
+
return {
|
|
21
|
+
/**
|
|
22
|
+
* Verifies the provided tag from the collection of `HTMLElementTagsMap` and returns a generated
|
|
23
|
+
* `HTMLElement` instance from it, if its qualified.
|
|
24
|
+
*
|
|
25
|
+
* @returns { VerifierTagAPI<KTag> }
|
|
26
|
+
*/
|
|
27
|
+
HTMLElement() {
|
|
28
|
+
if (!VAR.HTMLElementTags.has(tag))
|
|
29
|
+
Emit._NoSuchElementTagError(`${Method}.HTMLElement`, "tag", tag, "HTMLElement");
|
|
30
|
+
|
|
31
|
+
return VAR.HTMLElementTags.get(tag)?.Generate() ?? null;
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Verifies the provided tag from the collection of `SVGElementTagsMap` and returns generated
|
|
36
|
+
* `SVGElement` instance from it, if its qualified.
|
|
37
|
+
*
|
|
38
|
+
* @returns { VerifierTagAPI<K> }
|
|
39
|
+
*/
|
|
40
|
+
SVGElement() {
|
|
41
|
+
if (!VAR.SVGElementTags.has(tag))
|
|
42
|
+
Emit._NoSuchElementTagError(`${Method}.SVGElement`, "tag", tag, "SVGElement");
|
|
43
|
+
|
|
44
|
+
return VAR.SVGElementTags.get(tag)?.Generate() ?? null;
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Verifies the provided tag from the collection of `MathMLElementTagsMap` and returns generated
|
|
49
|
+
* `MathMlElement` instance from it, if its qualified.
|
|
50
|
+
*
|
|
51
|
+
* @returns { VerifierTagAPI<K> }
|
|
52
|
+
*/
|
|
53
|
+
MathElement() {
|
|
54
|
+
if (!VAR.MathMLElementTags.has(tag))
|
|
55
|
+
Emit._NoSuchElementTagError(`${Method}.MathMLElement`, "tag", tag, "MathMLElement");
|
|
56
|
+
|
|
57
|
+
return VAR.MathMLElementTags.get(tag)?.Generate();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import Emit from '../../custom/error/builder/error.builder.js';
|
|
2
|
+
import { IsChildNode, IsParentNode, IsPropertyAt } from '../../guards/data-types/data-types.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Mounts the given `childNode` to the specified `parentNode`.
|
|
6
|
+
*
|
|
7
|
+
* ***Reference***:
|
|
8
|
+
* `appendChild()`.
|
|
9
|
+
*
|
|
10
|
+
* @overload
|
|
11
|
+
* @param { ParentNode } parentNode - The parent node to mount the given `childNode` at.
|
|
12
|
+
* @param { ChildNode } childNode - The `childNode` to mount at `parentNode`.
|
|
13
|
+
* @returns { void }
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Mounts the given `childNode` collection to the specified `parentNode`.
|
|
17
|
+
*
|
|
18
|
+
* ***Reference***:
|
|
19
|
+
* `appendChild()`
|
|
20
|
+
*
|
|
21
|
+
* @overload
|
|
22
|
+
* @param { ParentNode } parentNode - The parent node to mount the given `childNode` collection at.
|
|
23
|
+
* @param { ...ChildNode } childNodes - The `childNode` collection to mount at `parentNode`.
|
|
24
|
+
* @returns { void }
|
|
25
|
+
*/
|
|
26
|
+
export default function Mount(parentNode, ...childNodes) {
|
|
27
|
+
const Method = "Mount", PCN = childNodes.length > 1 ? "childNodes" : "childNode";
|
|
28
|
+
|
|
29
|
+
if (!IsParentNode(parentNode))
|
|
30
|
+
Emit._ArgumentError(Method, "parentNode", parentNode, "ParentNode");
|
|
31
|
+
|
|
32
|
+
if (childNodes.length === 0) {
|
|
33
|
+
console.warn(`${Method}(@childNode: NOT_PROVIDED): Expects at least 1 or more child node(s) to mount! (Exited)`);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (!IsPropertyAt(parentNode, "appendChild")) {
|
|
38
|
+
console.warn(`${Method}(@parentNode: NOT_SUPPORTED_METHOD): The specified parent node does not support the property method 'appendChild'! (Exited)`);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
for (const Node of childNodes) {
|
|
43
|
+
if (!IsChildNode(Node))
|
|
44
|
+
Emit._ArgumentError(Method, PCN, Node, "ChildNode");
|
|
45
|
+
|
|
46
|
+
parentNode.appendChild(Node);
|
|
47
|
+
}
|
|
48
|
+
}
|