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
|
@@ -49,21 +49,31 @@ export function saveJsonFile(filename: string, data: any, spaces?: number): void
|
|
|
49
49
|
* Loads and parses a JSON from a remote URL using Fetch API.
|
|
50
50
|
*
|
|
51
51
|
* @param {string} url - The full URL to fetch JSON from.
|
|
52
|
-
* @param {
|
|
52
|
+
* @param {FetchTemplateOptions} [options] - Optional settings.
|
|
53
53
|
* @returns {Promise<any[] | Record<string | number | symbol, unknown>>} Parsed JSON object.
|
|
54
54
|
* @throws {Error} Throws if fetch fails, times out, or returns invalid JSON.
|
|
55
55
|
*/
|
|
56
|
-
export function fetchJson(url: string, options?:
|
|
56
|
+
export function fetchJson(url: string, options?: FetchTemplateOptions): Promise<any[] | Record<string | number | symbol, unknown>>;
|
|
57
57
|
/**
|
|
58
58
|
* Loads a remote file as a Blob using Fetch API.
|
|
59
59
|
*
|
|
60
60
|
* @param {string} url - The full URL to fetch the file from.
|
|
61
|
-
* @param {
|
|
61
|
+
* @param {FetchTemplateOptions} [options] - Optional fetch options.
|
|
62
62
|
* @param {string[]} [allowedMimeTypes] - Optional list of accepted MIME types (e.g., ['image/jpeg']).
|
|
63
63
|
* @returns {Promise<Blob>} - The fetched file as a Blob.
|
|
64
64
|
* @throws {Error} Throws if fetch fails, response is not ok, or MIME type is not allowed.
|
|
65
65
|
*/
|
|
66
|
-
export function fetchBlob(url: string, allowedMimeTypes?: string[], options?:
|
|
66
|
+
export function fetchBlob(url: string, allowedMimeTypes?: string[], options?: FetchTemplateOptions): Promise<Blob>;
|
|
67
|
+
/**
|
|
68
|
+
* Loads a remote file as a text using Fetch API.
|
|
69
|
+
*
|
|
70
|
+
* @param {string} url - The full URL to fetch the file from.
|
|
71
|
+
* @param {FetchTemplateOptions} [options] - Optional fetch options.
|
|
72
|
+
* @param {string[]} [allowedMimeTypes] - Optional list of accepted MIME types (e.g., ['image/jpeg']).
|
|
73
|
+
* @returns {Promise<string>} - The fetched file as a text.
|
|
74
|
+
* @throws {Error} Throws if fetch fails, response is not ok, or MIME type is not allowed.
|
|
75
|
+
*/
|
|
76
|
+
export function fetchText(url: string, allowedMimeTypes?: string[], options?: FetchTemplateOptions): Promise<string>;
|
|
67
77
|
/**
|
|
68
78
|
* Installs a script that toggles CSS classes on a given element
|
|
69
79
|
* based on the page's visibility or focus state, and optionally
|
package/dist/v1/basics/html.mjs
CHANGED
|
@@ -188,7 +188,7 @@ async function fetchTemplate(url, { method = 'GET', body, timeout = 0, retries =
|
|
|
188
188
|
* Loads and parses a JSON from a remote URL using Fetch API.
|
|
189
189
|
*
|
|
190
190
|
* @param {string} url - The full URL to fetch JSON from.
|
|
191
|
-
* @param {
|
|
191
|
+
* @param {FetchTemplateOptions} [options] - Optional settings.
|
|
192
192
|
* @returns {Promise<any[] | Record<string | number | symbol, unknown>>} Parsed JSON object.
|
|
193
193
|
* @throws {Error} Throws if fetch fails, times out, or returns invalid JSON.
|
|
194
194
|
*/
|
|
@@ -211,7 +211,7 @@ export async function fetchJson(url, options) {
|
|
|
211
211
|
* Loads a remote file as a Blob using Fetch API.
|
|
212
212
|
*
|
|
213
213
|
* @param {string} url - The full URL to fetch the file from.
|
|
214
|
-
* @param {
|
|
214
|
+
* @param {FetchTemplateOptions} [options] - Optional fetch options.
|
|
215
215
|
* @param {string[]} [allowedMimeTypes] - Optional list of accepted MIME types (e.g., ['image/jpeg']).
|
|
216
216
|
* @returns {Promise<Blob>} - The fetched file as a Blob.
|
|
217
217
|
* @throws {Error} Throws if fetch fails, response is not ok, or MIME type is not allowed.
|
|
@@ -232,6 +232,31 @@ export async function fetchBlob(url, allowedMimeTypes, options) {
|
|
|
232
232
|
.catch(reject);
|
|
233
233
|
});
|
|
234
234
|
}
|
|
235
|
+
/**
|
|
236
|
+
* Loads a remote file as a text using Fetch API.
|
|
237
|
+
*
|
|
238
|
+
* @param {string} url - The full URL to fetch the file from.
|
|
239
|
+
* @param {FetchTemplateOptions} [options] - Optional fetch options.
|
|
240
|
+
* @param {string[]} [allowedMimeTypes] - Optional list of accepted MIME types (e.g., ['image/jpeg']).
|
|
241
|
+
* @returns {Promise<string>} - The fetched file as a text.
|
|
242
|
+
* @throws {Error} Throws if fetch fails, response is not ok, or MIME type is not allowed.
|
|
243
|
+
*/
|
|
244
|
+
export async function fetchText(url, allowedMimeTypes, options) {
|
|
245
|
+
return new Promise((resolve, reject) => {
|
|
246
|
+
fetchTemplate(url, options)
|
|
247
|
+
.then(async (res) => {
|
|
248
|
+
const contentType = res.headers.get('content-type') || '';
|
|
249
|
+
if (Array.isArray(allowedMimeTypes) &&
|
|
250
|
+
allowedMimeTypes.length > 0 &&
|
|
251
|
+
!allowedMimeTypes.some((type) => contentType.includes(type))) {
|
|
252
|
+
throw new Error(`Blocked MIME type: ${contentType}`);
|
|
253
|
+
}
|
|
254
|
+
const data = await res.text();
|
|
255
|
+
return resolve(data);
|
|
256
|
+
})
|
|
257
|
+
.catch(reject);
|
|
258
|
+
});
|
|
259
|
+
}
|
|
235
260
|
///////////////////////////////////////////////////////////////////////////////
|
|
236
261
|
/**
|
|
237
262
|
* Installs a script that toggles CSS classes on a given element
|
package/dist/v1/basics/index.cjs
CHANGED
|
@@ -23,6 +23,7 @@ exports.formatDayTimer = clock.formatDayTimer;
|
|
|
23
23
|
exports.formatTimer = clock.formatTimer;
|
|
24
24
|
exports.getTimeDuration = clock.getTimeDuration;
|
|
25
25
|
exports.fetchJson = html.fetchJson;
|
|
26
|
+
exports.fetchText = html.fetchText;
|
|
26
27
|
exports.installWindowHiddenScript = html.installWindowHiddenScript;
|
|
27
28
|
exports.readBase64Blob = html.readBase64Blob;
|
|
28
29
|
exports.readFileBlob = html.readFileBlob;
|
|
@@ -51,6 +52,7 @@ exports.requestFullScreen = fullScreen.requestFullScreen;
|
|
|
51
52
|
exports.formatBytes = simpleMath.formatBytes;
|
|
52
53
|
exports.genFibonacciSeq = simpleMath.genFibonacciSeq;
|
|
53
54
|
exports.getAge = simpleMath.getAge;
|
|
55
|
+
exports.getPercentage = simpleMath.getPercentage;
|
|
54
56
|
exports.getSimplePerc = simpleMath.getSimplePerc;
|
|
55
57
|
exports.ruleOfThree = simpleMath.ruleOfThree;
|
|
56
58
|
exports.addAiMarkerShortcut = text.addAiMarkerShortcut;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getPercentage } from './simpleMath.mjs';
|
|
1
2
|
import { areElsCollTop } from './collision.mjs';
|
|
2
3
|
import { areElsCollBottom } from './collision.mjs';
|
|
3
4
|
import { areElsCollLeft } from './collision.mjs';
|
|
@@ -26,6 +27,7 @@ import { getHtmlElBordersWidth } from './html_deprecated.mjs';
|
|
|
26
27
|
import { getHtmlElMargin } from './html_deprecated.mjs';
|
|
27
28
|
import { getHtmlElPadding } from './html_deprecated.mjs';
|
|
28
29
|
import { fetchJson } from './html.mjs';
|
|
30
|
+
import { fetchText } from './html.mjs';
|
|
29
31
|
import { readJsonBlob } from './html.mjs';
|
|
30
32
|
import { readFileBlob } from './html.mjs';
|
|
31
33
|
import { readBase64Blob } from './html.mjs';
|
|
@@ -58,5 +60,5 @@ import { getTimeDuration } from './clock.mjs';
|
|
|
58
60
|
import { shuffleArray } from './array.mjs';
|
|
59
61
|
import { toTitleCase } from './text.mjs';
|
|
60
62
|
import { toTitleCaseLowerFirst } from './text.mjs';
|
|
61
|
-
export { areElsCollTop, areElsCollBottom, areElsCollLeft, areElsCollRight, areElsCollPerfTop, areElsCollPerfBottom, areElsCollPerfLeft, areElsCollPerfRight, areElsColliding, areElsPerfColliding, getElsColliding, getElsPerfColliding, getElsCollOverlap, getElsCollOverlapPos, getRectCenter, getElsRelativeCenterOffset, getElsCollDirDepth, getElsCollDetails, isInViewport, isScrolledIntoView, safeTextTrim, installWindowHiddenScript, genFibonacciSeq, getHtmlElBorders, getHtmlElBordersWidth, getHtmlElMargin, getHtmlElPadding, fetchJson, readJsonBlob, readFileBlob, readBase64Blob, saveJsonFile, documentIsFullScreen, isScreenFilled, requestFullScreen, exitFullScreen, isFullScreenMode, onFullScreenChange, offFullScreenChange, areHtmlElsColliding, isJsonObject, arraySortPositions, formatBytes, addAiMarkerShortcut, extendObjType, reorderObjTypeOrder, cloneObjTypeOrder, countObj, objType, ruleOfThree, getSimplePerc, asyncReplace, getAge, formatCustomTimer, formatDayTimer, formatTimer, getTimeDuration, shuffleArray, toTitleCase, toTitleCaseLowerFirst };
|
|
63
|
+
export { getPercentage, areElsCollTop, areElsCollBottom, areElsCollLeft, areElsCollRight, areElsCollPerfTop, areElsCollPerfBottom, areElsCollPerfLeft, areElsCollPerfRight, areElsColliding, areElsPerfColliding, getElsColliding, getElsPerfColliding, getElsCollOverlap, getElsCollOverlapPos, getRectCenter, getElsRelativeCenterOffset, getElsCollDirDepth, getElsCollDetails, isInViewport, isScrolledIntoView, safeTextTrim, installWindowHiddenScript, genFibonacciSeq, getHtmlElBorders, getHtmlElBordersWidth, getHtmlElMargin, getHtmlElPadding, fetchJson, fetchText, readJsonBlob, readFileBlob, readBase64Blob, saveJsonFile, documentIsFullScreen, isScreenFilled, requestFullScreen, exitFullScreen, isFullScreenMode, onFullScreenChange, offFullScreenChange, areHtmlElsColliding, isJsonObject, arraySortPositions, formatBytes, addAiMarkerShortcut, extendObjType, reorderObjTypeOrder, cloneObjTypeOrder, countObj, objType, ruleOfThree, getSimplePerc, asyncReplace, getAge, formatCustomTimer, formatDayTimer, formatTimer, getTimeDuration, shuffleArray, toTitleCase, toTitleCaseLowerFirst };
|
|
62
64
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/v1/basics/index.mjs
CHANGED
|
@@ -2,12 +2,12 @@ import arraySortPositions from '../../legacy/libs/arraySortPositions.mjs';
|
|
|
2
2
|
import asyncReplace from '../../legacy/libs/replaceAsync.mjs';
|
|
3
3
|
import { shuffleArray } from './array.mjs';
|
|
4
4
|
import { formatCustomTimer, formatDayTimer, formatTimer, getTimeDuration } from './clock.mjs';
|
|
5
|
-
import { readJsonBlob, saveJsonFile, fetchJson, installWindowHiddenScript, readFileBlob, readBase64Blob, } from './html.mjs';
|
|
5
|
+
import { readJsonBlob, saveJsonFile, fetchJson, installWindowHiddenScript, readFileBlob, readBase64Blob, fetchText, } from './html.mjs';
|
|
6
6
|
import { areHtmlElsColliding, getHtmlElBorders, getHtmlElBordersWidth, getHtmlElMargin, getHtmlElPadding, isInViewport, isScrolledIntoView, } from './html_deprecated.mjs';
|
|
7
7
|
import { extendObjType, reorderObjTypeOrder, cloneObjTypeOrder, objType } from './objFilter.mjs';
|
|
8
8
|
import { countObj, isJsonObject } from './objChecker.mjs';
|
|
9
9
|
import { documentIsFullScreen, isScreenFilled, requestFullScreen, exitFullScreen, isFullScreenMode, onFullScreenChange, offFullScreenChange, } from './fullScreen.mjs';
|
|
10
|
-
import { formatBytes, genFibonacciSeq, getAge, getSimplePerc, ruleOfThree } from './simpleMath.mjs';
|
|
10
|
+
import { formatBytes, genFibonacciSeq, getAge, getPercentage, getSimplePerc, ruleOfThree } from './simpleMath.mjs';
|
|
11
11
|
import { addAiMarkerShortcut, safeTextTrim, toTitleCase, toTitleCaseLowerFirst } from './text.mjs';
|
|
12
12
|
import { areElsCollTop, areElsCollBottom, areElsCollLeft, areElsCollRight, areElsCollPerfTop, areElsCollPerfBottom, areElsCollPerfLeft, areElsCollPerfRight, areElsColliding, areElsPerfColliding, getElsColliding, getElsPerfColliding, getElsCollOverlap, getElsCollOverlapPos, getRectCenter, getElsRelativeCenterOffset, getElsCollDirDepth, getElsCollDetails, } from './collision.mjs';
|
|
13
|
-
export { areElsCollTop, areElsCollBottom, areElsCollLeft, areElsCollRight, areElsCollPerfTop, areElsCollPerfBottom, areElsCollPerfLeft, areElsCollPerfRight, areElsColliding, areElsPerfColliding, getElsColliding, getElsPerfColliding, getElsCollOverlap, getElsCollOverlapPos, getRectCenter, getElsRelativeCenterOffset, getElsCollDirDepth, getElsCollDetails, isInViewport, isScrolledIntoView, safeTextTrim, installWindowHiddenScript, genFibonacciSeq, getHtmlElBorders, getHtmlElBordersWidth, getHtmlElMargin, getHtmlElPadding, fetchJson, readJsonBlob, readFileBlob, readBase64Blob, saveJsonFile, documentIsFullScreen, isScreenFilled, requestFullScreen, exitFullScreen, isFullScreenMode, onFullScreenChange, offFullScreenChange, areHtmlElsColliding, isJsonObject, arraySortPositions, formatBytes, addAiMarkerShortcut, extendObjType, reorderObjTypeOrder, cloneObjTypeOrder, countObj, objType, ruleOfThree, getSimplePerc, asyncReplace, getAge, formatCustomTimer, formatDayTimer, formatTimer, getTimeDuration, shuffleArray, toTitleCase, toTitleCaseLowerFirst, };
|
|
13
|
+
export { getPercentage, areElsCollTop, areElsCollBottom, areElsCollLeft, areElsCollRight, areElsCollPerfTop, areElsCollPerfBottom, areElsCollPerfLeft, areElsCollPerfRight, areElsColliding, areElsPerfColliding, getElsColliding, getElsPerfColliding, getElsCollOverlap, getElsCollOverlapPos, getRectCenter, getElsRelativeCenterOffset, getElsCollDirDepth, getElsCollDetails, isInViewport, isScrolledIntoView, safeTextTrim, installWindowHiddenScript, genFibonacciSeq, getHtmlElBorders, getHtmlElBordersWidth, getHtmlElMargin, getHtmlElPadding, fetchJson, fetchText, readJsonBlob, readFileBlob, readBase64Blob, saveJsonFile, documentIsFullScreen, isScreenFilled, requestFullScreen, exitFullScreen, isFullScreenMode, onFullScreenChange, offFullScreenChange, areHtmlElsColliding, isJsonObject, arraySortPositions, formatBytes, addAiMarkerShortcut, extendObjType, reorderObjTypeOrder, cloneObjTypeOrder, countObj, objType, ruleOfThree, getSimplePerc, asyncReplace, getAge, formatCustomTimer, formatDayTimer, formatTimer, getTimeDuration, shuffleArray, toTitleCase, toTitleCaseLowerFirst, };
|
|
@@ -40,10 +40,13 @@ function ruleOfThree(val1, val2, val3, inverse = false) {
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
/**
|
|
43
|
-
* Calculates a percentage of a
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
43
|
+
* Calculates the actual value that corresponds to a percentage of a base number.
|
|
44
|
+
* Unlike `getPercentage`, which tells how much something represents in percent,
|
|
45
|
+
* this function tells how much a given percentage *is worth* in value.
|
|
46
|
+
*
|
|
47
|
+
* @param {number} price - The base number to apply the percentage to.
|
|
48
|
+
* @param {number} percentage - The percentage to calculate from the base.
|
|
49
|
+
* @returns {number} The resulting value of the percentage.
|
|
47
50
|
*
|
|
48
51
|
* @example
|
|
49
52
|
* getSimplePerc(200, 15); // 30
|
|
@@ -52,6 +55,21 @@ function getSimplePerc(price, percentage) {
|
|
|
52
55
|
return price * (percentage / 100);
|
|
53
56
|
}
|
|
54
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Calculates how much percent a partial value represents of the total value.
|
|
60
|
+
*
|
|
61
|
+
* @param {number} part - The partial value to compare.
|
|
62
|
+
* @param {number} total - The total or maximum value.
|
|
63
|
+
* @returns {number} The percentage that 'part' represents of 'total'.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* getPercentage(5, 100); // 5
|
|
67
|
+
*/
|
|
68
|
+
function getPercentage(part, total) {
|
|
69
|
+
if (total === 0) return 0;
|
|
70
|
+
return (part / total) * 100;
|
|
71
|
+
}
|
|
72
|
+
|
|
55
73
|
/**
|
|
56
74
|
* Calculates the age based on the given date.
|
|
57
75
|
*
|
|
@@ -166,5 +184,6 @@ function genFibonacciSeq({
|
|
|
166
184
|
exports.formatBytes = formatBytes;
|
|
167
185
|
exports.genFibonacciSeq = genFibonacciSeq;
|
|
168
186
|
exports.getAge = getAge;
|
|
187
|
+
exports.getPercentage = getPercentage;
|
|
169
188
|
exports.getSimplePerc = getSimplePerc;
|
|
170
189
|
exports.ruleOfThree = ruleOfThree;
|
|
@@ -35,15 +35,29 @@
|
|
|
35
35
|
*/
|
|
36
36
|
export function ruleOfThree(val1: number, val2: number, val3: number, inverse?: boolean): number;
|
|
37
37
|
/**
|
|
38
|
-
* Calculates a percentage of a
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
38
|
+
* Calculates the actual value that corresponds to a percentage of a base number.
|
|
39
|
+
* Unlike `getPercentage`, which tells how much something represents in percent,
|
|
40
|
+
* this function tells how much a given percentage *is worth* in value.
|
|
41
|
+
*
|
|
42
|
+
* @param {number} price - The base number to apply the percentage to.
|
|
43
|
+
* @param {number} percentage - The percentage to calculate from the base.
|
|
44
|
+
* @returns {number} The resulting value of the percentage.
|
|
42
45
|
*
|
|
43
46
|
* @example
|
|
44
47
|
* getSimplePerc(200, 15); // 30
|
|
45
48
|
*/
|
|
46
49
|
export function getSimplePerc(price: number, percentage: number): number;
|
|
50
|
+
/**
|
|
51
|
+
* Calculates how much percent a partial value represents of the total value.
|
|
52
|
+
*
|
|
53
|
+
* @param {number} part - The partial value to compare.
|
|
54
|
+
* @param {number} total - The total or maximum value.
|
|
55
|
+
* @returns {number} The percentage that 'part' represents of 'total'.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* getPercentage(5, 100); // 5
|
|
59
|
+
*/
|
|
60
|
+
export function getPercentage(part: number, total: number): number;
|
|
47
61
|
/**
|
|
48
62
|
* Calculates the age based on the given date.
|
|
49
63
|
*
|
|
@@ -37,10 +37,13 @@ export function ruleOfThree(val1, val2, val3, inverse = false) {
|
|
|
37
37
|
return inverse ? Number(val1 * val2) / val3 : Number(val3 * val2) / val1;
|
|
38
38
|
}
|
|
39
39
|
/**
|
|
40
|
-
* Calculates a percentage of a
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
40
|
+
* Calculates the actual value that corresponds to a percentage of a base number.
|
|
41
|
+
* Unlike `getPercentage`, which tells how much something represents in percent,
|
|
42
|
+
* this function tells how much a given percentage *is worth* in value.
|
|
43
|
+
*
|
|
44
|
+
* @param {number} price - The base number to apply the percentage to.
|
|
45
|
+
* @param {number} percentage - The percentage to calculate from the base.
|
|
46
|
+
* @returns {number} The resulting value of the percentage.
|
|
44
47
|
*
|
|
45
48
|
* @example
|
|
46
49
|
* getSimplePerc(200, 15); // 30
|
|
@@ -48,6 +51,21 @@ export function ruleOfThree(val1, val2, val3, inverse = false) {
|
|
|
48
51
|
export function getSimplePerc(price, percentage) {
|
|
49
52
|
return price * (percentage / 100);
|
|
50
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Calculates how much percent a partial value represents of the total value.
|
|
56
|
+
*
|
|
57
|
+
* @param {number} part - The partial value to compare.
|
|
58
|
+
* @param {number} total - The total or maximum value.
|
|
59
|
+
* @returns {number} The percentage that 'part' represents of 'total'.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* getPercentage(5, 100); // 5
|
|
63
|
+
*/
|
|
64
|
+
export function getPercentage(part, total) {
|
|
65
|
+
if (total === 0)
|
|
66
|
+
return 0;
|
|
67
|
+
return (part / total) * 100;
|
|
68
|
+
}
|
|
51
69
|
/**
|
|
52
70
|
* Calculates the age based on the given date.
|
|
53
71
|
*
|
package/dist/v1/index.cjs
CHANGED
|
@@ -64,6 +64,7 @@ exports.requestFullScreen = fullScreen.requestFullScreen;
|
|
|
64
64
|
exports.formatBytes = simpleMath.formatBytes;
|
|
65
65
|
exports.genFibonacciSeq = simpleMath.genFibonacciSeq;
|
|
66
66
|
exports.getAge = simpleMath.getAge;
|
|
67
|
+
exports.getPercentage = simpleMath.getPercentage;
|
|
67
68
|
exports.getSimplePerc = simpleMath.getSimplePerc;
|
|
68
69
|
exports.ruleOfThree = simpleMath.ruleOfThree;
|
|
69
70
|
exports.addAiMarkerShortcut = text.addAiMarkerShortcut;
|
|
@@ -76,6 +77,7 @@ exports.TinyRateLimiter = TinyRateLimiter;
|
|
|
76
77
|
exports.TinyNotifyCenter = TinyNotifyCenter;
|
|
77
78
|
exports.TinyToastNotify = TinyToastNotify;
|
|
78
79
|
exports.fetchJson = html.fetchJson;
|
|
80
|
+
exports.fetchText = html.fetchText;
|
|
79
81
|
exports.installWindowHiddenScript = html.installWindowHiddenScript;
|
|
80
82
|
exports.readBase64Blob = html.readBase64Blob;
|
|
81
83
|
exports.readFileBlob = html.readFileBlob;
|
package/dist/v1/index.d.mts
CHANGED
|
@@ -20,6 +20,7 @@ import TinyRateLimiter from './libs/TinyRateLimiter.mjs';
|
|
|
20
20
|
import ColorSafeStringify from './libs/ColorSafeStringify.mjs';
|
|
21
21
|
import TinyPromiseQueue from './libs/TinyPromiseQueue.mjs';
|
|
22
22
|
import TinyLevelUp from '../legacy/libs/userLevel.mjs';
|
|
23
|
+
import { getPercentage } from './basics/simpleMath.mjs';
|
|
23
24
|
import { areElsCollTop } from './basics/collision.mjs';
|
|
24
25
|
import { areElsCollBottom } from './basics/collision.mjs';
|
|
25
26
|
import { areElsCollLeft } from './basics/collision.mjs';
|
|
@@ -54,6 +55,7 @@ import { getHtmlElMargin } from './basics/html_deprecated.mjs';
|
|
|
54
55
|
import { getHtmlElPadding } from './basics/html_deprecated.mjs';
|
|
55
56
|
import { getLatestBackupPath } from './fileManager/normalFuncs.mjs';
|
|
56
57
|
import { fetchJson } from './basics/html.mjs';
|
|
58
|
+
import { fetchText } from './basics/html.mjs';
|
|
57
59
|
import { readJsonBlob } from './basics/html.mjs';
|
|
58
60
|
import { readFileBlob } from './basics/html.mjs';
|
|
59
61
|
import { readBase64Blob } from './basics/html.mjs';
|
|
@@ -109,5 +111,5 @@ import { getTimeDuration } from './basics/clock.mjs';
|
|
|
109
111
|
import { shuffleArray } from './basics/array.mjs';
|
|
110
112
|
import { toTitleCase } from './basics/text.mjs';
|
|
111
113
|
import { toTitleCaseLowerFirst } from './basics/text.mjs';
|
|
112
|
-
export { TinyNewWinEvents, TinyIframeEvents, TinyLocalStorage, TinyEvents, TinyTimeout, TinyColorConverter, TinyClipboard, TinyTextRangeEditor, TinySmartScroller, UltraRandomMsgGen, TinyAfterScrollWatcher, TinyHtml, TinyNotifications, TinyDomReadyManager, TinyDragger, TinyDragDropDetector, TinyToastNotify, TinyNotifyCenter, TinyRateLimiter, ColorSafeStringify, TinyPromiseQueue, TinyLevelUp, areElsCollTop, areElsCollBottom, areElsCollLeft, areElsCollRight, areElsCollPerfTop, areElsCollPerfBottom, areElsCollPerfLeft, areElsCollPerfRight, areElsColliding, areElsPerfColliding, getElsColliding, getElsPerfColliding, getElsCollOverlap, getElsCollOverlapPos, getRectCenter, getElsRelativeCenterOffset, getElsCollDirDepth, getElsCollDetails, isInViewport, isScrolledIntoView, safeTextTrim, installWindowHiddenScript, genFibonacciSeq, isDirEmptyAsync, fileSizeAsync, dirSizeAsync, listFilesAsync, listDirsAsync, getHtmlElBorders, getHtmlElBordersWidth, getHtmlElMargin, getHtmlElPadding, getLatestBackupPath, fetchJson, readJsonBlob, readFileBlob, readBase64Blob, saveJsonFile, readJsonFile, writeJsonFile, ensureDirectory, clearDirectoryAsync, clearDirectory, fileExists, dirExists, isDirEmpty, ensureCopyFile, tryDeleteFile, writeTextFile, listFiles, listDirs, fileSize, dirSize, backupFile, restoreLatestBackup, renameFileBatch, renameFileRegex, renameFileAddPrefixSuffix, renameFileNormalizeCase, renameFilePadNumbers, documentIsFullScreen, isScreenFilled, requestFullScreen, exitFullScreen, isFullScreenMode, onFullScreenChange, offFullScreenChange, areHtmlElsColliding, isJsonObject, arraySortPositions, formatBytes, addAiMarkerShortcut, extendObjType, reorderObjTypeOrder, cloneObjTypeOrder, countObj, checkObj, objType, ruleOfThree, getSimplePerc, asyncReplace, getAge, formatCustomTimer, formatDayTimer, formatTimer, getTimeDuration, shuffleArray, toTitleCase, toTitleCaseLowerFirst };
|
|
114
|
+
export { TinyNewWinEvents, TinyIframeEvents, TinyLocalStorage, TinyEvents, TinyTimeout, TinyColorConverter, TinyClipboard, TinyTextRangeEditor, TinySmartScroller, UltraRandomMsgGen, TinyAfterScrollWatcher, TinyHtml, TinyNotifications, TinyDomReadyManager, TinyDragger, TinyDragDropDetector, TinyToastNotify, TinyNotifyCenter, TinyRateLimiter, ColorSafeStringify, TinyPromiseQueue, TinyLevelUp, getPercentage, areElsCollTop, areElsCollBottom, areElsCollLeft, areElsCollRight, areElsCollPerfTop, areElsCollPerfBottom, areElsCollPerfLeft, areElsCollPerfRight, areElsColliding, areElsPerfColliding, getElsColliding, getElsPerfColliding, getElsCollOverlap, getElsCollOverlapPos, getRectCenter, getElsRelativeCenterOffset, getElsCollDirDepth, getElsCollDetails, isInViewport, isScrolledIntoView, safeTextTrim, installWindowHiddenScript, genFibonacciSeq, isDirEmptyAsync, fileSizeAsync, dirSizeAsync, listFilesAsync, listDirsAsync, getHtmlElBorders, getHtmlElBordersWidth, getHtmlElMargin, getHtmlElPadding, getLatestBackupPath, fetchJson, fetchText, readJsonBlob, readFileBlob, readBase64Blob, saveJsonFile, readJsonFile, writeJsonFile, ensureDirectory, clearDirectoryAsync, clearDirectory, fileExists, dirExists, isDirEmpty, ensureCopyFile, tryDeleteFile, writeTextFile, listFiles, listDirs, fileSize, dirSize, backupFile, restoreLatestBackup, renameFileBatch, renameFileRegex, renameFileAddPrefixSuffix, renameFileNormalizeCase, renameFilePadNumbers, documentIsFullScreen, isScreenFilled, requestFullScreen, exitFullScreen, isFullScreenMode, onFullScreenChange, offFullScreenChange, areHtmlElsColliding, isJsonObject, arraySortPositions, formatBytes, addAiMarkerShortcut, extendObjType, reorderObjTypeOrder, cloneObjTypeOrder, countObj, checkObj, objType, ruleOfThree, getSimplePerc, asyncReplace, getAge, formatCustomTimer, formatDayTimer, formatTimer, getTimeDuration, shuffleArray, toTitleCase, toTitleCaseLowerFirst };
|
|
113
115
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/v1/index.mjs
CHANGED
|
@@ -6,14 +6,14 @@ import { formatCustomTimer, formatDayTimer, formatTimer, getTimeDuration, } from
|
|
|
6
6
|
import { extendObjType, reorderObjTypeOrder, cloneObjTypeOrder, objType, checkObj, } from './basics/objFilter.mjs';
|
|
7
7
|
import { countObj, isJsonObject } from './basics/objChecker.mjs';
|
|
8
8
|
import { documentIsFullScreen, isScreenFilled, requestFullScreen, exitFullScreen, isFullScreenMode, onFullScreenChange, offFullScreenChange, } from './basics/fullScreen.mjs';
|
|
9
|
-
import { formatBytes, genFibonacciSeq, getAge, getSimplePerc, ruleOfThree, } from './basics/simpleMath.mjs';
|
|
9
|
+
import { formatBytes, genFibonacciSeq, getAge, getPercentage, getSimplePerc, ruleOfThree, } from './basics/simpleMath.mjs';
|
|
10
10
|
import { addAiMarkerShortcut, safeTextTrim, toTitleCase, toTitleCaseLowerFirst, } from './basics/text.mjs';
|
|
11
11
|
import ColorSafeStringify from './libs/ColorSafeStringify.mjs';
|
|
12
12
|
import TinyPromiseQueue from './libs/TinyPromiseQueue.mjs';
|
|
13
13
|
import TinyRateLimiter from './libs/TinyRateLimiter.mjs';
|
|
14
14
|
import TinyNotifyCenter from './libs/TinyNotifyCenter.mjs';
|
|
15
15
|
import TinyToastNotify from './libs/TinyToastNotify.mjs';
|
|
16
|
-
import { readJsonBlob, saveJsonFile, fetchJson, installWindowHiddenScript, readFileBlob, readBase64Blob, } from './basics/html.mjs';
|
|
16
|
+
import { readJsonBlob, saveJsonFile, fetchJson, installWindowHiddenScript, readFileBlob, readBase64Blob, fetchText, } from './basics/html.mjs';
|
|
17
17
|
import { getHtmlElBorders, getHtmlElBordersWidth, getHtmlElMargin, getHtmlElPadding, areHtmlElsColliding, isInViewport, isScrolledIntoView, } from './basics/html_deprecated.mjs';
|
|
18
18
|
import TinyDragDropDetector from './libs/TinyDragDropDetector.mjs';
|
|
19
19
|
import { readJsonFile, writeJsonFile, ensureDirectory, clearDirectory, fileExists, dirExists, isDirEmpty, ensureCopyFile, tryDeleteFile, writeTextFile, listFiles, listDirs, fileSize, dirSize, backupFile, restoreLatestBackup, renameFileBatch, renameFileRegex, renameFileAddPrefixSuffix, renameFileNormalizeCase, renameFilePadNumbers, getLatestBackupPath, } from './fileManager/normalFuncs.mjs';
|
|
@@ -34,4 +34,4 @@ import TinyEvents from './libs/TinyEvents.mjs';
|
|
|
34
34
|
import TinyLocalStorage from './libs/TinyLocalStorage.mjs';
|
|
35
35
|
import TinyIframeEvents from './libs/TinyIframeEvents.mjs';
|
|
36
36
|
import TinyNewWinEvents from './libs/TinyNewWinEvents.mjs';
|
|
37
|
-
export { TinyNewWinEvents, TinyIframeEvents, TinyLocalStorage, TinyEvents, TinyTimeout, TinyColorConverter, TinyClipboard, TinyTextRangeEditor, TinySmartScroller, UltraRandomMsgGen, TinyAfterScrollWatcher, TinyHtml, TinyNotifications, TinyDomReadyManager, TinyDragger, TinyDragDropDetector, TinyToastNotify, TinyNotifyCenter, TinyRateLimiter, ColorSafeStringify, TinyPromiseQueue, TinyLevelUp, areElsCollTop, areElsCollBottom, areElsCollLeft, areElsCollRight, areElsCollPerfTop, areElsCollPerfBottom, areElsCollPerfLeft, areElsCollPerfRight, areElsColliding, areElsPerfColliding, getElsColliding, getElsPerfColliding, getElsCollOverlap, getElsCollOverlapPos, getRectCenter, getElsRelativeCenterOffset, getElsCollDirDepth, getElsCollDetails, isInViewport, isScrolledIntoView, safeTextTrim, installWindowHiddenScript, genFibonacciSeq, isDirEmptyAsync, fileSizeAsync, dirSizeAsync, listFilesAsync, listDirsAsync, getHtmlElBorders, getHtmlElBordersWidth, getHtmlElMargin, getHtmlElPadding, getLatestBackupPath, fetchJson, readJsonBlob, readFileBlob, readBase64Blob, saveJsonFile, readJsonFile, writeJsonFile, ensureDirectory, clearDirectoryAsync, clearDirectory, fileExists, dirExists, isDirEmpty, ensureCopyFile, tryDeleteFile, writeTextFile, listFiles, listDirs, fileSize, dirSize, backupFile, restoreLatestBackup, renameFileBatch, renameFileRegex, renameFileAddPrefixSuffix, renameFileNormalizeCase, renameFilePadNumbers, documentIsFullScreen, isScreenFilled, requestFullScreen, exitFullScreen, isFullScreenMode, onFullScreenChange, offFullScreenChange, areHtmlElsColliding, isJsonObject, arraySortPositions, formatBytes, addAiMarkerShortcut, extendObjType, reorderObjTypeOrder, cloneObjTypeOrder, countObj, checkObj, objType, ruleOfThree, getSimplePerc, asyncReplace, getAge, formatCustomTimer, formatDayTimer, formatTimer, getTimeDuration, shuffleArray, toTitleCase, toTitleCaseLowerFirst, };
|
|
37
|
+
export { TinyNewWinEvents, TinyIframeEvents, TinyLocalStorage, TinyEvents, TinyTimeout, TinyColorConverter, TinyClipboard, TinyTextRangeEditor, TinySmartScroller, UltraRandomMsgGen, TinyAfterScrollWatcher, TinyHtml, TinyNotifications, TinyDomReadyManager, TinyDragger, TinyDragDropDetector, TinyToastNotify, TinyNotifyCenter, TinyRateLimiter, ColorSafeStringify, TinyPromiseQueue, TinyLevelUp, getPercentage, areElsCollTop, areElsCollBottom, areElsCollLeft, areElsCollRight, areElsCollPerfTop, areElsCollPerfBottom, areElsCollPerfLeft, areElsCollPerfRight, areElsColliding, areElsPerfColliding, getElsColliding, getElsPerfColliding, getElsCollOverlap, getElsCollOverlapPos, getRectCenter, getElsRelativeCenterOffset, getElsCollDirDepth, getElsCollDetails, isInViewport, isScrolledIntoView, safeTextTrim, installWindowHiddenScript, genFibonacciSeq, isDirEmptyAsync, fileSizeAsync, dirSizeAsync, listFilesAsync, listDirsAsync, getHtmlElBorders, getHtmlElBordersWidth, getHtmlElMargin, getHtmlElPadding, getLatestBackupPath, fetchJson, fetchText, readJsonBlob, readFileBlob, readBase64Blob, saveJsonFile, readJsonFile, writeJsonFile, ensureDirectory, clearDirectoryAsync, clearDirectory, fileExists, dirExists, isDirEmpty, ensureCopyFile, tryDeleteFile, writeTextFile, listFiles, listDirs, fileSize, dirSize, backupFile, restoreLatestBackup, renameFileBatch, renameFileRegex, renameFileAddPrefixSuffix, renameFileNormalizeCase, renameFilePadNumbers, documentIsFullScreen, isScreenFilled, requestFullScreen, exitFullScreen, isFullScreenMode, onFullScreenChange, offFullScreenChange, areHtmlElsColliding, isJsonObject, arraySortPositions, formatBytes, addAiMarkerShortcut, extendObjType, reorderObjTypeOrder, cloneObjTypeOrder, countObj, checkObj, objType, ruleOfThree, getSimplePerc, asyncReplace, getAge, formatCustomTimer, formatDayTimer, formatTimer, getTimeDuration, shuffleArray, toTitleCase, toTitleCaseLowerFirst, };
|
|
@@ -225,6 +225,16 @@ const __elemCollision = {
|
|
|
225
225
|
* @typedef {HTMLInputElement|HTMLSelectElement|HTMLTextAreaElement|HTMLOptionElement} InputElement
|
|
226
226
|
*/
|
|
227
227
|
|
|
228
|
+
/**
|
|
229
|
+
* Represents a parsed HTML element in JSON-like array form.
|
|
230
|
+
*
|
|
231
|
+
* @typedef {[
|
|
232
|
+
* tagName: string, // The tag name of the element (e.g., 'div', 'img')
|
|
233
|
+
* attributes: Record<string, string>, // All element attributes as key-value pairs
|
|
234
|
+
* ...children: (string | HtmlParsed)[] // Text or nested elements
|
|
235
|
+
* ]} HtmlParsed
|
|
236
|
+
*/
|
|
237
|
+
|
|
228
238
|
/**
|
|
229
239
|
* TinyHtml is a utility class that provides static and instance-level methods
|
|
230
240
|
* for precise dimension and position computations on HTML elements.
|
|
@@ -240,6 +250,188 @@ class TinyHtml {
|
|
|
240
250
|
|
|
241
251
|
static Utils = { ...collision };
|
|
242
252
|
|
|
253
|
+
/**
|
|
254
|
+
* Fetches an HTML file from the given URL, parses it to JSON.
|
|
255
|
+
*
|
|
256
|
+
* @param {string | URL | globalThis.Request} url - The URL of the HTML file.
|
|
257
|
+
* @param {RequestInit} [ops] - Optional fetch configuration (e.g., method, headers, cache, etc).
|
|
258
|
+
* @returns {Promise<HtmlParsed[]>} A promise that resolves with the parsed JSON representation of the HTML structure.
|
|
259
|
+
*/
|
|
260
|
+
static async fetchHtmlFile(url, ops = { method: 'GET' }) {
|
|
261
|
+
const res = await fetch(url, ops);
|
|
262
|
+
|
|
263
|
+
const contentType = res.headers.get('Content-Type') || '';
|
|
264
|
+
if (!res.ok) throw new Error(`Failed to fetch: ${res.status} ${res.statusText}`);
|
|
265
|
+
|
|
266
|
+
// Only accept HTML responses
|
|
267
|
+
if (!contentType.includes('text/html')) {
|
|
268
|
+
throw new Error(`Invalid content type: ${contentType} (expected text/html)`);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const html = await res.text();
|
|
272
|
+
return TinyHtml.htmlToJson(html);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Fetches an HTML file from the given URL, parses it to JSON, then converts it to DOM nodes.
|
|
277
|
+
*
|
|
278
|
+
* @param {string} url - The URL of the HTML file.
|
|
279
|
+
* @param {RequestInit} [ops] - Optional fetch configuration (e.g., method, headers, cache, etc).
|
|
280
|
+
* @returns {Promise<(HTMLElement|Text)[]>} A promise that resolves with the DOM nodes.
|
|
281
|
+
*/
|
|
282
|
+
static async fetchHtmlNodes(url, ops) {
|
|
283
|
+
const json = await TinyHtml.fetchHtmlFile(url, ops);
|
|
284
|
+
return TinyHtml.jsonToNodes(json);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Fetches an HTML file from the given URL, parses it to JSON, then converts it to TinyHtml instances.
|
|
289
|
+
*
|
|
290
|
+
* @param {string} url - The URL of the HTML file.
|
|
291
|
+
* @param {RequestInit} [ops] - Optional fetch configuration (e.g., method, headers, cache, etc).
|
|
292
|
+
* @returns {Promise<TinyHtml[]>} A promise that resolves with the TinyHtml instances.
|
|
293
|
+
*/
|
|
294
|
+
static async fetchHtmlTinyElems(url, ops) {
|
|
295
|
+
const nodes = await TinyHtml.fetchHtmlNodes(url, ops);
|
|
296
|
+
return TinyHtml.toTinyElm(nodes);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Converts the content of a <template> to an array of HtmlParsed.
|
|
301
|
+
*
|
|
302
|
+
* @param {HTMLTemplateElement} nodes
|
|
303
|
+
* @returns {HtmlParsed[]}
|
|
304
|
+
*/
|
|
305
|
+
static templateToJson(nodes) {
|
|
306
|
+
return TinyHtml.htmlToJson(
|
|
307
|
+
[...nodes.content.childNodes]
|
|
308
|
+
.map((node) =>
|
|
309
|
+
node instanceof Element ? node.getHTML() : node instanceof Text ? node.textContent : '',
|
|
310
|
+
)
|
|
311
|
+
.join(''),
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Converts the content of a <template> to real DOM nodes.
|
|
317
|
+
*
|
|
318
|
+
* @param {HTMLTemplateElement} nodes
|
|
319
|
+
* @returns {(Element|Text)[]}
|
|
320
|
+
*/
|
|
321
|
+
static templateToNodes(nodes) {
|
|
322
|
+
/** @type {(Element|Text)[]} */
|
|
323
|
+
const result = [];
|
|
324
|
+
[...nodes.content.cloneNode(true).childNodes].map((node) => {
|
|
325
|
+
if (!(node instanceof Element) && !(node instanceof Text) && !(node instanceof Comment))
|
|
326
|
+
throw new Error(
|
|
327
|
+
`Expected only Element nodes in <template>, but found: ${node.constructor.name}`,
|
|
328
|
+
);
|
|
329
|
+
if (!(node instanceof Comment)) result.push(node);
|
|
330
|
+
});
|
|
331
|
+
return result;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Converts the content of a <template> to an array of TinyHtml elements.
|
|
336
|
+
*
|
|
337
|
+
* @param {HTMLTemplateElement} nodes
|
|
338
|
+
* @returns {TinyHtml[]}
|
|
339
|
+
*/
|
|
340
|
+
static templateToTinyElems(nodes) {
|
|
341
|
+
return TinyHtml.toTinyElm(TinyHtml.templateToNodes(nodes));
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Parses a full HTML string into a JSON-like structure.
|
|
346
|
+
*
|
|
347
|
+
* @param {string} htmlString - Full HTML markup as a string.
|
|
348
|
+
* @returns {HtmlParsed[]} An array of parsed HTML elements in structured format.
|
|
349
|
+
*/
|
|
350
|
+
static htmlToJson(htmlString) {
|
|
351
|
+
const container = document.createElement('div');
|
|
352
|
+
container.innerHTML = htmlString.trim();
|
|
353
|
+
|
|
354
|
+
const result = [];
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* @param {Node} el
|
|
358
|
+
* @returns {*}
|
|
359
|
+
*/
|
|
360
|
+
const parseElement = (el) => {
|
|
361
|
+
if (el instanceof Comment) return null;
|
|
362
|
+
if (el instanceof Text) {
|
|
363
|
+
const text = el.textContent?.trim();
|
|
364
|
+
return text ? text : null;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (!(el instanceof Element)) return null;
|
|
368
|
+
const tag = el.tagName.toLowerCase();
|
|
369
|
+
|
|
370
|
+
/** @type {Record<string, string>} */
|
|
371
|
+
const props = {};
|
|
372
|
+
for (const attr of el.attributes) {
|
|
373
|
+
props[TinyHtml.getPropName(attr.name)] = attr.value;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const children = Array.from(el.childNodes).map(parseElement).filter(Boolean);
|
|
377
|
+
return children.length > 0 ? [tag, props, ...children] : [tag, props];
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
for (const child of container.childNodes) {
|
|
381
|
+
const parsed = parseElement(child);
|
|
382
|
+
if (parsed) result.push(parsed);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
container.innerHTML = '';
|
|
386
|
+
return result;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Converts a JSON-like HTML structure back to DOM Elements.
|
|
391
|
+
*
|
|
392
|
+
* @param {HtmlParsed[]} jsonArray - Parsed JSON format of HTML.
|
|
393
|
+
* @returns {(HTMLElement|Text)[]} List of DOM nodes.
|
|
394
|
+
*/
|
|
395
|
+
static jsonToNodes(jsonArray) {
|
|
396
|
+
/**
|
|
397
|
+
* @param {HtmlParsed|string} nodeData
|
|
398
|
+
* @returns {HTMLElement|Text}
|
|
399
|
+
*/
|
|
400
|
+
const createElement = (nodeData) => {
|
|
401
|
+
if (typeof nodeData === 'string') {
|
|
402
|
+
return document.createTextNode(nodeData);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
if (!Array.isArray(nodeData)) return document.createTextNode('');
|
|
406
|
+
const [tag, props, ...children] = nodeData;
|
|
407
|
+
const el = document.createElement(tag);
|
|
408
|
+
|
|
409
|
+
for (const [key, value] of Object.entries(props)) {
|
|
410
|
+
el.setAttribute(TinyHtml.getAttrName(key), value);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
for (const child of children) {
|
|
414
|
+
const childEl = createElement(child);
|
|
415
|
+
if (childEl instanceof Comment) continue;
|
|
416
|
+
el.appendChild(childEl);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return el;
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
return jsonArray.map(createElement).filter((node) => !(node instanceof Comment));
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Converts a JSON-like HTML structure back to TinyHtml instances.
|
|
427
|
+
*
|
|
428
|
+
* @param {HtmlParsed[]} jsonArray - Parsed JSON format of HTML.
|
|
429
|
+
* @returns {TinyHtml[]} List of TinyHtml instances.
|
|
430
|
+
*/
|
|
431
|
+
static jsonToTinyElems(jsonArray) {
|
|
432
|
+
return TinyHtml.toTinyElm(TinyHtml.jsonToNodes(jsonArray));
|
|
433
|
+
}
|
|
434
|
+
|
|
243
435
|
/**
|
|
244
436
|
* Creates a new TinyHtml element from a tag name and optional attributes.
|
|
245
437
|
*
|
|
@@ -498,7 +690,8 @@ class TinyHtml {
|
|
|
498
690
|
if (
|
|
499
691
|
!(this.#el[index] instanceof Element) &&
|
|
500
692
|
!(this.#el[index] instanceof Window) &&
|
|
501
|
-
!(this.#el[index] instanceof Document)
|
|
693
|
+
!(this.#el[index] instanceof Document) &&
|
|
694
|
+
!(this.#el[index] instanceof Text)
|
|
502
695
|
)
|
|
503
696
|
throw new Error(`[TinyHtml] Invalid Element in ${where}().`);
|
|
504
697
|
return this.#el[index];
|
|
@@ -514,7 +707,11 @@ class TinyHtml {
|
|
|
514
707
|
_getElements(where) {
|
|
515
708
|
if (
|
|
516
709
|
!this.#el.every(
|
|
517
|
-
(el) =>
|
|
710
|
+
(el) =>
|
|
711
|
+
el instanceof Element ||
|
|
712
|
+
el instanceof Window ||
|
|
713
|
+
el instanceof Document ||
|
|
714
|
+
el instanceof Text,
|
|
518
715
|
)
|
|
519
716
|
)
|
|
520
717
|
throw new Error(`[TinyHtml] Invalid Element in ${where}().`);
|
|
@@ -866,11 +1063,11 @@ class TinyHtml {
|
|
|
866
1063
|
* This ensures consistent access to methods of the `TinyHtml` class regardless
|
|
867
1064
|
* of the input form.
|
|
868
1065
|
*
|
|
869
|
-
* @param {TinyElement|TinyElement[]} elems - A single element or an array of elements (DOM or TinyHtml).
|
|
1066
|
+
* @param {TinyElement|Text|(TinyElement|Text)[]} elems - A single element or an array of elements (DOM or TinyHtml).
|
|
870
1067
|
* @returns {TinyHtml[]} An array of TinyHtml instances corresponding to the input elements.
|
|
871
1068
|
*/
|
|
872
1069
|
static toTinyElm(elems) {
|
|
873
|
-
/** @param {TinyElement[]} item */
|
|
1070
|
+
/** @param {(TinyElement|Text)[]} item */
|
|
874
1071
|
const checkElement = (item) =>
|
|
875
1072
|
item.map((elem) => (!(elem instanceof TinyHtml) ? new TinyHtml(elem) : elem));
|
|
876
1073
|
if (!Array.isArray(elems)) return checkElement([elems]);
|
|
@@ -1832,6 +2029,15 @@ class TinyHtml {
|
|
|
1832
2029
|
*/
|
|
1833
2030
|
#el;
|
|
1834
2031
|
|
|
2032
|
+
/**
|
|
2033
|
+
* Returns the number of elements currently stored in the internal element list.
|
|
2034
|
+
*
|
|
2035
|
+
* @returns {number} The total count of elements.
|
|
2036
|
+
*/
|
|
2037
|
+
get size() {
|
|
2038
|
+
return this.#el.length;
|
|
2039
|
+
}
|
|
2040
|
+
|
|
1835
2041
|
/**
|
|
1836
2042
|
* Creates an instance of TinyHtml for a specific Element.
|
|
1837
2043
|
* Useful when you want to operate repeatedly on the same element using instance methods.
|