@unicom-cloud/utils 0.1.12 → 0.1.14

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.
Files changed (35) hide show
  1. package/filesize/src/constants.js +22 -22
  2. package/filesize/src/filesize.js +32 -33
  3. package/package.json +1 -1
  4. package/snapdom/src/api/preCache.js +23 -23
  5. package/snapdom/src/api/snapdom.js +67 -63
  6. package/snapdom/src/core/cache.js +19 -8
  7. package/snapdom/src/core/capture.js +46 -45
  8. package/snapdom/src/core/clone.js +94 -31
  9. package/snapdom/src/core/prepare.js +35 -38
  10. package/snapdom/src/modules/background.js +29 -20
  11. package/snapdom/src/modules/fonts.js +93 -101
  12. package/snapdom/src/modules/pseudo.js +70 -75
  13. package/snapdom/src/modules/styles.js +22 -22
  14. package/snapdom/src/utils/cssTools.js +41 -41
  15. package/snapdom/src/utils/helpers.js +143 -146
  16. package/tree/index.js +1 -1
  17. package/tree/search/index.js +90 -0
  18. package/tree.js +1 -1
  19. package/types/filesize/src/constants.d.ts +12 -12
  20. package/types/filesize/src/filesize.d.ts +63 -22
  21. package/types/snapdom/src/core/cache.d.ts +17 -11
  22. package/types/snapdom/src/core/capture.d.ts +4 -0
  23. package/types/snapdom/src/core/clone.d.ts +4 -5
  24. package/types/snapdom/src/core/prepare.d.ts +8 -1
  25. package/types/snapdom/src/modules/background.d.ts +1 -2
  26. package/types/snapdom/src/modules/pseudo.d.ts +1 -3
  27. package/types/snapdom/src/modules/styles.d.ts +1 -1
  28. package/types/snapdom/src/utils/cssTools.d.ts +1 -2
  29. package/types/tree/index.d.ts +1 -1
  30. package/types/tree/search/index.d.ts +80 -0
  31. package/types/ui-color/index.d.ts +28 -3
  32. package/ui-color/index.js +70 -63
  33. package/uiColor.js +19 -18
  34. package/tree/searchTree.js +0 -54
  35. package/types/tree/searchTree.d.ts +0 -20
