@unicom-cloud/utils 0.1.14 → 0.1.16
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/content-disposition/index.js +34 -31
- package/contentDisposition.js +7 -5
- package/file/saveAs.js +13 -14
- package/lunar/lib/Holiday.js +2 -6
- package/lunar/lib/HolidayUtil.js +113 -99
- package/lunar/lib/I18n.js +395 -975
- package/lunar/lib/JieQi.js +12 -16
- package/lunar/lib/Lunar.js +224 -672
- package/lunar/lib/LunarMonth.js +35 -98
- package/lunar/lib/LunarTime.js +24 -103
- package/lunar/lib/LunarUtil.js +426 -1728
- package/lunar/lib/LunarYear.js +473 -566
- package/lunar/lib/ShouXingUtil.js +6714 -6690
- package/lunar/lib/Solar.js +24 -49
- package/lunar/lib/SolarHalfYear.js +0 -2
- package/lunar/lib/SolarMonth.js +4 -6
- package/lunar/lib/SolarSeason.js +0 -2
- package/lunar/lib/SolarUtil.js +238 -226
- package/lunar/lib/SolarWeek.js +7 -11
- package/lunar/lib/SolarYear.js +0 -1
- package/mock/MockWebSocket.js +2 -2
- package/package.json +1 -1
- package/snapdom/src/api/snapdom.js +72 -71
- package/snapdom/src/core/prepare.js +21 -21
- package/snapdom/src/modules/background.js +23 -17
- package/snapdom/src/modules/fonts.js +99 -91
- package/snapdom/src/modules/pseudo.js +59 -70
- package/tinycolor/src/conversion.js +1 -1
- package/tinycolor/src/index.js +2 -19
- package/turbo-stream/src/encode.js +5 -8
- package/turbo-stream/src/shared.js +117 -119
- package/types/file/saveAs.d.ts +1 -1
- package/types/lunar/lib/Lunar.d.ts +0 -98
- package/types/lunar/lib/LunarMonth.d.ts +0 -12
- package/types/lunar/lib/LunarTime.d.ts +0 -22
- package/types/lunar/lib/LunarUtil.d.ts +0 -90
- package/types/lunar/lib/LunarYear.d.ts +0 -30
- package/types/lunar/lib/Solar.d.ts +0 -5
- package/types/snapdom/src/modules/pseudo.d.ts +1 -1
- package/types/tinycolor/src/conversion.d.ts +1 -1
|
@@ -1,112 +1,113 @@
|
|
|
1
1
|
import { captureDOM as v } from "../core/capture.js";
|
|
2
2
|
import { extendIconFonts as j } from "../modules/iconFonts.js";
|
|
3
|
-
import { isSafari as
|
|
4
|
-
async function
|
|
5
|
-
const
|
|
6
|
-
return
|
|
3
|
+
import { isSafari as x } from "../utils/helpers.js";
|
|
4
|
+
async function R(e, { scale: t = 1 } = {}) {
|
|
5
|
+
const n = new Image();
|
|
6
|
+
return n.src = e, await n.decode(), t !== 1 && (n.style.width = `${n.naturalWidth * t}px`, n.style.height = `${n.naturalHeight * t}px`), n;
|
|
7
7
|
}
|
|
8
|
-
async function
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
let
|
|
13
|
-
if (
|
|
14
|
-
throw
|
|
15
|
-
const s =
|
|
8
|
+
async function h(e, { dpr: t = 1, scale: n = 1 } = {}) {
|
|
9
|
+
const c = new Image();
|
|
10
|
+
c.src = e, c.crossOrigin = "anonymous", c.loading = "eager", c.decoding = "sync";
|
|
11
|
+
const o = x();
|
|
12
|
+
let a = !1;
|
|
13
|
+
if (o && (document.body.appendChild(c), a = !0), await c.decode(), o && await new Promise((w) => setTimeout(w, 100)), c.width === 0 || c.height === 0)
|
|
14
|
+
throw a && c.remove(), new Error("Image failed to load or has no dimensions");
|
|
15
|
+
const s = c.naturalWidth * n, r = c.naturalHeight * n, g = document.createElement("canvas");
|
|
16
16
|
g.width = Math.ceil(s * t), g.height = Math.ceil(r * t), g.style.width = `${s}px`, g.style.height = `${r}px`;
|
|
17
17
|
const d = g.getContext("2d");
|
|
18
|
-
return d.scale(t, t), d.drawImage(
|
|
18
|
+
return d.scale(t, t), d.drawImage(c, 0, 0, s, r), a && c.remove(), g;
|
|
19
19
|
}
|
|
20
|
-
async function
|
|
21
|
-
const
|
|
20
|
+
async function y(e, { type: t = "svg", scale: n = 1, backgroundColor: c = "#fff", quality: o } = {}) {
|
|
21
|
+
const a = {
|
|
22
22
|
jpg: "image/jpeg",
|
|
23
23
|
jpeg: "image/jpeg",
|
|
24
24
|
png: "image/png",
|
|
25
25
|
webp: "image/webp"
|
|
26
26
|
}[t] || "image/png";
|
|
27
27
|
if (t === "svg") {
|
|
28
|
-
const r = decodeURIComponent(
|
|
28
|
+
const r = decodeURIComponent(e.split(",")[1]);
|
|
29
29
|
return new Blob([r], { type: "image/svg+xml" });
|
|
30
30
|
}
|
|
31
31
|
const s = await u(
|
|
32
|
-
|
|
33
|
-
{ dpr: 1, scale:
|
|
34
|
-
|
|
32
|
+
e,
|
|
33
|
+
{ dpr: 1, scale: n },
|
|
34
|
+
c
|
|
35
35
|
);
|
|
36
36
|
return new Promise((r) => {
|
|
37
|
-
s.toBlob((g) => r(g), `${
|
|
37
|
+
s.toBlob((g) => r(g), `${a}`, o);
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
|
-
async function u(
|
|
41
|
-
const
|
|
42
|
-
if (!
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
const s =
|
|
46
|
-
return s.fillStyle =
|
|
40
|
+
async function u(e, { dpr: t = 1, scale: n = 1 }, c) {
|
|
41
|
+
const o = await h(e, { dpr: t, scale: n });
|
|
42
|
+
if (!c) return o;
|
|
43
|
+
const a = document.createElement("canvas");
|
|
44
|
+
a.width = o.width, a.height = o.height;
|
|
45
|
+
const s = a.getContext("2d");
|
|
46
|
+
return s.fillStyle = c, s.fillRect(0, 0, a.width, a.height), s.drawImage(o, 0, 0), a;
|
|
47
47
|
}
|
|
48
|
-
async function
|
|
49
|
-
const s = ["jpg", "jpeg", "webp"].includes(
|
|
50
|
-
return d.src = g.toDataURL(`image/${
|
|
48
|
+
async function m(e, { dpr: t = 1, scale: n = 1, backgroundColor: c, quality: o }, a = "png") {
|
|
49
|
+
const s = ["jpg", "jpeg", "webp"].includes(a) ? "#fff" : void 0, g = await u(e, { dpr: t, scale: n }, c ?? s), d = new Image();
|
|
50
|
+
return d.src = g.toDataURL(`image/${a}`, o), await d.decode(), d.style.width = `${g.width / t}px`, d.style.height = `${g.height / t}px`, d;
|
|
51
51
|
}
|
|
52
|
-
async function
|
|
52
|
+
async function B(e, {
|
|
53
53
|
dpr: t = 1,
|
|
54
|
-
scale:
|
|
55
|
-
backgroundColor:
|
|
56
|
-
format:
|
|
57
|
-
filename:
|
|
54
|
+
scale: n = 1,
|
|
55
|
+
backgroundColor: c,
|
|
56
|
+
format: o = "png",
|
|
57
|
+
filename: a = "snapDOM"
|
|
58
58
|
} = {}) {
|
|
59
|
-
if (
|
|
60
|
-
const
|
|
61
|
-
|
|
59
|
+
if (o === "svg") {
|
|
60
|
+
const b = await y(e), f = URL.createObjectURL(b), l = document.createElement("a");
|
|
61
|
+
l.href = f, l.download = `${a}.svg`, l.click(), URL.revokeObjectURL(f);
|
|
62
62
|
return;
|
|
63
63
|
}
|
|
64
|
-
const s = ["jpg", "jpeg", "webp"].includes(
|
|
64
|
+
const s = ["jpg", "jpeg", "webp"].includes(o) ? "#fff" : void 0, g = await u(e, { dpr: t, scale: n }, c ?? s), d = {
|
|
65
65
|
jpg: "image/jpeg",
|
|
66
66
|
jpeg: "image/jpeg",
|
|
67
67
|
png: "image/png",
|
|
68
68
|
webp: "image/webp"
|
|
69
|
-
}[
|
|
70
|
-
p.href = w, p.download = `${
|
|
69
|
+
}[o] || "image/png", w = g.toDataURL(d), p = document.createElement("a");
|
|
70
|
+
p.href = w, p.download = `${a}.${o}`, p.click();
|
|
71
71
|
}
|
|
72
|
-
async function i(
|
|
73
|
-
if (t = { scale: 1, ...t }, !
|
|
74
|
-
return t.iconFonts && j(t.iconFonts), await i.capture(
|
|
72
|
+
async function i(e, t = {}) {
|
|
73
|
+
if (t = { scale: 1, ...t }, !e) throw new Error("Element cannot be null or undefined");
|
|
74
|
+
return t.iconFonts && j(t.iconFonts), await i.capture(e, t);
|
|
75
75
|
}
|
|
76
|
-
i.capture = async (
|
|
77
|
-
const
|
|
76
|
+
i.capture = async (e, t = {}) => {
|
|
77
|
+
const n = await v(e, t), c = t.dpr ?? (window.devicePixelRatio || 1), o = t.scale || 1;
|
|
78
78
|
return {
|
|
79
|
-
url:
|
|
79
|
+
url: n,
|
|
80
80
|
options: t,
|
|
81
|
-
toRaw: () =>
|
|
82
|
-
toImg: () =>
|
|
83
|
-
toCanvas: () =>
|
|
84
|
-
toBlob: (
|
|
85
|
-
toPng: (
|
|
86
|
-
toJpg: (
|
|
87
|
-
toWebp: (
|
|
81
|
+
toRaw: () => n,
|
|
82
|
+
toImg: (a = {}) => R(n, { scale: o, ...a }),
|
|
83
|
+
toCanvas: (a = {}) => h(n, { dpr: c, scale: o, ...a }),
|
|
84
|
+
toBlob: (a = {}) => y(n, { scale: o, ...a }),
|
|
85
|
+
toPng: (a = {}) => m(n, { dpr: c, scale: o, ...a }, "png"),
|
|
86
|
+
toJpg: (a = {}) => m(n, { dpr: c, scale: o, ...a }, "jpeg"),
|
|
87
|
+
toWebp: (a = {}) => m(n, { dpr: c, scale: o, ...a }, "webp"),
|
|
88
88
|
download: ({
|
|
89
|
-
format:
|
|
90
|
-
filename: s = "
|
|
91
|
-
backgroundColor: r
|
|
92
|
-
|
|
89
|
+
format: a = "png",
|
|
90
|
+
filename: s = "snapDOM",
|
|
91
|
+
backgroundColor: r,
|
|
92
|
+
...g
|
|
93
|
+
} = {}) => B(n, { dpr: c, scale: o, format: a, filename: s, backgroundColor: r, ...g })
|
|
93
94
|
};
|
|
94
95
|
};
|
|
95
|
-
i.toRaw = async (
|
|
96
|
-
i.toImg = async (
|
|
97
|
-
i.toCanvas = async (
|
|
98
|
-
i.toBlob = async (
|
|
99
|
-
i.toPng = async (
|
|
100
|
-
i.toJpg = async (
|
|
101
|
-
i.toWebp = async (
|
|
102
|
-
i.download = async (
|
|
96
|
+
i.toRaw = async (e, t) => (await i.capture(e, t)).toRaw();
|
|
97
|
+
i.toImg = async (e, t) => (await i.capture(e, t)).toImg();
|
|
98
|
+
i.toCanvas = async (e, t) => (await i.capture(e, t)).toCanvas();
|
|
99
|
+
i.toBlob = async (e, t) => (await i.capture(e, t)).toBlob(t);
|
|
100
|
+
i.toPng = async (e, t) => (await i.capture(e, t)).toPng(t);
|
|
101
|
+
i.toJpg = async (e, t) => (await i.capture(e, t)).toJpg(t);
|
|
102
|
+
i.toWebp = async (e, t) => (await i.capture(e, t)).toWebp(t);
|
|
103
|
+
i.download = async (e, t = {}) => {
|
|
103
104
|
const {
|
|
104
|
-
format:
|
|
105
|
-
filename:
|
|
106
|
-
backgroundColor:
|
|
107
|
-
...
|
|
105
|
+
format: n = "png",
|
|
106
|
+
filename: c = "capture",
|
|
107
|
+
backgroundColor: o,
|
|
108
|
+
...a
|
|
108
109
|
} = t;
|
|
109
|
-
return await (await i.capture(
|
|
110
|
+
return await (await i.capture(e, a)).download({ format: n, filename: c, backgroundColor: o });
|
|
110
111
|
};
|
|
111
112
|
export {
|
|
112
113
|
i as snapdom
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import { cache as
|
|
1
|
+
import { cache as s } from "./cache.js";
|
|
2
2
|
import { inlinePseudoElements as p } from "../modules/pseudo.js";
|
|
3
3
|
import { inlineExternalDef as m } from "../modules/svgDefs.js";
|
|
4
4
|
import { generateCSSClasses as g } from "../utils/cssTools.js";
|
|
5
5
|
import { stripTranslate as u } from "../utils/helpers.js";
|
|
6
6
|
import { deepClone as h } from "./clone.js";
|
|
7
|
-
async function k(
|
|
8
|
-
let o,
|
|
7
|
+
async function k(n, c = !1, y = !1, f = {}) {
|
|
8
|
+
let o, d = "";
|
|
9
9
|
try {
|
|
10
|
-
o = h(
|
|
10
|
+
o = h(n, c, f, n);
|
|
11
11
|
} catch (e) {
|
|
12
12
|
throw console.warn("deepClone failed:", e), e;
|
|
13
13
|
}
|
|
14
14
|
try {
|
|
15
15
|
await p(
|
|
16
|
-
|
|
16
|
+
n,
|
|
17
17
|
o,
|
|
18
18
|
c,
|
|
19
|
-
|
|
19
|
+
y,
|
|
20
20
|
f.useProxy
|
|
21
21
|
);
|
|
22
22
|
} catch (e) {
|
|
@@ -29,36 +29,36 @@ async function k(l, c = !1, d = !1, f = {}) {
|
|
|
29
29
|
}
|
|
30
30
|
if (c) {
|
|
31
31
|
const e = g();
|
|
32
|
-
|
|
33
|
-
for (const [t, r] of
|
|
32
|
+
d = Array.from(e.entries()).map(([t, r]) => `.${r}{${t}}`).join("");
|
|
33
|
+
for (const [t, r] of s.preStyleMap.entries()) {
|
|
34
34
|
if (t.tagName === "STYLE") continue;
|
|
35
35
|
const a = e.get(r);
|
|
36
36
|
a && t.classList.add(a);
|
|
37
|
-
const i = t.style?.backgroundImage;
|
|
38
|
-
t.removeAttribute("style"), i && i !== "none" && (t.style.backgroundImage = i);
|
|
37
|
+
const i = t.style?.backgroundImage, l = t.dataset?.snapdomHasIcon;
|
|
38
|
+
t.removeAttribute("style"), i && i !== "none" && (t.style.backgroundImage = i), l && (t.style.verticalAlign = "middle", t.style.display = "inline");
|
|
39
39
|
}
|
|
40
40
|
} else
|
|
41
|
-
for (const [e, t] of
|
|
41
|
+
for (const [e, t] of s.preStyleMap.entries())
|
|
42
42
|
e.tagName !== "STYLE" && e.setAttribute("style", t.replace(/;/g, "; "));
|
|
43
|
-
for (const [e, t] of
|
|
43
|
+
for (const [e, t] of s.preNodeMap.entries()) {
|
|
44
44
|
const r = t.scrollLeft, a = t.scrollTop;
|
|
45
45
|
if ((r || a) && e instanceof HTMLElement) {
|
|
46
46
|
e.style.overflow = "hidden", e.style.scrollbarWidth = "none", e.style.msOverflowStyle = "none";
|
|
47
|
-
const
|
|
48
|
-
for (
|
|
49
|
-
|
|
50
|
-
e.appendChild(
|
|
47
|
+
const l = document.createElement("div");
|
|
48
|
+
for (l.style.transform = `translate(${-r}px, ${-a}px)`, l.style.willChange = "transform", l.style.display = "inline-block", l.style.width = "100%"; e.firstChild; )
|
|
49
|
+
l.appendChild(e.firstChild);
|
|
50
|
+
e.appendChild(l);
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
|
-
if (
|
|
54
|
-
const e =
|
|
55
|
-
|
|
53
|
+
if (n === s.preNodeMap.get(o)) {
|
|
54
|
+
const e = s.preStyle.get(n) || window.getComputedStyle(n);
|
|
55
|
+
s.preStyle.set(n, e);
|
|
56
56
|
const t = u(e.transform);
|
|
57
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 || "";
|
|
58
58
|
}
|
|
59
|
-
for (const [e, t] of
|
|
59
|
+
for (const [e, t] of s.preNodeMap.entries())
|
|
60
60
|
t.tagName === "PRE" && (e.style.marginTop = "0", e.style.marginBlockStart = "0");
|
|
61
|
-
return { clone: o, classCSS:
|
|
61
|
+
return { clone: o, classCSS: d };
|
|
62
62
|
}
|
|
63
63
|
export {
|
|
64
64
|
k as prepareClone
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { cache as c } from "../core/cache.js";
|
|
2
|
-
import { getStyle as
|
|
3
|
-
async function
|
|
4
|
-
const
|
|
2
|
+
import { getStyle as f, splitBackgroundImage as h, inlineSingleBackgroundEntry as y } from "../utils/helpers.js";
|
|
3
|
+
async function S(d, u, b = {}) {
|
|
4
|
+
const n = [[d, u]], p = [
|
|
5
5
|
"background-image",
|
|
6
6
|
"mask",
|
|
7
7
|
"mask-image",
|
|
@@ -9,26 +9,32 @@ async function w(u, d, k = {}) {
|
|
|
9
9
|
"mask-source",
|
|
10
10
|
"mask-box-image-source",
|
|
11
11
|
"mask-border-source",
|
|
12
|
-
"-webkit-mask-box-image-source"
|
|
12
|
+
"-webkit-mask-box-image-source",
|
|
13
|
+
"border-image",
|
|
14
|
+
"border-image-source",
|
|
15
|
+
"border-image-slice",
|
|
16
|
+
"border-image-width",
|
|
17
|
+
"border-image-outset",
|
|
18
|
+
"border-image-repeat"
|
|
13
19
|
];
|
|
14
|
-
for (;
|
|
15
|
-
const [
|
|
16
|
-
c.preStyle.has(
|
|
20
|
+
for (; n.length; ) {
|
|
21
|
+
const [r, i] = n.shift(), s = c.preStyle.get(r) || f(r);
|
|
22
|
+
c.preStyle.has(r) || c.preStyle.set(r, s);
|
|
17
23
|
for (const e of p) {
|
|
18
|
-
const
|
|
19
|
-
if (!
|
|
20
|
-
const
|
|
21
|
-
|
|
24
|
+
const a = s.getPropertyValue(e);
|
|
25
|
+
if (!a || a === "none") continue;
|
|
26
|
+
const k = h(a), g = await Promise.all(
|
|
27
|
+
k.map((o) => y(o, b))
|
|
22
28
|
);
|
|
23
|
-
g.some((
|
|
29
|
+
g.some((o) => o && o !== "none" && !/^url\(undefined/.test(o)) && i.style.setProperty(e, g.join(", "));
|
|
24
30
|
}
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
const l = Array.from(
|
|
31
|
+
const t = s.getPropertyValue("background-color");
|
|
32
|
+
t && t !== "transparent" && t !== "rgba(0, 0, 0, 0)" && (i.style.backgroundColor = t);
|
|
33
|
+
const l = Array.from(r.children), m = Array.from(i.children);
|
|
28
34
|
for (let e = 0; e < Math.min(l.length, m.length); e++)
|
|
29
|
-
|
|
35
|
+
n.push([l[e], m[e]]);
|
|
30
36
|
}
|
|
31
37
|
}
|
|
32
38
|
export {
|
|
33
|
-
|
|
39
|
+
S as inlineBackgroundImages
|
|
34
40
|
};
|
|
@@ -1,139 +1,147 @@
|
|
|
1
|
-
import { cache as
|
|
2
|
-
import { isIconFont as
|
|
3
|
-
import { extractURL as
|
|
4
|
-
async function E(m, i,
|
|
1
|
+
import { cache as n } from "../core/cache.js";
|
|
2
|
+
import { isIconFont as h } from "./iconFonts.js";
|
|
3
|
+
import { extractURL as U } from "../utils/helpers.js";
|
|
4
|
+
async function E(m, i, l, y = 32, u = "#000") {
|
|
5
5
|
i = i.replace(/^['"]+|['"]+$/g, "");
|
|
6
|
-
const t = window.devicePixelRatio || 1
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
e.
|
|
10
|
-
const
|
|
11
|
-
|
|
6
|
+
const t = window.devicePixelRatio || 1;
|
|
7
|
+
await document.fonts.ready;
|
|
8
|
+
const e = document.createElement("span");
|
|
9
|
+
e.textContent = m, e.style.position = "absolute", e.style.visibility = "hidden", e.style.fontFamily = `"${i}"`, e.style.fontWeight = l || "normal", e.style.fontSize = `${y}px`, e.style.lineHeight = "1", e.style.whiteSpace = "nowrap", e.style.padding = "0", e.style.margin = "0", document.body.appendChild(e);
|
|
10
|
+
const r = e.getBoundingClientRect(), d = Math.ceil(r.width), f = Math.ceil(r.height);
|
|
11
|
+
document.body.removeChild(e);
|
|
12
|
+
const c = document.createElement("canvas");
|
|
13
|
+
c.width = d * t, c.height = f * t;
|
|
14
|
+
const s = c.getContext("2d");
|
|
15
|
+
return s.scale(t, t), s.font = l ? `${l} ${y}px "${i}"` : `${y}px "${i}"`, s.textAlign = "left", s.textBaseline = "top", s.fillStyle = u, s.fillText(m, 0, 0), {
|
|
16
|
+
dataUrl: c.toDataURL(),
|
|
17
|
+
width: d,
|
|
18
|
+
height: f
|
|
19
|
+
};
|
|
12
20
|
}
|
|
13
|
-
function
|
|
21
|
+
function x(m) {
|
|
14
22
|
return Array.from(document.styleSheets).some((i) => i.href === m);
|
|
15
23
|
}
|
|
16
|
-
function
|
|
24
|
+
function L(m) {
|
|
17
25
|
return new Promise((i) => {
|
|
18
|
-
if (
|
|
19
|
-
const
|
|
20
|
-
|
|
26
|
+
if (x(m)) return i(null);
|
|
27
|
+
const l = document.createElement("link");
|
|
28
|
+
l.rel = "stylesheet", l.href = m, l.setAttribute("data-snapdom", "injected-import"), l.onload = () => i(l), l.onerror = () => i(null), document.head.appendChild(l);
|
|
21
29
|
});
|
|
22
30
|
}
|
|
23
|
-
async function
|
|
24
|
-
if (
|
|
31
|
+
async function k({ preCached: m = !1 } = {}) {
|
|
32
|
+
if (n.resource.has("fonts-embed-css")) {
|
|
25
33
|
if (m) {
|
|
26
34
|
const t = document.createElement("style");
|
|
27
|
-
t.setAttribute("data-snapdom", "embedFonts"), t.textContent =
|
|
35
|
+
t.setAttribute("data-snapdom", "embedFonts"), t.textContent = n.resource.get("fonts-embed-css"), document.head.appendChild(t);
|
|
28
36
|
}
|
|
29
|
-
return
|
|
37
|
+
return n.resource.get("fonts-embed-css");
|
|
30
38
|
}
|
|
31
|
-
const i = /@import\s+url\(["']?([^"')]+)["']?\)/g,
|
|
39
|
+
const i = /@import\s+url\(["']?([^"')]+)["']?\)/g, l = [];
|
|
32
40
|
for (const t of document.querySelectorAll("style")) {
|
|
33
|
-
const
|
|
34
|
-
for (const
|
|
35
|
-
const
|
|
36
|
-
|
|
41
|
+
const e = t.textContent || "", r = Array.from(e.matchAll(i));
|
|
42
|
+
for (const d of r) {
|
|
43
|
+
const f = d[1];
|
|
44
|
+
h(f) || x(f) || l.push(f);
|
|
37
45
|
}
|
|
38
46
|
}
|
|
39
|
-
await Promise.all(
|
|
40
|
-
const
|
|
47
|
+
await Promise.all(l.map(L));
|
|
48
|
+
const y = Array.from(
|
|
41
49
|
document.querySelectorAll('link[rel="stylesheet"]')
|
|
42
50
|
).filter((t) => t.href);
|
|
43
51
|
let u = "";
|
|
44
|
-
for (const t of
|
|
52
|
+
for (const t of y)
|
|
45
53
|
try {
|
|
46
|
-
const
|
|
47
|
-
if (
|
|
48
|
-
const
|
|
49
|
-
Array.from(
|
|
50
|
-
let
|
|
51
|
-
if (!
|
|
52
|
-
let
|
|
53
|
-
if (!
|
|
54
|
-
if (
|
|
55
|
-
return
|
|
56
|
-
original:
|
|
57
|
-
inlined: `url(${
|
|
54
|
+
const r = await (await fetch(t.href)).text();
|
|
55
|
+
if (h(t.href) || h(r)) continue;
|
|
56
|
+
const d = /url\((["']?)([^"')]+)\1\)/g, f = await Promise.all(
|
|
57
|
+
Array.from(r.matchAll(d)).map(async (s) => {
|
|
58
|
+
let b = U(s[0]);
|
|
59
|
+
if (!b) return null;
|
|
60
|
+
let o = b;
|
|
61
|
+
if (!o.startsWith("http") && !o.startsWith("data:") && (o = new URL(o, t.href).href), h(o)) return null;
|
|
62
|
+
if (n.resource.has(o))
|
|
63
|
+
return n.font.add(o), {
|
|
64
|
+
original: s[0],
|
|
65
|
+
inlined: `url(${n.resource.get(o)})`
|
|
58
66
|
};
|
|
59
|
-
if (
|
|
67
|
+
if (n.font.has(o)) return null;
|
|
60
68
|
try {
|
|
61
|
-
const
|
|
62
|
-
const
|
|
63
|
-
|
|
69
|
+
const p = await (await fetch(o)).blob(), w = await new Promise((a) => {
|
|
70
|
+
const g = new FileReader();
|
|
71
|
+
g.onload = () => a(g.result), g.readAsDataURL(p);
|
|
64
72
|
});
|
|
65
|
-
return
|
|
73
|
+
return n.resource.set(o, w), n.font.add(o), { original: s[0], inlined: `url(${w})` };
|
|
66
74
|
} catch {
|
|
67
|
-
return console.warn("[snapdom] Failed to fetch font resource:",
|
|
75
|
+
return console.warn("[snapdom] Failed to fetch font resource:", o), null;
|
|
68
76
|
}
|
|
69
77
|
})
|
|
70
78
|
);
|
|
71
|
-
let
|
|
72
|
-
for (const
|
|
73
|
-
|
|
74
|
-
u +=
|
|
79
|
+
let c = r;
|
|
80
|
+
for (const s of f)
|
|
81
|
+
s && (c = c.replace(s.original, s.inlined));
|
|
82
|
+
u += c + `
|
|
75
83
|
`;
|
|
76
84
|
} catch {
|
|
77
85
|
console.warn("[snapdom] Failed to fetch CSS:", t.href);
|
|
78
86
|
}
|
|
79
87
|
for (const t of document.styleSheets)
|
|
80
88
|
try {
|
|
81
|
-
if (!t.href ||
|
|
82
|
-
for (const
|
|
83
|
-
if (
|
|
84
|
-
const
|
|
85
|
-
if (!
|
|
86
|
-
const
|
|
87
|
-
if (!
|
|
88
|
-
u += `@font-face{font-family:${
|
|
89
|
+
if (!t.href || y.every((e) => e.href !== t.href)) {
|
|
90
|
+
for (const e of t.cssRules)
|
|
91
|
+
if (e.type === CSSRule.FONT_FACE_RULE) {
|
|
92
|
+
const r = e.style.getPropertyValue("src"), d = e.style.getPropertyValue("font-family");
|
|
93
|
+
if (!r || h(d)) continue;
|
|
94
|
+
const f = /url\((["']?)([^"')]+)\1\)/g, c = /local\((["']?)[^)]+?\1\)/g, s = !!r.match(f), b = !!r.match(c);
|
|
95
|
+
if (!s && b) {
|
|
96
|
+
u += `@font-face{font-family:${d};src:${r};font-style:${e.style.getPropertyValue("font-style") || "normal"};font-weight:${e.style.getPropertyValue("font-weight") || "normal"};}`;
|
|
89
97
|
continue;
|
|
90
98
|
}
|
|
91
|
-
let
|
|
92
|
-
const
|
|
93
|
-
for (const
|
|
94
|
-
let
|
|
95
|
-
if (!
|
|
96
|
-
let
|
|
97
|
-
if (!
|
|
98
|
-
if (
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
`url(${
|
|
99
|
+
let o = r;
|
|
100
|
+
const S = Array.from(r.matchAll(f));
|
|
101
|
+
for (const p of S) {
|
|
102
|
+
let w = p[2].trim();
|
|
103
|
+
if (!w) continue;
|
|
104
|
+
let a = w;
|
|
105
|
+
if (!a.startsWith("http") && !a.startsWith("data:") && (a = new URL(a, t.href || location.href).href), !h(a)) {
|
|
106
|
+
if (n.resource.has(a)) {
|
|
107
|
+
n.font.add(a), o = o.replace(
|
|
108
|
+
p[0],
|
|
109
|
+
`url(${n.resource.get(a)})`
|
|
102
110
|
);
|
|
103
111
|
continue;
|
|
104
112
|
}
|
|
105
|
-
if (!
|
|
113
|
+
if (!n.font.has(a))
|
|
106
114
|
try {
|
|
107
|
-
const A = await (await fetch(
|
|
108
|
-
const
|
|
109
|
-
|
|
115
|
+
const A = await (await fetch(a)).blob(), R = await new Promise((C) => {
|
|
116
|
+
const $ = new FileReader();
|
|
117
|
+
$.onload = () => C($.result), $.readAsDataURL(A);
|
|
110
118
|
});
|
|
111
|
-
|
|
119
|
+
n.resource.set(a, R), n.font.add(a), o = o.replace(p[0], `url(${R})`);
|
|
112
120
|
} catch {
|
|
113
|
-
console.warn("[snapdom] Failed to fetch font URL:",
|
|
121
|
+
console.warn("[snapdom] Failed to fetch font URL:", a);
|
|
114
122
|
}
|
|
115
123
|
}
|
|
116
124
|
}
|
|
117
|
-
u += `@font-face{font-family:${
|
|
125
|
+
u += `@font-face{font-family:${d};src:${o};font-style:${e.style.getPropertyValue("font-style") || "normal"};font-weight:${e.style.getPropertyValue("font-weight") || "normal"};}`;
|
|
118
126
|
}
|
|
119
127
|
}
|
|
120
|
-
} catch (
|
|
121
|
-
console.warn("[snapdom] Cannot access stylesheet", t.href,
|
|
128
|
+
} catch (e) {
|
|
129
|
+
console.warn("[snapdom] Cannot access stylesheet", t.href, e);
|
|
122
130
|
}
|
|
123
131
|
for (const t of document.fonts)
|
|
124
132
|
if (t.family && t.status === "loaded" && t._snapdomSrc) {
|
|
125
|
-
if (
|
|
126
|
-
let
|
|
127
|
-
if (!
|
|
128
|
-
if (
|
|
129
|
-
|
|
130
|
-
else if (!
|
|
133
|
+
if (h(t.family)) continue;
|
|
134
|
+
let e = t._snapdomSrc;
|
|
135
|
+
if (!e.startsWith("data:")) {
|
|
136
|
+
if (n.resource.has(t._snapdomSrc))
|
|
137
|
+
e = n.resource.get(t._snapdomSrc), n.font.add(t._snapdomSrc);
|
|
138
|
+
else if (!n.font.has(t._snapdomSrc))
|
|
131
139
|
try {
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
const
|
|
135
|
-
|
|
136
|
-
}),
|
|
140
|
+
const d = await (await fetch(t._snapdomSrc)).blob();
|
|
141
|
+
e = await new Promise((f) => {
|
|
142
|
+
const c = new FileReader();
|
|
143
|
+
c.onload = () => f(c.result), c.readAsDataURL(d);
|
|
144
|
+
}), n.resource.set(t._snapdomSrc, e), n.font.add(t._snapdomSrc);
|
|
137
145
|
} catch {
|
|
138
146
|
console.warn(
|
|
139
147
|
"[snapdom] Failed to fetch dynamic font src:",
|
|
@@ -142,15 +150,15 @@ async function T({ preCached: m = !1 } = {}) {
|
|
|
142
150
|
continue;
|
|
143
151
|
}
|
|
144
152
|
}
|
|
145
|
-
u += `@font-face{font-family:'${t.family}';src:url(${
|
|
153
|
+
u += `@font-face{font-family:'${t.family}';src:url(${e});font-style:${t.style || "normal"};font-weight:${t.weight || "normal"};}`;
|
|
146
154
|
}
|
|
147
|
-
if (u && (
|
|
155
|
+
if (u && (n.resource.set("fonts-embed-css", u), m)) {
|
|
148
156
|
const t = document.createElement("style");
|
|
149
157
|
t.setAttribute("data-snapdom", "embedFonts"), t.textContent = u, document.head.appendChild(t);
|
|
150
158
|
}
|
|
151
159
|
return u;
|
|
152
160
|
}
|
|
153
161
|
export {
|
|
154
|
-
|
|
162
|
+
k as embedCustomFonts,
|
|
155
163
|
E as iconToImage
|
|
156
164
|
};
|