@levischuck/tiny-html 0.0.2 → 0.0.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/index.d.ts CHANGED
@@ -13,5 +13,7 @@ export declare function writeHtml(input: HtmlNode | ParseResult, options?: Write
13
13
  */
14
14
  export declare function awaitHtmlNode(node: HtmlNode | Promise<HtmlNode>): Promise<HtmlNode>;
15
15
  export { htmlNodeTo } from './convert.ts';
16
+ export { decodeHtmlEntities, encodeHtmlEntities } from './entities.ts';
16
17
  export type { CreateElementFn, CreateElementProps } from './convert.ts';
18
+ export { getTextContent } from './writer.ts';
17
19
  export type { WriterOptions, ParseResult, HtmlNode, HtmlElement, HtmlProps, HtmlStyle } from './types.ts';
package/dist/index.js CHANGED
@@ -292,38 +292,38 @@ const F = /* @__PURE__ */ new Set([
292
292
  dArr: "⇓",
293
293
  hArr: "⇔"
294
294
  }, B = {};
295
- for (const [e, r] of Object.entries(I))
296
- B[r] || (B[r] = e);
297
- function v(e) {
298
- return !e || typeof e != "string" || !e.includes("&") ? e : e.replace(/&(?:#[xX]([0-9a-fA-F]+)|#(\d+)|([a-zA-Z][a-zA-Z0-9]*));/g, (r, n, u, f) => {
299
- if (n) {
300
- const a = parseInt(n, 16);
301
- return String.fromCodePoint(a);
302
- } else if (u) {
303
- const a = parseInt(u, 10);
304
- return String.fromCodePoint(a);
305
- } else if (f && I[f])
306
- return I[f];
307
- return r;
295
+ for (const [e, n] of Object.entries(I))
296
+ B[n] || (B[n] = e);
297
+ function w(e) {
298
+ return !e || typeof e != "string" || !e.includes("&") ? e : e.replace(/&(?:#[xX]([0-9a-fA-F]+)|#(\d+)|([a-zA-Z][a-zA-Z0-9]*));/g, (n, r, l, a) => {
299
+ if (r) {
300
+ const o = parseInt(r, 16);
301
+ return String.fromCodePoint(o);
302
+ } else if (l) {
303
+ const o = parseInt(l, 10);
304
+ return String.fromCodePoint(o);
305
+ } else if (a && I[a])
306
+ return I[a];
307
+ return n;
308
308
  });
309
309
  }
310
- function G(e, r = !1) {
310
+ function x(e, n = !1) {
311
311
  if (!e || typeof e != "string")
312
312
  return e;
313
- let n = e;
314
- return n = n.replace(/&/g, "&amp;"), n = n.replace(/</g, "&lt;"), n = n.replace(/>/g, "&gt;"), r && (n = n.replace(/"/g, "&quot;")), n;
313
+ let r = e;
314
+ return r = r.replace(/&/g, "&amp;"), r = r.replace(/</g, "&lt;"), r = r.replace(/>/g, "&gt;"), n && (r = r.replace(/"/g, "&quot;")), r;
315
315
  }
316
316
  function Z(e) {
317
317
  return e.replace(/\]\]>/g, "]]]]><![CDATA[>");
318
318
  }
319
319
  function k(e) {
320
- return e.replace(/[-_]([a-z])/g, (r, n) => n.toUpperCase());
320
+ return e.replace(/[-_]([a-z])/g, (n, r) => r.toUpperCase());
321
321
  }
322
322
  function $(e) {
323
- return e.replace(/[A-Z]/g, (r) => `-${r.toLowerCase()}`);
323
+ return e.replace(/[A-Z]/g, (n) => `-${n.toLowerCase()}`);
324
324
  }
325
- function y(e, r, n) {
326
- return new TextDecoder("utf-8").decode(e.slice(r, n));
325
+ function d(e, n, r) {
326
+ return new TextDecoder("utf-8").decode(e.slice(n, r));
327
327
  }
328
328
  function S(e) {
329
329
  return e === 32 || e === 9 || e === 10 || e === 13;
@@ -337,197 +337,197 @@ function N(e) {
337
337
  e === 58;
338
338
  }
339
339
  function K(e) {
340
- const n = new TextEncoder().encode(e), u = {
340
+ const r = new TextEncoder().encode(e), l = {
341
341
  node: []
342
- }, f = [], a = [];
343
- let c = "HTML", i = "TEXT", t = 0, p = 0, g = 0, A = 0, E = 0, d = "", P = 0, h = null;
342
+ }, a = [], o = [];
343
+ let u = "HTML", i = "TEXT", t = 0, c = 0, y = 0, A = 0, b = 0, g = "", P = 0, h = null;
344
344
  function M() {
345
- return a.length > 0 ? a[a.length - 1].children : f;
345
+ return o.length > 0 ? o[o.length - 1].children : a;
346
346
  }
347
- function X(s, o) {
348
- if (s >= o) return;
349
- const l = y(n, s, o), T = v(l);
347
+ function X(s, f) {
348
+ if (s >= f) return;
349
+ const p = d(r, s, f), T = w(p);
350
350
  T.length > 0 && M().push(T);
351
351
  }
352
- function U(s, o) {
353
- if (s >= o) return;
354
- const l = y(n, s, o);
355
- l.length > 0 && M().push(l);
352
+ function U(s, f) {
353
+ if (s >= f) return;
354
+ const p = d(r, s, f);
355
+ p.length > 0 && M().push(p);
356
356
  }
357
- function C(s, o) {
358
- const l = s.toLowerCase();
359
- return o === "SVG" && V.has(l) ? V.get(l) : l;
357
+ function C(s, f) {
358
+ const p = s.toLowerCase();
359
+ return f === "SVG" && V.has(p) ? V.get(p) : p;
360
360
  }
361
- function _(s, o) {
362
- const l = s.toLowerCase();
363
- if (l === "class") return "className";
364
- if (l === "for") return "htmlFor";
365
- if (o === "SVG")
366
- return z.has(l) ? z.get(l) : s;
367
- if (o === "HTML") {
368
- const T = k(l);
369
- return l === "class" || l === "for" ? l === "class" ? "className" : "htmlFor" : T;
361
+ function _(s, f) {
362
+ const p = s.toLowerCase();
363
+ if (p === "class") return "className";
364
+ if (p === "for") return "htmlFor";
365
+ if (f === "SVG")
366
+ return z.has(p) ? z.get(p) : s;
367
+ if (f === "HTML") {
368
+ const T = k(p);
369
+ return p === "class" || p === "for" ? p === "class" ? "className" : "htmlFor" : T;
370
370
  }
371
- return l;
371
+ return p;
372
372
  }
373
373
  function R(s) {
374
- const o = C(s, c);
375
- for (let l = a.length - 1; l >= 0; l--)
376
- if (a[l].type === o) {
377
- for (let T = a.length - 1; T >= l; T--) {
378
- const m = a.pop(), b = {
374
+ const f = C(s, u);
375
+ for (let p = o.length - 1; p >= 0; p--)
376
+ if (o[p].type === f) {
377
+ for (let T = o.length - 1; T >= p; T--) {
378
+ const m = o.pop(), E = {
379
379
  type: m.type,
380
380
  props: {
381
381
  ...m.props,
382
382
  children: m.children.length === 1 ? m.children[0] : m.children
383
383
  }
384
384
  };
385
- a.length > 0 ? a[a.length - 1].children.push(b) : f.push(b), (m.type === "svg" || m.type === "math") && (c = "HTML");
385
+ o.length > 0 ? o[o.length - 1].children.push(E) : a.push(E), (m.type === "svg" || m.type === "math") && (u = "HTML");
386
386
  }
387
387
  return;
388
388
  }
389
389
  }
390
- function w(s, o, l) {
391
- const T = C(s, c);
392
- if (a.length > 0 && q.has(T)) {
393
- const b = q.get(T), j = a[a.length - 1].type;
394
- b.has(j) && R(j);
390
+ function v(s, f, p) {
391
+ const T = C(s, u);
392
+ if (o.length > 0 && q.has(T)) {
393
+ const E = q.get(T), j = o[o.length - 1].type;
394
+ E.has(j) && R(j);
395
395
  }
396
- let m = c;
397
- if (T === "svg" ? m = "SVG" : T === "math" && (m = "MATHML"), F.has(T) || l) {
398
- const b = {
396
+ let m = u;
397
+ if (T === "svg" ? m = "SVG" : T === "math" && (m = "MATHML"), F.has(T) || p) {
398
+ const E = {
399
399
  type: T,
400
- props: o
400
+ props: f
401
401
  };
402
- M().push(b);
402
+ M().push(E);
403
403
  } else {
404
- const b = {
404
+ const E = {
405
405
  type: T,
406
- props: o,
406
+ props: f,
407
407
  children: [],
408
408
  namespace: m
409
409
  };
410
- a.push(b), c = m;
410
+ o.push(E), u = m;
411
411
  }
412
412
  }
413
- for (; t < n.length; ) {
414
- const s = n[t];
413
+ for (; t < r.length; ) {
414
+ const s = r[t];
415
415
  switch (i) {
416
416
  case "TEXT":
417
- s === 60 && (X(p, t), i = "TAG_OPEN"), t++;
417
+ s === 60 && (X(c, t), i = "TAG_OPEN"), t++;
418
418
  break;
419
419
  case "TAG_OPEN":
420
- s === 33 ? n[t + 1] === 45 && n[t + 2] === 45 ? (i = "COMMENT", t += 3) : n[t + 1] === 68 || n[t + 1] === 100 ? y(n, t, Math.min(t + 20, n.length)).toLowerCase().startsWith("!doctype") ? (i = "DOCTYPE", g = t, t++) : (p = t - 1, i = "TEXT") : n[t + 1] === 91 && n[t + 2] === 67 && y(n, t, Math.min(t + 9, n.length)).startsWith("![CDATA[") ? (i = "CDATA", p = t + 8, t += 8) : (p = t - 1, i = "TEXT") : s === 63 ? (i = "PROCESSING_INSTRUCTION", g = t - 1, t++) : s === 47 ? (i = "TAG_CLOSE_START", t++) : N(s) ? (g = t, i = "TAG_NAME", h = null, t++) : (p = t - 1, i = "TEXT");
420
+ s === 33 ? r[t + 1] === 45 && r[t + 2] === 45 ? (i = "COMMENT", t += 3) : r[t + 1] === 68 || r[t + 1] === 100 ? d(r, t, Math.min(t + 20, r.length)).toLowerCase().startsWith("!doctype") ? (i = "DOCTYPE", y = t, t++) : (c = t - 1, i = "TEXT") : r[t + 1] === 91 && r[t + 2] === 67 && d(r, t, Math.min(t + 9, r.length)).startsWith("![CDATA[") ? (i = "CDATA", c = t + 8, t += 8) : (c = t - 1, i = "TEXT") : s === 63 ? (i = "PROCESSING_INSTRUCTION", y = t - 1, t++) : s === 47 ? (i = "TAG_CLOSE_START", t++) : N(s) ? (y = t, i = "TAG_NAME", h = null, t++) : (c = t - 1, i = "TEXT");
421
421
  break;
422
422
  case "TAG_NAME":
423
423
  if (S(s)) {
424
- const o = y(n, g, t), l = o.toLowerCase();
424
+ const f = d(r, y, t), p = f.toLowerCase();
425
425
  h = {
426
- type: o,
426
+ type: f,
427
427
  props: {},
428
428
  children: [],
429
- namespace: l === "svg" ? "SVG" : l === "math" ? "MATHML" : c
429
+ namespace: p === "svg" ? "SVG" : p === "math" ? "MATHML" : u
430
430
  }, i = "ATTRIBUTES", t++;
431
431
  } else if (s === 62) {
432
- const o = y(n, g, t);
433
- w(o, {}, !1);
434
- const l = C(o, c);
435
- l === "script" ? (i = "SCRIPT_CONTENT", p = t + 1) : l === "style" ? (i = "STYLE_CONTENT", p = t + 1) : (i = "TEXT", p = t + 1), t++;
436
- } else s === 47 ? (i = "TAG_CLOSE_SELF", t++) : N(s) ? t++ : (p = g - 1, i = "TEXT");
432
+ const f = d(r, y, t);
433
+ v(f, {}, !1);
434
+ const p = C(f, u);
435
+ p === "script" ? (i = "SCRIPT_CONTENT", c = t + 1) : p === "style" ? (i = "STYLE_CONTENT", c = t + 1) : (i = "TEXT", c = t + 1), t++;
436
+ } else s === 47 ? (i = "TAG_CLOSE_SELF", t++) : N(s) ? t++ : (c = y - 1, i = "TEXT");
437
437
  break;
438
438
  case "ATTRIBUTES":
439
439
  if (S(s))
440
440
  t++;
441
441
  else if (s === 62) {
442
- const o = h.type;
443
- w(o, h.props, !1);
444
- const l = C(o, c);
445
- l === "script" ? (i = "SCRIPT_CONTENT", p = t + 1) : l === "style" ? (i = "STYLE_CONTENT", p = t + 1) : (i = "TEXT", p = t + 1), t++;
446
- } else s === 47 ? (i = "TAG_CLOSE_SELF", t++) : N(s) ? (A = t, i = "ATTRIBUTE_NAME", t++) : (p = g - 1, i = "TEXT");
442
+ const f = h.type;
443
+ v(f, h.props, !1);
444
+ const p = C(f, u);
445
+ p === "script" ? (i = "SCRIPT_CONTENT", c = t + 1) : p === "style" ? (i = "STYLE_CONTENT", c = t + 1) : (i = "TEXT", c = t + 1), t++;
446
+ } else s === 47 ? (i = "TAG_CLOSE_SELF", t++) : N(s) ? (A = t, i = "ATTRIBUTE_NAME", t++) : (c = y - 1, i = "TEXT");
447
447
  break;
448
448
  case "ATTRIBUTE_NAME":
449
449
  if (S(s)) {
450
- d = y(n, A, t);
451
- const o = _(d, h.namespace);
452
- h.props[o] = !0, d = "", i = "ATTRIBUTES", t++;
450
+ g = d(r, A, t);
451
+ const f = _(g, h.namespace);
452
+ h.props[f] = !0, g = "", i = "ATTRIBUTES", t++;
453
453
  } else if (s === 61)
454
- d = y(n, A, t), i = "ATTRIBUTE_VALUE_START", t++;
454
+ g = d(r, A, t), i = "ATTRIBUTE_VALUE_START", t++;
455
455
  else if (s === 62) {
456
- d = y(n, A, t);
457
- const o = _(d, h.namespace);
458
- h.props[o] = !0, d = "";
459
- const l = h.type;
460
- w(l, h.props, !1);
461
- const T = C(l, c);
462
- T === "script" ? (i = "SCRIPT_CONTENT", p = t + 1) : T === "style" ? (i = "STYLE_CONTENT", p = t + 1) : (i = "TEXT", p = t + 1), t++;
456
+ g = d(r, A, t);
457
+ const f = _(g, h.namespace);
458
+ h.props[f] = !0, g = "";
459
+ const p = h.type;
460
+ v(p, h.props, !1);
461
+ const T = C(p, u);
462
+ T === "script" ? (i = "SCRIPT_CONTENT", c = t + 1) : T === "style" ? (i = "STYLE_CONTENT", c = t + 1) : (i = "TEXT", c = t + 1), t++;
463
463
  } else if (s === 47) {
464
- d = y(n, A, t);
465
- const o = _(d, h.namespace);
466
- h.props[o] = !0, d = "", i = "TAG_CLOSE_SELF", t++;
467
- } else N(s) ? t++ : (p = g - 1, i = "TEXT");
464
+ g = d(r, A, t);
465
+ const f = _(g, h.namespace);
466
+ h.props[f] = !0, g = "", i = "TAG_CLOSE_SELF", t++;
467
+ } else N(s) ? t++ : (c = y - 1, i = "TEXT");
468
468
  break;
469
469
  case "ATTRIBUTE_VALUE_START":
470
- S(s) ? t++ : s === 34 || s === 39 ? (P = s, E = t + 1, i = "ATTRIBUTE_VALUE_QUOTED", t++) : (E = t, i = "ATTRIBUTE_VALUE_UNQUOTED");
470
+ S(s) ? t++ : s === 34 || s === 39 ? (P = s, b = t + 1, i = "ATTRIBUTE_VALUE_QUOTED", t++) : (b = t, i = "ATTRIBUTE_VALUE_UNQUOTED");
471
471
  break;
472
472
  case "ATTRIBUTE_VALUE_QUOTED":
473
473
  if (s === P) {
474
- const o = y(n, E, t), l = v(o), T = _(d, h.namespace);
475
- T === "style" ? h.props[T] = D(l) : h.props[T] = l, d = "", i = "ATTRIBUTES", t++;
474
+ const f = d(r, b, t), p = w(f), T = _(g, h.namespace);
475
+ T === "style" ? h.props[T] = D(p) : h.props[T] = p, g = "", i = "ATTRIBUTES", t++;
476
476
  } else
477
477
  t++;
478
478
  break;
479
479
  case "ATTRIBUTE_VALUE_UNQUOTED":
480
480
  if (S(s) || s === 62 || s === 47) {
481
- const o = y(n, E, t), l = v(o), T = _(d, h.namespace);
482
- T === "style" ? h.props[T] = D(l) : h.props[T] = l, d = "", i = "ATTRIBUTES";
481
+ const f = d(r, b, t), p = w(f), T = _(g, h.namespace);
482
+ T === "style" ? h.props[T] = D(p) : h.props[T] = p, g = "", i = "ATTRIBUTES";
483
483
  } else
484
484
  t++;
485
485
  break;
486
486
  case "TAG_CLOSE_SELF":
487
487
  if (s === 62) {
488
- const o = h.type;
489
- w(o, h.props, !0), i = "TEXT", p = t + 1, t++;
488
+ const f = h.type;
489
+ v(f, h.props, !0), i = "TEXT", c = t + 1, t++;
490
490
  } else
491
- p = g - 1, i = "TEXT";
491
+ c = y - 1, i = "TEXT";
492
492
  break;
493
493
  case "TAG_CLOSE_START":
494
- N(s) ? (g = t, i = "TAG_CLOSE_NAME", t++) : (p = t - 2, i = "TEXT");
494
+ N(s) ? (y = t, i = "TAG_CLOSE_NAME", t++) : (c = t - 2, i = "TEXT");
495
495
  break;
496
496
  case "TAG_CLOSE_NAME":
497
497
  if (s === 62) {
498
- const o = y(n, g, t);
499
- R(o), i = "TEXT", p = t + 1, t++;
500
- } else S(s) || N(s) ? t++ : (p = g - 2, i = "TEXT");
498
+ const f = d(r, y, t);
499
+ R(f), i = "TEXT", c = t + 1, t++;
500
+ } else S(s) || N(s) ? t++ : (c = y - 2, i = "TEXT");
501
501
  break;
502
502
  case "COMMENT":
503
- s === 45 && n[t + 1] === 45 && n[t + 2] === 62 ? (i = "TEXT", p = t + 3, t += 3) : t++;
503
+ s === 45 && r[t + 1] === 45 && r[t + 2] === 62 ? (i = "TEXT", c = t + 3, t += 3) : t++;
504
504
  break;
505
505
  case "DOCTYPE":
506
506
  if (s === 62) {
507
- const o = y(n, g, t + 1);
508
- u.doctype = "<" + o, i = "TEXT", p = t + 1, t++;
507
+ const f = d(r, y, t + 1);
508
+ l.doctype = "<" + f, i = "TEXT", c = t + 1, t++;
509
509
  } else
510
510
  t++;
511
511
  break;
512
512
  case "CDATA":
513
- s === 93 && n[t + 1] === 93 && n[t + 2] === 62 ? (U(p, t), i = "TEXT", p = t + 3, t += 3) : t++;
513
+ s === 93 && r[t + 1] === 93 && r[t + 2] === 62 ? (U(c, t), i = "TEXT", c = t + 3, t += 3) : t++;
514
514
  break;
515
515
  case "PROCESSING_INSTRUCTION":
516
- if (s === 63 && n[t + 1] === 62) {
517
- const o = y(n, g, t + 2);
518
- u.xml = o, i = "TEXT", p = t + 2, t += 2;
516
+ if (s === 63 && r[t + 1] === 62) {
517
+ const f = d(r, y, t + 2);
518
+ l.xml = f, i = "TEXT", c = t + 2, t += 2;
519
519
  } else
520
520
  t++;
521
521
  break;
522
522
  case "SCRIPT_CONTENT":
523
523
  case "STYLE_CONTENT":
524
- if (s === 60 && n[t + 1] === 47) {
525
- const o = i === "SCRIPT_CONTENT" ? "script" : "style", l = "</" + o, T = y(n, t, Math.min(t + l.length + 1, n.length)).toLowerCase();
526
- if (T.startsWith(l) && (T[l.length] === ">" || S(T.charCodeAt(l.length)))) {
527
- U(p, t);
528
- let m = t + l.length;
529
- for (; m < n.length && n[m] !== 62; ) m++;
530
- R(o), i = "TEXT", p = m + 1, t = m + 1;
524
+ if (s === 60 && r[t + 1] === 47) {
525
+ const f = i === "SCRIPT_CONTENT" ? "script" : "style", p = "</" + f, T = d(r, t, Math.min(t + p.length + 1, r.length)).toLowerCase();
526
+ if (T.startsWith(p) && (T[p.length] === ">" || S(T.charCodeAt(p.length)))) {
527
+ U(c, t);
528
+ let m = t + p.length;
529
+ for (; m < r.length && r[m] !== 62; ) m++;
530
+ R(f), i = "TEXT", c = m + 1, t = m + 1;
531
531
  } else
532
532
  t++;
533
533
  } else
@@ -537,197 +537,218 @@ function K(e) {
537
537
  t++;
538
538
  }
539
539
  }
540
- for (i === "TEXT" ? X(p, t) : (i === "SCRIPT_CONTENT" || i === "STYLE_CONTENT") && U(p, t); a.length > 0; ) {
541
- const s = a.pop(), o = {
540
+ for (i === "TEXT" ? X(c, t) : (i === "SCRIPT_CONTENT" || i === "STYLE_CONTENT") && U(c, t); o.length > 0; ) {
541
+ const s = o.pop(), f = {
542
542
  type: s.type,
543
543
  props: {
544
544
  ...s.props,
545
545
  children: s.children.length === 1 ? s.children[0] : s.children
546
546
  }
547
547
  };
548
- a.length > 0 ? a[a.length - 1].children.push(o) : f.push(o);
548
+ o.length > 0 ? o[o.length - 1].children.push(f) : a.push(f);
549
549
  }
550
- return f.length === 0 ? u.node = null : f.length === 1 ? u.node = f[0] : u.node = f, u;
550
+ return a.length === 0 ? l.node = null : a.length === 1 ? l.node = a[0] : l.node = a, l;
551
551
  }
552
552
  function D(e) {
553
- const r = {}, u = v(e).split(/;(?![^(]*\))/);
554
- for (const f of u) {
555
- const a = f.indexOf(":");
556
- if (a === -1) continue;
557
- const c = f.substring(0, a).trim(), i = f.substring(a + 1).trim();
558
- c && i && (r[k(c)] = i);
553
+ const n = {}, l = w(e).split(/;(?![^(]*\))/);
554
+ for (const a of l) {
555
+ const o = a.indexOf(":");
556
+ if (o === -1) continue;
557
+ const u = a.substring(0, o).trim(), i = a.substring(o + 1).trim();
558
+ u && i && (n[k(u)] = i);
559
559
  }
560
- return r;
560
+ return n;
561
561
  }
562
- function H(e, r = {}) {
563
- const n = [];
564
- let u, f, a;
562
+ function H(e, n = {}) {
563
+ const r = [];
564
+ let l, a, o;
565
565
  if (e && typeof e == "object" && "node" in e) {
566
- const p = e;
567
- u = p.node, f = p.xml ?? r.xml, a = p.doctype ?? r.doctype;
566
+ const c = e;
567
+ l = c.node, a = c.xml ?? n.xml, o = c.doctype ?? n.doctype;
568
568
  } else
569
- u = e, f = r.xml, a = r.doctype;
570
- f && (n.push(f), f.endsWith(`
571
- `) || n.push(`
572
- `)), a && (n.push(a), a.endsWith(`
573
- `) || n.push(`
569
+ l = e, a = n.xml, o = n.doctype;
570
+ a && (r.push(a), a.endsWith(`
571
+ `) || r.push(`
572
+ `)), o && (r.push(o), o.endsWith(`
573
+ `) || r.push(`
574
574
  `));
575
- const c = r.useCDataForScripts ?? !1, i = r.useCDataForStyles ?? !1, t = r.voidTrailingSlash ?? !0;
576
- return O(u, n, "HTML", c, i, t), n.join("");
575
+ const u = n.useCDataForScripts ?? !1, i = n.useCDataForStyles ?? !1, t = n.voidTrailingSlash ?? !0;
576
+ return O(l, r, "HTML", u, i, t), r.join("");
577
577
  }
578
- function O(e, r, n, u, f, a) {
578
+ function O(e, n, r, l, a, o) {
579
579
  if (e != null) {
580
580
  if (typeof e == "string") {
581
- r.push(G(e, !1));
581
+ n.push(x(e, !1));
582
582
  return;
583
583
  }
584
584
  if (typeof e == "number" || typeof e == "bigint") {
585
- r.push(String(e));
585
+ n.push(String(e));
586
586
  return;
587
587
  }
588
588
  if (typeof e == "boolean") {
589
- r.push(String(e));
589
+ n.push(String(e));
590
590
  return;
591
591
  }
592
592
  if (Array.isArray(e)) {
593
- for (const c of e)
594
- O(c, r, n, u, f, a);
593
+ for (const u of e)
594
+ O(u, n, r, l, a, o);
595
595
  return;
596
596
  }
597
597
  if (typeof e != "function" && typeof e != "symbol" && !(e && typeof e == "object" && "then" in e && typeof e.then == "function") && e && typeof e == "object" && "type" in e && "props" in e) {
598
- J(e, r, n, u, f, a);
598
+ J(e, n, r, l, a, o);
599
599
  return;
600
600
  }
601
601
  }
602
602
  }
603
- function J(e, r, n, u, f, a) {
604
- var g;
605
- const c = e.type;
606
- let i = n;
607
- if (c === "svg" ? i = "SVG" : c === "math" && (i = "MATHML"), r.push("<"), r.push(c), e.props) {
608
- for (const [A, E] of Object.entries(e.props))
609
- if (A !== "children" && !(E === !1 || E === null || E === void 0))
610
- if (E === !0)
611
- r.push(" "), r.push(A);
612
- else if (A === "style" && typeof E == "object") {
613
- const d = ee(E);
614
- d && (r.push(' style="'), r.push(G(d, !0)), r.push('"'));
603
+ function J(e, n, r, l, a, o) {
604
+ var y;
605
+ const u = e.type;
606
+ let i = r;
607
+ if (u === "svg" ? i = "SVG" : u === "math" && (i = "MATHML"), n.push("<"), n.push(u), e.props) {
608
+ for (const [A, b] of Object.entries(e.props))
609
+ if (A !== "children" && !(b === !1 || b === null || b === void 0))
610
+ if (b === !0)
611
+ n.push(" "), n.push(A);
612
+ else if (A === "style" && typeof b == "object") {
613
+ const g = ee(b);
614
+ g && (n.push(' style="'), n.push(x(g, !0)), n.push('"'));
615
615
  } else
616
- r.push(" "), r.push(A), r.push('="'), r.push(G(String(E), !0)), r.push('"');
616
+ n.push(" "), n.push(A), n.push('="'), n.push(x(String(b), !0)), n.push('"');
617
617
  }
618
- const t = (g = e.props) == null ? void 0 : g.children;
619
- let p;
620
- if (t && typeof t == "object" && "then" in t && typeof t.then == "function" ? p = !1 : p = t != null && (Array.isArray(t) && t.length > 0 || !Array.isArray(t) && t !== !1), F.has(c))
621
- a ? r.push(" />") : r.push(">");
622
- else if (p) {
623
- r.push(">");
618
+ const t = (y = e.props) == null ? void 0 : y.children;
619
+ let c;
620
+ if (t && typeof t == "object" && "then" in t && typeof t.then == "function" ? c = !1 : c = t != null && (Array.isArray(t) && t.length > 0 || !Array.isArray(t) && t !== !1), F.has(u))
621
+ o ? n.push(" />") : n.push(">");
622
+ else if (c) {
623
+ n.push(">");
624
624
  const A = t;
625
- c === "script" && u || c === "style" && f ? (r.push("<![CDATA["), x(A, r, i, u, f, a), r.push("]]>")) : c === "script" || c === "style" ? W(A, r) : O(A, r, i, u, f, a), r.push("</"), r.push(c), r.push(">");
626
- } else Q.has(c), r.push("></"), r.push(c), r.push(">");
625
+ u === "script" && l || u === "style" && a ? (n.push("<![CDATA["), G(A, n, i, l, a, o), n.push("]]>")) : u === "script" || u === "style" ? W(A, n) : O(A, n, i, l, a, o), n.push("</"), n.push(u), n.push(">");
626
+ } else Q.has(u), n.push("></"), n.push(u), n.push(">");
627
627
  }
628
- function x(e, r, n, u, f, a, c) {
628
+ function G(e, n, r, l, a, o, u) {
629
629
  if (e != null) {
630
630
  if (typeof e == "string") {
631
- r.push(Z(e));
631
+ n.push(Z(e));
632
632
  return;
633
633
  }
634
634
  if (typeof e == "number" || typeof e == "bigint" || typeof e == "boolean") {
635
- r.push(String(e));
635
+ n.push(String(e));
636
636
  return;
637
637
  }
638
638
  if (Array.isArray(e)) {
639
639
  for (const i of e)
640
- x(i, r, n, u, f, a);
640
+ G(i, n, r, l, a, o);
641
641
  return;
642
642
  }
643
- O(e, r, n, u, f, a);
643
+ O(e, n, r, l, a, o);
644
644
  }
645
645
  }
646
- function W(e, r) {
646
+ function W(e, n) {
647
647
  if (e != null) {
648
648
  if (typeof e == "string") {
649
- r.push(e);
649
+ n.push(e);
650
650
  return;
651
651
  }
652
652
  if (typeof e == "number" || typeof e == "bigint" || typeof e == "boolean") {
653
- r.push(String(e));
653
+ n.push(String(e));
654
654
  return;
655
655
  }
656
656
  if (Array.isArray(e)) {
657
- for (const n of e)
658
- W(n, r);
657
+ for (const r of e)
658
+ W(r, n);
659
659
  return;
660
660
  }
661
661
  }
662
662
  }
663
663
  function ee(e) {
664
- const r = [];
665
- for (const [n, u] of Object.entries(e))
666
- if (u) {
667
- const f = $(n);
668
- r.push(`${f}: ${u}`);
664
+ const n = [];
665
+ for (const [r, l] of Object.entries(e))
666
+ if (l) {
667
+ const a = $(r);
668
+ n.push(`${a}: ${l}`);
669
669
  }
670
- return r.join("; ");
670
+ return n.join("; ");
671
+ }
672
+ function te(e) {
673
+ const n = [];
674
+ function r(l) {
675
+ if (typeof l == "string" && n.push(l), (typeof l == "number" || typeof l == "bigint" || typeof l == "boolean") && n.push(`${l}`), typeof l == "object" && l !== null && "type" in l && l.props && "children" in l.props) {
676
+ const a = l.props.children;
677
+ if (l.type === "script" || l.type === "style" || l.type === "template" || a && typeof a == "object" && "then" in a && typeof a.then == "function")
678
+ return;
679
+ if (Array.isArray(a))
680
+ for (const o of a)
681
+ r(o);
682
+ else a != null && r(a);
683
+ }
684
+ if (Array.isArray(l))
685
+ for (const a of l)
686
+ r(a);
687
+ }
688
+ return r(e), n.join("");
671
689
  }
672
690
  async function L(e) {
673
691
  if (e == null)
674
692
  return e;
675
693
  if (e && typeof e == "object" && "then" in e && typeof e.then == "function") {
676
- const r = await e;
677
- return L(r);
694
+ const n = await e;
695
+ return L(n);
678
696
  }
679
697
  if (typeof e == "string" || typeof e == "number" || typeof e == "boolean" || typeof e == "bigint")
680
698
  return e;
681
699
  if (typeof e != "function" && typeof e != "symbol") {
682
700
  if (Array.isArray(e))
683
701
  return await Promise.all(
684
- e.filter((n) => !(n == null || typeof n == "function" || typeof n == "symbol")).map((n) => L(n))
702
+ e.filter((r) => !(r == null || typeof r == "function" || typeof r == "symbol")).map((r) => L(r))
685
703
  );
686
704
  if (e && typeof e == "object" && "type" in e && "props" in e) {
687
- const r = e, n = {};
688
- for (const [u, f] of Object.entries(r.props))
689
- if (u === "children") {
690
- const a = await L(f);
691
- a != null && (n.children = a);
705
+ const n = e, r = {};
706
+ for (const [l, a] of Object.entries(n.props))
707
+ if (l === "children") {
708
+ const o = await L(a);
709
+ o != null && (r.children = o);
692
710
  } else {
693
- if (typeof f == "function" || typeof f == "symbol")
711
+ if (typeof a == "function" || typeof a == "symbol")
694
712
  continue;
695
- f != null && (n[u] = f);
713
+ a != null && (r[l] = a);
696
714
  }
697
715
  return {
698
- type: r.type,
699
- props: n
716
+ type: n.type,
717
+ props: r
700
718
  };
701
719
  }
702
720
  }
703
721
  }
704
- function Y(e, r) {
722
+ function Y(e, n) {
705
723
  if (e == null || typeof e == "string" || typeof e == "number" || typeof e == "boolean" || typeof e == "bigint")
706
724
  return e;
707
725
  if (Array.isArray(e))
708
- return e.map((n) => Y(n, r));
726
+ return e.map((r) => Y(r, n));
709
727
  if (typeof e == "object" && "type" in e && "props" in e) {
710
- const { type: n, props: u } = e, { children: f, ...a } = u;
711
- if (f !== void 0) {
712
- const c = Y(f, r);
713
- return c === null ? r(n, a) : Array.isArray(c) ? r(n, a, ...c) : r(n, a, c);
728
+ const { type: r, props: l } = e, { children: a, ...o } = l;
729
+ if (a !== void 0) {
730
+ const u = Y(a, n);
731
+ return u === null ? n(r, o) : Array.isArray(u) ? n(r, o, ...u) : n(r, o, u);
714
732
  }
715
- return r(n, a);
733
+ return n(r, o);
716
734
  }
717
735
  return null;
718
736
  }
719
- function te(e) {
737
+ function re(e) {
720
738
  return K(e);
721
739
  }
722
- function re(e, r = {}) {
723
- return e && typeof e == "object" && "node" in e ? H(e, r) : e === void 0 ? "" : H(e, r);
740
+ function ne(e, n = {}) {
741
+ return e && typeof e == "object" && "node" in e ? H(e, n) : e === void 0 ? "" : H(e, n);
724
742
  }
725
- async function ne(e) {
743
+ async function ie(e) {
726
744
  return await L(e);
727
745
  }
728
746
  export {
729
- ne as awaitHtmlNode,
747
+ ie as awaitHtmlNode,
748
+ w as decodeHtmlEntities,
749
+ x as encodeHtmlEntities,
750
+ te as getTextContent,
730
751
  Y as htmlNodeTo,
731
- te as readHtml,
732
- re as writeHtml
752
+ re as readHtml,
753
+ ne as writeHtml
733
754
  };
package/dist/writer.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  import { HtmlNode, WriterOptions, ParseResult } from './types.ts';
2
2
  export declare function renderHtml(input: HtmlNode | ParseResult, options?: WriterOptions): string;
3
+ export declare function getTextContent(node: HtmlNode): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@levischuck/tiny-html",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.js",