@@ -1,94 +1,89 @@
1
- import { iconToImage as z } from "./fonts.js";
2
- import { isIconFont as U } from "./iconFonts.js";
3
- import { getStyleKey as N } from "../utils/cssTools.js";
4
- import { getStyle as W, snapshotComputedStyle as V, parseContent as j, extractURL as A, fetchImage as D, safeEncodeURI as L, splitBackgroundImage as R, inlineSingleBackgroundEntry as K } from "../utils/helpers.js";
5
- async function M(c, r, h, F, u, T = !1, w) {
6
- if (!(c instanceof Element) || !(r instanceof Element)) return;
1
+ import { cache as M } from "../core/cache.js";
2
+ import { iconToImage as q } from "./fonts.js";
3
+ import { isIconFont as v } from "./iconFonts.js";
4
+ import { getStyleKey as $ } from "../utils/cssTools.js";
5
+ import { getStyle as G, snapshotComputedStyle as z, parseContent as H, extractURL as J, fetchImage as Q, safeEncodeURI as Y, splitBackgroundImage as Z, inlineSingleBackgroundEntry as tt } from "../utils/helpers.js";
6
+ async function et(c, s, p, R = !1, b) {
7
+ if (!(c instanceof Element) || !(s instanceof Element)) return;
7
8
  for (const n of ["::before", "::after", "::first-letter"])
8
9
  try {
9
- const t = W(c, n);
10
+ const t = G(c, n);
10
11
  if (!t || typeof t[Symbol.iterator] != "function") continue;
11
12
  if (n === "::first-letter") {
12
- const l = getComputedStyle(c);
13
- if (!(t.color !== l.color || t.fontSize !== l.fontSize || t.fontWeight !== l.fontWeight)) continue;
14
- const d = Array.from(r.childNodes).find(
15
- (e) => e.nodeType === Node.TEXT_NODE && e.textContent && e.textContent.trim().length > 0
13
+ const e = getComputedStyle(c);
14
+ if (!(t.color !== e.color || t.fontSize !== e.fontSize || t.fontWeight !== e.fontWeight)) continue;
15
+ const i = Array.from(s.childNodes).find(
16
+ (C) => C.nodeType === Node.TEXT_NODE && C.textContent && C.textContent.trim().length > 0
16
17
  );
17
- if (!d) continue;
18
- const g = d.textContent, f = g.match(/^([^\p{L}\p{N}\s]*[\p{L}\p{N}](?:['’])?)/u)?.[0], x = g.slice(f?.length || 0);
19
- if (!f || /[\uD800-\uDFFF]/.test(f)) continue;
20
- const a = document.createElement("span");
21
- a.textContent = f, a.dataset.snapdomPseudo = "::first-letter";
22
- const s = V(t), I = N(s, "span", u);
23
- h.set(a, I);
24
- const E = document.createTextNode(x);
25
- r.replaceChild(E, d), r.insertBefore(a, E);
18
+ if (!i) continue;
19
+ const N = i.textContent, d = N.match(/^([^\p{L}\p{N}\s]*[\p{L}\p{N}](?:['’])?)/u)?.[0], O = N.slice(d?.length || 0);
20
+ if (!d || /[\uD800-\uDFFF]/.test(d)) continue;
21
+ const f = document.createElement("span");
22
+ f.textContent = d, f.dataset.snapdomPseudo = "::first-letter";
23
+ const X = z(t), _ = $(X, "span", p);
24
+ M.preStyleMap.set(f, _);
25
+ const T = document.createTextNode(O);
26
+ s.replaceChild(T, i), s.insertBefore(f, T);
26
27
  continue;
27
28
  }
28
- const B = t.getPropertyValue("content"), y = t.getPropertyValue("background-image"), m = t.getPropertyValue("background-color"), $ = B !== "none", C = y && y !== "none", b = m && m !== "transparent" && m !== "rgba(0, 0, 0, 0)";
29
- if ($ || C || b) {
30
- const l = t.getPropertyValue("font-family"), p = parseInt(t.getPropertyValue("font-size")) || 32, d = parseInt(t.getPropertyValue("font-weight")) || !1, g = t.getPropertyValue("color") || "#000", o = document.createElement("span");
31
- o.dataset.snapdomPseudo = n;
32
- const f = V(t), x = N(f, "span", u);
33
- h.set(o, x);
34
- const a = U(l), s = j(B);
35
- if (a && s.length === 1) {
36
- const e = document.createElement("img");
37
- e.src = await z(
38
- s,
39
- l,
40
- d,
41
- p,
42
- g
43
- ), e.style = `width:${p}px;height:auto;object-fit:contain;`, o.appendChild(e);
44
- } else if (s.startsWith("url(")) {
45
- const e = A(s);
46
- if (e && e.trim() !== "")
47
- try {
48
- const i = document.createElement("img"), P = await D(
49
- L(e, { useProxy: w })
50
- );
51
- i.src = P, i.style = `width:${p}px;height:auto;object-fit:contain;`, o.appendChild(i);
52
- } catch (i) {
53
- console.error(
54
- `[snapdom] Error in pseudo ${n} for`,
55
- c,
56
- i
57
- );
58
- }
59
- } else !a && s && s !== "none" && (o.textContent = s);
60
- if (C)
29
+ const h = t.getPropertyValue("content"), m = t.getPropertyValue("background-image"), l = t.getPropertyValue("background-color"), E = t.getPropertyValue("font-family"), g = parseInt(t.getPropertyValue("font-size")) || 32, U = parseInt(t.getPropertyValue("font-weight")) || !1, W = t.getPropertyValue("color") || "#000", j = t.getPropertyValue("display"), A = parseFloat(t.getPropertyValue("width")), D = parseFloat(t.getPropertyValue("height")), V = t.getPropertyValue("border-style"), w = t.getPropertyValue("transform"), S = v(E);
30
+ let r;
31
+ /counter\s*\(|counters\s*\(/.test(h) ? r = "- " : r = H(h);
32
+ const B = h !== "none" && r !== "", u = m && m !== "none", y = l && l !== "transparent" && l !== "rgba(0, 0, 0, 0)", F = j !== "inline" && (A > 0 || D > 0), k = V && V !== "none", I = w && w !== "none";
33
+ if (!(B || u || y || F || k || I)) continue;
34
+ const o = document.createElement("span");
35
+ o.dataset.snapdomPseudo = n;
36
+ const L = z(t), K = $(L, "span", p);
37
+ if (M.preStyleMap.set(o, K), S && r.length === 1) {
38
+ const e = document.createElement("img");
39
+ e.src = await q(
40
+ r,
41
+ E,
42
+ U,
43
+ g,
44
+ W
45
+ ), e.style = `width:${g}px;height:auto;object-fit:contain;`, o.appendChild(e);
46
+ } else if (r.startsWith("url(")) {
47
+ const e = J(r);
48
+ if (e && e.trim() !== "")
61
49
  try {
62
- const e = R(y), i = await Promise.all(
63
- e.map((P) => K(P))
64
- );
65
- o.style.backgroundImage = i.join(", ");
66
- } catch (e) {
67
- console.warn(
68
- `[snapdom] Failed to inline background-image for ${n}`,
69
- e
50
+ const a = document.createElement("img"), i = await Q(
51
+ Y(e, { useProxy: b })
70
52
  );
53
+ a.src = i, a.style = `width:${g}px;height:auto;object-fit:contain;`, o.appendChild(a);
54
+ } catch (a) {
55
+ console.error(`[snapdom] Error in pseudo ${n} for`, c, a);
71
56
  }
72
- if (b && (o.style.backgroundColor = m), !(o.childNodes.length > 0 || o.textContent && o.textContent.trim() !== "" || C || b)) continue;
73
- n === "::before" ? r.insertBefore(o, r.firstChild) : r.appendChild(o);
74
- }
57
+ } else !S && B && (o.textContent = r);
58
+ if (u)
59
+ try {
60
+ const e = Z(m), a = await Promise.all(
61
+ e.map((i) => tt(i))
62
+ );
63
+ o.style.backgroundImage = a.join(", ");
64
+ } catch (e) {
65
+ console.warn(
66
+ `[snapdom] Failed to inline background-image for ${n}`,
67
+ e
68
+ );
69
+ }
70
+ if (y && (o.style.backgroundColor = l), !(o.childNodes.length > 0 || o.textContent && o.textContent.trim() !== "" || u || y || F || k || I)) continue;
71
+ n === "::before" ? s.insertBefore(o, s.firstChild) : s.appendChild(o);
75
72
  } catch (t) {
76
73
  console.warn(`[snapdom] Failed to capture ${n} for`, c, t);
77
74
  }
78
- const S = Array.from(c.children), k = Array.from(r.children).filter(
75
+ const x = Array.from(c.children), P = Array.from(s.children).filter(
79
76
  (n) => !n.dataset.snapdomPseudo
80
77
  );
81
- for (let n = 0; n < Math.min(S.length, k.length); n++)
82
- await M(
83
- S[n],
84
- k[n],
85
- h,
86
- F,
87
- u,
88
- T,
89
- w
78
+ for (let n = 0; n < Math.min(x.length, P.length); n++)
79
+ await et(
80
+ x[n],
81
+ P[n],
82
+ p,
83
+ R,
84
+ b
90
85
  );
91
86
  }
92
87
  export {
93
- M as inlinePseudoElements
88
+ et as inlinePseudoElements
94
89
  };
@@ -1,31 +1,31 @@
1
- import { getStyleKey as c } from "../utils/cssTools.js";
2
- import { getStyle as d } from "../utils/helpers.js";
3
- const p = /* @__PURE__ */ new WeakMap(), r = /* @__PURE__ */ new Map();
4
- function y(t) {
5
- const o = {};
6
- for (let n = 0; n < t.length; n++) {
7
- const e = t[n];
8
- let a = t.getPropertyValue(e);
9
- (e === "background-image" || e === "content") && a.includes("url(") && !a.includes("data:") && (a = "none"), o[e] = a;
1
+ import { cache as e } from "../core/cache.js";
2
+ import { getStyleKey as y } from "../utils/cssTools.js";
3
+ import { getStyle as m } from "../utils/helpers.js";
4
+ function g(t) {
5
+ const a = {};
6
+ for (let o = 0; o < t.length; o++) {
7
+ const s = t[o];
8
+ let n = t.getPropertyValue(s);
9
+ (s === "background-image" || s === "content") && n.includes("url(") && !n.includes("data:") && (n = "none"), a[s] = n;
10
10
  }
11
- return o;
11
+ return a;
12
12
  }
13
- function S(t, o, n, e, a) {
13
+ function d(t, a, o) {
14
14
  if (t.tagName === "STYLE") return;
15
- e.has(t) || e.set(t, d(t));
16
- const m = e.get(t);
17
- if (!p.has(t)) {
18
- const s = y(m);
19
- p.set(t, s);
15
+ e.preStyle.has(t) || e.preStyle.set(t, m(t));
16
+ const s = e.preStyle.get(t);
17
+ if (!e.snapshot.has(t)) {
18
+ const p = g(s);
19
+ e.snapshot.set(t, p);
20
20
  }
21
- const g = p.get(t), i = Object.entries(g).sort(([s], [l]) => s.localeCompare(l)).map(([s, l]) => `${s}:${l}`).join(";");
22
- if (r.has(i)) {
23
- n.set(o, r.get(i));
21
+ const n = e.snapshot.get(t), l = Object.entries(n).sort(([p], [r]) => p.localeCompare(r)).map(([p, r]) => `${p}:${r}`).join(";");
22
+ if (e.snapshotKey.has(l)) {
23
+ e.preStyleMap.set(a, e.snapshotKey.get(l));
24
24
  return;
25
25
  }
26
- const f = t.tagName?.toLowerCase() || "div", h = c(g, f, a);
27
- r.set(i, h), n.set(o, h);
26
+ const h = t.tagName?.toLowerCase() || "div", i = y(n, h, o);
27
+ e.snapshotKey.set(l, i), e.preStyleMap.set(a, i);
28
28
  }
29
29
  export {
30
- S as inlineAllStyles
30
+ d as inlineAllStyles
31
31
  };
@@ -1,4 +1,4 @@
1
- import { defaultStylesCache as r } from "../core/cache.js";
1
+ import { cache as r } from "../core/cache.js";
2
2
  const d = [
3
3
  "div",
4
4
  "span",
@@ -39,12 +39,12 @@ const d = [
39
39
  "th"
40
40
  ];
41
41
  function u() {
42
- for (let t of d)
43
- c(t);
42
+ for (let e of d)
43
+ c(e);
44
44
  }
45
- function c(t) {
46
- if (r.has(t))
47
- return r.get(t);
45
+ function c(e) {
46
+ if (r.defaultStyle.has(e))
47
+ return r.defaultStyle.get(e);
48
48
  if ((/* @__PURE__ */ new Set([
49
49
  "script",
50
50
  "style",
@@ -52,54 +52,54 @@ function c(t) {
52
52
  "link",
53
53
  "noscript",
54
54
  "template"
55
- ])).has(t)) {
55
+ ])).has(e)) {
56
56
  const o = {};
57
- return r.set(t, o), o;
57
+ return r.defaultStyle.set(e, o), o;
58
58
  }
59
- let e = document.getElementById("snapdom-sandbox");
60
- e || (e = document.createElement("div"), e.id = "snapdom-sandbox", e.style.position = "absolute", e.style.left = "-9999px", e.style.top = "-9999px", e.style.width = "0", e.style.height = "0", e.style.overflow = "hidden", document.body.appendChild(e));
61
- const n = document.createElement(t);
62
- n.style.all = "initial", e.appendChild(n);
63
- const l = getComputedStyle(n), a = {};
64
- for (let o of l)
65
- a[o] = l.getPropertyValue(o);
66
- return e.removeChild(n), r.set(t, a), a;
59
+ let t = document.getElementById("snapdom-sandbox");
60
+ t || (t = document.createElement("div"), t.id = "snapdom-sandbox", t.style.position = "absolute", t.style.left = "-9999px", t.style.top = "-9999px", t.style.width = "0", t.style.height = "0", t.style.overflow = "hidden", document.body.appendChild(t));
61
+ const n = document.createElement(e);
62
+ n.style.all = "initial", t.appendChild(n);
63
+ const a = getComputedStyle(n), l = {};
64
+ for (let o of a)
65
+ l[o] = a.getPropertyValue(o);
66
+ return t.removeChild(n), r.defaultStyle.set(e, l), l;
67
67
  }
68
- function p(t, s, e = !1) {
69
- const n = [], l = c(s);
70
- for (let [a, o] of Object.entries(t))
71
- if (!e)
72
- o && n.push(`${a}:${o}`);
68
+ function p(e, s, t = !1) {
69
+ const n = [], a = c(s);
70
+ for (let [l, o] of Object.entries(e))
71
+ if (!t)
72
+ o && n.push(`${l}:${o}`);
73
73
  else {
74
- const i = l[a];
75
- o && o !== i && n.push(`${a}:${o}`);
74
+ const i = a[l];
75
+ o && o !== i && n.push(`${l}:${o}`);
76
76
  }
77
77
  return n.sort().join(";");
78
78
  }
79
- function y(t) {
79
+ function y(e) {
80
80
  const s = /* @__PURE__ */ new Set();
81
- return t.nodeType !== Node.ELEMENT_NODE && t.nodeType !== Node.DOCUMENT_FRAGMENT_NODE ? [] : (t.tagName && s.add(t.tagName.toLowerCase()), typeof t.querySelectorAll == "function" && t.querySelectorAll("*").forEach((e) => s.add(e.tagName.toLowerCase())), Array.from(s));
81
+ return e.nodeType !== Node.ELEMENT_NODE && e.nodeType !== Node.DOCUMENT_FRAGMENT_NODE ? [] : (e.tagName && s.add(e.tagName.toLowerCase()), typeof e.querySelectorAll == "function" && e.querySelectorAll("*").forEach((t) => s.add(t.tagName.toLowerCase())), Array.from(s));
82
82
  }
83
- function h(t) {
83
+ function h(e) {
84
84
  const s = /* @__PURE__ */ new Map();
85
- for (let n of t) {
86
- const l = r.get(n);
87
- if (!l) continue;
88
- const a = Object.entries(l).map(([o, i]) => `${o}:${i};`).sort().join("");
89
- s.has(a) || s.set(a, []), s.get(a).push(n);
85
+ for (let n of e) {
86
+ const a = r.defaultStyle.get(n);
87
+ if (!a) continue;
88
+ const l = Object.entries(a).map(([o, i]) => `${o}:${i};`).sort().join("");
89
+ s.has(l) || s.set(l, []), s.get(l).push(n);
90
90
  }
91
- let e = "";
92
- for (let [n, l] of s.entries())
93
- e += `${l.join(",")} { ${n} }
91
+ let t = "";
92
+ for (let [n, a] of s.entries())
93
+ t += `${a.join(",")} { ${n} }
94
94
  `;
95
- return e;
95
+ return t;
96
96
  }
97
- function m(t) {
98
- const s = new Set(t.values()), e = /* @__PURE__ */ new Map();
99
- let n = 1;
100
- for (const l of s)
101
- e.set(l, `c${n++}`);
102
- return e;
97
+ function m() {
98
+ const e = new Set(r.preStyleMap.values()), s = /* @__PURE__ */ new Map();
99
+ let t = 1;
100
+ for (const n of e)
101
+ s.set(n, `c${t++}`);
102
+ return s;
103
103
  }
104
104
  export {
105
105
  y as collectUsedTagNames,