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.
@@ -445,6 +445,139 @@ Finds elements by tag name in a namespace (defaults to XHTML).
445
445
 
446
446
  ---
447
447
 
448
+ ## 📑 HTML Parser
449
+
450
+ ### Type Definition
451
+
452
+ A parsed HTML element represented as an array:
453
+
454
+ ```ts
455
+ type HtmlParsed = [
456
+ tagName: string, // e.g., 'div', 'span', 'img'
457
+ attributes: Record<string, string>, // Key-value map of all element attributes
458
+ ...children: (string | HtmlParsed)[] // Nested elements or text content
459
+ ];
460
+ ```
461
+
462
+ ---
463
+
464
+ ### `fetchHtmlFile(url, ops?)`
465
+
466
+ Fetches an HTML file from a URL and parses it into a JSON-like structure.
467
+
468
+ ```ts
469
+ static async fetchHtmlFile(url: string | URL | Request, ops?: RequestInit): Promise<HtmlParsed[]>
470
+ ```
471
+
472
+ * **url**: The URL of the HTML file.
473
+ * **ops**: (Optional) `fetch()` options (method, headers, etc).
474
+ * **Returns**: Promise of parsed JSON structure (`HtmlParsed[]`).
475
+ * Throws if content-type is not `text/html`.
476
+
477
+ ---
478
+
479
+ ### `fetchHtmlNodes(url, ops?)`
480
+
481
+ Fetches an HTML file and returns it as DOM nodes.
482
+
483
+ ```ts
484
+ static async fetchHtmlNodes(url: string, ops?: RequestInit): Promise<(HTMLElement | Text)[]>
485
+ ```
486
+
487
+ * **Returns**: Array of real DOM nodes (`HTMLElement | Text`).
488
+
489
+ ---
490
+
491
+ ### `fetchHtmlTinyElems(url, ops?)`
492
+
493
+ Fetches HTML and converts it into an array of `TinyHtml` instances.
494
+
495
+ ```ts
496
+ static async fetchHtmlTinyElems(url: string, ops?: RequestInit): Promise<TinyHtml[]>
497
+ ```
498
+
499
+ * **Returns**: `TinyHtml[]` (custom wrapper elements).
500
+
501
+ ---
502
+
503
+ ### `templateToJson(template)`
504
+
505
+ Converts the contents of a `<template>` to JSON format.
506
+
507
+ ```ts
508
+ static templateToJson(template: HTMLTemplateElement): HtmlParsed[]
509
+ ```
510
+
511
+ * **Returns**: Structured JSON representation (`HtmlParsed[]`).
512
+
513
+ ---
514
+
515
+ ### `templateToNodes(template)`
516
+
517
+ Converts `<template>` content into real DOM nodes.
518
+
519
+ ```ts
520
+ static templateToNodes(template: HTMLTemplateElement): (Element | Text)[]
521
+ ```
522
+
523
+ * **Returns**: Cloned nodes excluding comments.
524
+ * Throws error if unexpected node types are encountered.
525
+
526
+ ---
527
+
528
+ ### `templateToTinyElems(template)`
529
+
530
+ Converts a `<template>` into `TinyHtml` instances.
531
+
532
+ ```ts
533
+ static templateToTinyElems(template: HTMLTemplateElement): TinyHtml[]
534
+ ```
535
+
536
+ * **Returns**: Array of `TinyHtml` objects.
537
+
538
+ ---
539
+
540
+ ### `htmlToJson(htmlString)`
541
+
542
+ Parses an HTML string into structured JSON (`HtmlParsed[]`).
543
+
544
+ ```ts
545
+ static htmlToJson(htmlString: string): HtmlParsed[]
546
+ ```
547
+
548
+ * **Returns**: Parsed representation as an array of `[tag, attributes, ...children]`.
549
+
550
+ ---
551
+
552
+ ### `jsonToNodes(jsonArray)`
553
+
554
+ Converts parsed JSON back into actual DOM nodes.
555
+
556
+ ```ts
557
+ static jsonToNodes(jsonArray: HtmlParsed[]): (HTMLElement | Text)[]
558
+ ```
559
+
560
+ * **Returns**: Array of DOM elements/text nodes.
561
+ * Skips comments.
562
+
563
+ ---
564
+
565
+ ### `jsonToTinyElems(jsonArray)`
566
+
567
+ Converts parsed JSON back into `TinyHtml` instances.
568
+
569
+ ```ts
570
+ static jsonToTinyElems(jsonArray: HtmlParsed[]): TinyHtml[]
571
+ ```
572
+
573
+ * **Returns**: TinyHtml-wrapped elements.
574
+
575
+ ---
576
+
577
+ 💡 **Tip**: All conversion methods rely on a consistent internal format (`HtmlParsed[]`). That means you can seamlessly switch between representations (JSON ⇄ DOM ⇄ TinyHtml)!
578
+
579
+ ---
580
+
448
581
  ## 🧩 Internal Element Access
449
582
 
450
583
  ### `exists(index)`
@@ -461,6 +594,13 @@ Returns the raw DOM element associated with this instance.
461
594
 
462
595
  ---
463
596
 
597
+ ### `size`
598
+ Returns the number of elements currently stored in the internal element list.
599
+
600
+ - **Returns**: `number`
601
+
602
+ ---
603
+
464
604
  ### `extract(index)`
465
605
  Extracts a single DOM element from the internal list at the specified index.
466
606
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiny-essentials",
3
- "version": "1.20.1",
3
+ "version": "1.20.2",
4
4
  "description": "Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.",
5
5
  "scripts": {
6
6
  "test": "npm run test:mjs && npm run test:cjs && npm run test:js",