@ibiz-template/vue3-components 0.7.41-alpha.45 → 0.7.41-alpha.47
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/ibiz-markdown-editor-DlU-04L2.js +1 -0
- package/dist/{index-DgqTP4Vw.js → index-B0XiEslO.js} +1 -1
- package/dist/{index-Dr0VM8iS.js → index-B6kaPJDO.js} +1 -1
- package/dist/index-BniHS_Fs.js +11 -0
- package/dist/index.min.css +1 -1
- package/dist/index.system.min.js +1 -1
- package/dist/{wang-editor-homqxACE.js → wang-editor-Bx-SzrX-.js} +1 -1
- package/dist/{xlsx-util-C5MBICNN.js → xlsx-util-DgYbktap.js} +1 -1
- package/es/common/button-list/button-list.css +1 -1
- package/es/control/drbar/drbar.controller.mjs +6 -1
- package/es/control/drbar/drbar.mjs +5 -5
- package/es/control/drtab/drtab-control.util.mjs +1 -1
- package/es/control/drtab/drtab.controller.mjs +6 -1
- package/es/control/drtab/drtab.mjs +5 -5
- package/es/control/form/form-detail/form-item/composite-form-item-ex/composite-form-item-ex.controller.mjs +14 -3
- package/es/control/form/form-detail/form-item/composite-form-item-ex/composite-form-item-ex.mjs +2 -2
- package/es/control/form/form-detail/form-item/composite-form-item-ex/composite-form-item-ex.state.mjs +6 -0
- package/es/editor/code/monaco-editor/monaco-editor.mjs +2 -2
- package/es/editor/markdown/ibiz-markdown-editor/ibiz-markdown-editor.mjs +10 -6
- package/es/index.mjs +1 -0
- package/es/locale/en/index.mjs +13 -0
- package/es/locale/zh-CN/index.mjs +13 -0
- package/es/node_modules/.pnpm/modern-screenshot@4.6.7/node_modules/modern-screenshot/dist/index.mjs +1652 -0
- package/es/util/ai-chat-util/ai-chat-util.mjs +57 -23
- package/es/util/app-util/app-util.mjs +2 -4
- package/es/util/index.mjs +1 -0
- package/es/util/inline-ai-util/inline-ai-textarea/inline-ai-textarea.hook.mjs +3 -3
- package/es/util/screen-shot-util/screen-shot/components/index.mjs +3 -0
- package/es/util/screen-shot-util/screen-shot/components/screen-shot/screen-shot.css +1 -0
- package/es/util/screen-shot-util/screen-shot/components/screen-shot/screen-shot.mjs +113 -0
- package/es/util/screen-shot-util/screen-shot/components/screen-shot-toolbar/screen-shot-toolbar.css +1 -0
- package/es/util/screen-shot-util/screen-shot/components/screen-shot-toolbar/screen-shot-toolbar.mjs +105 -0
- package/es/util/screen-shot-util/screen-shot/constant/index.mjs +172 -0
- package/es/util/screen-shot-util/screen-shot/controller/index.mjs +3 -0
- package/es/util/screen-shot-util/screen-shot/controller/screen-shot.controller.mjs +366 -0
- package/es/util/screen-shot-util/screen-shot/controller/screen-shot.store.mjs +51 -0
- package/es/util/screen-shot-util/screen-shot/icon/icon.mjs +90 -0
- package/es/util/screen-shot-util/screen-shot/icon/index.mjs +3 -0
- package/es/util/screen-shot-util/screen-shot/index.mjs +4 -0
- package/es/util/screen-shot-util/screen-shot/module/draw-arrow.mjs +109 -0
- package/es/util/screen-shot-util/screen-shot/module/draw-circle.mjs +25 -0
- package/es/util/screen-shot-util/screen-shot/module/draw-mosaic.mjs +54 -0
- package/es/util/screen-shot-util/screen-shot/module/draw-pencil.mjs +15 -0
- package/es/util/screen-shot-util/screen-shot/module/draw-rectangle.mjs +12 -0
- package/es/util/screen-shot-util/screen-shot/module/draw-text.mjs +12 -0
- package/es/util/screen-shot-util/screen-shot/module/index.mjs +8 -0
- package/es/util/screen-shot-util/screen-shot/type/index.mjs +16 -0
- package/es/util/screen-shot-util/screen-shot/util/index.mjs +8 -0
- package/es/util/screen-shot-util/screen-shot-util.mjs +67 -0
- package/es/view-engine/edit-view.engine.mjs +6 -1
- package/es/view-engine/opt-view.engine.mjs +6 -1
- package/es/web-app/main.mjs +2 -0
- package/lib/common/button-list/button-list.css +1 -1
- package/lib/control/drbar/drbar.cjs +4 -4
- package/lib/control/drbar/drbar.controller.cjs +6 -1
- package/lib/control/drtab/drtab-control.util.cjs +1 -1
- package/lib/control/drtab/drtab.cjs +4 -4
- package/lib/control/drtab/drtab.controller.cjs +6 -1
- package/lib/control/form/form-detail/form-item/composite-form-item-ex/composite-form-item-ex.cjs +2 -2
- package/lib/control/form/form-detail/form-item/composite-form-item-ex/composite-form-item-ex.controller.cjs +14 -3
- package/lib/control/form/form-detail/form-item/composite-form-item-ex/composite-form-item-ex.state.cjs +6 -0
- package/lib/editor/code/monaco-editor/monaco-editor.cjs +2 -2
- package/lib/editor/markdown/ibiz-markdown-editor/ibiz-markdown-editor.cjs +10 -6
- package/lib/index.cjs +2 -0
- package/lib/locale/en/index.cjs +13 -0
- package/lib/locale/zh-CN/index.cjs +13 -0
- package/lib/node_modules/.pnpm/modern-screenshot@4.6.7/node_modules/modern-screenshot/dist/index.cjs +1667 -0
- package/lib/util/ai-chat-util/ai-chat-util.cjs +57 -23
- package/lib/util/app-util/app-util.cjs +2 -4
- package/lib/util/index.cjs +2 -0
- package/lib/util/inline-ai-util/inline-ai-textarea/inline-ai-textarea.hook.cjs +3 -3
- package/lib/util/screen-shot-util/screen-shot/components/index.cjs +7 -0
- package/lib/util/screen-shot-util/screen-shot/components/screen-shot/screen-shot.cjs +115 -0
- package/lib/util/screen-shot-util/screen-shot/components/screen-shot/screen-shot.css +1 -0
- package/lib/util/screen-shot-util/screen-shot/components/screen-shot-toolbar/screen-shot-toolbar.cjs +107 -0
- package/lib/util/screen-shot-util/screen-shot/components/screen-shot-toolbar/screen-shot-toolbar.css +1 -0
- package/lib/util/screen-shot-util/screen-shot/constant/index.cjs +174 -0
- package/lib/util/screen-shot-util/screen-shot/controller/index.cjs +7 -0
- package/lib/util/screen-shot-util/screen-shot/controller/screen-shot.controller.cjs +368 -0
- package/lib/util/screen-shot-util/screen-shot/controller/screen-shot.store.cjs +53 -0
- package/lib/util/screen-shot-util/screen-shot/icon/icon.cjs +100 -0
- package/lib/util/screen-shot-util/screen-shot/icon/index.cjs +15 -0
- package/lib/util/screen-shot-util/screen-shot/index.cjs +8 -0
- package/lib/util/screen-shot-util/screen-shot/module/draw-arrow.cjs +111 -0
- package/lib/util/screen-shot-util/screen-shot/module/draw-circle.cjs +27 -0
- package/lib/util/screen-shot-util/screen-shot/module/draw-mosaic.cjs +56 -0
- package/lib/util/screen-shot-util/screen-shot/module/draw-pencil.cjs +18 -0
- package/lib/util/screen-shot-util/screen-shot/module/draw-rectangle.cjs +14 -0
- package/lib/util/screen-shot-util/screen-shot/module/draw-text.cjs +14 -0
- package/lib/util/screen-shot-util/screen-shot/module/index.cjs +18 -0
- package/lib/util/screen-shot-util/screen-shot/type/index.cjs +18 -0
- package/lib/util/screen-shot-util/screen-shot/util/index.cjs +10 -0
- package/lib/util/screen-shot-util/screen-shot-util.cjs +69 -0
- package/lib/view-engine/edit-view.engine.cjs +6 -1
- package/lib/view-engine/opt-view.engine.cjs +6 -1
- package/lib/web-app/main.cjs +2 -0
- package/package.json +6 -5
- package/dist/ibiz-markdown-editor-C7wR017-.js +0 -1
- package/dist/index-yLzheFmH.js +0 -11
package/lib/node_modules/.pnpm/modern-screenshot@4.6.7/node_modules/modern-screenshot/dist/index.cjs
ADDED
|
@@ -0,0 +1,1667 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function changeJpegDpi(uint8Array, dpi) {
|
|
4
|
+
uint8Array[13] = 1;
|
|
5
|
+
uint8Array[14] = dpi >> 8;
|
|
6
|
+
uint8Array[15] = dpi & 255;
|
|
7
|
+
uint8Array[16] = dpi >> 8;
|
|
8
|
+
uint8Array[17] = dpi & 255;
|
|
9
|
+
return uint8Array;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const _P = "p".charCodeAt(0);
|
|
13
|
+
const _H = "H".charCodeAt(0);
|
|
14
|
+
const _Y = "Y".charCodeAt(0);
|
|
15
|
+
const _S = "s".charCodeAt(0);
|
|
16
|
+
let pngDataTable;
|
|
17
|
+
function createPngDataTable() {
|
|
18
|
+
const crcTable = new Int32Array(256);
|
|
19
|
+
for (let n = 0; n < 256; n++) {
|
|
20
|
+
let c = n;
|
|
21
|
+
for (let k = 0; k < 8; k++) {
|
|
22
|
+
c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
|
|
23
|
+
}
|
|
24
|
+
crcTable[n] = c;
|
|
25
|
+
}
|
|
26
|
+
return crcTable;
|
|
27
|
+
}
|
|
28
|
+
function calcCrc(uint8Array) {
|
|
29
|
+
let c = -1;
|
|
30
|
+
if (!pngDataTable)
|
|
31
|
+
pngDataTable = createPngDataTable();
|
|
32
|
+
for (let n = 0; n < uint8Array.length; n++) {
|
|
33
|
+
c = pngDataTable[(c ^ uint8Array[n]) & 255] ^ c >>> 8;
|
|
34
|
+
}
|
|
35
|
+
return c ^ -1;
|
|
36
|
+
}
|
|
37
|
+
function searchStartOfPhys(uint8Array) {
|
|
38
|
+
const length = uint8Array.length - 1;
|
|
39
|
+
for (let i = length; i >= 4; i--) {
|
|
40
|
+
if (uint8Array[i - 4] === 9 && uint8Array[i - 3] === _P && uint8Array[i - 2] === _H && uint8Array[i - 1] === _Y && uint8Array[i] === _S) {
|
|
41
|
+
return i - 3;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return 0;
|
|
45
|
+
}
|
|
46
|
+
function changePngDpi(uint8Array, dpi, overwritepHYs = false) {
|
|
47
|
+
const physChunk = new Uint8Array(13);
|
|
48
|
+
dpi *= 39.3701;
|
|
49
|
+
physChunk[0] = _P;
|
|
50
|
+
physChunk[1] = _H;
|
|
51
|
+
physChunk[2] = _Y;
|
|
52
|
+
physChunk[3] = _S;
|
|
53
|
+
physChunk[4] = dpi >>> 24;
|
|
54
|
+
physChunk[5] = dpi >>> 16;
|
|
55
|
+
physChunk[6] = dpi >>> 8;
|
|
56
|
+
physChunk[7] = dpi & 255;
|
|
57
|
+
physChunk[8] = physChunk[4];
|
|
58
|
+
physChunk[9] = physChunk[5];
|
|
59
|
+
physChunk[10] = physChunk[6];
|
|
60
|
+
physChunk[11] = physChunk[7];
|
|
61
|
+
physChunk[12] = 1;
|
|
62
|
+
const crc = calcCrc(physChunk);
|
|
63
|
+
const crcChunk = new Uint8Array(4);
|
|
64
|
+
crcChunk[0] = crc >>> 24;
|
|
65
|
+
crcChunk[1] = crc >>> 16;
|
|
66
|
+
crcChunk[2] = crc >>> 8;
|
|
67
|
+
crcChunk[3] = crc & 255;
|
|
68
|
+
if (overwritepHYs) {
|
|
69
|
+
const startingIndex = searchStartOfPhys(uint8Array);
|
|
70
|
+
uint8Array.set(physChunk, startingIndex);
|
|
71
|
+
uint8Array.set(crcChunk, startingIndex + 13);
|
|
72
|
+
return uint8Array;
|
|
73
|
+
} else {
|
|
74
|
+
const chunkLength = new Uint8Array(4);
|
|
75
|
+
chunkLength[0] = 0;
|
|
76
|
+
chunkLength[1] = 0;
|
|
77
|
+
chunkLength[2] = 0;
|
|
78
|
+
chunkLength[3] = 9;
|
|
79
|
+
const finalHeader = new Uint8Array(54);
|
|
80
|
+
finalHeader.set(uint8Array, 0);
|
|
81
|
+
finalHeader.set(chunkLength, 33);
|
|
82
|
+
finalHeader.set(physChunk, 37);
|
|
83
|
+
finalHeader.set(crcChunk, 50);
|
|
84
|
+
return finalHeader;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const b64PhysSignature1 = "AAlwSFlz";
|
|
88
|
+
const b64PhysSignature2 = "AAAJcEhZ";
|
|
89
|
+
const b64PhysSignature3 = "AAAACXBI";
|
|
90
|
+
function detectPhysChunkFromDataUrl(dataUrl) {
|
|
91
|
+
let b64index = dataUrl.indexOf(b64PhysSignature1);
|
|
92
|
+
if (b64index === -1) {
|
|
93
|
+
b64index = dataUrl.indexOf(b64PhysSignature2);
|
|
94
|
+
}
|
|
95
|
+
if (b64index === -1) {
|
|
96
|
+
b64index = dataUrl.indexOf(b64PhysSignature3);
|
|
97
|
+
}
|
|
98
|
+
return b64index;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const PREFIX = "[modern-screenshot]";
|
|
102
|
+
const IN_BROWSER = typeof window !== "undefined";
|
|
103
|
+
const SUPPORT_WEB_WORKER = IN_BROWSER && "Worker" in window;
|
|
104
|
+
const SUPPORT_ATOB = IN_BROWSER && "atob" in window;
|
|
105
|
+
const SUPPORT_BTOA = IN_BROWSER && "btoa" in window;
|
|
106
|
+
const USER_AGENT = IN_BROWSER ? window.navigator?.userAgent : "";
|
|
107
|
+
const IN_CHROME = USER_AGENT.includes("Chrome");
|
|
108
|
+
const IN_SAFARI = USER_AGENT.includes("AppleWebKit") && !IN_CHROME;
|
|
109
|
+
const IN_FIREFOX = USER_AGENT.includes("Firefox");
|
|
110
|
+
const isContext = (value) => value && "__CONTEXT__" in value;
|
|
111
|
+
const isCssFontFaceRule = (rule) => rule.constructor.name === "CSSFontFaceRule";
|
|
112
|
+
const isCSSImportRule = (rule) => rule.constructor.name === "CSSImportRule";
|
|
113
|
+
const isElementNode = (node) => node.nodeType === 1;
|
|
114
|
+
const isSVGElementNode = (node) => typeof node.className === "object";
|
|
115
|
+
const isSVGImageElementNode = (node) => node.tagName === "image";
|
|
116
|
+
const isSVGUseElementNode = (node) => node.tagName === "use";
|
|
117
|
+
const isHTMLElementNode = (node) => isElementNode(node) && typeof node.style !== "undefined" && !isSVGElementNode(node);
|
|
118
|
+
const isCommentNode = (node) => node.nodeType === 8;
|
|
119
|
+
const isTextNode = (node) => node.nodeType === 3;
|
|
120
|
+
const isImageElement = (node) => node.tagName === "IMG";
|
|
121
|
+
const isVideoElement = (node) => node.tagName === "VIDEO";
|
|
122
|
+
const isCanvasElement = (node) => node.tagName === "CANVAS";
|
|
123
|
+
const isTextareaElement = (node) => node.tagName === "TEXTAREA";
|
|
124
|
+
const isInputElement = (node) => node.tagName === "INPUT";
|
|
125
|
+
const isStyleElement = (node) => node.tagName === "STYLE";
|
|
126
|
+
const isScriptElement = (node) => node.tagName === "SCRIPT";
|
|
127
|
+
const isSelectElement = (node) => node.tagName === "SELECT";
|
|
128
|
+
const isSlotElement = (node) => node.tagName === "SLOT";
|
|
129
|
+
const isIFrameElement = (node) => node.tagName === "IFRAME";
|
|
130
|
+
const consoleWarn = (...args) => console.warn(PREFIX, ...args);
|
|
131
|
+
function supportWebp(ownerDocument) {
|
|
132
|
+
const canvas = ownerDocument?.createElement?.("canvas");
|
|
133
|
+
if (canvas) {
|
|
134
|
+
canvas.height = canvas.width = 1;
|
|
135
|
+
}
|
|
136
|
+
return Boolean(canvas) && "toDataURL" in canvas && Boolean(canvas.toDataURL("image/webp").includes("image/webp"));
|
|
137
|
+
}
|
|
138
|
+
const isDataUrl = (url) => url.startsWith("data:");
|
|
139
|
+
function resolveUrl(url, baseUrl) {
|
|
140
|
+
if (url.match(/^[a-z]+:\/\//i))
|
|
141
|
+
return url;
|
|
142
|
+
if (IN_BROWSER && url.match(/^\/\//))
|
|
143
|
+
return window.location.protocol + url;
|
|
144
|
+
if (url.match(/^[a-z]+:/i))
|
|
145
|
+
return url;
|
|
146
|
+
if (!IN_BROWSER)
|
|
147
|
+
return url;
|
|
148
|
+
const doc = getDocument().implementation.createHTMLDocument();
|
|
149
|
+
const base = doc.createElement("base");
|
|
150
|
+
const a = doc.createElement("a");
|
|
151
|
+
doc.head.appendChild(base);
|
|
152
|
+
doc.body.appendChild(a);
|
|
153
|
+
if (baseUrl)
|
|
154
|
+
base.href = baseUrl;
|
|
155
|
+
a.href = url;
|
|
156
|
+
return a.href;
|
|
157
|
+
}
|
|
158
|
+
function getDocument(target) {
|
|
159
|
+
return (target && isElementNode(target) ? target?.ownerDocument : target) ?? window.document;
|
|
160
|
+
}
|
|
161
|
+
const XMLNS = "http://www.w3.org/2000/svg";
|
|
162
|
+
function createSvg(width, height, ownerDocument) {
|
|
163
|
+
const svg = getDocument(ownerDocument).createElementNS(XMLNS, "svg");
|
|
164
|
+
svg.setAttributeNS(null, "width", width.toString());
|
|
165
|
+
svg.setAttributeNS(null, "height", height.toString());
|
|
166
|
+
svg.setAttributeNS(null, "viewBox", `0 0 ${width} ${height}`);
|
|
167
|
+
return svg;
|
|
168
|
+
}
|
|
169
|
+
function svgToDataUrl(svg, removeControlCharacter) {
|
|
170
|
+
let xhtml = new XMLSerializer().serializeToString(svg);
|
|
171
|
+
if (removeControlCharacter) {
|
|
172
|
+
xhtml = xhtml.replace(/[\u0000-\u0008\v\f\u000E-\u001F\uD800-\uDFFF\uFFFE\uFFFF]/gu, "");
|
|
173
|
+
}
|
|
174
|
+
return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(xhtml)}`;
|
|
175
|
+
}
|
|
176
|
+
async function canvasToBlob(canvas, type = "image/png", quality = 1) {
|
|
177
|
+
try {
|
|
178
|
+
return await new Promise((resolve, reject) => {
|
|
179
|
+
canvas.toBlob((blob) => {
|
|
180
|
+
if (blob) {
|
|
181
|
+
resolve(blob);
|
|
182
|
+
} else {
|
|
183
|
+
reject(new Error("Blob is null"));
|
|
184
|
+
}
|
|
185
|
+
}, type, quality);
|
|
186
|
+
});
|
|
187
|
+
} catch (error) {
|
|
188
|
+
if (SUPPORT_ATOB) {
|
|
189
|
+
return dataUrlToBlob(canvas.toDataURL(type, quality));
|
|
190
|
+
}
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
function dataUrlToBlob(dataUrl) {
|
|
195
|
+
const [header, base64] = dataUrl.split(",");
|
|
196
|
+
const type = header.match(/data:(.+);/)?.[1] ?? void 0;
|
|
197
|
+
const decoded = window.atob(base64);
|
|
198
|
+
const length = decoded.length;
|
|
199
|
+
const buffer = new Uint8Array(length);
|
|
200
|
+
for (let i = 0; i < length; i += 1) {
|
|
201
|
+
buffer[i] = decoded.charCodeAt(i);
|
|
202
|
+
}
|
|
203
|
+
return new Blob([buffer], { type });
|
|
204
|
+
}
|
|
205
|
+
function readBlob(blob, type) {
|
|
206
|
+
return new Promise((resolve, reject) => {
|
|
207
|
+
const reader = new FileReader();
|
|
208
|
+
reader.onload = () => resolve(reader.result);
|
|
209
|
+
reader.onerror = () => reject(reader.error);
|
|
210
|
+
reader.onabort = () => reject(new Error(`Failed read blob to ${type}`));
|
|
211
|
+
if (type === "dataUrl") {
|
|
212
|
+
reader.readAsDataURL(blob);
|
|
213
|
+
} else if (type === "arrayBuffer") {
|
|
214
|
+
reader.readAsArrayBuffer(blob);
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
const blobToDataUrl = (blob) => readBlob(blob, "dataUrl");
|
|
219
|
+
const blobToArrayBuffer = (blob) => readBlob(blob, "arrayBuffer");
|
|
220
|
+
function createImage(url, ownerDocument) {
|
|
221
|
+
const img = getDocument(ownerDocument).createElement("img");
|
|
222
|
+
img.decoding = "sync";
|
|
223
|
+
img.loading = "eager";
|
|
224
|
+
img.src = url;
|
|
225
|
+
return img;
|
|
226
|
+
}
|
|
227
|
+
function loadMedia(media, options) {
|
|
228
|
+
return new Promise((resolve) => {
|
|
229
|
+
const { timeout, ownerDocument, onError: userOnError, onWarn } = options ?? {};
|
|
230
|
+
const node = typeof media === "string" ? createImage(media, getDocument(ownerDocument)) : media;
|
|
231
|
+
let timer = null;
|
|
232
|
+
let removeEventListeners = null;
|
|
233
|
+
function onResolve() {
|
|
234
|
+
resolve(node);
|
|
235
|
+
timer && clearTimeout(timer);
|
|
236
|
+
removeEventListeners?.();
|
|
237
|
+
}
|
|
238
|
+
if (timeout) {
|
|
239
|
+
timer = setTimeout(onResolve, timeout);
|
|
240
|
+
}
|
|
241
|
+
if (isVideoElement(node)) {
|
|
242
|
+
const currentSrc = node.currentSrc || node.src;
|
|
243
|
+
if (!currentSrc) {
|
|
244
|
+
if (node.poster) {
|
|
245
|
+
return loadMedia(node.poster, options).then(resolve);
|
|
246
|
+
}
|
|
247
|
+
return onResolve();
|
|
248
|
+
}
|
|
249
|
+
if (node.readyState >= 2) {
|
|
250
|
+
return onResolve();
|
|
251
|
+
}
|
|
252
|
+
const onLoadeddata = onResolve;
|
|
253
|
+
const onError = (error) => {
|
|
254
|
+
onWarn?.(
|
|
255
|
+
"Failed video load",
|
|
256
|
+
currentSrc,
|
|
257
|
+
error
|
|
258
|
+
);
|
|
259
|
+
userOnError?.(error);
|
|
260
|
+
onResolve();
|
|
261
|
+
};
|
|
262
|
+
removeEventListeners = () => {
|
|
263
|
+
node.removeEventListener("loadeddata", onLoadeddata);
|
|
264
|
+
node.removeEventListener("error", onError);
|
|
265
|
+
};
|
|
266
|
+
node.addEventListener("loadeddata", onLoadeddata, { once: true });
|
|
267
|
+
node.addEventListener("error", onError, { once: true });
|
|
268
|
+
} else {
|
|
269
|
+
const currentSrc = isSVGImageElementNode(node) ? node.href.baseVal : node.currentSrc || node.src;
|
|
270
|
+
if (!currentSrc) {
|
|
271
|
+
return onResolve();
|
|
272
|
+
}
|
|
273
|
+
const onLoad = async () => {
|
|
274
|
+
if (isImageElement(node) && "decode" in node) {
|
|
275
|
+
try {
|
|
276
|
+
await node.decode();
|
|
277
|
+
} catch (error) {
|
|
278
|
+
onWarn?.(
|
|
279
|
+
"Failed to decode image, trying to render anyway",
|
|
280
|
+
node.dataset.originalSrc || currentSrc,
|
|
281
|
+
error
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
onResolve();
|
|
286
|
+
};
|
|
287
|
+
const onError = (error) => {
|
|
288
|
+
onWarn?.(
|
|
289
|
+
"Failed image load",
|
|
290
|
+
node.dataset.originalSrc || currentSrc,
|
|
291
|
+
error
|
|
292
|
+
);
|
|
293
|
+
onResolve();
|
|
294
|
+
};
|
|
295
|
+
if (isImageElement(node) && node.complete) {
|
|
296
|
+
return onLoad();
|
|
297
|
+
}
|
|
298
|
+
removeEventListeners = () => {
|
|
299
|
+
node.removeEventListener("load", onLoad);
|
|
300
|
+
node.removeEventListener("error", onError);
|
|
301
|
+
};
|
|
302
|
+
node.addEventListener("load", onLoad, { once: true });
|
|
303
|
+
node.addEventListener("error", onError, { once: true });
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
async function waitUntilLoad(node, options) {
|
|
308
|
+
if (isHTMLElementNode(node)) {
|
|
309
|
+
if (isImageElement(node) || isVideoElement(node)) {
|
|
310
|
+
await loadMedia(node, options);
|
|
311
|
+
} else {
|
|
312
|
+
await Promise.all(
|
|
313
|
+
["img", "video"].flatMap((selectors) => {
|
|
314
|
+
return Array.from(node.querySelectorAll(selectors)).map((el) => loadMedia(el, options));
|
|
315
|
+
})
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
const uuid = /* @__PURE__ */ function uuid2() {
|
|
321
|
+
let counter = 0;
|
|
322
|
+
const random = () => `0000${(Math.random() * 36 ** 4 << 0).toString(36)}`.slice(-4);
|
|
323
|
+
return () => {
|
|
324
|
+
counter += 1;
|
|
325
|
+
return `u${random()}${counter}`;
|
|
326
|
+
};
|
|
327
|
+
}();
|
|
328
|
+
function splitFontFamily(fontFamily) {
|
|
329
|
+
return fontFamily?.split(",").map((val) => val.trim().replace(/"|'/g, "").toLowerCase()).filter(Boolean);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
let uid = 0;
|
|
333
|
+
function createLogger(debug) {
|
|
334
|
+
const prefix = `${PREFIX}[#${uid}]`;
|
|
335
|
+
uid++;
|
|
336
|
+
return {
|
|
337
|
+
// eslint-disable-next-line no-console
|
|
338
|
+
time: (label) => debug && console.time(`${prefix} ${label}`),
|
|
339
|
+
// eslint-disable-next-line no-console
|
|
340
|
+
timeEnd: (label) => debug && console.timeEnd(`${prefix} ${label}`),
|
|
341
|
+
warn: (...args) => debug && consoleWarn(...args)
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function getDefaultRequestInit(bypassingCache) {
|
|
346
|
+
return {
|
|
347
|
+
cache: bypassingCache ? "no-cache" : "force-cache"
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
async function orCreateContext(node, options) {
|
|
352
|
+
return isContext(node) ? node : createContext(node, { ...options, autoDestruct: true });
|
|
353
|
+
}
|
|
354
|
+
async function createContext(node, options) {
|
|
355
|
+
const { scale = 1, workerUrl, workerNumber = 1 } = options || {};
|
|
356
|
+
const debug = Boolean(options?.debug);
|
|
357
|
+
const features = options?.features ?? true;
|
|
358
|
+
const ownerDocument = node.ownerDocument ?? (IN_BROWSER ? window.document : void 0);
|
|
359
|
+
const ownerWindow = node.ownerDocument?.defaultView ?? (IN_BROWSER ? window : void 0);
|
|
360
|
+
const requests = /* @__PURE__ */ new Map();
|
|
361
|
+
const context = {
|
|
362
|
+
// Options
|
|
363
|
+
width: 0,
|
|
364
|
+
height: 0,
|
|
365
|
+
quality: 1,
|
|
366
|
+
type: "image/png",
|
|
367
|
+
scale,
|
|
368
|
+
backgroundColor: null,
|
|
369
|
+
style: null,
|
|
370
|
+
filter: null,
|
|
371
|
+
maximumCanvasSize: 0,
|
|
372
|
+
timeout: 3e4,
|
|
373
|
+
progress: null,
|
|
374
|
+
debug,
|
|
375
|
+
fetch: {
|
|
376
|
+
requestInit: getDefaultRequestInit(options?.fetch?.bypassingCache),
|
|
377
|
+
placeholderImage: "data:image/png;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7",
|
|
378
|
+
bypassingCache: false,
|
|
379
|
+
...options?.fetch
|
|
380
|
+
},
|
|
381
|
+
fetchFn: null,
|
|
382
|
+
font: {},
|
|
383
|
+
drawImageInterval: 100,
|
|
384
|
+
workerUrl: null,
|
|
385
|
+
workerNumber,
|
|
386
|
+
onCloneEachNode: null,
|
|
387
|
+
onCloneNode: null,
|
|
388
|
+
onEmbedNode: null,
|
|
389
|
+
onCreateForeignObjectSvg: null,
|
|
390
|
+
includeStyleProperties: null,
|
|
391
|
+
autoDestruct: false,
|
|
392
|
+
...options,
|
|
393
|
+
// InternalContext
|
|
394
|
+
__CONTEXT__: true,
|
|
395
|
+
log: createLogger(debug),
|
|
396
|
+
node,
|
|
397
|
+
ownerDocument,
|
|
398
|
+
ownerWindow,
|
|
399
|
+
dpi: scale === 1 ? null : 96 * scale,
|
|
400
|
+
svgStyleElement: createStyleElement(ownerDocument),
|
|
401
|
+
svgDefsElement: ownerDocument?.createElementNS(XMLNS, "defs"),
|
|
402
|
+
svgStyles: /* @__PURE__ */ new Map(),
|
|
403
|
+
defaultComputedStyles: /* @__PURE__ */ new Map(),
|
|
404
|
+
workers: [
|
|
405
|
+
...Array.from({
|
|
406
|
+
length: SUPPORT_WEB_WORKER && workerUrl && workerNumber ? workerNumber : 0
|
|
407
|
+
})
|
|
408
|
+
].map(() => {
|
|
409
|
+
try {
|
|
410
|
+
const worker = new Worker(workerUrl);
|
|
411
|
+
worker.onmessage = async (event) => {
|
|
412
|
+
const { url, result } = event.data;
|
|
413
|
+
if (result) {
|
|
414
|
+
requests.get(url)?.resolve?.(result);
|
|
415
|
+
} else {
|
|
416
|
+
requests.get(url)?.reject?.(new Error(`Error receiving message from worker: ${url}`));
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
worker.onmessageerror = (event) => {
|
|
420
|
+
const { url } = event.data;
|
|
421
|
+
requests.get(url)?.reject?.(new Error(`Error receiving message from worker: ${url}`));
|
|
422
|
+
};
|
|
423
|
+
return worker;
|
|
424
|
+
} catch (error) {
|
|
425
|
+
context.log.warn("Failed to new Worker", error);
|
|
426
|
+
return null;
|
|
427
|
+
}
|
|
428
|
+
}).filter(Boolean),
|
|
429
|
+
fontFamilies: /* @__PURE__ */ new Map(),
|
|
430
|
+
fontCssTexts: /* @__PURE__ */ new Map(),
|
|
431
|
+
acceptOfImage: `${[
|
|
432
|
+
supportWebp(ownerDocument) && "image/webp",
|
|
433
|
+
"image/svg+xml",
|
|
434
|
+
"image/*",
|
|
435
|
+
"*/*"
|
|
436
|
+
].filter(Boolean).join(",")};q=0.8`,
|
|
437
|
+
requests,
|
|
438
|
+
drawImageCount: 0,
|
|
439
|
+
tasks: [],
|
|
440
|
+
features,
|
|
441
|
+
isEnable: (key) => {
|
|
442
|
+
if (key === "restoreScrollPosition") {
|
|
443
|
+
return typeof features === "boolean" ? false : features[key] ?? false;
|
|
444
|
+
}
|
|
445
|
+
if (typeof features === "boolean") {
|
|
446
|
+
return features;
|
|
447
|
+
}
|
|
448
|
+
return features[key] ?? true;
|
|
449
|
+
},
|
|
450
|
+
shadowRoots: []
|
|
451
|
+
};
|
|
452
|
+
context.log.time("wait until load");
|
|
453
|
+
await waitUntilLoad(node, { timeout: context.timeout, onWarn: context.log.warn });
|
|
454
|
+
context.log.timeEnd("wait until load");
|
|
455
|
+
const { width, height } = resolveBoundingBox(node, context);
|
|
456
|
+
context.width = width;
|
|
457
|
+
context.height = height;
|
|
458
|
+
return context;
|
|
459
|
+
}
|
|
460
|
+
function createStyleElement(ownerDocument) {
|
|
461
|
+
if (!ownerDocument)
|
|
462
|
+
return void 0;
|
|
463
|
+
const style = ownerDocument.createElement("style");
|
|
464
|
+
const cssText = style.ownerDocument.createTextNode(`
|
|
465
|
+
.______background-clip--text {
|
|
466
|
+
background-clip: text;
|
|
467
|
+
-webkit-background-clip: text;
|
|
468
|
+
}
|
|
469
|
+
`);
|
|
470
|
+
style.appendChild(cssText);
|
|
471
|
+
return style;
|
|
472
|
+
}
|
|
473
|
+
function resolveBoundingBox(node, context) {
|
|
474
|
+
let { width, height } = context;
|
|
475
|
+
if (isElementNode(node) && (!width || !height)) {
|
|
476
|
+
const box = node.getBoundingClientRect();
|
|
477
|
+
width = width || box.width || Number(node.getAttribute("width")) || 0;
|
|
478
|
+
height = height || box.height || Number(node.getAttribute("height")) || 0;
|
|
479
|
+
}
|
|
480
|
+
return { width, height };
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
async function imageToCanvas(image, context) {
|
|
484
|
+
const {
|
|
485
|
+
log,
|
|
486
|
+
timeout,
|
|
487
|
+
drawImageCount,
|
|
488
|
+
drawImageInterval
|
|
489
|
+
} = context;
|
|
490
|
+
log.time("image to canvas");
|
|
491
|
+
const loaded = await loadMedia(image, { timeout, onWarn: context.log.warn });
|
|
492
|
+
const { canvas, context2d } = createCanvas(image.ownerDocument, context);
|
|
493
|
+
const drawImage = () => {
|
|
494
|
+
try {
|
|
495
|
+
context2d?.drawImage(loaded, 0, 0, canvas.width, canvas.height);
|
|
496
|
+
} catch (error) {
|
|
497
|
+
context.log.warn("Failed to drawImage", error);
|
|
498
|
+
}
|
|
499
|
+
};
|
|
500
|
+
drawImage();
|
|
501
|
+
if (context.isEnable("fixSvgXmlDecode")) {
|
|
502
|
+
for (let i = 0; i < drawImageCount; i++) {
|
|
503
|
+
await new Promise((resolve) => {
|
|
504
|
+
setTimeout(() => {
|
|
505
|
+
context2d?.clearRect(0, 0, canvas.width, canvas.height);
|
|
506
|
+
drawImage();
|
|
507
|
+
resolve();
|
|
508
|
+
}, i + drawImageInterval);
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
context.drawImageCount = 0;
|
|
513
|
+
log.timeEnd("image to canvas");
|
|
514
|
+
return canvas;
|
|
515
|
+
}
|
|
516
|
+
function createCanvas(ownerDocument, context) {
|
|
517
|
+
const { width, height, scale, backgroundColor, maximumCanvasSize: max } = context;
|
|
518
|
+
const canvas = ownerDocument.createElement("canvas");
|
|
519
|
+
canvas.width = Math.floor(width * scale);
|
|
520
|
+
canvas.height = Math.floor(height * scale);
|
|
521
|
+
canvas.style.width = `${width}px`;
|
|
522
|
+
canvas.style.height = `${height}px`;
|
|
523
|
+
if (max) {
|
|
524
|
+
if (canvas.width > max || canvas.height > max) {
|
|
525
|
+
if (canvas.width > max && canvas.height > max) {
|
|
526
|
+
if (canvas.width > canvas.height) {
|
|
527
|
+
canvas.height *= max / canvas.width;
|
|
528
|
+
canvas.width = max;
|
|
529
|
+
} else {
|
|
530
|
+
canvas.width *= max / canvas.height;
|
|
531
|
+
canvas.height = max;
|
|
532
|
+
}
|
|
533
|
+
} else if (canvas.width > max) {
|
|
534
|
+
canvas.height *= max / canvas.width;
|
|
535
|
+
canvas.width = max;
|
|
536
|
+
} else {
|
|
537
|
+
canvas.width *= max / canvas.height;
|
|
538
|
+
canvas.height = max;
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
const context2d = canvas.getContext("2d");
|
|
543
|
+
if (context2d && backgroundColor) {
|
|
544
|
+
context2d.fillStyle = backgroundColor;
|
|
545
|
+
context2d.fillRect(0, 0, canvas.width, canvas.height);
|
|
546
|
+
}
|
|
547
|
+
return { canvas, context2d };
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
function cloneCanvas(canvas, context) {
|
|
551
|
+
if (canvas.ownerDocument) {
|
|
552
|
+
try {
|
|
553
|
+
const dataURL = canvas.toDataURL();
|
|
554
|
+
if (dataURL !== "data:,") {
|
|
555
|
+
return createImage(dataURL, canvas.ownerDocument);
|
|
556
|
+
}
|
|
557
|
+
} catch (error) {
|
|
558
|
+
context.log.warn("Failed to clone canvas", error);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
const cloned = canvas.cloneNode(false);
|
|
562
|
+
const ctx = canvas.getContext("2d");
|
|
563
|
+
const clonedCtx = cloned.getContext("2d");
|
|
564
|
+
try {
|
|
565
|
+
if (ctx && clonedCtx) {
|
|
566
|
+
clonedCtx.putImageData(
|
|
567
|
+
ctx.getImageData(0, 0, canvas.width, canvas.height),
|
|
568
|
+
0,
|
|
569
|
+
0
|
|
570
|
+
);
|
|
571
|
+
}
|
|
572
|
+
return cloned;
|
|
573
|
+
} catch (error) {
|
|
574
|
+
context.log.warn("Failed to clone canvas", error);
|
|
575
|
+
}
|
|
576
|
+
return cloned;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
function cloneIframe(iframe, context) {
|
|
580
|
+
try {
|
|
581
|
+
if (iframe?.contentDocument?.body) {
|
|
582
|
+
return cloneNode(iframe.contentDocument.body, context);
|
|
583
|
+
}
|
|
584
|
+
} catch (error) {
|
|
585
|
+
context.log.warn("Failed to clone iframe", error);
|
|
586
|
+
}
|
|
587
|
+
return iframe.cloneNode(false);
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
function cloneImage(image) {
|
|
591
|
+
const cloned = image.cloneNode(false);
|
|
592
|
+
if (image.currentSrc && image.currentSrc !== image.src) {
|
|
593
|
+
cloned.src = image.currentSrc;
|
|
594
|
+
cloned.srcset = "";
|
|
595
|
+
}
|
|
596
|
+
if (cloned.loading === "lazy") {
|
|
597
|
+
cloned.loading = "eager";
|
|
598
|
+
}
|
|
599
|
+
return cloned;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
async function cloneVideo(video, context) {
|
|
603
|
+
if (video.ownerDocument && !video.currentSrc && video.poster) {
|
|
604
|
+
return createImage(video.poster, video.ownerDocument);
|
|
605
|
+
}
|
|
606
|
+
const cloned = video.cloneNode(false);
|
|
607
|
+
cloned.crossOrigin = "anonymous";
|
|
608
|
+
if (video.currentSrc && video.currentSrc !== video.src) {
|
|
609
|
+
cloned.src = video.currentSrc;
|
|
610
|
+
}
|
|
611
|
+
const ownerDocument = cloned.ownerDocument;
|
|
612
|
+
if (ownerDocument) {
|
|
613
|
+
let canPlay = true;
|
|
614
|
+
await loadMedia(cloned, { onError: () => canPlay = false, onWarn: context.log.warn });
|
|
615
|
+
if (!canPlay) {
|
|
616
|
+
if (video.poster) {
|
|
617
|
+
return createImage(video.poster, video.ownerDocument);
|
|
618
|
+
}
|
|
619
|
+
return cloned;
|
|
620
|
+
}
|
|
621
|
+
cloned.currentTime = video.currentTime;
|
|
622
|
+
await new Promise((resolve) => {
|
|
623
|
+
cloned.addEventListener("seeked", resolve, { once: true });
|
|
624
|
+
});
|
|
625
|
+
const canvas = ownerDocument.createElement("canvas");
|
|
626
|
+
canvas.width = video.offsetWidth;
|
|
627
|
+
canvas.height = video.offsetHeight;
|
|
628
|
+
try {
|
|
629
|
+
const ctx = canvas.getContext("2d");
|
|
630
|
+
if (ctx)
|
|
631
|
+
ctx.drawImage(cloned, 0, 0, canvas.width, canvas.height);
|
|
632
|
+
} catch (error) {
|
|
633
|
+
context.log.warn("Failed to clone video", error);
|
|
634
|
+
if (video.poster) {
|
|
635
|
+
return createImage(video.poster, video.ownerDocument);
|
|
636
|
+
}
|
|
637
|
+
return cloned;
|
|
638
|
+
}
|
|
639
|
+
return cloneCanvas(canvas, context);
|
|
640
|
+
}
|
|
641
|
+
return cloned;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
function cloneElement(node, context) {
|
|
645
|
+
if (isCanvasElement(node)) {
|
|
646
|
+
return cloneCanvas(node, context);
|
|
647
|
+
}
|
|
648
|
+
if (isIFrameElement(node)) {
|
|
649
|
+
return cloneIframe(node, context);
|
|
650
|
+
}
|
|
651
|
+
if (isImageElement(node)) {
|
|
652
|
+
return cloneImage(node);
|
|
653
|
+
}
|
|
654
|
+
if (isVideoElement(node)) {
|
|
655
|
+
return cloneVideo(node, context);
|
|
656
|
+
}
|
|
657
|
+
return node.cloneNode(false);
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
function getSandBox(context) {
|
|
661
|
+
let sandbox = context.sandbox;
|
|
662
|
+
if (!sandbox) {
|
|
663
|
+
const { ownerDocument } = context;
|
|
664
|
+
try {
|
|
665
|
+
if (ownerDocument) {
|
|
666
|
+
sandbox = ownerDocument.createElement("iframe");
|
|
667
|
+
sandbox.id = `__SANDBOX__${uuid()}`;
|
|
668
|
+
sandbox.width = "0";
|
|
669
|
+
sandbox.height = "0";
|
|
670
|
+
sandbox.style.visibility = "hidden";
|
|
671
|
+
sandbox.style.position = "fixed";
|
|
672
|
+
ownerDocument.body.appendChild(sandbox);
|
|
673
|
+
sandbox.srcdoc = '<!DOCTYPE html><meta charset="UTF-8"><title></title><body>';
|
|
674
|
+
context.sandbox = sandbox;
|
|
675
|
+
}
|
|
676
|
+
} catch (error) {
|
|
677
|
+
context.log.warn("Failed to getSandBox", error);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
return sandbox;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
const ignoredStyles = [
|
|
684
|
+
"width",
|
|
685
|
+
"height",
|
|
686
|
+
"-webkit-text-fill-color"
|
|
687
|
+
];
|
|
688
|
+
const includedAttributes = [
|
|
689
|
+
"stroke",
|
|
690
|
+
"fill"
|
|
691
|
+
];
|
|
692
|
+
function getDefaultStyle(node, pseudoElement, context) {
|
|
693
|
+
const { defaultComputedStyles } = context;
|
|
694
|
+
const nodeName = node.nodeName.toLowerCase();
|
|
695
|
+
const isSvgNode = isSVGElementNode(node) && nodeName !== "svg";
|
|
696
|
+
const attributes = isSvgNode ? includedAttributes.map((name) => [name, node.getAttribute(name)]).filter(([, value]) => value !== null) : [];
|
|
697
|
+
const key = [
|
|
698
|
+
isSvgNode && "svg",
|
|
699
|
+
nodeName,
|
|
700
|
+
attributes.map((name, value) => `${name}=${value}`).join(","),
|
|
701
|
+
pseudoElement
|
|
702
|
+
].filter(Boolean).join(":");
|
|
703
|
+
if (defaultComputedStyles.has(key))
|
|
704
|
+
return defaultComputedStyles.get(key);
|
|
705
|
+
const sandbox = getSandBox(context);
|
|
706
|
+
const sandboxWindow = sandbox?.contentWindow;
|
|
707
|
+
if (!sandboxWindow)
|
|
708
|
+
return /* @__PURE__ */ new Map();
|
|
709
|
+
const sandboxDocument = sandboxWindow?.document;
|
|
710
|
+
let root;
|
|
711
|
+
let el;
|
|
712
|
+
if (isSvgNode) {
|
|
713
|
+
root = sandboxDocument.createElementNS(XMLNS, "svg");
|
|
714
|
+
el = root.ownerDocument.createElementNS(root.namespaceURI, nodeName);
|
|
715
|
+
attributes.forEach(([name, value]) => {
|
|
716
|
+
el.setAttributeNS(null, name, value);
|
|
717
|
+
});
|
|
718
|
+
root.appendChild(el);
|
|
719
|
+
} else {
|
|
720
|
+
root = el = sandboxDocument.createElement(nodeName);
|
|
721
|
+
}
|
|
722
|
+
el.textContent = " ";
|
|
723
|
+
sandboxDocument.body.appendChild(root);
|
|
724
|
+
const computedStyle = sandboxWindow.getComputedStyle(el, pseudoElement);
|
|
725
|
+
const styles = /* @__PURE__ */ new Map();
|
|
726
|
+
for (let len = computedStyle.length, i = 0; i < len; i++) {
|
|
727
|
+
const name = computedStyle.item(i);
|
|
728
|
+
if (ignoredStyles.includes(name))
|
|
729
|
+
continue;
|
|
730
|
+
styles.set(name, computedStyle.getPropertyValue(name));
|
|
731
|
+
}
|
|
732
|
+
sandboxDocument.body.removeChild(root);
|
|
733
|
+
defaultComputedStyles.set(key, styles);
|
|
734
|
+
return styles;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
function getDiffStyle(style, defaultStyle, includeStyleProperties) {
|
|
738
|
+
const diffStyle = /* @__PURE__ */ new Map();
|
|
739
|
+
const prefixs = [];
|
|
740
|
+
const prefixTree = /* @__PURE__ */ new Map();
|
|
741
|
+
if (includeStyleProperties) {
|
|
742
|
+
for (const name of includeStyleProperties) {
|
|
743
|
+
applyTo(name);
|
|
744
|
+
}
|
|
745
|
+
} else {
|
|
746
|
+
for (let len = style.length, i = 0; i < len; i++) {
|
|
747
|
+
const name = style.item(i);
|
|
748
|
+
applyTo(name);
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
for (let len = prefixs.length, i = 0; i < len; i++) {
|
|
752
|
+
prefixTree.get(prefixs[i])?.forEach((value, name) => diffStyle.set(name, value));
|
|
753
|
+
}
|
|
754
|
+
function applyTo(name) {
|
|
755
|
+
const value = style.getPropertyValue(name);
|
|
756
|
+
const priority = style.getPropertyPriority(name);
|
|
757
|
+
const subIndex = name.lastIndexOf("-");
|
|
758
|
+
const prefix = subIndex > -1 ? name.substring(0, subIndex) : void 0;
|
|
759
|
+
if (prefix) {
|
|
760
|
+
let map = prefixTree.get(prefix);
|
|
761
|
+
if (!map) {
|
|
762
|
+
map = /* @__PURE__ */ new Map();
|
|
763
|
+
prefixTree.set(prefix, map);
|
|
764
|
+
}
|
|
765
|
+
map.set(name, [value, priority]);
|
|
766
|
+
}
|
|
767
|
+
if (defaultStyle.get(name) === value && !priority)
|
|
768
|
+
return;
|
|
769
|
+
if (prefix) {
|
|
770
|
+
prefixs.push(prefix);
|
|
771
|
+
} else {
|
|
772
|
+
diffStyle.set(name, [value, priority]);
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
return diffStyle;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
function copyCssStyles(node, cloned, isRoot, context) {
|
|
779
|
+
const { ownerWindow, includeStyleProperties, currentParentNodeStyle } = context;
|
|
780
|
+
const clonedStyle = cloned.style;
|
|
781
|
+
const computedStyle = ownerWindow.getComputedStyle(node);
|
|
782
|
+
const defaultStyle = getDefaultStyle(node, null, context);
|
|
783
|
+
currentParentNodeStyle?.forEach((_, key) => {
|
|
784
|
+
defaultStyle.delete(key);
|
|
785
|
+
});
|
|
786
|
+
const style = getDiffStyle(computedStyle, defaultStyle, includeStyleProperties);
|
|
787
|
+
style.delete("transition-property");
|
|
788
|
+
style.delete("all");
|
|
789
|
+
style.delete("d");
|
|
790
|
+
style.delete("content");
|
|
791
|
+
if (isRoot) {
|
|
792
|
+
style.delete("margin-top");
|
|
793
|
+
style.delete("margin-right");
|
|
794
|
+
style.delete("margin-bottom");
|
|
795
|
+
style.delete("margin-left");
|
|
796
|
+
style.delete("margin-block-start");
|
|
797
|
+
style.delete("margin-block-end");
|
|
798
|
+
style.delete("margin-inline-start");
|
|
799
|
+
style.delete("margin-inline-end");
|
|
800
|
+
style.set("box-sizing", ["border-box", ""]);
|
|
801
|
+
}
|
|
802
|
+
if (style.get("background-clip")?.[0] === "text") {
|
|
803
|
+
cloned.classList.add("______background-clip--text");
|
|
804
|
+
}
|
|
805
|
+
if (IN_CHROME) {
|
|
806
|
+
if (!style.has("font-kerning"))
|
|
807
|
+
style.set("font-kerning", ["normal", ""]);
|
|
808
|
+
if ((style.get("overflow-x")?.[0] === "hidden" || style.get("overflow-y")?.[0] === "hidden") && style.get("text-overflow")?.[0] === "ellipsis" && node.scrollWidth === node.clientWidth) {
|
|
809
|
+
style.set("text-overflow", ["clip", ""]);
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
for (let len = clonedStyle.length, i = 0; i < len; i++) {
|
|
813
|
+
clonedStyle.removeProperty(clonedStyle.item(i));
|
|
814
|
+
}
|
|
815
|
+
style.forEach(([value, priority], name) => {
|
|
816
|
+
clonedStyle.setProperty(name, value, priority);
|
|
817
|
+
});
|
|
818
|
+
return style;
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
function copyInputValue(node, cloned) {
|
|
822
|
+
if (isTextareaElement(node) || isInputElement(node) || isSelectElement(node)) {
|
|
823
|
+
cloned.setAttribute("value", node.value);
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
const pseudoClasses = [
|
|
828
|
+
"::before",
|
|
829
|
+
"::after"
|
|
830
|
+
// '::placeholder', TODO
|
|
831
|
+
];
|
|
832
|
+
const scrollbarPseudoClasses = [
|
|
833
|
+
"::-webkit-scrollbar",
|
|
834
|
+
"::-webkit-scrollbar-button",
|
|
835
|
+
// '::-webkit-scrollbar:horizontal', TODO
|
|
836
|
+
"::-webkit-scrollbar-thumb",
|
|
837
|
+
"::-webkit-scrollbar-track",
|
|
838
|
+
"::-webkit-scrollbar-track-piece",
|
|
839
|
+
// '::-webkit-scrollbar:vertical', TODO
|
|
840
|
+
"::-webkit-scrollbar-corner",
|
|
841
|
+
"::-webkit-resizer"
|
|
842
|
+
];
|
|
843
|
+
function copyPseudoClass(node, cloned, copyScrollbar, context, addWordToFontFamilies) {
|
|
844
|
+
const { ownerWindow, svgStyleElement, svgStyles, currentNodeStyle } = context;
|
|
845
|
+
if (!svgStyleElement || !ownerWindow)
|
|
846
|
+
return;
|
|
847
|
+
function copyBy(pseudoClass) {
|
|
848
|
+
const computedStyle = ownerWindow.getComputedStyle(node, pseudoClass);
|
|
849
|
+
let content = computedStyle.getPropertyValue("content");
|
|
850
|
+
if (!content || content === "none")
|
|
851
|
+
return;
|
|
852
|
+
addWordToFontFamilies?.(content);
|
|
853
|
+
content = content.replace(/(')|(")|(counter\(.+\))/g, "");
|
|
854
|
+
const klasses = [uuid()];
|
|
855
|
+
const defaultStyle = getDefaultStyle(node, pseudoClass, context);
|
|
856
|
+
currentNodeStyle?.forEach((_, key) => {
|
|
857
|
+
defaultStyle.delete(key);
|
|
858
|
+
});
|
|
859
|
+
const style = getDiffStyle(computedStyle, defaultStyle, context.includeStyleProperties);
|
|
860
|
+
style.delete("content");
|
|
861
|
+
style.delete("-webkit-locale");
|
|
862
|
+
if (style.get("background-clip")?.[0] === "text") {
|
|
863
|
+
cloned.classList.add("______background-clip--text");
|
|
864
|
+
}
|
|
865
|
+
const cloneStyle = [
|
|
866
|
+
`content: '${content}';`
|
|
867
|
+
];
|
|
868
|
+
style.forEach(([value, priority], name) => {
|
|
869
|
+
cloneStyle.push(`${name}: ${value}${priority ? " !important" : ""};`);
|
|
870
|
+
});
|
|
871
|
+
if (cloneStyle.length === 1)
|
|
872
|
+
return;
|
|
873
|
+
try {
|
|
874
|
+
cloned.className = [cloned.className, ...klasses].join(" ");
|
|
875
|
+
} catch (err) {
|
|
876
|
+
context.log.warn("Failed to copyPseudoClass", err);
|
|
877
|
+
return;
|
|
878
|
+
}
|
|
879
|
+
const cssText = cloneStyle.join("\n ");
|
|
880
|
+
let allClasses = svgStyles.get(cssText);
|
|
881
|
+
if (!allClasses) {
|
|
882
|
+
allClasses = [];
|
|
883
|
+
svgStyles.set(cssText, allClasses);
|
|
884
|
+
}
|
|
885
|
+
allClasses.push(`.${klasses[0]}${pseudoClass}`);
|
|
886
|
+
}
|
|
887
|
+
pseudoClasses.forEach(copyBy);
|
|
888
|
+
if (copyScrollbar)
|
|
889
|
+
scrollbarPseudoClasses.forEach(copyBy);
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
const excludeParentNodes = /* @__PURE__ */ new Set([
|
|
893
|
+
"symbol"
|
|
894
|
+
// test/fixtures/svg.symbol.html
|
|
895
|
+
]);
|
|
896
|
+
async function appendChildNode(node, cloned, child, context, addWordToFontFamilies) {
|
|
897
|
+
if (isElementNode(child) && (isStyleElement(child) || isScriptElement(child)))
|
|
898
|
+
return;
|
|
899
|
+
if (context.filter && !context.filter(child))
|
|
900
|
+
return;
|
|
901
|
+
if (excludeParentNodes.has(cloned.nodeName) || excludeParentNodes.has(child.nodeName)) {
|
|
902
|
+
context.currentParentNodeStyle = void 0;
|
|
903
|
+
} else {
|
|
904
|
+
context.currentParentNodeStyle = context.currentNodeStyle;
|
|
905
|
+
}
|
|
906
|
+
const childCloned = await cloneNode(child, context, false, addWordToFontFamilies);
|
|
907
|
+
if (context.isEnable("restoreScrollPosition")) {
|
|
908
|
+
restoreScrollPosition(node, childCloned);
|
|
909
|
+
}
|
|
910
|
+
cloned.appendChild(childCloned);
|
|
911
|
+
}
|
|
912
|
+
async function cloneChildNodes(node, cloned, context, addWordToFontFamilies) {
|
|
913
|
+
let firstChild = node.firstChild;
|
|
914
|
+
if (isElementNode(node)) {
|
|
915
|
+
if (node.shadowRoot) {
|
|
916
|
+
firstChild = node.shadowRoot?.firstChild;
|
|
917
|
+
context.shadowRoots.push(node.shadowRoot);
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
for (let child = firstChild; child; child = child.nextSibling) {
|
|
921
|
+
if (isCommentNode(child))
|
|
922
|
+
continue;
|
|
923
|
+
if (isElementNode(child) && isSlotElement(child) && typeof child.assignedNodes === "function") {
|
|
924
|
+
const nodes = child.assignedNodes();
|
|
925
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
926
|
+
await appendChildNode(node, cloned, nodes[i], context, addWordToFontFamilies);
|
|
927
|
+
}
|
|
928
|
+
} else {
|
|
929
|
+
await appendChildNode(node, cloned, child, context, addWordToFontFamilies);
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
function restoreScrollPosition(node, chlidCloned) {
|
|
934
|
+
if (!isHTMLElementNode(node) || !isHTMLElementNode(chlidCloned))
|
|
935
|
+
return;
|
|
936
|
+
const { scrollTop, scrollLeft } = node;
|
|
937
|
+
if (!scrollTop && !scrollLeft) {
|
|
938
|
+
return;
|
|
939
|
+
}
|
|
940
|
+
const { transform } = chlidCloned.style;
|
|
941
|
+
const matrix = new DOMMatrix(transform);
|
|
942
|
+
const { a, b, c, d } = matrix;
|
|
943
|
+
matrix.a = 1;
|
|
944
|
+
matrix.b = 0;
|
|
945
|
+
matrix.c = 0;
|
|
946
|
+
matrix.d = 1;
|
|
947
|
+
matrix.translateSelf(-scrollLeft, -scrollTop);
|
|
948
|
+
matrix.a = a;
|
|
949
|
+
matrix.b = b;
|
|
950
|
+
matrix.c = c;
|
|
951
|
+
matrix.d = d;
|
|
952
|
+
chlidCloned.style.transform = matrix.toString();
|
|
953
|
+
}
|
|
954
|
+
function applyCssStyleWithOptions(cloned, context) {
|
|
955
|
+
const { backgroundColor, width, height, style: styles } = context;
|
|
956
|
+
const clonedStyle = cloned.style;
|
|
957
|
+
if (backgroundColor)
|
|
958
|
+
clonedStyle.setProperty("background-color", backgroundColor, "important");
|
|
959
|
+
if (width)
|
|
960
|
+
clonedStyle.setProperty("width", `${width}px`, "important");
|
|
961
|
+
if (height)
|
|
962
|
+
clonedStyle.setProperty("height", `${height}px`, "important");
|
|
963
|
+
if (styles) {
|
|
964
|
+
for (const name in styles) clonedStyle[name] = styles[name];
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
const NORMAL_ATTRIBUTE_RE = /^[\w-:]+$/;
|
|
968
|
+
async function cloneNode(node, context, isRoot = false, addWordToFontFamilies) {
|
|
969
|
+
const { ownerDocument, ownerWindow, fontFamilies, onCloneEachNode } = context;
|
|
970
|
+
if (ownerDocument && isTextNode(node)) {
|
|
971
|
+
if (addWordToFontFamilies && /\S/.test(node.data)) {
|
|
972
|
+
addWordToFontFamilies(node.data);
|
|
973
|
+
}
|
|
974
|
+
return ownerDocument.createTextNode(node.data);
|
|
975
|
+
}
|
|
976
|
+
if (ownerDocument && ownerWindow && isElementNode(node) && (isHTMLElementNode(node) || isSVGElementNode(node))) {
|
|
977
|
+
const cloned2 = await cloneElement(node, context);
|
|
978
|
+
if (context.isEnable("removeAbnormalAttributes")) {
|
|
979
|
+
const names = cloned2.getAttributeNames();
|
|
980
|
+
for (let len = names.length, i = 0; i < len; i++) {
|
|
981
|
+
const name = names[i];
|
|
982
|
+
if (!NORMAL_ATTRIBUTE_RE.test(name)) {
|
|
983
|
+
cloned2.removeAttribute(name);
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
const style = context.currentNodeStyle = copyCssStyles(node, cloned2, isRoot, context);
|
|
988
|
+
if (isRoot)
|
|
989
|
+
applyCssStyleWithOptions(cloned2, context);
|
|
990
|
+
let copyScrollbar = false;
|
|
991
|
+
if (context.isEnable("copyScrollbar")) {
|
|
992
|
+
const overflow = [
|
|
993
|
+
style.get("overflow-x")?.[0],
|
|
994
|
+
style.get("overflow-y")?.[0]
|
|
995
|
+
];
|
|
996
|
+
copyScrollbar = overflow.includes("scroll") || (overflow.includes("auto") || overflow.includes("overlay")) && (node.scrollHeight > node.clientHeight || node.scrollWidth > node.clientWidth);
|
|
997
|
+
}
|
|
998
|
+
const textTransform = style.get("text-transform")?.[0];
|
|
999
|
+
const families = splitFontFamily(style.get("font-family")?.[0]);
|
|
1000
|
+
const addWordToFontFamilies2 = families ? (word) => {
|
|
1001
|
+
if (textTransform === "uppercase") {
|
|
1002
|
+
word = word.toUpperCase();
|
|
1003
|
+
} else if (textTransform === "lowercase") {
|
|
1004
|
+
word = word.toLowerCase();
|
|
1005
|
+
} else if (textTransform === "capitalize") {
|
|
1006
|
+
word = word[0].toUpperCase() + word.substring(1);
|
|
1007
|
+
}
|
|
1008
|
+
families.forEach((family) => {
|
|
1009
|
+
let fontFamily = fontFamilies.get(family);
|
|
1010
|
+
if (!fontFamily) {
|
|
1011
|
+
fontFamilies.set(family, fontFamily = /* @__PURE__ */ new Set());
|
|
1012
|
+
}
|
|
1013
|
+
word.split("").forEach((text) => fontFamily.add(text));
|
|
1014
|
+
});
|
|
1015
|
+
} : void 0;
|
|
1016
|
+
copyPseudoClass(
|
|
1017
|
+
node,
|
|
1018
|
+
cloned2,
|
|
1019
|
+
copyScrollbar,
|
|
1020
|
+
context,
|
|
1021
|
+
addWordToFontFamilies2
|
|
1022
|
+
);
|
|
1023
|
+
copyInputValue(node, cloned2);
|
|
1024
|
+
if (!isVideoElement(node)) {
|
|
1025
|
+
await cloneChildNodes(
|
|
1026
|
+
node,
|
|
1027
|
+
cloned2,
|
|
1028
|
+
context,
|
|
1029
|
+
addWordToFontFamilies2
|
|
1030
|
+
);
|
|
1031
|
+
}
|
|
1032
|
+
await onCloneEachNode?.(cloned2);
|
|
1033
|
+
return cloned2;
|
|
1034
|
+
}
|
|
1035
|
+
const cloned = node.cloneNode(false);
|
|
1036
|
+
await cloneChildNodes(node, cloned, context);
|
|
1037
|
+
await onCloneEachNode?.(cloned);
|
|
1038
|
+
return cloned;
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
function destroyContext(context) {
|
|
1042
|
+
context.ownerDocument = void 0;
|
|
1043
|
+
context.ownerWindow = void 0;
|
|
1044
|
+
context.svgStyleElement = void 0;
|
|
1045
|
+
context.svgDefsElement = void 0;
|
|
1046
|
+
context.svgStyles.clear();
|
|
1047
|
+
context.defaultComputedStyles.clear();
|
|
1048
|
+
if (context.sandbox) {
|
|
1049
|
+
try {
|
|
1050
|
+
context.sandbox.remove();
|
|
1051
|
+
} catch (err) {
|
|
1052
|
+
context.log.warn("Failed to destroyContext", err);
|
|
1053
|
+
}
|
|
1054
|
+
context.sandbox = void 0;
|
|
1055
|
+
}
|
|
1056
|
+
context.workers = [];
|
|
1057
|
+
context.fontFamilies.clear();
|
|
1058
|
+
context.fontCssTexts.clear();
|
|
1059
|
+
context.requests.clear();
|
|
1060
|
+
context.tasks = [];
|
|
1061
|
+
context.shadowRoots = [];
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
function baseFetch(options) {
|
|
1065
|
+
const { url, timeout, responseType, ...requestInit } = options;
|
|
1066
|
+
const controller = new AbortController();
|
|
1067
|
+
const timer = timeout ? setTimeout(() => controller.abort(), timeout) : void 0;
|
|
1068
|
+
return fetch(url, { signal: controller.signal, ...requestInit }).then((response) => {
|
|
1069
|
+
if (!response.ok) {
|
|
1070
|
+
throw new Error("Failed fetch, not 2xx response", { cause: response });
|
|
1071
|
+
}
|
|
1072
|
+
switch (responseType) {
|
|
1073
|
+
case "arrayBuffer":
|
|
1074
|
+
return response.arrayBuffer();
|
|
1075
|
+
case "dataUrl":
|
|
1076
|
+
return response.blob().then(blobToDataUrl);
|
|
1077
|
+
case "text":
|
|
1078
|
+
default:
|
|
1079
|
+
return response.text();
|
|
1080
|
+
}
|
|
1081
|
+
}).finally(() => clearTimeout(timer));
|
|
1082
|
+
}
|
|
1083
|
+
function contextFetch(context, options) {
|
|
1084
|
+
const { url: rawUrl, requestType = "text", responseType = "text", imageDom } = options;
|
|
1085
|
+
let url = rawUrl;
|
|
1086
|
+
const {
|
|
1087
|
+
timeout,
|
|
1088
|
+
acceptOfImage,
|
|
1089
|
+
requests,
|
|
1090
|
+
fetchFn,
|
|
1091
|
+
fetch: {
|
|
1092
|
+
requestInit,
|
|
1093
|
+
bypassingCache,
|
|
1094
|
+
placeholderImage
|
|
1095
|
+
},
|
|
1096
|
+
font,
|
|
1097
|
+
workers,
|
|
1098
|
+
fontFamilies
|
|
1099
|
+
} = context;
|
|
1100
|
+
if (requestType === "image" && (IN_SAFARI || IN_FIREFOX)) {
|
|
1101
|
+
context.drawImageCount++;
|
|
1102
|
+
}
|
|
1103
|
+
let request = requests.get(rawUrl);
|
|
1104
|
+
if (!request) {
|
|
1105
|
+
if (bypassingCache) {
|
|
1106
|
+
if (bypassingCache instanceof RegExp && bypassingCache.test(url)) {
|
|
1107
|
+
url += (/\?/.test(url) ? "&" : "?") + (/* @__PURE__ */ new Date()).getTime();
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
const canFontMinify = requestType.startsWith("font") && font && font.minify;
|
|
1111
|
+
const fontTexts = /* @__PURE__ */ new Set();
|
|
1112
|
+
if (canFontMinify) {
|
|
1113
|
+
const families = requestType.split(";")[1].split(",");
|
|
1114
|
+
families.forEach((family) => {
|
|
1115
|
+
if (!fontFamilies.has(family))
|
|
1116
|
+
return;
|
|
1117
|
+
fontFamilies.get(family).forEach((text) => fontTexts.add(text));
|
|
1118
|
+
});
|
|
1119
|
+
}
|
|
1120
|
+
const needFontMinify = canFontMinify && fontTexts.size;
|
|
1121
|
+
const baseFetchOptions = {
|
|
1122
|
+
url,
|
|
1123
|
+
timeout,
|
|
1124
|
+
responseType: needFontMinify ? "arrayBuffer" : responseType,
|
|
1125
|
+
headers: requestType === "image" ? { accept: acceptOfImage } : void 0,
|
|
1126
|
+
...requestInit
|
|
1127
|
+
};
|
|
1128
|
+
request = {
|
|
1129
|
+
type: requestType,
|
|
1130
|
+
resolve: void 0,
|
|
1131
|
+
reject: void 0,
|
|
1132
|
+
response: null
|
|
1133
|
+
};
|
|
1134
|
+
request.response = (async () => {
|
|
1135
|
+
if (fetchFn && requestType === "image") {
|
|
1136
|
+
const result = await fetchFn(rawUrl);
|
|
1137
|
+
if (result)
|
|
1138
|
+
return result;
|
|
1139
|
+
}
|
|
1140
|
+
if (!IN_SAFARI && rawUrl.startsWith("http") && workers.length) {
|
|
1141
|
+
return new Promise((resolve, reject) => {
|
|
1142
|
+
const worker = workers[requests.size & workers.length - 1];
|
|
1143
|
+
worker.postMessage({ rawUrl, ...baseFetchOptions });
|
|
1144
|
+
request.resolve = resolve;
|
|
1145
|
+
request.reject = reject;
|
|
1146
|
+
});
|
|
1147
|
+
}
|
|
1148
|
+
return baseFetch(baseFetchOptions);
|
|
1149
|
+
})().catch((error) => {
|
|
1150
|
+
requests.delete(rawUrl);
|
|
1151
|
+
if (requestType === "image" && placeholderImage) {
|
|
1152
|
+
context.log.warn("Failed to fetch image base64, trying to use placeholder image", url);
|
|
1153
|
+
return typeof placeholderImage === "string" ? placeholderImage : placeholderImage(imageDom);
|
|
1154
|
+
}
|
|
1155
|
+
throw error;
|
|
1156
|
+
});
|
|
1157
|
+
requests.set(rawUrl, request);
|
|
1158
|
+
}
|
|
1159
|
+
return request.response;
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
async function replaceCssUrlToDataUrl(cssText, baseUrl, context, isImage) {
|
|
1163
|
+
if (!hasCssUrl(cssText))
|
|
1164
|
+
return cssText;
|
|
1165
|
+
for (const [rawUrl, url] of parseCssUrls(cssText, baseUrl)) {
|
|
1166
|
+
try {
|
|
1167
|
+
const dataUrl = await contextFetch(
|
|
1168
|
+
context,
|
|
1169
|
+
{
|
|
1170
|
+
url,
|
|
1171
|
+
requestType: isImage ? "image" : "text",
|
|
1172
|
+
responseType: "dataUrl"
|
|
1173
|
+
}
|
|
1174
|
+
);
|
|
1175
|
+
cssText = cssText.replace(toRE(rawUrl), `$1${dataUrl}$3`);
|
|
1176
|
+
} catch (error) {
|
|
1177
|
+
context.log.warn("Failed to fetch css data url", rawUrl, error);
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
return cssText;
|
|
1181
|
+
}
|
|
1182
|
+
function hasCssUrl(cssText) {
|
|
1183
|
+
return /url\((['"]?)([^'"]+?)\1\)/.test(cssText);
|
|
1184
|
+
}
|
|
1185
|
+
const URL_RE = /url\((['"]?)([^'"]+?)\1\)/g;
|
|
1186
|
+
function parseCssUrls(cssText, baseUrl) {
|
|
1187
|
+
const result = [];
|
|
1188
|
+
cssText.replace(URL_RE, (raw, quotation, url) => {
|
|
1189
|
+
result.push([url, resolveUrl(url, baseUrl)]);
|
|
1190
|
+
return raw;
|
|
1191
|
+
});
|
|
1192
|
+
return result.filter(([url]) => !isDataUrl(url));
|
|
1193
|
+
}
|
|
1194
|
+
function toRE(url) {
|
|
1195
|
+
const escaped = url.replace(/([.*+?^${}()|\[\]\/\\])/g, "\\$1");
|
|
1196
|
+
return new RegExp(`(url\\(['"]?)(${escaped})(['"]?\\))`, "g");
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
const properties = [
|
|
1200
|
+
"background-image",
|
|
1201
|
+
"border-image-source",
|
|
1202
|
+
"-webkit-border-image",
|
|
1203
|
+
"-webkit-mask-image",
|
|
1204
|
+
"list-style-image"
|
|
1205
|
+
];
|
|
1206
|
+
function embedCssStyleImage(style, context) {
|
|
1207
|
+
return properties.map((property) => {
|
|
1208
|
+
const value = style.getPropertyValue(property);
|
|
1209
|
+
if (!value || value === "none") {
|
|
1210
|
+
return null;
|
|
1211
|
+
}
|
|
1212
|
+
if (IN_SAFARI || IN_FIREFOX) {
|
|
1213
|
+
context.drawImageCount++;
|
|
1214
|
+
}
|
|
1215
|
+
return replaceCssUrlToDataUrl(value, null, context, true).then((newValue) => {
|
|
1216
|
+
if (!newValue || value === newValue)
|
|
1217
|
+
return;
|
|
1218
|
+
style.setProperty(
|
|
1219
|
+
property,
|
|
1220
|
+
newValue,
|
|
1221
|
+
style.getPropertyPriority(property)
|
|
1222
|
+
);
|
|
1223
|
+
});
|
|
1224
|
+
}).filter(Boolean);
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
function embedImageElement(cloned, context) {
|
|
1228
|
+
if (isImageElement(cloned)) {
|
|
1229
|
+
const originalSrc = cloned.currentSrc || cloned.src;
|
|
1230
|
+
if (!isDataUrl(originalSrc)) {
|
|
1231
|
+
return [
|
|
1232
|
+
contextFetch(context, {
|
|
1233
|
+
url: originalSrc,
|
|
1234
|
+
imageDom: cloned,
|
|
1235
|
+
requestType: "image",
|
|
1236
|
+
responseType: "dataUrl"
|
|
1237
|
+
}).then((url) => {
|
|
1238
|
+
if (!url)
|
|
1239
|
+
return;
|
|
1240
|
+
cloned.srcset = "";
|
|
1241
|
+
cloned.dataset.originalSrc = originalSrc;
|
|
1242
|
+
cloned.src = url || "";
|
|
1243
|
+
})
|
|
1244
|
+
];
|
|
1245
|
+
}
|
|
1246
|
+
if (IN_SAFARI || IN_FIREFOX) {
|
|
1247
|
+
context.drawImageCount++;
|
|
1248
|
+
}
|
|
1249
|
+
} else if (isSVGElementNode(cloned) && !isDataUrl(cloned.href.baseVal)) {
|
|
1250
|
+
const originalSrc = cloned.href.baseVal;
|
|
1251
|
+
return [
|
|
1252
|
+
contextFetch(context, {
|
|
1253
|
+
url: originalSrc,
|
|
1254
|
+
imageDom: cloned,
|
|
1255
|
+
requestType: "image",
|
|
1256
|
+
responseType: "dataUrl"
|
|
1257
|
+
}).then((url) => {
|
|
1258
|
+
if (!url)
|
|
1259
|
+
return;
|
|
1260
|
+
cloned.dataset.originalSrc = originalSrc;
|
|
1261
|
+
cloned.href.baseVal = url || "";
|
|
1262
|
+
})
|
|
1263
|
+
];
|
|
1264
|
+
}
|
|
1265
|
+
return [];
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
function embedSvgUse(cloned, context) {
|
|
1269
|
+
const { ownerDocument, svgDefsElement } = context;
|
|
1270
|
+
const href = cloned.getAttribute("href") ?? cloned.getAttribute("xlink:href");
|
|
1271
|
+
if (!href)
|
|
1272
|
+
return [];
|
|
1273
|
+
const [svgUrl, id] = href.split("#");
|
|
1274
|
+
if (id) {
|
|
1275
|
+
const query = `#${id}`;
|
|
1276
|
+
const definition = context.shadowRoots.reduce(
|
|
1277
|
+
(res, root) => {
|
|
1278
|
+
return res ?? root.querySelector(`svg ${query}`);
|
|
1279
|
+
},
|
|
1280
|
+
ownerDocument?.querySelector(`svg ${query}`)
|
|
1281
|
+
);
|
|
1282
|
+
if (svgUrl) {
|
|
1283
|
+
cloned.setAttribute("href", query);
|
|
1284
|
+
}
|
|
1285
|
+
if (svgDefsElement?.querySelector(query))
|
|
1286
|
+
return [];
|
|
1287
|
+
if (definition) {
|
|
1288
|
+
svgDefsElement?.appendChild(definition.cloneNode(true));
|
|
1289
|
+
return [];
|
|
1290
|
+
} else if (svgUrl) {
|
|
1291
|
+
return [
|
|
1292
|
+
contextFetch(context, {
|
|
1293
|
+
url: svgUrl,
|
|
1294
|
+
responseType: "text"
|
|
1295
|
+
}).then((svgData) => {
|
|
1296
|
+
svgDefsElement?.insertAdjacentHTML("beforeend", svgData);
|
|
1297
|
+
})
|
|
1298
|
+
];
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
return [];
|
|
1302
|
+
}
|
|
1303
|
+
|
|
1304
|
+
function embedNode(cloned, context) {
|
|
1305
|
+
const { tasks } = context;
|
|
1306
|
+
if (isElementNode(cloned)) {
|
|
1307
|
+
if (isImageElement(cloned) || isSVGImageElementNode(cloned)) {
|
|
1308
|
+
tasks.push(...embedImageElement(cloned, context));
|
|
1309
|
+
}
|
|
1310
|
+
if (isSVGUseElementNode(cloned)) {
|
|
1311
|
+
tasks.push(...embedSvgUse(cloned, context));
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
if (isHTMLElementNode(cloned)) {
|
|
1315
|
+
tasks.push(...embedCssStyleImage(cloned.style, context));
|
|
1316
|
+
}
|
|
1317
|
+
cloned.childNodes.forEach((child) => {
|
|
1318
|
+
embedNode(child, context);
|
|
1319
|
+
});
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
async function embedWebFont(clone, context) {
|
|
1323
|
+
const {
|
|
1324
|
+
ownerDocument,
|
|
1325
|
+
svgStyleElement,
|
|
1326
|
+
fontFamilies,
|
|
1327
|
+
fontCssTexts,
|
|
1328
|
+
tasks,
|
|
1329
|
+
font
|
|
1330
|
+
} = context;
|
|
1331
|
+
if (!ownerDocument || !svgStyleElement || !fontFamilies.size) {
|
|
1332
|
+
return;
|
|
1333
|
+
}
|
|
1334
|
+
if (font && font.cssText) {
|
|
1335
|
+
const cssText = filterPreferredFormat(font.cssText, context);
|
|
1336
|
+
svgStyleElement.appendChild(ownerDocument.createTextNode(`${cssText}
|
|
1337
|
+
`));
|
|
1338
|
+
} else {
|
|
1339
|
+
const styleSheets = Array.from(ownerDocument.styleSheets).filter((styleSheet) => {
|
|
1340
|
+
try {
|
|
1341
|
+
return "cssRules" in styleSheet && Boolean(styleSheet.cssRules.length);
|
|
1342
|
+
} catch (error) {
|
|
1343
|
+
context.log.warn(`Error while reading CSS rules from ${styleSheet.href}`, error);
|
|
1344
|
+
return false;
|
|
1345
|
+
}
|
|
1346
|
+
});
|
|
1347
|
+
await Promise.all(
|
|
1348
|
+
styleSheets.flatMap((styleSheet) => {
|
|
1349
|
+
return Array.from(styleSheet.cssRules).map(async (cssRule, index) => {
|
|
1350
|
+
if (isCSSImportRule(cssRule)) {
|
|
1351
|
+
let importIndex = index + 1;
|
|
1352
|
+
const baseUrl = cssRule.href;
|
|
1353
|
+
let cssText = "";
|
|
1354
|
+
try {
|
|
1355
|
+
cssText = await contextFetch(context, {
|
|
1356
|
+
url: baseUrl,
|
|
1357
|
+
requestType: "text",
|
|
1358
|
+
responseType: "text"
|
|
1359
|
+
});
|
|
1360
|
+
} catch (error) {
|
|
1361
|
+
context.log.warn(`Error fetch remote css import from ${baseUrl}`, error);
|
|
1362
|
+
}
|
|
1363
|
+
const replacedCssText = cssText.replace(
|
|
1364
|
+
URL_RE,
|
|
1365
|
+
(raw, quotation, url) => raw.replace(url, resolveUrl(url, baseUrl))
|
|
1366
|
+
);
|
|
1367
|
+
for (const rule of parseCss(replacedCssText)) {
|
|
1368
|
+
try {
|
|
1369
|
+
styleSheet.insertRule(
|
|
1370
|
+
rule,
|
|
1371
|
+
rule.startsWith("@import") ? importIndex += 1 : styleSheet.cssRules.length
|
|
1372
|
+
);
|
|
1373
|
+
} catch (error) {
|
|
1374
|
+
context.log.warn("Error inserting rule from remote css import", { rule, error });
|
|
1375
|
+
}
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
});
|
|
1379
|
+
})
|
|
1380
|
+
);
|
|
1381
|
+
const cssRules = styleSheets.flatMap((styleSheet) => Array.from(styleSheet.cssRules));
|
|
1382
|
+
cssRules.filter((cssRule) => isCssFontFaceRule(cssRule) && hasCssUrl(cssRule.style.getPropertyValue("src")) && splitFontFamily(cssRule.style.getPropertyValue("font-family"))?.some((val) => fontFamilies.has(val))).forEach((value) => {
|
|
1383
|
+
const rule = value;
|
|
1384
|
+
const cssText = fontCssTexts.get(rule.cssText);
|
|
1385
|
+
if (cssText) {
|
|
1386
|
+
svgStyleElement.appendChild(ownerDocument.createTextNode(`${cssText}
|
|
1387
|
+
`));
|
|
1388
|
+
} else {
|
|
1389
|
+
tasks.push(
|
|
1390
|
+
replaceCssUrlToDataUrl(
|
|
1391
|
+
rule.cssText,
|
|
1392
|
+
rule.parentStyleSheet ? rule.parentStyleSheet.href : null,
|
|
1393
|
+
context
|
|
1394
|
+
).then((cssText2) => {
|
|
1395
|
+
cssText2 = filterPreferredFormat(cssText2, context);
|
|
1396
|
+
fontCssTexts.set(rule.cssText, cssText2);
|
|
1397
|
+
svgStyleElement.appendChild(ownerDocument.createTextNode(`${cssText2}
|
|
1398
|
+
`));
|
|
1399
|
+
})
|
|
1400
|
+
);
|
|
1401
|
+
}
|
|
1402
|
+
});
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
const COMMENTS_RE = /(\/\*[\s\S]*?\*\/)/g;
|
|
1406
|
+
const KEYFRAMES_RE = /((@.*?keyframes [\s\S]*?){([\s\S]*?}\s*?)})/gi;
|
|
1407
|
+
function parseCss(source) {
|
|
1408
|
+
if (source == null)
|
|
1409
|
+
return [];
|
|
1410
|
+
const result = [];
|
|
1411
|
+
let cssText = source.replace(COMMENTS_RE, "");
|
|
1412
|
+
while (true) {
|
|
1413
|
+
const matches = KEYFRAMES_RE.exec(cssText);
|
|
1414
|
+
if (!matches)
|
|
1415
|
+
break;
|
|
1416
|
+
result.push(matches[0]);
|
|
1417
|
+
}
|
|
1418
|
+
cssText = cssText.replace(KEYFRAMES_RE, "");
|
|
1419
|
+
const IMPORT_RE = /@import[\s\S]*?url\([^)]*\)[\s\S]*?;/gi;
|
|
1420
|
+
const UNIFIED_RE = new RegExp(
|
|
1421
|
+
// eslint-disable-next-line
|
|
1422
|
+
"((\\s*?(?:\\/\\*[\\s\\S]*?\\*\\/)?\\s*?@media[\\s\\S]*?){([\\s\\S]*?)}\\s*?})|(([\\s\\S]*?){([\\s\\S]*?)})",
|
|
1423
|
+
"gi"
|
|
1424
|
+
);
|
|
1425
|
+
while (true) {
|
|
1426
|
+
let matches = IMPORT_RE.exec(cssText);
|
|
1427
|
+
if (!matches) {
|
|
1428
|
+
matches = UNIFIED_RE.exec(cssText);
|
|
1429
|
+
if (!matches) {
|
|
1430
|
+
break;
|
|
1431
|
+
} else {
|
|
1432
|
+
IMPORT_RE.lastIndex = UNIFIED_RE.lastIndex;
|
|
1433
|
+
}
|
|
1434
|
+
} else {
|
|
1435
|
+
UNIFIED_RE.lastIndex = IMPORT_RE.lastIndex;
|
|
1436
|
+
}
|
|
1437
|
+
result.push(matches[0]);
|
|
1438
|
+
}
|
|
1439
|
+
return result;
|
|
1440
|
+
}
|
|
1441
|
+
const URL_WITH_FORMAT_RE = /url\([^)]+\)\s*format\((["']?)([^"']+)\1\)/g;
|
|
1442
|
+
const FONT_SRC_RE = /src:\s*(?:url\([^)]+\)\s*format\([^)]+\)[,;]\s*)+/g;
|
|
1443
|
+
function filterPreferredFormat(str, context) {
|
|
1444
|
+
const { font } = context;
|
|
1445
|
+
const preferredFormat = font ? font?.preferredFormat : void 0;
|
|
1446
|
+
return preferredFormat ? str.replace(FONT_SRC_RE, (match) => {
|
|
1447
|
+
while (true) {
|
|
1448
|
+
const [src, , format] = URL_WITH_FORMAT_RE.exec(match) || [];
|
|
1449
|
+
if (!format)
|
|
1450
|
+
return "";
|
|
1451
|
+
if (format === preferredFormat)
|
|
1452
|
+
return `src: ${src};`;
|
|
1453
|
+
}
|
|
1454
|
+
}) : str;
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1457
|
+
async function domToForeignObjectSvg(node, options) {
|
|
1458
|
+
const context = await orCreateContext(node, options);
|
|
1459
|
+
if (isElementNode(context.node) && isSVGElementNode(context.node))
|
|
1460
|
+
return context.node;
|
|
1461
|
+
const {
|
|
1462
|
+
ownerDocument,
|
|
1463
|
+
log,
|
|
1464
|
+
tasks,
|
|
1465
|
+
svgStyleElement,
|
|
1466
|
+
svgDefsElement,
|
|
1467
|
+
svgStyles,
|
|
1468
|
+
font,
|
|
1469
|
+
progress,
|
|
1470
|
+
autoDestruct,
|
|
1471
|
+
onCloneNode,
|
|
1472
|
+
onEmbedNode,
|
|
1473
|
+
onCreateForeignObjectSvg
|
|
1474
|
+
} = context;
|
|
1475
|
+
log.time("clone node");
|
|
1476
|
+
const clone = await cloneNode(context.node, context, true);
|
|
1477
|
+
if (svgStyleElement && ownerDocument) {
|
|
1478
|
+
let allCssText = "";
|
|
1479
|
+
svgStyles.forEach((klasses, cssText) => {
|
|
1480
|
+
allCssText += `${klasses.join(",\n")} {
|
|
1481
|
+
${cssText}
|
|
1482
|
+
}
|
|
1483
|
+
`;
|
|
1484
|
+
});
|
|
1485
|
+
svgStyleElement.appendChild(ownerDocument.createTextNode(allCssText));
|
|
1486
|
+
}
|
|
1487
|
+
log.timeEnd("clone node");
|
|
1488
|
+
await onCloneNode?.(clone);
|
|
1489
|
+
if (font !== false && isElementNode(clone)) {
|
|
1490
|
+
log.time("embed web font");
|
|
1491
|
+
await embedWebFont(clone, context);
|
|
1492
|
+
log.timeEnd("embed web font");
|
|
1493
|
+
}
|
|
1494
|
+
log.time("embed node");
|
|
1495
|
+
embedNode(clone, context);
|
|
1496
|
+
const count = tasks.length;
|
|
1497
|
+
let current = 0;
|
|
1498
|
+
const runTask = async () => {
|
|
1499
|
+
while (true) {
|
|
1500
|
+
const task = tasks.pop();
|
|
1501
|
+
if (!task)
|
|
1502
|
+
break;
|
|
1503
|
+
try {
|
|
1504
|
+
await task;
|
|
1505
|
+
} catch (error) {
|
|
1506
|
+
context.log.warn("Failed to run task", error);
|
|
1507
|
+
}
|
|
1508
|
+
progress?.(++current, count);
|
|
1509
|
+
}
|
|
1510
|
+
};
|
|
1511
|
+
progress?.(current, count);
|
|
1512
|
+
await Promise.all([...Array.from({ length: 4 })].map(runTask));
|
|
1513
|
+
log.timeEnd("embed node");
|
|
1514
|
+
await onEmbedNode?.(clone);
|
|
1515
|
+
const svg = createForeignObjectSvg(clone, context);
|
|
1516
|
+
svgDefsElement && svg.insertBefore(svgDefsElement, svg.children[0]);
|
|
1517
|
+
svgStyleElement && svg.insertBefore(svgStyleElement, svg.children[0]);
|
|
1518
|
+
autoDestruct && destroyContext(context);
|
|
1519
|
+
await onCreateForeignObjectSvg?.(svg);
|
|
1520
|
+
return svg;
|
|
1521
|
+
}
|
|
1522
|
+
function createForeignObjectSvg(clone, context) {
|
|
1523
|
+
const { width, height } = context;
|
|
1524
|
+
const svg = createSvg(width, height, clone.ownerDocument);
|
|
1525
|
+
const foreignObject = svg.ownerDocument.createElementNS(svg.namespaceURI, "foreignObject");
|
|
1526
|
+
foreignObject.setAttributeNS(null, "x", "0%");
|
|
1527
|
+
foreignObject.setAttributeNS(null, "y", "0%");
|
|
1528
|
+
foreignObject.setAttributeNS(null, "width", "100%");
|
|
1529
|
+
foreignObject.setAttributeNS(null, "height", "100%");
|
|
1530
|
+
foreignObject.append(clone);
|
|
1531
|
+
svg.appendChild(foreignObject);
|
|
1532
|
+
return svg;
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
async function domToCanvas(node, options) {
|
|
1536
|
+
const context = await orCreateContext(node, options);
|
|
1537
|
+
const svg = await domToForeignObjectSvg(context);
|
|
1538
|
+
const dataUrl = svgToDataUrl(svg, context.isEnable("removeControlCharacter"));
|
|
1539
|
+
if (!context.autoDestruct) {
|
|
1540
|
+
context.svgStyleElement = createStyleElement(context.ownerDocument);
|
|
1541
|
+
context.svgDefsElement = context.ownerDocument?.createElementNS(XMLNS, "defs");
|
|
1542
|
+
context.svgStyles.clear();
|
|
1543
|
+
}
|
|
1544
|
+
const image = createImage(dataUrl, svg.ownerDocument);
|
|
1545
|
+
return await imageToCanvas(image, context);
|
|
1546
|
+
}
|
|
1547
|
+
|
|
1548
|
+
async function domToBlob(node, options) {
|
|
1549
|
+
const context = await orCreateContext(node, options);
|
|
1550
|
+
const { log, type, quality, dpi } = context;
|
|
1551
|
+
const canvas = await domToCanvas(context);
|
|
1552
|
+
log.time("canvas to blob");
|
|
1553
|
+
const blob = await canvasToBlob(canvas, type, quality);
|
|
1554
|
+
if (["image/png", "image/jpeg"].includes(type) && dpi) {
|
|
1555
|
+
const arrayBuffer = await blobToArrayBuffer(blob.slice(0, 33));
|
|
1556
|
+
let uint8Array = new Uint8Array(arrayBuffer);
|
|
1557
|
+
if (type === "image/png") {
|
|
1558
|
+
uint8Array = changePngDpi(uint8Array, dpi);
|
|
1559
|
+
} else if (type === "image/jpeg") {
|
|
1560
|
+
uint8Array = changeJpegDpi(uint8Array, dpi);
|
|
1561
|
+
}
|
|
1562
|
+
log.timeEnd("canvas to blob");
|
|
1563
|
+
return new Blob([uint8Array, blob.slice(33)], { type });
|
|
1564
|
+
}
|
|
1565
|
+
log.timeEnd("canvas to blob");
|
|
1566
|
+
return blob;
|
|
1567
|
+
}
|
|
1568
|
+
|
|
1569
|
+
async function domToDataUrl(node, options) {
|
|
1570
|
+
const context = await orCreateContext(node, options);
|
|
1571
|
+
const { log, quality, type, dpi } = context;
|
|
1572
|
+
const canvas = await domToCanvas(context);
|
|
1573
|
+
log.time("canvas to data url");
|
|
1574
|
+
let dataUrl = canvas.toDataURL(type, quality);
|
|
1575
|
+
if (["image/png", "image/jpeg"].includes(type) && dpi && SUPPORT_ATOB && SUPPORT_BTOA) {
|
|
1576
|
+
const [format, body] = dataUrl.split(",");
|
|
1577
|
+
let headerLength = 0;
|
|
1578
|
+
let overwritepHYs = false;
|
|
1579
|
+
if (type === "image/png") {
|
|
1580
|
+
const b64Index = detectPhysChunkFromDataUrl(body);
|
|
1581
|
+
if (b64Index >= 0) {
|
|
1582
|
+
headerLength = Math.ceil((b64Index + 28) / 3) * 4;
|
|
1583
|
+
overwritepHYs = true;
|
|
1584
|
+
} else {
|
|
1585
|
+
headerLength = 33 / 3 * 4;
|
|
1586
|
+
}
|
|
1587
|
+
} else if (type === "image/jpeg") {
|
|
1588
|
+
headerLength = 18 / 3 * 4;
|
|
1589
|
+
}
|
|
1590
|
+
const stringHeader = body.substring(0, headerLength);
|
|
1591
|
+
const restOfData = body.substring(headerLength);
|
|
1592
|
+
const headerBytes = window.atob(stringHeader);
|
|
1593
|
+
const uint8Array = new Uint8Array(headerBytes.length);
|
|
1594
|
+
for (let i = 0; i < uint8Array.length; i++) {
|
|
1595
|
+
uint8Array[i] = headerBytes.charCodeAt(i);
|
|
1596
|
+
}
|
|
1597
|
+
const finalArray = type === "image/png" ? changePngDpi(uint8Array, dpi, overwritepHYs) : changeJpegDpi(uint8Array, dpi);
|
|
1598
|
+
const base64Header = window.btoa(String.fromCharCode(...finalArray));
|
|
1599
|
+
dataUrl = [format, ",", base64Header, restOfData].join("");
|
|
1600
|
+
}
|
|
1601
|
+
log.timeEnd("canvas to data url");
|
|
1602
|
+
return dataUrl;
|
|
1603
|
+
}
|
|
1604
|
+
|
|
1605
|
+
async function domToSvg(node, options) {
|
|
1606
|
+
const context = await orCreateContext(node, options);
|
|
1607
|
+
const { width, height, ownerDocument } = context;
|
|
1608
|
+
const dataUrl = await domToDataUrl(context);
|
|
1609
|
+
const svg = createSvg(width, height, ownerDocument);
|
|
1610
|
+
const svgImage = svg.ownerDocument.createElementNS(svg.namespaceURI, "image");
|
|
1611
|
+
svgImage.setAttributeNS(null, "href", dataUrl);
|
|
1612
|
+
svgImage.setAttributeNS(null, "height", "100%");
|
|
1613
|
+
svgImage.setAttributeNS(null, "width", "100%");
|
|
1614
|
+
svg.appendChild(svgImage);
|
|
1615
|
+
return svgToDataUrl(svg, context.isEnable("removeControlCharacter"));
|
|
1616
|
+
}
|
|
1617
|
+
|
|
1618
|
+
async function domToImage(node, options) {
|
|
1619
|
+
const context = await orCreateContext(node, options);
|
|
1620
|
+
const { ownerDocument, width, height, scale, type } = context;
|
|
1621
|
+
const url = type === "image/svg+xml" ? await domToSvg(context) : await domToDataUrl(context);
|
|
1622
|
+
const image = createImage(url, ownerDocument);
|
|
1623
|
+
image.width = Math.floor(width * scale);
|
|
1624
|
+
image.height = Math.floor(height * scale);
|
|
1625
|
+
image.style.width = `${width}px`;
|
|
1626
|
+
image.style.height = `${height}px`;
|
|
1627
|
+
return image;
|
|
1628
|
+
}
|
|
1629
|
+
|
|
1630
|
+
async function domToJpeg(node, options) {
|
|
1631
|
+
return domToDataUrl(
|
|
1632
|
+
await orCreateContext(node, { ...options, type: "image/jpeg" })
|
|
1633
|
+
);
|
|
1634
|
+
}
|
|
1635
|
+
|
|
1636
|
+
async function domToPixel(node, options) {
|
|
1637
|
+
const context = await orCreateContext(node, options);
|
|
1638
|
+
const canvas = await domToCanvas(context);
|
|
1639
|
+
return canvas.getContext("2d").getImageData(0, 0, canvas.width, canvas.height).data;
|
|
1640
|
+
}
|
|
1641
|
+
|
|
1642
|
+
async function domToPng(node, options) {
|
|
1643
|
+
return domToDataUrl(
|
|
1644
|
+
await orCreateContext(node, { ...options, type: "image/png" })
|
|
1645
|
+
);
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
async function domToWebp(node, options) {
|
|
1649
|
+
return domToDataUrl(
|
|
1650
|
+
await orCreateContext(node, { ...options, type: "image/webp" })
|
|
1651
|
+
);
|
|
1652
|
+
}
|
|
1653
|
+
|
|
1654
|
+
exports.createContext = createContext;
|
|
1655
|
+
exports.destroyContext = destroyContext;
|
|
1656
|
+
exports.domToBlob = domToBlob;
|
|
1657
|
+
exports.domToCanvas = domToCanvas;
|
|
1658
|
+
exports.domToDataUrl = domToDataUrl;
|
|
1659
|
+
exports.domToForeignObjectSvg = domToForeignObjectSvg;
|
|
1660
|
+
exports.domToImage = domToImage;
|
|
1661
|
+
exports.domToJpeg = domToJpeg;
|
|
1662
|
+
exports.domToPixel = domToPixel;
|
|
1663
|
+
exports.domToPng = domToPng;
|
|
1664
|
+
exports.domToSvg = domToSvg;
|
|
1665
|
+
exports.domToWebp = domToWebp;
|
|
1666
|
+
exports.loadMedia = loadMedia;
|
|
1667
|
+
exports.waitUntilLoad = waitUntilLoad;
|