tiny-essentials 1.22.2 → 1.22.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/dist/v1/TinyAdvancedRaffle.min.js +1 -1
- package/dist/v1/TinyDragger.min.js +1 -1
- package/dist/v1/TinyEssentials.min.js +1 -1
- package/dist/v1/TinyEvents.min.js +1 -1
- package/dist/v1/TinyHtml.min.js +1 -1
- package/dist/v1/TinyIframeEvents.min.js +1 -1
- package/dist/v1/TinyLocalStorage.min.js +1 -1
- package/dist/v1/TinyNewWinEvents.min.js +1 -1
- package/dist/v1/TinySmartScroller.min.js +1 -1
- package/dist/v1/TinyUploadClicker.min.js +1 -1
- package/dist/v1/libs/TinyAdvancedRaffle.cjs +13 -13
- package/dist/v1/libs/TinyAdvancedRaffle.d.mts +19 -19
- package/dist/v1/libs/TinyAdvancedRaffle.mjs +13 -13
- package/dist/v1/libs/TinyEvents.cjs +98 -73
- package/dist/v1/libs/TinyEvents.d.mts +22 -22
- package/dist/v1/libs/TinyEvents.mjs +103 -77
- package/dist/v1/libs/TinyHtml.cjs +69 -6
- package/dist/v1/libs/TinyHtml.d.mts +44 -6
- package/dist/v1/libs/TinyHtml.mjs +64 -6
- package/dist/v1/libs/TinyIframeEvents.cjs +11 -11
- package/dist/v1/libs/TinyIframeEvents.d.mts +19 -19
- package/dist/v1/libs/TinyIframeEvents.mjs +11 -11
- package/dist/v1/libs/TinyLocalStorage.cjs +12 -12
- package/dist/v1/libs/TinyLocalStorage.d.mts +21 -21
- package/dist/v1/libs/TinyLocalStorage.mjs +12 -12
- package/dist/v1/libs/TinyNewWinEvents.cjs +11 -11
- package/dist/v1/libs/TinyNewWinEvents.d.mts +19 -19
- package/dist/v1/libs/TinyNewWinEvents.mjs +11 -11
- package/dist/v1/libs/TinySmartScroller.cjs +12 -12
- package/dist/v1/libs/TinySmartScroller.d.mts +53 -30
- package/dist/v1/libs/TinySmartScroller.mjs +12 -12
- package/docs/v1/libs/TinyEvents.md +93 -105
- package/docs/v1/libs/TinyHtml.md +4 -2
- package/package.json +1 -1
|
@@ -28,6 +28,20 @@ class TinyEvents {
|
|
|
28
28
|
#maxListeners = 10;
|
|
29
29
|
/** @type {boolean} */
|
|
30
30
|
#throwMaxListeners = false;
|
|
31
|
+
/**
|
|
32
|
+
* Normalizes the event parameter into an array of strings.
|
|
33
|
+
*
|
|
34
|
+
* @param {string|string[]} event
|
|
35
|
+
* @param {string} method
|
|
36
|
+
* @returns {string[]}
|
|
37
|
+
*/
|
|
38
|
+
#normalizeEvents(event, method) {
|
|
39
|
+
if (typeof event === 'string')
|
|
40
|
+
return [event];
|
|
41
|
+
if (Array.isArray(event) && event.every((e) => typeof e === 'string'))
|
|
42
|
+
return event;
|
|
43
|
+
throw new TypeError(`${method}(event): event must be a string or string[]`);
|
|
44
|
+
}
|
|
31
45
|
/**
|
|
32
46
|
* Enables or disables throwing an error when the maximum number of listeners is exceeded.
|
|
33
47
|
*
|
|
@@ -75,72 +89,76 @@ class TinyEvents {
|
|
|
75
89
|
/**
|
|
76
90
|
* Adds a listener to the beginning of the listeners array for the specified event.
|
|
77
91
|
*
|
|
78
|
-
* @param {string} event - Event name.
|
|
92
|
+
* @param {string|string[]} event - Event name.
|
|
79
93
|
* @param {handler} handler - The callback function.
|
|
80
94
|
*/
|
|
81
95
|
prependListener(event, handler) {
|
|
82
|
-
|
|
83
|
-
throw new TypeError('prepend(event, handler): event name must be a string');
|
|
96
|
+
const events = this.#normalizeEvents(event, 'prependListener');
|
|
84
97
|
if (typeof handler !== 'function')
|
|
85
|
-
throw new TypeError('
|
|
86
|
-
|
|
98
|
+
throw new TypeError('prependListener(event, handler): handler must be a function');
|
|
99
|
+
for (const ev of events)
|
|
100
|
+
this.#prepend(ev, handler);
|
|
87
101
|
}
|
|
88
102
|
/**
|
|
89
103
|
* Adds a one-time listener to the beginning of the listeners array for the specified event.
|
|
90
104
|
*
|
|
91
|
-
* @param {string} event - Event name.
|
|
105
|
+
* @param {string|string[]} event - Event name.
|
|
92
106
|
* @param {handler} handler - The callback function.
|
|
93
|
-
* @returns {handler} - The wrapped handler used internally.
|
|
107
|
+
* @returns {handler[]} - The wrapped handler used internally.
|
|
94
108
|
*/
|
|
95
109
|
prependListenerOnce(event, handler) {
|
|
96
|
-
|
|
97
|
-
throw new TypeError('prependOnceListener(event, handler): event name must be a string');
|
|
110
|
+
const events = this.#normalizeEvents(event, 'prependListenerOnce');
|
|
98
111
|
if (typeof handler !== 'function')
|
|
99
|
-
throw new TypeError('
|
|
100
|
-
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
112
|
+
throw new TypeError('prependListenerOnce(event, handler): handler must be a function');
|
|
113
|
+
const wrappedHandlers = [];
|
|
114
|
+
for (const ev of events) {
|
|
115
|
+
/** @type {handler} */
|
|
116
|
+
const wrapped = (...args) => {
|
|
117
|
+
this.off(ev, wrapped);
|
|
118
|
+
handler(...args);
|
|
119
|
+
};
|
|
120
|
+
this.#prepend(ev, wrapped, { once: true });
|
|
121
|
+
wrappedHandlers.push(wrapped);
|
|
122
|
+
}
|
|
123
|
+
return wrappedHandlers;
|
|
107
124
|
}
|
|
108
125
|
////////////////////////////////////////////////////////////
|
|
109
126
|
/**
|
|
110
127
|
* Adds a event listener.
|
|
111
128
|
*
|
|
112
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
129
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
113
130
|
* @param {handler} handler - Callback function to be called when event fires.
|
|
114
131
|
* @param {Object} [settings={}] - Optional settings.
|
|
115
132
|
* @param {boolean} [settings.once=false] - This is a once event.
|
|
116
133
|
*/
|
|
117
134
|
#on(event, handler, { once = false } = {}) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
eventData =
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
135
|
+
const events = this.#normalizeEvents(event, 'on');
|
|
136
|
+
for (const ev of events) {
|
|
137
|
+
let eventData = this.#listeners.get(ev);
|
|
138
|
+
if (!Array.isArray(eventData)) {
|
|
139
|
+
eventData = [];
|
|
140
|
+
this.#listeners.set(ev, eventData);
|
|
141
|
+
}
|
|
142
|
+
eventData.push({ handler, config: { once } });
|
|
143
|
+
// Warn if listener count exceeds the max allowed
|
|
144
|
+
const max = this.#maxListeners;
|
|
145
|
+
if (max > 0 && eventData.length > max) {
|
|
146
|
+
const warnMessage = `Possible memory leak detected. ${eventData.length} "${ev}" listeners added. ` +
|
|
147
|
+
`Use setMaxListeners() to increase limit.`;
|
|
148
|
+
if (!this.#throwMaxListeners)
|
|
149
|
+
console.warn(warnMessage);
|
|
150
|
+
else
|
|
151
|
+
throw new Error(warnMessage);
|
|
152
|
+
}
|
|
133
153
|
}
|
|
134
154
|
}
|
|
135
155
|
/**
|
|
136
156
|
* Adds a event listener.
|
|
137
157
|
*
|
|
138
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
158
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
139
159
|
* @param {handler} handler - Callback function to be called when event fires.
|
|
140
160
|
*/
|
|
141
161
|
on(event, handler) {
|
|
142
|
-
if (typeof event !== 'string')
|
|
143
|
-
throw new TypeError('on(event, handler): event name must be a string');
|
|
144
162
|
if (typeof handler !== 'function')
|
|
145
163
|
throw new TypeError('on(event, handler): handler must be a function');
|
|
146
164
|
return this.#on(event, handler);
|
|
@@ -148,28 +166,30 @@ class TinyEvents {
|
|
|
148
166
|
/**
|
|
149
167
|
* Registers an event listener that runs only once, then is removed.
|
|
150
168
|
*
|
|
151
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
169
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
152
170
|
* @param {handler} handler - The callback function to run on event.
|
|
153
|
-
* @returns {handler} - The wrapped version of the handler.
|
|
171
|
+
* @returns {handler[]} - The wrapped version of the handler.
|
|
154
172
|
*/
|
|
155
173
|
once(event, handler) {
|
|
156
|
-
|
|
157
|
-
throw new TypeError('The event name must be a string.');
|
|
174
|
+
const events = this.#normalizeEvents(event, 'once');
|
|
158
175
|
if (typeof handler !== 'function')
|
|
159
176
|
throw new TypeError('once(event, handler): handler must be a function');
|
|
160
|
-
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
177
|
+
const wrappedHandlers = [];
|
|
178
|
+
for (const ev of events) {
|
|
179
|
+
/** @type {handler} */
|
|
180
|
+
const wrapped = (...args) => {
|
|
181
|
+
this.off(ev, wrapped);
|
|
182
|
+
handler(...args);
|
|
183
|
+
};
|
|
184
|
+
this.#on(ev, wrapped, { once: true });
|
|
185
|
+
wrappedHandlers.push(wrapped);
|
|
186
|
+
}
|
|
187
|
+
return wrappedHandlers;
|
|
168
188
|
}
|
|
169
189
|
/**
|
|
170
190
|
* Adds a event listener.
|
|
171
191
|
*
|
|
172
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
192
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
173
193
|
* @param {handler} handler - Callback function to be called when event fires.
|
|
174
194
|
*/
|
|
175
195
|
appendListener(event, handler) {
|
|
@@ -178,9 +198,9 @@ class TinyEvents {
|
|
|
178
198
|
/**
|
|
179
199
|
* Registers an event listener that runs only once, then is removed.
|
|
180
200
|
*
|
|
181
|
-
* @param {string} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
201
|
+
* @param {string|string[]} event - Event name, such as 'onScrollBoundary' or 'onAutoScroll'.
|
|
182
202
|
* @param {handler} handler - The callback function to run on event.
|
|
183
|
-
* @returns {handler} - The wrapped version of the handler.
|
|
203
|
+
* @returns {handler[]} - The wrapped version of the handler.
|
|
184
204
|
*/
|
|
185
205
|
appendListenerOnce(event, handler) {
|
|
186
206
|
return this.once(event, handler);
|
|
@@ -189,33 +209,34 @@ class TinyEvents {
|
|
|
189
209
|
/**
|
|
190
210
|
* Removes a previously registered event listener.
|
|
191
211
|
*
|
|
192
|
-
* @param {string} event - The name of the event to remove the handler from.
|
|
212
|
+
* @param {string|string[]} event - The name of the event to remove the handler from.
|
|
193
213
|
* @param {handler} handler - The specific callback function to remove.
|
|
194
214
|
*/
|
|
195
215
|
off(event, handler) {
|
|
196
|
-
|
|
197
|
-
throw new TypeError('off(event, handler): event name must be a string');
|
|
216
|
+
const events = this.#normalizeEvents(event, 'off');
|
|
198
217
|
if (typeof handler !== 'function')
|
|
199
218
|
throw new TypeError('off(event, handler): handler must be a function');
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
219
|
+
for (const ev of events) {
|
|
220
|
+
const listeners = this.#listeners.get(ev);
|
|
221
|
+
if (!Array.isArray(listeners))
|
|
222
|
+
continue;
|
|
223
|
+
const index = listeners.findIndex((listener) => listener.handler === handler);
|
|
224
|
+
if (index !== -1)
|
|
225
|
+
listeners.splice(index, 1);
|
|
226
|
+
// Optionally clean up empty arrays (optional)
|
|
227
|
+
if (listeners.length === 0)
|
|
228
|
+
this.#listeners.delete(ev);
|
|
229
|
+
}
|
|
209
230
|
}
|
|
210
231
|
/**
|
|
211
232
|
* Removes all event listeners of a specific type from the element.
|
|
212
233
|
*
|
|
213
|
-
* @param {string} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
234
|
+
* @param {string|string[]} event - The event type to remove (e.g. 'onScrollBoundary').
|
|
214
235
|
*/
|
|
215
236
|
offAll(event) {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
237
|
+
const events = this.#normalizeEvents(event, 'offAll');
|
|
238
|
+
for (const ev of events)
|
|
239
|
+
this.#listeners.delete(ev);
|
|
219
240
|
}
|
|
220
241
|
/**
|
|
221
242
|
* Removes all event listeners of all types from the element.
|
|
@@ -291,19 +312,24 @@ class TinyEvents {
|
|
|
291
312
|
/**
|
|
292
313
|
* Emits an event, triggering all registered handlers for that event.
|
|
293
314
|
*
|
|
294
|
-
* @param {string} event - The event name to emit.
|
|
315
|
+
* @param {string|string[]} event - The event name to emit.
|
|
295
316
|
* @param {...any} payload - Optional data to pass to each handler.
|
|
296
|
-
* @returns {boolean} True if any listeners were called, false otherwise.
|
|
317
|
+
* @returns {boolean[]} True if any listeners were called, false otherwise.
|
|
297
318
|
*/
|
|
298
319
|
emit(event, ...payload) {
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
const
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
320
|
+
const events = this.#normalizeEvents(event, 'emit');
|
|
321
|
+
const called = [];
|
|
322
|
+
for (const ev of events) {
|
|
323
|
+
const listeners = this.#listeners.get(ev);
|
|
324
|
+
if (!Array.isArray(listeners) || listeners.length === 0) {
|
|
325
|
+
called.push(false);
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
// Call all listeners with the provided data
|
|
329
|
+
listeners.forEach((listener) => listener.handler(...payload));
|
|
330
|
+
called.push(true);
|
|
331
|
+
}
|
|
332
|
+
return called;
|
|
307
333
|
}
|
|
308
334
|
///////////////////////////////////
|
|
309
335
|
/**
|
|
@@ -663,7 +663,7 @@ class TinyHtml {
|
|
|
663
663
|
* ```
|
|
664
664
|
*
|
|
665
665
|
* @param {string} tagName - The HTML tag name (e.g., 'div', 'span', 'button').
|
|
666
|
-
* @param {Record<string, string|null>} [attrs] - Optional key-value pairs representing HTML attributes.
|
|
666
|
+
* @param {Record<string, string|number|null>} [attrs] - Optional key-value pairs representing HTML attributes.
|
|
667
667
|
* If the value is `null`, the attribute will still be set with an empty value.
|
|
668
668
|
* @returns {TinyHtml<HTMLElement>} - A new instance of TinyHtml representing the created element.
|
|
669
669
|
* @throws {TypeError} - If `tagName` is not a string, or `attrs` is not a plain object when defined.
|
|
@@ -859,13 +859,22 @@ class TinyHtml {
|
|
|
859
859
|
|
|
860
860
|
// TITLE: Element getter
|
|
861
861
|
|
|
862
|
+
/**
|
|
863
|
+
* Returns the current targets held by this instance.
|
|
864
|
+
*
|
|
865
|
+
* @returns {ConstructorElValues[]} - The instance's targets element.
|
|
866
|
+
*/
|
|
867
|
+
get elements() {
|
|
868
|
+
return [...this.#el];
|
|
869
|
+
}
|
|
870
|
+
|
|
862
871
|
/**
|
|
863
872
|
* Iterates over all elements, executing the provided callback on each.
|
|
864
873
|
* @param {(element: TinyHtmlAny, index: number, items: TinyHtmlAny[]) => void} callback - Function invoked for each element.
|
|
865
874
|
* @returns {this} The current instance for chaining.
|
|
866
875
|
*/
|
|
867
876
|
forEach(callback) {
|
|
868
|
-
const elems = this.
|
|
877
|
+
const elems = this.elements.map((el, index) => this.extract(index));
|
|
869
878
|
for (const index in elems) callback(elems[index], Number(index), elems);
|
|
870
879
|
return this;
|
|
871
880
|
}
|
|
@@ -907,6 +916,7 @@ class TinyHtml {
|
|
|
907
916
|
}
|
|
908
917
|
|
|
909
918
|
/**
|
|
919
|
+
* @deprecated Use the getter {@link elements} instead.
|
|
910
920
|
* Returns the current targets held by this instance.
|
|
911
921
|
*
|
|
912
922
|
* @returns {ConstructorElValues[]} - The instance's targets element.
|
|
@@ -6534,7 +6544,7 @@ class TinyHtml {
|
|
|
6534
6544
|
* @template {TinyElement|TinyElement[]} T
|
|
6535
6545
|
* @param {T} el - Target element(s).
|
|
6536
6546
|
* @param {string|Record<string, string|null>} name - Attribute name or an object of attributes.
|
|
6537
|
-
* @param {string|null} [value=null] - Attribute value (ignored if "name" is an object).
|
|
6547
|
+
* @param {string|number|null} [value=null] - Attribute value (ignored if "name" is an object).
|
|
6538
6548
|
* @returns {T}
|
|
6539
6549
|
*/
|
|
6540
6550
|
static setAttr(el, name, value = null) {
|
|
@@ -6543,8 +6553,8 @@ class TinyHtml {
|
|
|
6543
6553
|
// Multiple attributes at once
|
|
6544
6554
|
if (typeof name === 'object' && name !== null) {
|
|
6545
6555
|
Object.entries(name).forEach(([attr, val]) => {
|
|
6546
|
-
if (val !== null && typeof val !== 'string')
|
|
6547
|
-
throw new TypeError(`The value for "${attr}" must be a string or null.`);
|
|
6556
|
+
if (val !== null && typeof val !== 'string' && typeof val !== 'number')
|
|
6557
|
+
throw new TypeError(`The value for "${attr}" must be a string/number or null.`);
|
|
6548
6558
|
elems.forEach((elem) => {
|
|
6549
6559
|
if (val === null) elem.removeAttribute(TinyHtml.getAttrName(attr));
|
|
6550
6560
|
else elem.setAttribute(TinyHtml.getAttrName(attr), val);
|
|
@@ -6569,7 +6579,7 @@ class TinyHtml {
|
|
|
6569
6579
|
/**
|
|
6570
6580
|
* Set one or multiple attributes on an element.
|
|
6571
6581
|
* @param {string|Record<string, string|null>} name - Attribute name or an object of attributes.
|
|
6572
|
-
* @param {string|null} [value=null] - Attribute value (ignored if "name" is an object).
|
|
6582
|
+
* @param {string|number|null} [value=null] - Attribute value (ignored if "name" is an object).
|
|
6573
6583
|
* @returns {this}
|
|
6574
6584
|
*/
|
|
6575
6585
|
setAttr(name, value) {
|
|
@@ -6736,6 +6746,59 @@ class TinyHtml {
|
|
|
6736
6746
|
return TinyHtml.toggleProp(this, name, force);
|
|
6737
6747
|
}
|
|
6738
6748
|
|
|
6749
|
+
/**
|
|
6750
|
+
* Get properties on an element.
|
|
6751
|
+
*
|
|
6752
|
+
* @param {TinyHtmlElement} el - Target element.
|
|
6753
|
+
* @param {string} name - Property name.
|
|
6754
|
+
* @returns {any} - Property value if getting, otherwise `undefined`.
|
|
6755
|
+
*/
|
|
6756
|
+
static prop(el, name) {
|
|
6757
|
+
if (typeof name !== 'string')
|
|
6758
|
+
throw new TypeError('Invalid arguments passed to prop(). Expected string for "name".');
|
|
6759
|
+
const elem = TinyHtml._preElem(el, 'prop');
|
|
6760
|
+
// @ts-ignore
|
|
6761
|
+
return elem[name];
|
|
6762
|
+
}
|
|
6763
|
+
|
|
6764
|
+
/**
|
|
6765
|
+
* Get properties on an element.
|
|
6766
|
+
*
|
|
6767
|
+
* @param {string} name - Property name.
|
|
6768
|
+
* @returns {any} - Property value if getting, otherwise `undefined`.
|
|
6769
|
+
*/
|
|
6770
|
+
prop(name) {
|
|
6771
|
+
return TinyHtml.prop(this, name);
|
|
6772
|
+
}
|
|
6773
|
+
|
|
6774
|
+
/**
|
|
6775
|
+
* Set a property on elements.
|
|
6776
|
+
* @template {TinyElement|TinyElement[]} T
|
|
6777
|
+
* @param {T} el - Target element(s).
|
|
6778
|
+
* @param {string} name - Property name.
|
|
6779
|
+
* @param {any} value - Value to set.
|
|
6780
|
+
* @returns {T}
|
|
6781
|
+
*/
|
|
6782
|
+
static setProp(el, name, value) {
|
|
6783
|
+
if (typeof name !== 'string' || name.trim() === '')
|
|
6784
|
+
throw new Error('Property name must be a non-empty string.');
|
|
6785
|
+
TinyHtml._preElems(el, 'setProp').forEach((elem) => {
|
|
6786
|
+
// @ts-ignore
|
|
6787
|
+
elem[name] = value;
|
|
6788
|
+
});
|
|
6789
|
+
return el;
|
|
6790
|
+
}
|
|
6791
|
+
|
|
6792
|
+
/**
|
|
6793
|
+
* Set a property on this element.
|
|
6794
|
+
* @param {string} name - Property name.
|
|
6795
|
+
* @param {any} value - Value to set.
|
|
6796
|
+
* @returns {this} The same element.
|
|
6797
|
+
*/
|
|
6798
|
+
setProp(name, value) {
|
|
6799
|
+
return TinyHtml.setProp(this, name, value);
|
|
6800
|
+
}
|
|
6801
|
+
|
|
6739
6802
|
/////////////////////////////////////////////////////
|
|
6740
6803
|
|
|
6741
6804
|
// TITLE: Remove Element
|
|
@@ -401,12 +401,12 @@ declare class TinyHtml<TinyHtmlT extends ConstructorElValues | ConstructorElValu
|
|
|
401
401
|
* ```
|
|
402
402
|
*
|
|
403
403
|
* @param {string} tagName - The HTML tag name (e.g., 'div', 'span', 'button').
|
|
404
|
-
* @param {Record<string, string|null>} [attrs] - Optional key-value pairs representing HTML attributes.
|
|
404
|
+
* @param {Record<string, string|number|null>} [attrs] - Optional key-value pairs representing HTML attributes.
|
|
405
405
|
* If the value is `null`, the attribute will still be set with an empty value.
|
|
406
406
|
* @returns {TinyHtml<HTMLElement>} - A new instance of TinyHtml representing the created element.
|
|
407
407
|
* @throws {TypeError} - If `tagName` is not a string, or `attrs` is not a plain object when defined.
|
|
408
408
|
*/
|
|
409
|
-
static createFrom(tagName: string, attrs?: Record<string, string | null>): TinyHtml<HTMLElement>;
|
|
409
|
+
static createFrom(tagName: string, attrs?: Record<string, string | number | null>): TinyHtml<HTMLElement>;
|
|
410
410
|
/**
|
|
411
411
|
* Creates a new DOM element with the specified tag name and options, then wraps it in a TinyHtml instance.
|
|
412
412
|
*
|
|
@@ -2358,10 +2358,10 @@ declare class TinyHtml<TinyHtmlT extends ConstructorElValues | ConstructorElValu
|
|
|
2358
2358
|
* @template {TinyElement|TinyElement[]} T
|
|
2359
2359
|
* @param {T} el - Target element(s).
|
|
2360
2360
|
* @param {string|Record<string, string|null>} name - Attribute name or an object of attributes.
|
|
2361
|
-
* @param {string|null} [value=null] - Attribute value (ignored if "name" is an object).
|
|
2361
|
+
* @param {string|number|null} [value=null] - Attribute value (ignored if "name" is an object).
|
|
2362
2362
|
* @returns {T}
|
|
2363
2363
|
*/
|
|
2364
|
-
static setAttr<T extends TinyElement | TinyElement[]>(el: T, name: string | Record<string, string | null>, value?: string | null): T;
|
|
2364
|
+
static setAttr<T extends TinyElement | TinyElement[]>(el: T, name: string | Record<string, string | null>, value?: string | number | null): T;
|
|
2365
2365
|
/**
|
|
2366
2366
|
* Remove attribute(s) from an element.
|
|
2367
2367
|
* @template {TinyElement|TinyElement[]} T
|
|
@@ -2409,6 +2409,23 @@ declare class TinyHtml<TinyHtmlT extends ConstructorElValues | ConstructorElValu
|
|
|
2409
2409
|
* @returns {T}
|
|
2410
2410
|
*/
|
|
2411
2411
|
static toggleProp<T extends TinyElement | TinyElement[]>(el: T, name: string | string[], force?: boolean): T;
|
|
2412
|
+
/**
|
|
2413
|
+
* Get properties on an element.
|
|
2414
|
+
*
|
|
2415
|
+
* @param {TinyHtmlElement} el - Target element.
|
|
2416
|
+
* @param {string} name - Property name.
|
|
2417
|
+
* @returns {any} - Property value if getting, otherwise `undefined`.
|
|
2418
|
+
*/
|
|
2419
|
+
static prop(el: TinyHtmlElement, name: string): any;
|
|
2420
|
+
/**
|
|
2421
|
+
* Set a property on elements.
|
|
2422
|
+
* @template {TinyElement|TinyElement[]} T
|
|
2423
|
+
* @param {T} el - Target element(s).
|
|
2424
|
+
* @param {string} name - Property name.
|
|
2425
|
+
* @param {any} value - Value to set.
|
|
2426
|
+
* @returns {T}
|
|
2427
|
+
*/
|
|
2428
|
+
static setProp<T extends TinyElement | TinyElement[]>(el: T, name: string, value: any): T;
|
|
2412
2429
|
/**
|
|
2413
2430
|
* Removes an element from the DOM.
|
|
2414
2431
|
* @template {TinyElement|TinyElement[]} T
|
|
@@ -2583,6 +2600,12 @@ declare class TinyHtml<TinyHtmlT extends ConstructorElValues | ConstructorElValu
|
|
|
2583
2600
|
* @returns {TinyHtml<HTMLCollectionOf<Element>>} An array of TinyHtml instances wrapping the found elements.
|
|
2584
2601
|
*/
|
|
2585
2602
|
getElementsByTagNameNS(localName: string, namespaceURI?: string | null): TinyHtml<HTMLCollectionOf<Element>>;
|
|
2603
|
+
/**
|
|
2604
|
+
* Returns the current targets held by this instance.
|
|
2605
|
+
*
|
|
2606
|
+
* @returns {ConstructorElValues[]} - The instance's targets element.
|
|
2607
|
+
*/
|
|
2608
|
+
get elements(): ConstructorElValues[];
|
|
2586
2609
|
/**
|
|
2587
2610
|
* Iterates over all elements, executing the provided callback on each.
|
|
2588
2611
|
* @param {(element: TinyHtmlAny, index: number, items: TinyHtmlAny[]) => void} callback - Function invoked for each element.
|
|
@@ -2611,6 +2634,7 @@ declare class TinyHtml<TinyHtmlT extends ConstructorElValues | ConstructorElValu
|
|
|
2611
2634
|
*/
|
|
2612
2635
|
exists(index: number): boolean;
|
|
2613
2636
|
/**
|
|
2637
|
+
* @deprecated Use the getter {@link elements} instead.
|
|
2614
2638
|
* Returns the current targets held by this instance.
|
|
2615
2639
|
*
|
|
2616
2640
|
* @returns {ConstructorElValues[]} - The instance's targets element.
|
|
@@ -3513,10 +3537,10 @@ declare class TinyHtml<TinyHtmlT extends ConstructorElValues | ConstructorElValu
|
|
|
3513
3537
|
/**
|
|
3514
3538
|
* Set one or multiple attributes on an element.
|
|
3515
3539
|
* @param {string|Record<string, string|null>} name - Attribute name or an object of attributes.
|
|
3516
|
-
* @param {string|null} [value=null] - Attribute value (ignored if "name" is an object).
|
|
3540
|
+
* @param {string|number|null} [value=null] - Attribute value (ignored if "name" is an object).
|
|
3517
3541
|
* @returns {this}
|
|
3518
3542
|
*/
|
|
3519
|
-
setAttr(name: string | Record<string, string | null>, value?: string | null): this;
|
|
3543
|
+
setAttr(name: string | Record<string, string | null>, value?: string | number | null): this;
|
|
3520
3544
|
/**
|
|
3521
3545
|
* Remove attribute(s) from an element.
|
|
3522
3546
|
* @param {string} name Space-separated list of attributes.
|
|
@@ -3554,6 +3578,20 @@ declare class TinyHtml<TinyHtmlT extends ConstructorElValues | ConstructorElValu
|
|
|
3554
3578
|
* @returns {this}
|
|
3555
3579
|
*/
|
|
3556
3580
|
toggleProp(name: string | string[], force?: boolean): this;
|
|
3581
|
+
/**
|
|
3582
|
+
* Get properties on an element.
|
|
3583
|
+
*
|
|
3584
|
+
* @param {string} name - Property name.
|
|
3585
|
+
* @returns {any} - Property value if getting, otherwise `undefined`.
|
|
3586
|
+
*/
|
|
3587
|
+
prop(name: string): any;
|
|
3588
|
+
/**
|
|
3589
|
+
* Set a property on this element.
|
|
3590
|
+
* @param {string} name - Property name.
|
|
3591
|
+
* @param {any} value - Value to set.
|
|
3592
|
+
* @returns {this} The same element.
|
|
3593
|
+
*/
|
|
3594
|
+
setProp(name: string, value: any): this;
|
|
3557
3595
|
/**
|
|
3558
3596
|
* Removes the element from the DOM.
|
|
3559
3597
|
* @returns {this}
|
|
@@ -558,7 +558,7 @@ class TinyHtml {
|
|
|
558
558
|
* ```
|
|
559
559
|
*
|
|
560
560
|
* @param {string} tagName - The HTML tag name (e.g., 'div', 'span', 'button').
|
|
561
|
-
* @param {Record<string, string|null>} [attrs] - Optional key-value pairs representing HTML attributes.
|
|
561
|
+
* @param {Record<string, string|number|null>} [attrs] - Optional key-value pairs representing HTML attributes.
|
|
562
562
|
* If the value is `null`, the attribute will still be set with an empty value.
|
|
563
563
|
* @returns {TinyHtml<HTMLElement>} - A new instance of TinyHtml representing the created element.
|
|
564
564
|
* @throws {TypeError} - If `tagName` is not a string, or `attrs` is not a plain object when defined.
|
|
@@ -734,13 +734,21 @@ class TinyHtml {
|
|
|
734
734
|
}
|
|
735
735
|
//////////////////////////////////////////////////////////////////
|
|
736
736
|
// TITLE: Element getter
|
|
737
|
+
/**
|
|
738
|
+
* Returns the current targets held by this instance.
|
|
739
|
+
*
|
|
740
|
+
* @returns {ConstructorElValues[]} - The instance's targets element.
|
|
741
|
+
*/
|
|
742
|
+
get elements() {
|
|
743
|
+
return [...this.#el];
|
|
744
|
+
}
|
|
737
745
|
/**
|
|
738
746
|
* Iterates over all elements, executing the provided callback on each.
|
|
739
747
|
* @param {(element: TinyHtmlAny, index: number, items: TinyHtmlAny[]) => void} callback - Function invoked for each element.
|
|
740
748
|
* @returns {this} The current instance for chaining.
|
|
741
749
|
*/
|
|
742
750
|
forEach(callback) {
|
|
743
|
-
const elems = this.
|
|
751
|
+
const elems = this.elements.map((el, index) => this.extract(index));
|
|
744
752
|
for (const index in elems)
|
|
745
753
|
callback(elems[index], Number(index), elems);
|
|
746
754
|
return this;
|
|
@@ -785,6 +793,7 @@ class TinyHtml {
|
|
|
785
793
|
return true;
|
|
786
794
|
}
|
|
787
795
|
/**
|
|
796
|
+
* @deprecated Use the getter {@link elements} instead.
|
|
788
797
|
* Returns the current targets held by this instance.
|
|
789
798
|
*
|
|
790
799
|
* @returns {ConstructorElValues[]} - The instance's targets element.
|
|
@@ -5859,7 +5868,7 @@ class TinyHtml {
|
|
|
5859
5868
|
* @template {TinyElement|TinyElement[]} T
|
|
5860
5869
|
* @param {T} el - Target element(s).
|
|
5861
5870
|
* @param {string|Record<string, string|null>} name - Attribute name or an object of attributes.
|
|
5862
|
-
* @param {string|null} [value=null] - Attribute value (ignored if "name" is an object).
|
|
5871
|
+
* @param {string|number|null} [value=null] - Attribute value (ignored if "name" is an object).
|
|
5863
5872
|
* @returns {T}
|
|
5864
5873
|
*/
|
|
5865
5874
|
static setAttr(el, name, value = null) {
|
|
@@ -5867,8 +5876,8 @@ class TinyHtml {
|
|
|
5867
5876
|
// Multiple attributes at once
|
|
5868
5877
|
if (typeof name === 'object' && name !== null) {
|
|
5869
5878
|
Object.entries(name).forEach(([attr, val]) => {
|
|
5870
|
-
if (val !== null && typeof val !== 'string')
|
|
5871
|
-
throw new TypeError(`The value for "${attr}" must be a string or null.`);
|
|
5879
|
+
if (val !== null && typeof val !== 'string' && typeof val !== 'number')
|
|
5880
|
+
throw new TypeError(`The value for "${attr}" must be a string/number or null.`);
|
|
5872
5881
|
elems.forEach((elem) => {
|
|
5873
5882
|
if (val === null)
|
|
5874
5883
|
elem.removeAttribute(TinyHtml.getAttrName(attr));
|
|
@@ -5894,7 +5903,7 @@ class TinyHtml {
|
|
|
5894
5903
|
/**
|
|
5895
5904
|
* Set one or multiple attributes on an element.
|
|
5896
5905
|
* @param {string|Record<string, string|null>} name - Attribute name or an object of attributes.
|
|
5897
|
-
* @param {string|null} [value=null] - Attribute value (ignored if "name" is an object).
|
|
5906
|
+
* @param {string|number|null} [value=null] - Attribute value (ignored if "name" is an object).
|
|
5898
5907
|
* @returns {this}
|
|
5899
5908
|
*/
|
|
5900
5909
|
setAttr(name, value) {
|
|
@@ -6051,6 +6060,55 @@ class TinyHtml {
|
|
|
6051
6060
|
toggleProp(name, force) {
|
|
6052
6061
|
return TinyHtml.toggleProp(this, name, force);
|
|
6053
6062
|
}
|
|
6063
|
+
/**
|
|
6064
|
+
* Get properties on an element.
|
|
6065
|
+
*
|
|
6066
|
+
* @param {TinyHtmlElement} el - Target element.
|
|
6067
|
+
* @param {string} name - Property name.
|
|
6068
|
+
* @returns {any} - Property value if getting, otherwise `undefined`.
|
|
6069
|
+
*/
|
|
6070
|
+
static prop(el, name) {
|
|
6071
|
+
if (typeof name !== 'string')
|
|
6072
|
+
throw new TypeError('Invalid arguments passed to prop(). Expected string for "name".');
|
|
6073
|
+
const elem = TinyHtml._preElem(el, 'prop');
|
|
6074
|
+
// @ts-ignore
|
|
6075
|
+
return elem[name];
|
|
6076
|
+
}
|
|
6077
|
+
/**
|
|
6078
|
+
* Get properties on an element.
|
|
6079
|
+
*
|
|
6080
|
+
* @param {string} name - Property name.
|
|
6081
|
+
* @returns {any} - Property value if getting, otherwise `undefined`.
|
|
6082
|
+
*/
|
|
6083
|
+
prop(name) {
|
|
6084
|
+
return TinyHtml.prop(this, name);
|
|
6085
|
+
}
|
|
6086
|
+
/**
|
|
6087
|
+
* Set a property on elements.
|
|
6088
|
+
* @template {TinyElement|TinyElement[]} T
|
|
6089
|
+
* @param {T} el - Target element(s).
|
|
6090
|
+
* @param {string} name - Property name.
|
|
6091
|
+
* @param {any} value - Value to set.
|
|
6092
|
+
* @returns {T}
|
|
6093
|
+
*/
|
|
6094
|
+
static setProp(el, name, value) {
|
|
6095
|
+
if (typeof name !== 'string' || name.trim() === '')
|
|
6096
|
+
throw new Error('Property name must be a non-empty string.');
|
|
6097
|
+
TinyHtml._preElems(el, 'setProp').forEach((elem) => {
|
|
6098
|
+
// @ts-ignore
|
|
6099
|
+
elem[name] = value;
|
|
6100
|
+
});
|
|
6101
|
+
return el;
|
|
6102
|
+
}
|
|
6103
|
+
/**
|
|
6104
|
+
* Set a property on this element.
|
|
6105
|
+
* @param {string} name - Property name.
|
|
6106
|
+
* @param {any} value - Value to set.
|
|
6107
|
+
* @returns {this} The same element.
|
|
6108
|
+
*/
|
|
6109
|
+
setProp(name, value) {
|
|
6110
|
+
return TinyHtml.setProp(this, name, value);
|
|
6111
|
+
}
|
|
6054
6112
|
/////////////////////////////////////////////////////
|
|
6055
6113
|
// TITLE: Remove Element
|
|
6056
6114
|
/**
|