tiny-essentials 1.20.1 → 1.20.2
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/README.md +5 -0
- package/dist/v1/TinyBasicsEs.min.js +1 -1
- package/dist/v1/TinyDragger.min.js +1 -1
- package/dist/v1/TinyEssentials.min.js +1 -1
- package/dist/v1/TinyHtml.min.js +1 -1
- package/dist/v1/TinySmartScroller.min.js +1 -1
- package/dist/v1/TinyUploadClicker.min.js +1 -1
- package/dist/v1/basics/html.cjs +33 -2
- package/dist/v1/basics/html.d.mts +14 -4
- package/dist/v1/basics/html.mjs +27 -2
- package/dist/v1/basics/index.cjs +2 -0
- package/dist/v1/basics/index.d.mts +3 -1
- package/dist/v1/basics/index.mjs +3 -3
- package/dist/v1/basics/simpleMath.cjs +23 -4
- package/dist/v1/basics/simpleMath.d.mts +18 -4
- package/dist/v1/basics/simpleMath.mjs +22 -4
- package/dist/v1/index.cjs +2 -0
- package/dist/v1/index.d.mts +3 -1
- package/dist/v1/index.mjs +3 -3
- package/dist/v1/libs/TinyHtml.cjs +210 -4
- package/dist/v1/libs/TinyHtml.d.mts +109 -3
- package/dist/v1/libs/TinyHtml.mjs +184 -4
- package/docs/v1/basics/html.md +78 -22
- package/docs/v1/basics/simpleMath.md +22 -4
- package/docs/v1/libs/TinyHtml.md +140 -0
- package/package.json +1 -1
|
@@ -153,6 +153,12 @@ export type SetValueList = SetValueBase | SetValueBase[];
|
|
|
153
153
|
* Includes common input types used in forms.
|
|
154
154
|
*/
|
|
155
155
|
export type InputElement = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement | HTMLOptionElement;
|
|
156
|
+
/**
|
|
157
|
+
* Represents a parsed HTML element in JSON-like array form.
|
|
158
|
+
*/
|
|
159
|
+
export type HtmlParsed = [tagName: string, // The tag name of the element (e.g., 'div', 'img')
|
|
160
|
+
attributes: Record<string, string>, // All element attributes as key-value pairs
|
|
161
|
+
...children: (string | HtmlParsed)[]];
|
|
156
162
|
/**
|
|
157
163
|
* Possible directions from which a collision was detected and locked.
|
|
158
164
|
*
|
|
@@ -183,6 +189,15 @@ export type InputElement = HTMLInputElement | HTMLSelectElement | HTMLTextAreaEl
|
|
|
183
189
|
*
|
|
184
190
|
* @typedef {HTMLInputElement|HTMLSelectElement|HTMLTextAreaElement|HTMLOptionElement} InputElement
|
|
185
191
|
*/
|
|
192
|
+
/**
|
|
193
|
+
* Represents a parsed HTML element in JSON-like array form.
|
|
194
|
+
*
|
|
195
|
+
* @typedef {[
|
|
196
|
+
* tagName: string, // The tag name of the element (e.g., 'div', 'img')
|
|
197
|
+
* attributes: Record<string, string>, // All element attributes as key-value pairs
|
|
198
|
+
* ...children: (string | HtmlParsed)[] // Text or nested elements
|
|
199
|
+
* ]} HtmlParsed
|
|
200
|
+
*/
|
|
186
201
|
/**
|
|
187
202
|
* TinyHtml is a utility class that provides static and instance-level methods
|
|
188
203
|
* for precise dimension and position computations on HTML elements.
|
|
@@ -236,6 +251,15 @@ declare class TinyHtml {
|
|
|
236
251
|
*
|
|
237
252
|
* @typedef {HTMLInputElement|HTMLSelectElement|HTMLTextAreaElement|HTMLOptionElement} InputElement
|
|
238
253
|
*/
|
|
254
|
+
/**
|
|
255
|
+
* Represents a parsed HTML element in JSON-like array form.
|
|
256
|
+
*
|
|
257
|
+
* @typedef {[
|
|
258
|
+
* tagName: string, // The tag name of the element (e.g., 'div', 'img')
|
|
259
|
+
* attributes: Record<string, string>, // All element attributes as key-value pairs
|
|
260
|
+
* ...children: (string | HtmlParsed)[] // Text or nested elements
|
|
261
|
+
* ]} HtmlParsed
|
|
262
|
+
*/
|
|
239
263
|
/**
|
|
240
264
|
* TinyHtml is a utility class that provides static and instance-level methods
|
|
241
265
|
* for precise dimension and position computations on HTML elements.
|
|
@@ -248,6 +272,15 @@ declare class TinyHtml {
|
|
|
248
272
|
*/
|
|
249
273
|
rect1: TinyCollision.ObjRect, rect2: TinyCollision.ObjRect) => string | null;
|
|
250
274
|
getElsPerfColliding: (rect1: TinyCollision.ObjRect /**
|
|
275
|
+
* Represents a parsed HTML element in JSON-like array form.
|
|
276
|
+
*
|
|
277
|
+
* @typedef {[
|
|
278
|
+
* tagName: string, // The tag name of the element (e.g., 'div', 'img')
|
|
279
|
+
* attributes: Record<string, string>, // All element attributes as key-value pairs
|
|
280
|
+
* ...children: (string | HtmlParsed)[] // Text or nested elements
|
|
281
|
+
* ]} HtmlParsed
|
|
282
|
+
*/
|
|
283
|
+
/**
|
|
251
284
|
* TinyHtml is a utility class that provides static and instance-level methods
|
|
252
285
|
* for precise dimension and position computations on HTML elements.
|
|
253
286
|
* It mimics some jQuery functionalities while using native browser APIs.
|
|
@@ -256,7 +289,8 @@ declare class TinyHtml {
|
|
|
256
289
|
* and offset utilities. This class serves as a lightweight alternative using modern DOM APIs.
|
|
257
290
|
*
|
|
258
291
|
* @class
|
|
259
|
-
|
|
292
|
+
*/
|
|
293
|
+
, rect2: TinyCollision.ObjRect) => "top" | "bottom" | "left" | "right" | null;
|
|
260
294
|
getElsCollOverlap: (rect1: TinyCollision.ObjRect, rect2: TinyCollision.ObjRect) => {
|
|
261
295
|
overlapLeft: number;
|
|
262
296
|
overlapRight: number;
|
|
@@ -277,6 +311,72 @@ declare class TinyHtml {
|
|
|
277
311
|
y: number;
|
|
278
312
|
};
|
|
279
313
|
};
|
|
314
|
+
/**
|
|
315
|
+
* Fetches an HTML file from the given URL, parses it to JSON.
|
|
316
|
+
*
|
|
317
|
+
* @param {string | URL | globalThis.Request} url - The URL of the HTML file.
|
|
318
|
+
* @param {RequestInit} [ops] - Optional fetch configuration (e.g., method, headers, cache, etc).
|
|
319
|
+
* @returns {Promise<HtmlParsed[]>} A promise that resolves with the parsed JSON representation of the HTML structure.
|
|
320
|
+
*/
|
|
321
|
+
static fetchHtmlFile(url: string | URL | globalThis.Request, ops?: RequestInit): Promise<HtmlParsed[]>;
|
|
322
|
+
/**
|
|
323
|
+
* Fetches an HTML file from the given URL, parses it to JSON, then converts it to DOM nodes.
|
|
324
|
+
*
|
|
325
|
+
* @param {string} url - The URL of the HTML file.
|
|
326
|
+
* @param {RequestInit} [ops] - Optional fetch configuration (e.g., method, headers, cache, etc).
|
|
327
|
+
* @returns {Promise<(HTMLElement|Text)[]>} A promise that resolves with the DOM nodes.
|
|
328
|
+
*/
|
|
329
|
+
static fetchHtmlNodes(url: string, ops?: RequestInit): Promise<(HTMLElement | Text)[]>;
|
|
330
|
+
/**
|
|
331
|
+
* Fetches an HTML file from the given URL, parses it to JSON, then converts it to TinyHtml instances.
|
|
332
|
+
*
|
|
333
|
+
* @param {string} url - The URL of the HTML file.
|
|
334
|
+
* @param {RequestInit} [ops] - Optional fetch configuration (e.g., method, headers, cache, etc).
|
|
335
|
+
* @returns {Promise<TinyHtml[]>} A promise that resolves with the TinyHtml instances.
|
|
336
|
+
*/
|
|
337
|
+
static fetchHtmlTinyElems(url: string, ops?: RequestInit): Promise<TinyHtml[]>;
|
|
338
|
+
/**
|
|
339
|
+
* Converts the content of a <template> to an array of HtmlParsed.
|
|
340
|
+
*
|
|
341
|
+
* @param {HTMLTemplateElement} nodes
|
|
342
|
+
* @returns {HtmlParsed[]}
|
|
343
|
+
*/
|
|
344
|
+
static templateToJson(nodes: HTMLTemplateElement): HtmlParsed[];
|
|
345
|
+
/**
|
|
346
|
+
* Converts the content of a <template> to real DOM nodes.
|
|
347
|
+
*
|
|
348
|
+
* @param {HTMLTemplateElement} nodes
|
|
349
|
+
* @returns {(Element|Text)[]}
|
|
350
|
+
*/
|
|
351
|
+
static templateToNodes(nodes: HTMLTemplateElement): (Element | Text)[];
|
|
352
|
+
/**
|
|
353
|
+
* Converts the content of a <template> to an array of TinyHtml elements.
|
|
354
|
+
*
|
|
355
|
+
* @param {HTMLTemplateElement} nodes
|
|
356
|
+
* @returns {TinyHtml[]}
|
|
357
|
+
*/
|
|
358
|
+
static templateToTinyElems(nodes: HTMLTemplateElement): TinyHtml[];
|
|
359
|
+
/**
|
|
360
|
+
* Parses a full HTML string into a JSON-like structure.
|
|
361
|
+
*
|
|
362
|
+
* @param {string} htmlString - Full HTML markup as a string.
|
|
363
|
+
* @returns {HtmlParsed[]} An array of parsed HTML elements in structured format.
|
|
364
|
+
*/
|
|
365
|
+
static htmlToJson(htmlString: string): HtmlParsed[];
|
|
366
|
+
/**
|
|
367
|
+
* Converts a JSON-like HTML structure back to DOM Elements.
|
|
368
|
+
*
|
|
369
|
+
* @param {HtmlParsed[]} jsonArray - Parsed JSON format of HTML.
|
|
370
|
+
* @returns {(HTMLElement|Text)[]} List of DOM nodes.
|
|
371
|
+
*/
|
|
372
|
+
static jsonToNodes(jsonArray: HtmlParsed[]): (HTMLElement | Text)[];
|
|
373
|
+
/**
|
|
374
|
+
* Converts a JSON-like HTML structure back to TinyHtml instances.
|
|
375
|
+
*
|
|
376
|
+
* @param {HtmlParsed[]} jsonArray - Parsed JSON format of HTML.
|
|
377
|
+
* @returns {TinyHtml[]} List of TinyHtml instances.
|
|
378
|
+
*/
|
|
379
|
+
static jsonToTinyElems(jsonArray: HtmlParsed[]): TinyHtml[];
|
|
280
380
|
/**
|
|
281
381
|
* Creates a new TinyHtml element from a tag name and optional attributes.
|
|
282
382
|
*
|
|
@@ -576,10 +676,10 @@ declare class TinyHtml {
|
|
|
576
676
|
* This ensures consistent access to methods of the `TinyHtml` class regardless
|
|
577
677
|
* of the input form.
|
|
578
678
|
*
|
|
579
|
-
* @param {TinyElement|TinyElement[]} elems - A single element or an array of elements (DOM or TinyHtml).
|
|
679
|
+
* @param {TinyElement|Text|(TinyElement|Text)[]} elems - A single element or an array of elements (DOM or TinyHtml).
|
|
580
680
|
* @returns {TinyHtml[]} An array of TinyHtml instances corresponding to the input elements.
|
|
581
681
|
*/
|
|
582
|
-
static toTinyElm(elems: TinyElement | TinyElement[]): TinyHtml[];
|
|
682
|
+
static toTinyElm(elems: TinyElement | Text | (TinyElement | Text)[]): TinyHtml[];
|
|
583
683
|
/**
|
|
584
684
|
* Extracts native `Element` instances from one or more elements,
|
|
585
685
|
* which can be either raw DOM elements or wrapped in `TinyHtml`.
|
|
@@ -2197,6 +2297,12 @@ declare class TinyHtml {
|
|
|
2197
2297
|
* @returns {TinyNode|TinyNode[]}
|
|
2198
2298
|
*/
|
|
2199
2299
|
replaceAll(targets: TinyNode | TinyNode[]): TinyNode | TinyNode[];
|
|
2300
|
+
/**
|
|
2301
|
+
* Returns the number of elements currently stored in the internal element list.
|
|
2302
|
+
*
|
|
2303
|
+
* @returns {number} The total count of elements.
|
|
2304
|
+
*/
|
|
2305
|
+
get size(): number;
|
|
2200
2306
|
/**
|
|
2201
2307
|
* Returns the full computed CSS styles for the given element.
|
|
2202
2308
|
*
|
|
@@ -186,6 +186,15 @@ const __elemCollision = {
|
|
|
186
186
|
*
|
|
187
187
|
* @typedef {HTMLInputElement|HTMLSelectElement|HTMLTextAreaElement|HTMLOptionElement} InputElement
|
|
188
188
|
*/
|
|
189
|
+
/**
|
|
190
|
+
* Represents a parsed HTML element in JSON-like array form.
|
|
191
|
+
*
|
|
192
|
+
* @typedef {[
|
|
193
|
+
* tagName: string, // The tag name of the element (e.g., 'div', 'img')
|
|
194
|
+
* attributes: Record<string, string>, // All element attributes as key-value pairs
|
|
195
|
+
* ...children: (string | HtmlParsed)[] // Text or nested elements
|
|
196
|
+
* ]} HtmlParsed
|
|
197
|
+
*/
|
|
189
198
|
/**
|
|
190
199
|
* TinyHtml is a utility class that provides static and instance-level methods
|
|
191
200
|
* for precise dimension and position computations on HTML elements.
|
|
@@ -199,6 +208,165 @@ const __elemCollision = {
|
|
|
199
208
|
class TinyHtml {
|
|
200
209
|
/** @typedef {import('../basics/collision.mjs').ObjRect} ObjRect */
|
|
201
210
|
static Utils = { ...TinyCollision };
|
|
211
|
+
/**
|
|
212
|
+
* Fetches an HTML file from the given URL, parses it to JSON.
|
|
213
|
+
*
|
|
214
|
+
* @param {string | URL | globalThis.Request} url - The URL of the HTML file.
|
|
215
|
+
* @param {RequestInit} [ops] - Optional fetch configuration (e.g., method, headers, cache, etc).
|
|
216
|
+
* @returns {Promise<HtmlParsed[]>} A promise that resolves with the parsed JSON representation of the HTML structure.
|
|
217
|
+
*/
|
|
218
|
+
static async fetchHtmlFile(url, ops = { method: 'GET' }) {
|
|
219
|
+
const res = await fetch(url, ops);
|
|
220
|
+
const contentType = res.headers.get('Content-Type') || '';
|
|
221
|
+
if (!res.ok)
|
|
222
|
+
throw new Error(`Failed to fetch: ${res.status} ${res.statusText}`);
|
|
223
|
+
// Only accept HTML responses
|
|
224
|
+
if (!contentType.includes('text/html')) {
|
|
225
|
+
throw new Error(`Invalid content type: ${contentType} (expected text/html)`);
|
|
226
|
+
}
|
|
227
|
+
const html = await res.text();
|
|
228
|
+
return TinyHtml.htmlToJson(html);
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Fetches an HTML file from the given URL, parses it to JSON, then converts it to DOM nodes.
|
|
232
|
+
*
|
|
233
|
+
* @param {string} url - The URL of the HTML file.
|
|
234
|
+
* @param {RequestInit} [ops] - Optional fetch configuration (e.g., method, headers, cache, etc).
|
|
235
|
+
* @returns {Promise<(HTMLElement|Text)[]>} A promise that resolves with the DOM nodes.
|
|
236
|
+
*/
|
|
237
|
+
static async fetchHtmlNodes(url, ops) {
|
|
238
|
+
const json = await TinyHtml.fetchHtmlFile(url, ops);
|
|
239
|
+
return TinyHtml.jsonToNodes(json);
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Fetches an HTML file from the given URL, parses it to JSON, then converts it to TinyHtml instances.
|
|
243
|
+
*
|
|
244
|
+
* @param {string} url - The URL of the HTML file.
|
|
245
|
+
* @param {RequestInit} [ops] - Optional fetch configuration (e.g., method, headers, cache, etc).
|
|
246
|
+
* @returns {Promise<TinyHtml[]>} A promise that resolves with the TinyHtml instances.
|
|
247
|
+
*/
|
|
248
|
+
static async fetchHtmlTinyElems(url, ops) {
|
|
249
|
+
const nodes = await TinyHtml.fetchHtmlNodes(url, ops);
|
|
250
|
+
return TinyHtml.toTinyElm(nodes);
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Converts the content of a <template> to an array of HtmlParsed.
|
|
254
|
+
*
|
|
255
|
+
* @param {HTMLTemplateElement} nodes
|
|
256
|
+
* @returns {HtmlParsed[]}
|
|
257
|
+
*/
|
|
258
|
+
static templateToJson(nodes) {
|
|
259
|
+
return TinyHtml.htmlToJson([...nodes.content.childNodes]
|
|
260
|
+
.map((node) => node instanceof Element ? node.getHTML() : node instanceof Text ? node.textContent : '')
|
|
261
|
+
.join(''));
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Converts the content of a <template> to real DOM nodes.
|
|
265
|
+
*
|
|
266
|
+
* @param {HTMLTemplateElement} nodes
|
|
267
|
+
* @returns {(Element|Text)[]}
|
|
268
|
+
*/
|
|
269
|
+
static templateToNodes(nodes) {
|
|
270
|
+
/** @type {(Element|Text)[]} */
|
|
271
|
+
const result = [];
|
|
272
|
+
[...nodes.content.cloneNode(true).childNodes].map((node) => {
|
|
273
|
+
if (!(node instanceof Element) && !(node instanceof Text) && !(node instanceof Comment))
|
|
274
|
+
throw new Error(`Expected only Element nodes in <template>, but found: ${node.constructor.name}`);
|
|
275
|
+
if (!(node instanceof Comment))
|
|
276
|
+
result.push(node);
|
|
277
|
+
});
|
|
278
|
+
return result;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Converts the content of a <template> to an array of TinyHtml elements.
|
|
282
|
+
*
|
|
283
|
+
* @param {HTMLTemplateElement} nodes
|
|
284
|
+
* @returns {TinyHtml[]}
|
|
285
|
+
*/
|
|
286
|
+
static templateToTinyElems(nodes) {
|
|
287
|
+
return TinyHtml.toTinyElm(TinyHtml.templateToNodes(nodes));
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Parses a full HTML string into a JSON-like structure.
|
|
291
|
+
*
|
|
292
|
+
* @param {string} htmlString - Full HTML markup as a string.
|
|
293
|
+
* @returns {HtmlParsed[]} An array of parsed HTML elements in structured format.
|
|
294
|
+
*/
|
|
295
|
+
static htmlToJson(htmlString) {
|
|
296
|
+
const container = document.createElement('div');
|
|
297
|
+
container.innerHTML = htmlString.trim();
|
|
298
|
+
const result = [];
|
|
299
|
+
/**
|
|
300
|
+
* @param {Node} el
|
|
301
|
+
* @returns {*}
|
|
302
|
+
*/
|
|
303
|
+
const parseElement = (el) => {
|
|
304
|
+
if (el instanceof Comment)
|
|
305
|
+
return null;
|
|
306
|
+
if (el instanceof Text) {
|
|
307
|
+
const text = el.textContent?.trim();
|
|
308
|
+
return text ? text : null;
|
|
309
|
+
}
|
|
310
|
+
if (!(el instanceof Element))
|
|
311
|
+
return null;
|
|
312
|
+
const tag = el.tagName.toLowerCase();
|
|
313
|
+
/** @type {Record<string, string>} */
|
|
314
|
+
const props = {};
|
|
315
|
+
for (const attr of el.attributes) {
|
|
316
|
+
props[TinyHtml.getPropName(attr.name)] = attr.value;
|
|
317
|
+
}
|
|
318
|
+
const children = Array.from(el.childNodes).map(parseElement).filter(Boolean);
|
|
319
|
+
return children.length > 0 ? [tag, props, ...children] : [tag, props];
|
|
320
|
+
};
|
|
321
|
+
for (const child of container.childNodes) {
|
|
322
|
+
const parsed = parseElement(child);
|
|
323
|
+
if (parsed)
|
|
324
|
+
result.push(parsed);
|
|
325
|
+
}
|
|
326
|
+
container.innerHTML = '';
|
|
327
|
+
return result;
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Converts a JSON-like HTML structure back to DOM Elements.
|
|
331
|
+
*
|
|
332
|
+
* @param {HtmlParsed[]} jsonArray - Parsed JSON format of HTML.
|
|
333
|
+
* @returns {(HTMLElement|Text)[]} List of DOM nodes.
|
|
334
|
+
*/
|
|
335
|
+
static jsonToNodes(jsonArray) {
|
|
336
|
+
/**
|
|
337
|
+
* @param {HtmlParsed|string} nodeData
|
|
338
|
+
* @returns {HTMLElement|Text}
|
|
339
|
+
*/
|
|
340
|
+
const createElement = (nodeData) => {
|
|
341
|
+
if (typeof nodeData === 'string') {
|
|
342
|
+
return document.createTextNode(nodeData);
|
|
343
|
+
}
|
|
344
|
+
if (!Array.isArray(nodeData))
|
|
345
|
+
return document.createTextNode('');
|
|
346
|
+
const [tag, props, ...children] = nodeData;
|
|
347
|
+
const el = document.createElement(tag);
|
|
348
|
+
for (const [key, value] of Object.entries(props)) {
|
|
349
|
+
el.setAttribute(TinyHtml.getAttrName(key), value);
|
|
350
|
+
}
|
|
351
|
+
for (const child of children) {
|
|
352
|
+
const childEl = createElement(child);
|
|
353
|
+
if (childEl instanceof Comment)
|
|
354
|
+
continue;
|
|
355
|
+
el.appendChild(childEl);
|
|
356
|
+
}
|
|
357
|
+
return el;
|
|
358
|
+
};
|
|
359
|
+
return jsonArray.map(createElement).filter((node) => !(node instanceof Comment));
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Converts a JSON-like HTML structure back to TinyHtml instances.
|
|
363
|
+
*
|
|
364
|
+
* @param {HtmlParsed[]} jsonArray - Parsed JSON format of HTML.
|
|
365
|
+
* @returns {TinyHtml[]} List of TinyHtml instances.
|
|
366
|
+
*/
|
|
367
|
+
static jsonToTinyElems(jsonArray) {
|
|
368
|
+
return TinyHtml.toTinyElm(TinyHtml.jsonToNodes(jsonArray));
|
|
369
|
+
}
|
|
202
370
|
/**
|
|
203
371
|
* Creates a new TinyHtml element from a tag name and optional attributes.
|
|
204
372
|
*
|
|
@@ -440,7 +608,8 @@ class TinyHtml {
|
|
|
440
608
|
_getElement(where, index) {
|
|
441
609
|
if (!(this.#el[index] instanceof Element) &&
|
|
442
610
|
!(this.#el[index] instanceof Window) &&
|
|
443
|
-
!(this.#el[index] instanceof Document)
|
|
611
|
+
!(this.#el[index] instanceof Document) &&
|
|
612
|
+
!(this.#el[index] instanceof Text))
|
|
444
613
|
throw new Error(`[TinyHtml] Invalid Element in ${where}().`);
|
|
445
614
|
return this.#el[index];
|
|
446
615
|
}
|
|
@@ -452,7 +621,10 @@ class TinyHtml {
|
|
|
452
621
|
* @readonly
|
|
453
622
|
*/
|
|
454
623
|
_getElements(where) {
|
|
455
|
-
if (!this.#el.every((el) => el instanceof Element ||
|
|
624
|
+
if (!this.#el.every((el) => el instanceof Element ||
|
|
625
|
+
el instanceof Window ||
|
|
626
|
+
el instanceof Document ||
|
|
627
|
+
el instanceof Text))
|
|
456
628
|
throw new Error(`[TinyHtml] Invalid Element in ${where}().`);
|
|
457
629
|
return [...this.#el];
|
|
458
630
|
}
|
|
@@ -751,11 +923,11 @@ class TinyHtml {
|
|
|
751
923
|
* This ensures consistent access to methods of the `TinyHtml` class regardless
|
|
752
924
|
* of the input form.
|
|
753
925
|
*
|
|
754
|
-
* @param {TinyElement|TinyElement[]} elems - A single element or an array of elements (DOM or TinyHtml).
|
|
926
|
+
* @param {TinyElement|Text|(TinyElement|Text)[]} elems - A single element or an array of elements (DOM or TinyHtml).
|
|
755
927
|
* @returns {TinyHtml[]} An array of TinyHtml instances corresponding to the input elements.
|
|
756
928
|
*/
|
|
757
929
|
static toTinyElm(elems) {
|
|
758
|
-
/** @param {TinyElement[]} item */
|
|
930
|
+
/** @param {(TinyElement|Text)[]} item */
|
|
759
931
|
const checkElement = (item) => item.map((elem) => (!(elem instanceof TinyHtml) ? new TinyHtml(elem) : elem));
|
|
760
932
|
if (!Array.isArray(elems))
|
|
761
933
|
return checkElement([elems]);
|
|
@@ -1623,6 +1795,14 @@ class TinyHtml {
|
|
|
1623
1795
|
* @type {ConstructorElValues[]}
|
|
1624
1796
|
*/
|
|
1625
1797
|
#el;
|
|
1798
|
+
/**
|
|
1799
|
+
* Returns the number of elements currently stored in the internal element list.
|
|
1800
|
+
*
|
|
1801
|
+
* @returns {number} The total count of elements.
|
|
1802
|
+
*/
|
|
1803
|
+
get size() {
|
|
1804
|
+
return this.#el.length;
|
|
1805
|
+
}
|
|
1626
1806
|
/**
|
|
1627
1807
|
* Creates an instance of TinyHtml for a specific Element.
|
|
1628
1808
|
* Useful when you want to operate repeatedly on the same element using instance methods.
|
package/docs/v1/basics/html.md
CHANGED
|
@@ -142,11 +142,11 @@ saveJsonFile('yasmin.json', data);
|
|
|
142
142
|
|
|
143
143
|
### 🌐 `fetchTemplate(...)`
|
|
144
144
|
|
|
145
|
-
Loads data from a remote URL using Fetch API, with support for custom HTTP methods, retries, timeouts, headers, and even external abort controllers.
|
|
145
|
+
Loads data from a remote URL using the Fetch API, with support for custom HTTP methods, retries, timeouts, headers, and even external abort controllers.
|
|
146
146
|
|
|
147
147
|
#### 📥 Parameters
|
|
148
148
|
|
|
149
|
-
* `url` *(string)*: The full URL to fetch
|
|
149
|
+
* `url` *(string)*: The full URL to fetch from (must start with `http://`, `https://`, `/`, `./`, or `../`).
|
|
150
150
|
* `options` *(object)* *(optional)*:
|
|
151
151
|
|
|
152
152
|
* `signal` *(`AbortSignal` | `null`)*: Custom abort signal.
|
|
@@ -156,42 +156,88 @@ Loads data from a remote URL using Fetch API, with support for custom HTTP metho
|
|
|
156
156
|
* `headers` *(object)*: Additional headers to include in the request.
|
|
157
157
|
* `body` *(object)*: Request body. If the value is a plain object, it will be automatically stringified as JSON.
|
|
158
158
|
|
|
159
|
-
#### `fetchJson(url, options?)
|
|
159
|
+
#### `fetchJson(url, options?)`
|
|
160
160
|
|
|
161
|
-
Loads and parses a JSON.
|
|
161
|
+
Loads and parses a remote JSON file.
|
|
162
162
|
|
|
163
|
-
*
|
|
164
|
-
* `options` *(object)* *(optional)*:
|
|
163
|
+
* **Parameters**:
|
|
165
164
|
|
|
166
|
-
|
|
165
|
+
* `url` *(string)*: URL to fetch the JSON from.
|
|
166
|
+
* `options` *(object)* *(optional)*: Same structure as `fetchTemplate`.
|
|
167
167
|
|
|
168
|
-
|
|
168
|
+
* **Returns**:
|
|
169
|
+
`Promise<any[] | Record<string | number | symbol, unknown>>` — The parsed JSON data.
|
|
169
170
|
|
|
170
|
-
*
|
|
171
|
-
* `allowedMimeTypes` Optional list of accepted MIME types (e.g., ['image/jpeg']).
|
|
172
|
-
* `options` *(object)* *(optional)*:
|
|
171
|
+
* **Throws**:
|
|
173
172
|
|
|
174
|
-
|
|
173
|
+
* `Error` if the fetch fails, times out, or returns invalid JSON.
|
|
174
|
+
* `Error` if the `Content-Type` is not `application/json`.
|
|
175
175
|
|
|
176
|
-
|
|
176
|
+
---
|
|
177
177
|
|
|
178
|
-
|
|
179
|
-
* Retry logic is **disabled**
|
|
180
|
-
* Abortion is handled externally
|
|
178
|
+
#### `fetchBlob(url, allowedMimeTypes?, options?)`
|
|
181
179
|
|
|
182
|
-
|
|
180
|
+
Loads a remote file as a Blob object.
|
|
181
|
+
|
|
182
|
+
* **Parameters**:
|
|
183
|
+
|
|
184
|
+
* `url` *(string)*: URL of the remote file.
|
|
185
|
+
* `allowedMimeTypes` *(string\[])* *(optional)*: List of accepted MIME types.
|
|
186
|
+
* `options` *(object)* *(optional)*: Same structure as `fetchTemplate`.
|
|
187
|
+
|
|
188
|
+
* **Returns**:
|
|
189
|
+
`Promise<Blob>`
|
|
190
|
+
|
|
191
|
+
* **Throws**:
|
|
192
|
+
|
|
193
|
+
* `Error` if fetch fails or the MIME type is not allowed.
|
|
194
|
+
* `Error` if the response is not OK.
|
|
195
|
+
|
|
196
|
+
---
|
|
197
|
+
|
|
198
|
+
#### `fetchText(url, allowedMimeTypes?, options?)`
|
|
199
|
+
|
|
200
|
+
Loads a remote file as **plain text** using the Fetch API.
|
|
201
|
+
|
|
202
|
+
* **Parameters**:
|
|
203
|
+
|
|
204
|
+
* `url` *(string)*: Full URL of the file to fetch.
|
|
205
|
+
* `allowedMimeTypes` *(string\[])* *(optional)*: List of accepted MIME types (e.g., `['text/plain']`).
|
|
206
|
+
* `options` *(object)* *(optional)*: Same structure as `fetchTemplate`.
|
|
207
|
+
|
|
208
|
+
* **Returns**:
|
|
209
|
+
`Promise<string>` — The content of the file as a text string.
|
|
210
|
+
|
|
211
|
+
* **Throws**:
|
|
183
212
|
|
|
184
|
-
* `
|
|
213
|
+
* `Error` if the fetch fails or the response is not OK.
|
|
214
|
+
* `Error` if the MIME type is not in the allowed list (when provided).
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
#### 📤 Returns (for all helpers)
|
|
219
|
+
|
|
220
|
+
* `Promise<any>` — Depends on the function:
|
|
221
|
+
|
|
222
|
+
* `fetchJson`: Parsed JSON
|
|
223
|
+
* `fetchBlob`: File as a Blob
|
|
224
|
+
* `fetchText`: File as plain text
|
|
225
|
+
|
|
226
|
+
---
|
|
185
227
|
|
|
186
228
|
#### ⚠️ Throws
|
|
187
229
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
* `Error` if the
|
|
230
|
+
All functions may throw the following:
|
|
231
|
+
|
|
232
|
+
* `Error` if the request fails or the response times out.
|
|
233
|
+
* `Error` if `Content-Type` does not match expected type (JSON, Blob, Text, etc).
|
|
234
|
+
* `Error` if response is malformed or rejected by MIME type filter.
|
|
235
|
+
|
|
236
|
+
---
|
|
191
237
|
|
|
192
238
|
#### 🧠 Tip
|
|
193
239
|
|
|
194
|
-
|
|
240
|
+
Using a custom `signal` disables both `timeout` and `retries`. This is useful when you want to handle cancellation yourself, like in user interfaces or abortable workflows.
|
|
195
241
|
|
|
196
242
|
#### 🧪 Example
|
|
197
243
|
|
|
@@ -210,6 +256,16 @@ const data = await fetchJson('https://api.example.com/data', {
|
|
|
210
256
|
});
|
|
211
257
|
```
|
|
212
258
|
|
|
259
|
+
```js
|
|
260
|
+
const text = await fetchText('/example.txt', ['text/plain'], {
|
|
261
|
+
timeout: 3000,
|
|
262
|
+
});
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
```js
|
|
266
|
+
const blob = await fetchBlob('/image.jpg', ['image/jpeg']);
|
|
267
|
+
```
|
|
268
|
+
|
|
213
269
|
---
|
|
214
270
|
|
|
215
271
|
### 📦 `HtmlElBoxSides` Type
|
|
@@ -36,19 +36,37 @@ ruleOfThree(2, 6, 3, true); // → 4
|
|
|
36
36
|
|
|
37
37
|
### 💯 `getSimplePerc(price, percentage)`
|
|
38
38
|
|
|
39
|
-
**Calculates the percentage of a
|
|
39
|
+
**Calculates the actual value that corresponds to a percentage of a base number.**
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
Unlike `getPercentage`, which tells how much something represents in percent,
|
|
42
|
+
this function tells how much a given percentage *is worth* in actual value.
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
- **`price`**: The base number to apply the percentage to.
|
|
45
|
+
- **`percentage`**: The percentage to calculate from the base.
|
|
45
46
|
|
|
47
|
+
#### Example:
|
|
46
48
|
```js
|
|
47
49
|
getSimplePerc(200, 15); // → 30
|
|
48
50
|
```
|
|
49
51
|
|
|
50
52
|
---
|
|
51
53
|
|
|
54
|
+
### 📊 `getPercentage(part, total)`
|
|
55
|
+
|
|
56
|
+
**Calculates how much percent a partial value represents of the total value.**
|
|
57
|
+
|
|
58
|
+
This function is useful when you want to know what fraction of the total a value is, in percentage form.
|
|
59
|
+
|
|
60
|
+
- **`part`**: The partial value to compare.
|
|
61
|
+
- **`total`**: The total or maximum value.
|
|
62
|
+
|
|
63
|
+
#### Example:
|
|
64
|
+
```js
|
|
65
|
+
getPercentage(5, 100); // → 5
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
52
70
|
### 🎂 `getAge(timeData, now)`
|
|
53
71
|
|
|
54
72
|
**Calculates the age based on a birthdate.**
|