@unicom-cloud/utils 0.1.12 → 0.1.13
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/filesize/src/constants.js +22 -22
- package/filesize/src/filesize.js +32 -33
- package/package.json +1 -1
- package/snapdom/src/api/preCache.js +23 -23
- package/snapdom/src/api/snapdom.js +42 -42
- package/snapdom/src/core/cache.js +19 -8
- package/snapdom/src/core/capture.js +60 -59
- package/snapdom/src/core/clone.js +96 -31
- package/snapdom/src/core/prepare.js +35 -38
- package/snapdom/src/modules/background.js +29 -20
- package/snapdom/src/modules/fonts.js +97 -105
- package/snapdom/src/modules/pseudo.js +32 -32
- package/snapdom/src/modules/styles.js +22 -22
- package/snapdom/src/utils/cssTools.js +41 -41
- package/snapdom/src/utils/helpers.js +143 -146
- package/tree/index.js +1 -1
- package/tree/search/index.js +90 -0
- package/tree.js +1 -1
- package/types/filesize/src/constants.d.ts +12 -12
- package/types/filesize/src/filesize.d.ts +63 -22
- package/types/snapdom/src/core/cache.d.ts +17 -11
- package/types/snapdom/src/core/capture.d.ts +4 -0
- package/types/snapdom/src/core/clone.d.ts +4 -5
- package/types/snapdom/src/core/prepare.d.ts +8 -1
- package/types/snapdom/src/modules/background.d.ts +1 -2
- package/types/snapdom/src/modules/pseudo.d.ts +1 -3
- package/types/snapdom/src/modules/styles.d.ts +1 -1
- package/types/snapdom/src/utils/cssTools.d.ts +1 -2
- package/types/tree/index.d.ts +1 -1
- package/types/tree/search/index.d.ts +80 -0
- package/types/ui-color/index.d.ts +28 -3
- package/ui-color/index.js +70 -63
- package/uiColor.js +19 -18
- package/tree/searchTree.js +0 -54
- package/types/tree/searchTree.d.ts +0 -20
|
@@ -1,51 +1,116 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { cache as r } from "./cache.js";
|
|
2
|
+
import { inlineAllStyles as f } from "../modules/styles.js";
|
|
3
|
+
function u(e, n, a = {}, o) {
|
|
4
|
+
if (!e) throw new Error("Invalid node");
|
|
5
|
+
const p = /* @__PURE__ */ new Set();
|
|
6
|
+
let d = null;
|
|
7
|
+
if (e.nodeType === Node.TEXT_NODE || e.nodeType !== Node.ELEMENT_NODE)
|
|
8
|
+
return e.cloneNode(!0);
|
|
9
|
+
if (e.getAttribute("data-capture") === "exclude") {
|
|
4
10
|
const t = document.createElement("div"), i = e.getBoundingClientRect();
|
|
5
|
-
return t.style.cssText = `display:
|
|
11
|
+
return t.style.cssText = `display:inline-block;width:${i.width}px;height:${i.height}px;visibility:hidden;`, t;
|
|
6
12
|
}
|
|
13
|
+
if (a.exclude && Array.isArray(a.exclude))
|
|
14
|
+
for (const t of a.exclude)
|
|
15
|
+
try {
|
|
16
|
+
if (e.matches?.(t)) {
|
|
17
|
+
const i = document.createElement("div"), l = e.getBoundingClientRect();
|
|
18
|
+
return i.style.cssText = `display:inline-block;width:${l.width}px;height:${l.height}px;visibility:hidden;`, i;
|
|
19
|
+
}
|
|
20
|
+
} catch (i) {
|
|
21
|
+
console.warn(`Invalid selector in exclude option: ${t}`, i);
|
|
22
|
+
}
|
|
23
|
+
if (typeof a.filter == "function")
|
|
24
|
+
try {
|
|
25
|
+
if (!a.filter(e, o || e)) {
|
|
26
|
+
const t = document.createElement("div"), i = e.getBoundingClientRect();
|
|
27
|
+
return t.style.cssText = `display:inline-block;width:${i.width}px;height:${i.height}px;visibility:hidden;`, t;
|
|
28
|
+
}
|
|
29
|
+
} catch (t) {
|
|
30
|
+
console.warn("Error in filter function:", t);
|
|
31
|
+
}
|
|
7
32
|
if (e.tagName === "IFRAME") {
|
|
8
33
|
const t = document.createElement("div");
|
|
9
|
-
return t.
|
|
34
|
+
return t.style.cssText = `width:${e.offsetWidth}px;height:${e.offsetHeight}px;background-image:repeating-linear-gradient(45deg,#ddd,#ddd 5px,#f9f9f9 5px,#f9f9f9 10px);display:flex;align-items:center;justify-content:center;font-size:12px;color:#555;border:1px solid #aaa;`, t;
|
|
10
35
|
}
|
|
11
|
-
if (e.
|
|
36
|
+
if (e.getAttribute("data-capture") === "placeholder") {
|
|
12
37
|
const t = e.cloneNode(!1);
|
|
13
|
-
|
|
38
|
+
r.preNodeMap.set(t, e), f(e, t, n);
|
|
14
39
|
const i = document.createElement("div");
|
|
15
|
-
return i.textContent = e.getAttribute("data-placeholder-text") || "", i.style.cssText = "color
|
|
40
|
+
return i.textContent = e.getAttribute("data-placeholder-text") || "", i.style.cssText = "color:#666;font-size:12px;text-align:center;line-height:1.4;padding:0.5em;box-sizing:border-box;", t.appendChild(i), t;
|
|
16
41
|
}
|
|
17
42
|
if (e.tagName === "CANVAS") {
|
|
18
43
|
const t = e.toDataURL(), i = document.createElement("img");
|
|
19
|
-
return i.src = t, i.width = e.width, i.height = e.height,
|
|
44
|
+
return i.src = t, i.width = e.width, i.height = e.height, r.preNodeMap.set(i, e), f(e, i, n), i;
|
|
45
|
+
}
|
|
46
|
+
let c;
|
|
47
|
+
try {
|
|
48
|
+
c = e.cloneNode(!1), r.preNodeMap.set(c, e);
|
|
49
|
+
} catch (t) {
|
|
50
|
+
throw console.error("[Snapdom] Failed to clone node:", e, t), t;
|
|
51
|
+
}
|
|
52
|
+
if (e instanceof HTMLTextAreaElement) {
|
|
53
|
+
c.textContent = e.value, c.value = e.value;
|
|
54
|
+
const t = e.getBoundingClientRect();
|
|
55
|
+
return c.style.width = `${t.width}px`, c.style.height = `${t.height}px`, c;
|
|
20
56
|
}
|
|
21
|
-
if (e
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
57
|
+
if (e instanceof HTMLInputElement)
|
|
58
|
+
return c.value = e.value, c.setAttribute("value", e.value), e.checked !== void 0 && (c.checked = e.checked, e.checked && c.setAttribute("checked", ""), e.indeterminate && (c.indeterminate = e.indeterminate)), c;
|
|
59
|
+
if (e instanceof HTMLSelectElement && (d = e.value), f(e, c, n), e.shadowRoot)
|
|
60
|
+
if (Array.from(e.shadowRoot.querySelectorAll("slot")).length > 0) {
|
|
61
|
+
for (const i of e.shadowRoot.childNodes)
|
|
62
|
+
if (i.nodeType === Node.ELEMENT_NODE && i.tagName === "STYLE") {
|
|
63
|
+
const l = i.textContent || "";
|
|
64
|
+
l.trim() && n && (r.preStyle || (r.preStyle = /* @__PURE__ */ new WeakMap()), r.preStyle.set(i, l));
|
|
65
|
+
}
|
|
66
|
+
} else {
|
|
67
|
+
const i = document.createDocumentFragment();
|
|
68
|
+
for (const l of e.shadowRoot.childNodes) {
|
|
69
|
+
if (l.nodeType === Node.ELEMENT_NODE && l.tagName === "STYLE") {
|
|
70
|
+
const s = l.textContent || "";
|
|
71
|
+
s.trim() && n && (r.preStyle || (r.preStyle = /* @__PURE__ */ new WeakMap()), r.preStyle.set(l, s));
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
const h = u(
|
|
75
|
+
l,
|
|
76
|
+
n,
|
|
77
|
+
a,
|
|
78
|
+
o || e
|
|
79
|
+
);
|
|
80
|
+
h && i.appendChild(h);
|
|
81
|
+
}
|
|
82
|
+
c.appendChild(i);
|
|
25
83
|
}
|
|
26
|
-
|
|
84
|
+
if (e.tagName === "SLOT") {
|
|
85
|
+
const t = e.assignedNodes?.({ flatten: !0 }) || [], i = t.length > 0 ? t : Array.from(e.childNodes), l = document.createDocumentFragment();
|
|
86
|
+
for (const h of i) {
|
|
87
|
+
const s = u(
|
|
88
|
+
h,
|
|
89
|
+
n,
|
|
90
|
+
a,
|
|
91
|
+
o || e
|
|
92
|
+
);
|
|
93
|
+
s && l.appendChild(s);
|
|
94
|
+
}
|
|
95
|
+
return l;
|
|
27
96
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
t.value === e.value ? t.setAttribute("selected", "") : t.removeAttribute("selected");
|
|
32
|
-
})), o(e, l, c, r, s);
|
|
33
|
-
const h = document.createDocumentFragment();
|
|
34
|
-
if (e.childNodes.forEach((t) => {
|
|
35
|
-
const i = f(
|
|
97
|
+
for (const t of e.childNodes) {
|
|
98
|
+
if (p.has(t)) continue;
|
|
99
|
+
const i = u(
|
|
36
100
|
t,
|
|
37
|
-
c,
|
|
38
|
-
r,
|
|
39
101
|
n,
|
|
40
|
-
|
|
102
|
+
a,
|
|
103
|
+
o || e
|
|
41
104
|
);
|
|
42
|
-
i &&
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
105
|
+
i && c.appendChild(i);
|
|
106
|
+
}
|
|
107
|
+
if (d !== null && c instanceof HTMLSelectElement) {
|
|
108
|
+
c.value = d;
|
|
109
|
+
for (const t of c.options)
|
|
110
|
+
t.value === d ? t.setAttribute("selected", "") : t.removeAttribute("selected");
|
|
46
111
|
}
|
|
47
|
-
return
|
|
112
|
+
return c;
|
|
48
113
|
}
|
|
49
114
|
export {
|
|
50
|
-
|
|
115
|
+
u as deepClone
|
|
51
116
|
};
|
|
@@ -1,68 +1,65 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
let o;
|
|
1
|
+
import { cache as n } from "./cache.js";
|
|
2
|
+
import { inlinePseudoElements as p } from "../modules/pseudo.js";
|
|
3
|
+
import { inlineExternalDef as m } from "../modules/svgDefs.js";
|
|
4
|
+
import { generateCSSClasses as g } from "../utils/cssTools.js";
|
|
5
|
+
import { stripTranslate as u } from "../utils/helpers.js";
|
|
6
|
+
import { deepClone as h } from "./clone.js";
|
|
7
|
+
async function k(l, c = !1, d = !1, f = {}) {
|
|
8
|
+
let o, y = "";
|
|
9
9
|
try {
|
|
10
|
-
o =
|
|
10
|
+
o = h(l, c, f, l);
|
|
11
11
|
} catch (e) {
|
|
12
12
|
throw console.warn("deepClone failed:", e), e;
|
|
13
13
|
}
|
|
14
14
|
try {
|
|
15
|
-
await
|
|
16
|
-
|
|
15
|
+
await p(
|
|
16
|
+
l,
|
|
17
17
|
o,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
m,
|
|
22
|
-
p
|
|
18
|
+
c,
|
|
19
|
+
d,
|
|
20
|
+
f.useProxy
|
|
23
21
|
);
|
|
24
22
|
} catch (e) {
|
|
25
23
|
console.warn("inlinePseudoElements failed:", e);
|
|
26
24
|
}
|
|
27
25
|
try {
|
|
28
|
-
|
|
26
|
+
m(o);
|
|
29
27
|
} catch (e) {
|
|
30
28
|
console.warn("inlineExternalDef failed:", e);
|
|
31
29
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
for (const [t, l] of a.entries()) {
|
|
30
|
+
if (c) {
|
|
31
|
+
const e = g();
|
|
32
|
+
y = Array.from(e.entries()).map(([t, r]) => `.${r}{${t}}`).join("");
|
|
33
|
+
for (const [t, r] of n.preStyleMap.entries()) {
|
|
37
34
|
if (t.tagName === "STYLE") continue;
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
t.removeAttribute("style"),
|
|
35
|
+
const a = e.get(r);
|
|
36
|
+
a && t.classList.add(a);
|
|
37
|
+
const i = t.style?.backgroundImage;
|
|
38
|
+
t.removeAttribute("style"), i && i !== "none" && (t.style.backgroundImage = i);
|
|
42
39
|
}
|
|
43
40
|
} else
|
|
44
|
-
for (const [e, t] of
|
|
41
|
+
for (const [e, t] of n.preStyleMap.entries())
|
|
45
42
|
e.tagName !== "STYLE" && e.setAttribute("style", t.replace(/;/g, "; "));
|
|
46
|
-
for (const [e, t] of
|
|
47
|
-
const
|
|
48
|
-
if ((
|
|
43
|
+
for (const [e, t] of n.preNodeMap.entries()) {
|
|
44
|
+
const r = t.scrollLeft, a = t.scrollTop;
|
|
45
|
+
if ((r || a) && e instanceof HTMLElement) {
|
|
49
46
|
e.style.overflow = "hidden", e.style.scrollbarWidth = "none", e.style.msOverflowStyle = "none";
|
|
50
47
|
const s = document.createElement("div");
|
|
51
|
-
for (s.style.transform = `translate(${-
|
|
48
|
+
for (s.style.transform = `translate(${-r}px, ${-a}px)`, s.style.willChange = "transform", s.style.display = "inline-block", s.style.width = "100%"; e.firstChild; )
|
|
52
49
|
s.appendChild(e.firstChild);
|
|
53
50
|
e.appendChild(s);
|
|
54
51
|
}
|
|
55
52
|
}
|
|
56
|
-
if (
|
|
57
|
-
const e =
|
|
58
|
-
|
|
59
|
-
const t =
|
|
53
|
+
if (l === n.preNodeMap.get(o)) {
|
|
54
|
+
const e = n.preStyle.get(l) || window.getComputedStyle(l);
|
|
55
|
+
n.preStyle.set(l, e);
|
|
56
|
+
const t = u(e.transform);
|
|
60
57
|
o.style.margin = "0", o.style.position = "static", o.style.top = "auto", o.style.left = "auto", o.style.right = "auto", o.style.bottom = "auto", o.style.zIndex = "auto", o.style.float = "none", o.style.clear = "none", o.style.transform = t || "";
|
|
61
58
|
}
|
|
62
|
-
for (const [e, t] of
|
|
59
|
+
for (const [e, t] of n.preNodeMap.entries())
|
|
63
60
|
t.tagName === "PRE" && (e.style.marginTop = "0", e.style.marginBlockStart = "0");
|
|
64
|
-
return { clone: o, classCSS:
|
|
61
|
+
return { clone: o, classCSS: y };
|
|
65
62
|
}
|
|
66
63
|
export {
|
|
67
|
-
|
|
64
|
+
k as prepareClone
|
|
68
65
|
};
|
|
@@ -1,25 +1,34 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import { cache as c } from "../core/cache.js";
|
|
2
|
+
import { getStyle as y, splitBackgroundImage as h, inlineSingleBackgroundEntry as b } from "../utils/helpers.js";
|
|
3
|
+
async function w(u, d, k = {}) {
|
|
4
|
+
const t = [[u, d]], p = [
|
|
5
|
+
"background-image",
|
|
6
|
+
"mask",
|
|
7
|
+
"mask-image",
|
|
8
|
+
"-webkit-mask-image",
|
|
9
|
+
"mask-source",
|
|
10
|
+
"mask-box-image-source",
|
|
11
|
+
"mask-border-source",
|
|
12
|
+
"-webkit-mask-box-image-source"
|
|
13
|
+
];
|
|
14
|
+
for (; t.length; ) {
|
|
15
|
+
const [o, s] = t.shift(), a = c.preStyle.get(o) || y(o);
|
|
16
|
+
c.preStyle.has(o) || c.preStyle.set(o, a);
|
|
17
|
+
for (const e of p) {
|
|
18
|
+
const i = a.getPropertyValue(e);
|
|
19
|
+
if (!i || i === "none") continue;
|
|
20
|
+
const f = h(i), g = await Promise.all(
|
|
21
|
+
f.map((r) => b(r, k))
|
|
22
|
+
);
|
|
23
|
+
g.some((r) => r && r !== "none" && !/^url\(undefined/.test(r)) && s.style.setProperty(e, g.join(", "));
|
|
13
24
|
}
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
);
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
for (let n = 0; n < Math.min(g.length, d.length); n++)
|
|
20
|
-
r.push([g[n], d[n]]);
|
|
25
|
+
const n = a.getPropertyValue("background-color");
|
|
26
|
+
n && n !== "transparent" && n !== "rgba(0, 0, 0, 0)" && (s.style.backgroundColor = n);
|
|
27
|
+
const l = Array.from(o.children), m = Array.from(s.children);
|
|
28
|
+
for (let e = 0; e < Math.min(l.length, m.length); e++)
|
|
29
|
+
t.push([l[e], m[e]]);
|
|
21
30
|
}
|
|
22
31
|
}
|
|
23
32
|
export {
|
|
24
|
-
|
|
33
|
+
w as inlineBackgroundImages
|
|
25
34
|
};
|
|
@@ -1,141 +1,139 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { isIconFont as
|
|
3
|
-
import { extractURL as
|
|
4
|
-
async function
|
|
5
|
-
|
|
1
|
+
import { cache as o } from "../core/cache.js";
|
|
2
|
+
import { isIconFont as y } from "./iconFonts.js";
|
|
3
|
+
import { extractURL as L } from "../utils/helpers.js";
|
|
4
|
+
async function E(u, i, a, p = 32, m = "#000") {
|
|
5
|
+
i = i.replace(/^['"]+|['"]+$/g, "");
|
|
6
6
|
const t = window.devicePixelRatio || 1, s = document.createElement("canvas").getContext("2d");
|
|
7
|
-
s.font =
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
return
|
|
7
|
+
s.font = a ? `${a} ${p}px "${i}"` : `${p}px "${i}"`;
|
|
8
|
+
const c = s.measureText(u), l = c.actualBoundingBoxAscent || p * 0.8, f = c.actualBoundingBoxDescent || p * 0.2, d = l + f, w = c.width, e = document.createElement("canvas");
|
|
9
|
+
e.width = Math.ceil(w * t), e.height = Math.ceil(d * t);
|
|
10
|
+
const h = e.getContext("2d");
|
|
11
|
+
return h.scale(t, t), h.font = s.font, h.textAlign = "left", h.textBaseline = "alphabetic", h.fillStyle = m, h.fillText(u, 0, l), e.toDataURL();
|
|
12
12
|
}
|
|
13
|
-
function
|
|
14
|
-
return Array.from(document.styleSheets).some((
|
|
13
|
+
function $(u) {
|
|
14
|
+
return Array.from(document.styleSheets).some((i) => i.href === u);
|
|
15
15
|
}
|
|
16
|
-
function
|
|
17
|
-
return new Promise((
|
|
18
|
-
if (
|
|
19
|
-
const
|
|
20
|
-
|
|
16
|
+
function P(u) {
|
|
17
|
+
return new Promise((i) => {
|
|
18
|
+
if ($(u)) return i(null);
|
|
19
|
+
const a = document.createElement("link");
|
|
20
|
+
a.rel = "stylesheet", a.href = u, a.setAttribute("data-snapdom", "injected-import"), a.onload = () => i(a), a.onerror = () => i(null), document.head.appendChild(a);
|
|
21
21
|
});
|
|
22
22
|
}
|
|
23
|
-
async function
|
|
24
|
-
if (
|
|
25
|
-
if (
|
|
23
|
+
async function T({ preCached: u = !1 } = {}) {
|
|
24
|
+
if (o.resource.has("fonts-embed-css")) {
|
|
25
|
+
if (u) {
|
|
26
26
|
const t = document.createElement("style");
|
|
27
|
-
t.setAttribute("data-snapdom", "embedFonts"), t.textContent =
|
|
27
|
+
t.setAttribute("data-snapdom", "embedFonts"), t.textContent = o.resource.get("fonts-embed-css"), document.head.appendChild(t);
|
|
28
28
|
}
|
|
29
|
-
return
|
|
29
|
+
return o.resource.get("fonts-embed-css");
|
|
30
30
|
}
|
|
31
|
-
const
|
|
31
|
+
const i = /@import\s+url\(["']?([^"')]+)["']?\)/g, a = [];
|
|
32
32
|
for (const t of document.querySelectorAll("style")) {
|
|
33
|
-
const
|
|
34
|
-
for (const
|
|
35
|
-
const
|
|
36
|
-
|
|
33
|
+
const n = t.textContent || "", s = Array.from(n.matchAll(i));
|
|
34
|
+
for (const c of s) {
|
|
35
|
+
const l = c[1];
|
|
36
|
+
y(l) || $(l) || a.push(l);
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
|
-
await Promise.all(
|
|
40
|
-
const
|
|
39
|
+
await Promise.all(a.map(P));
|
|
40
|
+
const p = Array.from(
|
|
41
41
|
document.querySelectorAll('link[rel="stylesheet"]')
|
|
42
42
|
).filter((t) => t.href);
|
|
43
|
-
let
|
|
44
|
-
for (const t of
|
|
43
|
+
let m = "";
|
|
44
|
+
for (const t of p)
|
|
45
45
|
try {
|
|
46
46
|
const s = await (await fetch(t.href)).text();
|
|
47
|
-
if (
|
|
48
|
-
const
|
|
49
|
-
Array.from(s.matchAll(
|
|
50
|
-
let
|
|
51
|
-
if (!
|
|
52
|
-
let
|
|
53
|
-
if (!
|
|
54
|
-
if (
|
|
55
|
-
return
|
|
47
|
+
if (y(t.href) || y(s)) continue;
|
|
48
|
+
const c = /url\((["']?)([^"')]+)\1\)/g, l = await Promise.all(
|
|
49
|
+
Array.from(s.matchAll(c)).map(async (d) => {
|
|
50
|
+
let w = L(d[0]);
|
|
51
|
+
if (!w) return null;
|
|
52
|
+
let e = w;
|
|
53
|
+
if (!e.startsWith("http") && !e.startsWith("data:") && (e = new URL(e, t.href).href), y(e)) return null;
|
|
54
|
+
if (o.resource.has(e))
|
|
55
|
+
return o.font.add(e), {
|
|
56
56
|
original: d[0],
|
|
57
|
-
inlined: `url(${
|
|
57
|
+
inlined: `url(${o.resource.get(e)})`
|
|
58
58
|
};
|
|
59
|
-
if (
|
|
59
|
+
if (o.font.has(e)) return null;
|
|
60
60
|
try {
|
|
61
|
-
const
|
|
62
|
-
const
|
|
63
|
-
|
|
61
|
+
const g = await (await fetch(e)).blob(), b = await new Promise((r) => {
|
|
62
|
+
const x = new FileReader();
|
|
63
|
+
x.onload = () => r(x.result), x.readAsDataURL(g);
|
|
64
64
|
});
|
|
65
|
-
return
|
|
65
|
+
return o.resource.set(e, b), o.font.add(e), { original: d[0], inlined: `url(${b})` };
|
|
66
66
|
} catch {
|
|
67
|
-
return console.warn("[snapdom] Failed to fetch font resource:",
|
|
67
|
+
return console.warn("[snapdom] Failed to fetch font resource:", e), null;
|
|
68
68
|
}
|
|
69
69
|
})
|
|
70
70
|
);
|
|
71
|
-
let
|
|
72
|
-
for (const d of
|
|
73
|
-
d && (
|
|
74
|
-
|
|
71
|
+
let f = s;
|
|
72
|
+
for (const d of l)
|
|
73
|
+
d && (f = f.replace(d.original, d.inlined));
|
|
74
|
+
m += f + `
|
|
75
75
|
`;
|
|
76
76
|
} catch {
|
|
77
77
|
console.warn("[snapdom] Failed to fetch CSS:", t.href);
|
|
78
78
|
}
|
|
79
79
|
for (const t of document.styleSheets)
|
|
80
80
|
try {
|
|
81
|
-
if (!t.href ||
|
|
82
|
-
for (const
|
|
83
|
-
if (
|
|
84
|
-
const s =
|
|
85
|
-
if (!s ||
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
81
|
+
if (!t.href || p.every((n) => n.href !== t.href)) {
|
|
82
|
+
for (const n of t.cssRules)
|
|
83
|
+
if (n.type === CSSRule.FONT_FACE_RULE) {
|
|
84
|
+
const s = n.style.getPropertyValue("src"), c = n.style.getPropertyValue("font-family");
|
|
85
|
+
if (!s || y(c)) continue;
|
|
86
|
+
const l = /url\((["']?)([^"')]+)\1\)/g, f = /local\((["']?)[^)]+?\1\)/g, d = l.test(s), w = f.test(s);
|
|
87
|
+
if (!d && w) {
|
|
88
|
+
m += `@font-face{font-family:${c};src:${s};font-style:${n.style.getPropertyValue("font-style") || "normal"};font-weight:${n.style.getPropertyValue("font-weight") || "normal"};}`;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
let e = s;
|
|
92
|
+
const h = Array.from(s.matchAll(l));
|
|
93
|
+
for (const g of h) {
|
|
94
|
+
let b = g[2].trim();
|
|
95
|
+
if (!b) continue;
|
|
96
|
+
let r = b;
|
|
97
|
+
if (!r.startsWith("http") && !r.startsWith("data:") && (r = new URL(r, t.href || location.href).href), !y(r)) {
|
|
98
|
+
if (o.resource.has(r)) {
|
|
99
|
+
o.font.add(r), e = e.replace(
|
|
100
|
+
g[0],
|
|
101
|
+
`url(${o.resource.get(r)})`
|
|
98
102
|
);
|
|
99
103
|
continue;
|
|
100
104
|
}
|
|
101
|
-
if (!
|
|
105
|
+
if (!o.font.has(r))
|
|
102
106
|
try {
|
|
103
|
-
const
|
|
107
|
+
const A = await (await fetch(r)).blob(), S = await new Promise((C) => {
|
|
104
108
|
const R = new FileReader();
|
|
105
|
-
R.onload = () =>
|
|
109
|
+
R.onload = () => C(R.result), R.readAsDataURL(A);
|
|
106
110
|
});
|
|
107
|
-
|
|
111
|
+
o.resource.set(r, S), o.font.add(r), e = e.replace(g[0], `url(${S})`);
|
|
108
112
|
} catch {
|
|
109
|
-
console.warn("[snapdom] Failed to fetch font URL:",
|
|
113
|
+
console.warn("[snapdom] Failed to fetch font URL:", r);
|
|
110
114
|
}
|
|
111
115
|
}
|
|
112
116
|
}
|
|
113
|
-
|
|
114
|
-
font-family: ${i};
|
|
115
|
-
src: ${a};
|
|
116
|
-
font-style: ${o.style.getPropertyValue("font-style") || "normal"};
|
|
117
|
-
font-weight: ${o.style.getPropertyValue("font-weight") || "normal"};
|
|
118
|
-
}
|
|
119
|
-
`;
|
|
117
|
+
m += `@font-face{font-family:${c};src:${e};font-style:${n.style.getPropertyValue("font-style") || "normal"};font-weight:${n.style.getPropertyValue("font-weight") || "normal"};}`;
|
|
120
118
|
}
|
|
121
119
|
}
|
|
122
|
-
} catch (
|
|
123
|
-
console.warn("[snapdom] Cannot access stylesheet", t.href,
|
|
120
|
+
} catch (n) {
|
|
121
|
+
console.warn("[snapdom] Cannot access stylesheet", t.href, n);
|
|
124
122
|
}
|
|
125
123
|
for (const t of document.fonts)
|
|
126
124
|
if (t.family && t.status === "loaded" && t._snapdomSrc) {
|
|
127
|
-
if (
|
|
128
|
-
let
|
|
129
|
-
if (!
|
|
130
|
-
if (
|
|
131
|
-
|
|
132
|
-
else if (!
|
|
125
|
+
if (y(t.family)) continue;
|
|
126
|
+
let n = t._snapdomSrc;
|
|
127
|
+
if (!n.startsWith("data:")) {
|
|
128
|
+
if (o.resource.has(t._snapdomSrc))
|
|
129
|
+
n = o.resource.get(t._snapdomSrc), o.font.add(t._snapdomSrc);
|
|
130
|
+
else if (!o.font.has(t._snapdomSrc))
|
|
133
131
|
try {
|
|
134
|
-
const
|
|
135
|
-
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
}),
|
|
132
|
+
const c = await (await fetch(t._snapdomSrc)).blob();
|
|
133
|
+
n = await new Promise((l) => {
|
|
134
|
+
const f = new FileReader();
|
|
135
|
+
f.onload = () => l(f.result), f.readAsDataURL(c);
|
|
136
|
+
}), o.resource.set(t._snapdomSrc, n), o.font.add(t._snapdomSrc);
|
|
139
137
|
} catch {
|
|
140
138
|
console.warn(
|
|
141
139
|
"[snapdom] Failed to fetch dynamic font src:",
|
|
@@ -144,21 +142,15 @@ async function P({ preCached: m = !1 } = {}) {
|
|
|
144
142
|
continue;
|
|
145
143
|
}
|
|
146
144
|
}
|
|
147
|
-
|
|
148
|
-
font-family: '${t.family}';
|
|
149
|
-
src: url(${o});
|
|
150
|
-
font-style: ${t.style || "normal"};
|
|
151
|
-
font-weight: ${t.weight || "normal"};
|
|
152
|
-
}
|
|
153
|
-
`;
|
|
145
|
+
m += `@font-face{font-family:'${t.family}';src:url(${n});font-style:${t.style || "normal"};font-weight:${t.weight || "normal"};}`;
|
|
154
146
|
}
|
|
155
|
-
if (
|
|
147
|
+
if (m && (o.resource.set("fonts-embed-css", m), u)) {
|
|
156
148
|
const t = document.createElement("style");
|
|
157
|
-
t.setAttribute("data-snapdom", "embedFonts"), t.textContent =
|
|
149
|
+
t.setAttribute("data-snapdom", "embedFonts"), t.textContent = m, document.head.appendChild(t);
|
|
158
150
|
}
|
|
159
|
-
return
|
|
151
|
+
return m;
|
|
160
152
|
}
|
|
161
153
|
export {
|
|
162
|
-
|
|
163
|
-
|
|
154
|
+
T as embedCustomFonts,
|
|
155
|
+
E as iconToImage
|
|
164
156
|
};
|