@levischuck/tiny-html 0.1.0 → 0.1.1

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/index.d.ts CHANGED
@@ -27,4 +27,5 @@ export { htmlNodeTo } from './convert.ts';
27
27
  export { decodeHtmlEntities, encodeHtmlEntities } from './entities.ts';
28
28
  export type { CreateElementFn, CreateElementProps } from './convert.ts';
29
29
  export { getTextContent } from './writer.ts';
30
+ export { htmlNodeToHtmlElement } from './utils.ts';
30
31
  export type { WriterOptions, ParseResult, HtmlNode, HtmlElement, HtmlProps, HtmlStyle, SafeHtmlOptions, AllowedTag, ParserOptions, ParserAttributeNaming, WriterAttributeNaming, } from './types.ts';
package/dist/index.js CHANGED
@@ -13,7 +13,7 @@ const $ = /* @__PURE__ */ new Set([
13
13
  "source",
14
14
  "track",
15
15
  "wbr"
16
- ]), K = /* @__PURE__ */ new Set([
16
+ ]), J = /* @__PURE__ */ new Set([
17
17
  // Containers
18
18
  "div",
19
19
  "span",
@@ -110,7 +110,7 @@ const $ = /* @__PURE__ */ new Set([
110
110
  "body",
111
111
  "title",
112
112
  "meta"
113
- ]), H = /* @__PURE__ */ new Map([
113
+ ]), z = /* @__PURE__ */ new Map([
114
114
  ["p", /* @__PURE__ */ new Set(["p"])],
115
115
  ["li", /* @__PURE__ */ new Set(["li"])],
116
116
  ["dt", /* @__PURE__ */ new Set(["dt", "dd"])],
@@ -313,13 +313,13 @@ function G(e, t = !1) {
313
313
  let i = e;
314
314
  return i = i.replace(/&/g, "&amp;"), i = i.replace(/</g, "&lt;"), i = i.replace(/>/g, "&gt;"), t && (i = i.replace(/"/g, "&quot;")), i;
315
315
  }
316
- function J(e) {
316
+ function ee(e) {
317
317
  return e.replace(/\]\]>/g, "]]]]><![CDATA[>");
318
318
  }
319
319
  function Z(e) {
320
320
  return e.replace(/[-_]([a-z])/g, (t, i) => i.toUpperCase());
321
321
  }
322
- function ee(e) {
322
+ function te(e) {
323
323
  return e.replace(/[A-Z]/g, (t) => `-${t.toLowerCase()}`);
324
324
  }
325
325
  function b(e, t, i) {
@@ -336,11 +336,21 @@ function S(e) {
336
336
  e === 95 || // _
337
337
  e === 58;
338
338
  }
339
- function te(e, t = {}) {
339
+ function Ae(e) {
340
+ if (typeof e == "string" && e.trim().startsWith("<") && (e = Q(e).node), typeof e == "object" && e && "type" in e)
341
+ return e;
342
+ if (Array.isArray(e)) {
343
+ for (const t of e)
344
+ if (typeof t == "object" && t && "type" in t)
345
+ return t;
346
+ }
347
+ throw new Error("Could not find HtmlElement in decoded Html");
348
+ }
349
+ function Q(e, t = {}) {
340
350
  const i = t.attributeNaming ?? "reactName", s = new TextEncoder().encode(e), a = {
341
351
  node: []
342
352
  }, p = [], o = [];
343
- let T = "HTML", n = "TEXT", r = 0, h = 0, g = 0, E = 0, N = 0, y = "", q = 0, d = null;
353
+ let T = "HTML", n = "TEXT", r = 0, h = 0, g = 0, E = 0, N = 0, A = "", q = 0, d = null;
344
354
  function x() {
345
355
  return o.length > 0 ? o[o.length - 1].children : p;
346
356
  }
@@ -367,26 +377,26 @@ function te(e, t = {}) {
367
377
  for (let u = o.length - 1; u >= 0; u--)
368
378
  if (o[u].type === c) {
369
379
  for (let m = o.length - 1; m >= u; m--) {
370
- const A = o.pop(), w = {
371
- type: A.type,
380
+ const y = o.pop(), w = {
381
+ type: y.type,
372
382
  props: {
373
- ...A.props,
374
- children: A.children.length === 1 ? A.children[0] : A.children
383
+ ...y.props,
384
+ children: y.children.length === 1 ? y.children[0] : y.children
375
385
  }
376
386
  };
377
- o.length > 0 ? o[o.length - 1].children.push(w) : p.push(w), (A.type === "svg" || A.type === "math") && (T = "HTML");
387
+ o.length > 0 ? o[o.length - 1].children.push(w) : p.push(w), (y.type === "svg" || y.type === "math") && (T = "HTML");
378
388
  }
379
389
  return;
380
390
  }
381
391
  }
382
392
  function _(f, c, u) {
383
393
  const m = v(f, T);
384
- if (o.length > 0 && H.has(m)) {
385
- const w = H.get(m), z = o[o.length - 1].type;
386
- w.has(z) && I(z);
394
+ if (o.length > 0 && z.has(m)) {
395
+ const w = z.get(m), H = o[o.length - 1].type;
396
+ w.has(H) && I(H);
387
397
  }
388
- let A = T;
389
- if (m === "svg" ? A = "SVG" : m === "math" && (A = "MATHML"), $.has(m) || u) {
398
+ let y = T;
399
+ if (m === "svg" ? y = "SVG" : m === "math" && (y = "MATHML"), $.has(m) || u) {
390
400
  const w = {
391
401
  type: m,
392
402
  props: c
@@ -397,9 +407,9 @@ function te(e, t = {}) {
397
407
  type: m,
398
408
  props: c,
399
409
  children: [],
400
- namespace: A
410
+ namespace: y
401
411
  };
402
- o.push(w), T = A;
412
+ o.push(w), T = y;
403
413
  }
404
414
  }
405
415
  for (; r < s.length; ) {
@@ -439,23 +449,23 @@ function te(e, t = {}) {
439
449
  break;
440
450
  case "ATTRIBUTE_NAME":
441
451
  if (C(f)) {
442
- y = b(s, E, r);
443
- const c = L(y, d.namespace);
444
- d.props[c] = !0, y = "", n = "ATTRIBUTES", r++;
452
+ A = b(s, E, r);
453
+ const c = L(A, d.namespace);
454
+ d.props[c] = !0, A = "", n = "ATTRIBUTES", r++;
445
455
  } else if (f === 61)
446
- y = b(s, E, r), n = "ATTRIBUTE_VALUE_START", r++;
456
+ A = b(s, E, r), n = "ATTRIBUTE_VALUE_START", r++;
447
457
  else if (f === 62) {
448
- y = b(s, E, r);
449
- const c = L(y, d.namespace);
450
- d.props[c] = !0, y = "";
458
+ A = b(s, E, r);
459
+ const c = L(A, d.namespace);
460
+ d.props[c] = !0, A = "";
451
461
  const u = d.type;
452
462
  _(u, d.props, !1);
453
463
  const m = v(u, T);
454
464
  m === "script" ? (n = "SCRIPT_CONTENT", h = r + 1) : m === "style" ? (n = "STYLE_CONTENT", h = r + 1) : (n = "TEXT", h = r + 1), r++;
455
465
  } else if (f === 47) {
456
- y = b(s, E, r);
457
- const c = L(y, d.namespace);
458
- d.props[c] = !0, y = "", n = "TAG_CLOSE_SELF", r++;
466
+ A = b(s, E, r);
467
+ const c = L(A, d.namespace);
468
+ d.props[c] = !0, A = "", n = "TAG_CLOSE_SELF", r++;
459
469
  } else S(f) ? r++ : (h = g - 1, n = "TEXT");
460
470
  break;
461
471
  case "ATTRIBUTE_VALUE_START":
@@ -463,15 +473,15 @@ function te(e, t = {}) {
463
473
  break;
464
474
  case "ATTRIBUTE_VALUE_QUOTED":
465
475
  if (f === q) {
466
- const c = b(s, N, r), u = O(c), m = L(y, d.namespace);
467
- m === "style" ? d.props[m] = F(u) : d.props[m] = u, y = "", n = "ATTRIBUTES", r++;
476
+ const c = b(s, N, r), u = O(c), m = L(A, d.namespace);
477
+ m === "style" ? d.props[m] = F(u) : d.props[m] = u, A = "", n = "ATTRIBUTES", r++;
468
478
  } else
469
479
  r++;
470
480
  break;
471
481
  case "ATTRIBUTE_VALUE_UNQUOTED":
472
482
  if (C(f) || f === 62 || f === 47) {
473
- const c = b(s, N, r), u = O(c), m = L(y, d.namespace);
474
- m === "style" ? d.props[m] = F(u) : d.props[m] = u, y = "", n = "ATTRIBUTES";
483
+ const c = b(s, N, r), u = O(c), m = L(A, d.namespace);
484
+ m === "style" ? d.props[m] = F(u) : d.props[m] = u, A = "", n = "ATTRIBUTES";
475
485
  } else
476
486
  r++;
477
487
  break;
@@ -517,9 +527,9 @@ function te(e, t = {}) {
517
527
  const c = n === "SCRIPT_CONTENT" ? "script" : "style", u = "</" + c, m = b(s, r, Math.min(r + u.length + 1, s.length)).toLowerCase();
518
528
  if (m.startsWith(u) && (m[u.length] === ">" || C(m.charCodeAt(u.length)))) {
519
529
  M(h, r);
520
- let A = r + u.length;
521
- for (; A < s.length && s[A] !== 62; ) A++;
522
- I(c), n = "TEXT", h = A + 1, r = A + 1;
530
+ let y = r + u.length;
531
+ for (; y < s.length && s[y] !== 62; ) y++;
532
+ I(c), n = "TEXT", h = y + 1, r = y + 1;
523
533
  } else
524
534
  r++;
525
535
  } else
@@ -611,8 +621,8 @@ function ie(e, t, i, l, s, a, p) {
611
621
  if (E === !0)
612
622
  t.push(" "), t.push(N);
613
623
  else if (g === "style" && typeof E == "object") {
614
- const y = ne(E);
615
- y && (t.push(' style="'), t.push(G(y, !0)), t.push('"'));
624
+ const A = ne(E);
625
+ A && (t.push(' style="'), t.push(G(A, !0)), t.push('"'));
616
626
  } else
617
627
  t.push(" "), t.push(N), t.push('="'), t.push(G(String(E), !0)), t.push('"');
618
628
  }
@@ -623,13 +633,13 @@ function ie(e, t, i, l, s, a, p) {
623
633
  else if (r) {
624
634
  t.push(">");
625
635
  const g = n;
626
- o === "script" && l || o === "style" && s ? (t.push("<![CDATA["), j(g, t, T, l, s, a, p), t.push("]]>")) : o === "script" || o === "style" ? Q(g, t) : R(g, t, T, l, s, a, p), t.push("</"), t.push(o), t.push(">");
627
- } else K.has(o), t.push("></"), t.push(o), t.push(">");
636
+ o === "script" && l || o === "style" && s ? (t.push("<![CDATA["), j(g, t, T, l, s, a, p), t.push("]]>")) : o === "script" || o === "style" ? K(g, t) : R(g, t, T, l, s, a, p), t.push("</"), t.push(o), t.push(">");
637
+ } else J.has(o), t.push("></"), t.push(o), t.push(">");
628
638
  }
629
639
  function j(e, t, i, l, s, a, p, o) {
630
640
  if (e != null) {
631
641
  if (typeof e == "string") {
632
- t.push(J(e));
642
+ t.push(ee(e));
633
643
  return;
634
644
  }
635
645
  if (typeof e == "number" || typeof e == "bigint" || typeof e == "boolean") {
@@ -644,7 +654,7 @@ function j(e, t, i, l, s, a, p, o) {
644
654
  R(e, t, i, l, s, a, p);
645
655
  }
646
656
  }
647
- function Q(e, t) {
657
+ function K(e, t) {
648
658
  if (e != null) {
649
659
  if (typeof e == "string") {
650
660
  t.push(e);
@@ -656,7 +666,7 @@ function Q(e, t) {
656
666
  }
657
667
  if (Array.isArray(e)) {
658
668
  for (const i of e)
659
- Q(i, t);
669
+ K(i, t);
660
670
  return;
661
671
  }
662
672
  }
@@ -665,12 +675,12 @@ function ne(e) {
665
675
  const t = [];
666
676
  for (const [i, l] of Object.entries(e))
667
677
  if (l) {
668
- const s = ee(i);
678
+ const s = te(i);
669
679
  t.push(`${s}: ${l}`);
670
680
  }
671
681
  return t.join("; ");
672
682
  }
673
- function ye(e) {
683
+ function be(e) {
674
684
  const t = [];
675
685
  function i(l) {
676
686
  if (typeof l == "string" && t.push(l), (typeof l == "number" || typeof l == "bigint" || typeof l == "boolean") && t.push(`${l}`), typeof l == "object" && l !== null && "type" in l && l.props && "children" in l.props) {
@@ -896,7 +906,7 @@ function X(e, t) {
896
906
  props: p
897
907
  };
898
908
  }
899
- function Ae(e, t = {}) {
909
+ function ye(e, t = {}) {
900
910
  const i = pe(t);
901
911
  return X(e, i);
902
912
  }
@@ -915,25 +925,26 @@ function Y(e, t) {
915
925
  }
916
926
  return null;
917
927
  }
918
- function be(e, t = {}) {
919
- return te(e, t);
920
- }
921
928
  function Ee(e, t = {}) {
929
+ return Q(e, t);
930
+ }
931
+ function we(e, t = {}) {
922
932
  return e && typeof e == "object" && "node" in e ? k(e, t) : e === void 0 ? "" : k(e, t);
923
933
  }
924
- async function we(e) {
934
+ async function Ne(e) {
925
935
  return await U(e);
926
936
  }
927
- function Ne(e, t = {}) {
928
- return Ae(e, t);
937
+ function Ce(e, t = {}) {
938
+ return ye(e, t);
929
939
  }
930
940
  export {
931
- we as awaitHtmlNode,
941
+ Ne as awaitHtmlNode,
932
942
  O as decodeHtmlEntities,
933
943
  G as encodeHtmlEntities,
934
- ye as getTextContent,
944
+ be as getTextContent,
935
945
  Y as htmlNodeTo,
936
- be as readHtml,
937
- Ne as safeHtml,
938
- Ee as writeHtml
946
+ Ae as htmlNodeToHtmlElement,
947
+ Ee as readHtml,
948
+ Ce as safeHtml,
949
+ we as writeHtml
939
950
  };
package/dist/utils.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { HtmlElement, HtmlNode } from './types.ts';
1
2
  /**
2
3
  * Converts a kebab-case or snake_case string to camelCase
3
4
  * @example toCamelCase("foo-bar") => "fooBar"
@@ -32,3 +33,11 @@ export declare function isWhitespace(code: number): boolean;
32
33
  * Checks if a character code is valid for tag/attribute names
33
34
  */
34
35
  export declare function isNameChar(code: number): boolean;
36
+ /**
37
+ * Find the first HtmlElement in the HtmlNode, may be an html string.
38
+ *
39
+ * @param node - the result of readHtml or a string of HTML
40
+ * @returns the first HtmlElement in the HtmlNode
41
+ * @throws an error if no HtmlElement is found
42
+ */
43
+ export declare function htmlNodeToHtmlElement(node: HtmlNode): HtmlElement;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@levischuck/tiny-html",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.js",