@zumer/snapdom 1.9.5 → 1.9.6-dev.0
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/snapdom.js +114 -109
- package/dist/snapdom.min.js +7 -7
- package/dist/snapdom.min.mjs +12 -12
- package/dist/snapdom.mjs +113 -108
- package/package.json +1 -1
package/dist/snapdom.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
/*
|
|
3
3
|
* snapdom
|
|
4
|
-
* v.1.9.
|
|
4
|
+
* v.1.9.6-dev.0
|
|
5
5
|
* Author Juan Martin Muda
|
|
6
6
|
* License MIT
|
|
7
7
|
**/
|
|
@@ -57,13 +57,29 @@
|
|
|
57
57
|
};
|
|
58
58
|
|
|
59
59
|
// src/core/cache.js
|
|
60
|
-
var
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
60
|
+
var cache = {
|
|
61
|
+
image: /* @__PURE__ */ new Map(),
|
|
62
|
+
background: /* @__PURE__ */ new Map(),
|
|
63
|
+
resource: /* @__PURE__ */ new Map(),
|
|
64
|
+
defaultStyle: /* @__PURE__ */ new Map(),
|
|
65
|
+
baseStyle: /* @__PURE__ */ new Map(),
|
|
66
|
+
computedStyle: /* @__PURE__ */ new WeakMap(),
|
|
67
|
+
font: /* @__PURE__ */ new Set(),
|
|
68
|
+
snapshot: /* @__PURE__ */ new WeakMap(),
|
|
69
|
+
snapshotKey: /* @__PURE__ */ new Map(),
|
|
70
|
+
preStyleMap: /* @__PURE__ */ new Map(),
|
|
71
|
+
preStyle: /* @__PURE__ */ new WeakMap(),
|
|
72
|
+
preNodeMap: /* @__PURE__ */ new Map(),
|
|
73
|
+
reset: resetCache
|
|
74
|
+
};
|
|
75
|
+
function resetCache() {
|
|
76
|
+
cache.computedStyle = /* @__PURE__ */ new WeakMap();
|
|
77
|
+
cache.snapshot = /* @__PURE__ */ new WeakMap();
|
|
78
|
+
cache.snapshotKey.clear();
|
|
79
|
+
cache.preStyleMap.clear();
|
|
80
|
+
cache.preStyle = /* @__PURE__ */ new WeakMap();
|
|
81
|
+
cache.preNodeMap.clear();
|
|
82
|
+
}
|
|
67
83
|
|
|
68
84
|
// src/utils/cssTools.js
|
|
69
85
|
var commonTags = [
|
|
@@ -111,13 +127,13 @@
|
|
|
111
127
|
}
|
|
112
128
|
}
|
|
113
129
|
function getDefaultStyleForTag(tagName) {
|
|
114
|
-
if (
|
|
115
|
-
return
|
|
130
|
+
if (cache.defaultStyle.has(tagName)) {
|
|
131
|
+
return cache.defaultStyle.get(tagName);
|
|
116
132
|
}
|
|
117
133
|
const skipTags = /* @__PURE__ */ new Set(["script", "style", "meta", "link", "noscript", "template"]);
|
|
118
134
|
if (skipTags.has(tagName)) {
|
|
119
135
|
const empty = {};
|
|
120
|
-
|
|
136
|
+
cache.defaultStyle.set(tagName, empty);
|
|
121
137
|
return empty;
|
|
122
138
|
}
|
|
123
139
|
let sandbox = document.getElementById("snapdom-sandbox");
|
|
@@ -141,7 +157,7 @@
|
|
|
141
157
|
defaults[prop] = styles.getPropertyValue(prop);
|
|
142
158
|
}
|
|
143
159
|
sandbox.removeChild(el);
|
|
144
|
-
|
|
160
|
+
cache.defaultStyle.set(tagName, defaults);
|
|
145
161
|
return defaults;
|
|
146
162
|
}
|
|
147
163
|
function getStyleKey(snapshot, tagName, compress = false) {
|
|
@@ -177,7 +193,7 @@
|
|
|
177
193
|
function generateDedupedBaseCSS(usedTagNames) {
|
|
178
194
|
const groups = /* @__PURE__ */ new Map();
|
|
179
195
|
for (let tagName of usedTagNames) {
|
|
180
|
-
const styles =
|
|
196
|
+
const styles = cache.defaultStyle.get(tagName);
|
|
181
197
|
if (!styles) continue;
|
|
182
198
|
const key = Object.entries(styles).map(([k, v]) => `${k}:${v};`).sort().join("");
|
|
183
199
|
if (!groups.has(key)) {
|
|
@@ -192,8 +208,8 @@
|
|
|
192
208
|
}
|
|
193
209
|
return css;
|
|
194
210
|
}
|
|
195
|
-
function generateCSSClasses(
|
|
196
|
-
const keySet = new Set(
|
|
211
|
+
function generateCSSClasses() {
|
|
212
|
+
const keySet = new Set(cache.preStyleMap.values());
|
|
197
213
|
const classMap = /* @__PURE__ */ new Map();
|
|
198
214
|
let counter = 1;
|
|
199
215
|
for (const key of keySet) {
|
|
@@ -205,16 +221,15 @@
|
|
|
205
221
|
// src/utils/helpers.js
|
|
206
222
|
function inlineSingleBackgroundEntry(_0) {
|
|
207
223
|
return __async(this, arguments, function* (entry, options = {}) {
|
|
208
|
-
const
|
|
209
|
-
const rawUrl = match == null ? void 0 : match[1];
|
|
224
|
+
const rawUrl = extractURL(entry);
|
|
210
225
|
const isGradient = /^((repeating-)?(linear|radial|conic)-gradient)\(/i.test(entry);
|
|
211
226
|
if (rawUrl) {
|
|
212
227
|
const encodedUrl = safeEncodeURI(rawUrl);
|
|
213
|
-
if (
|
|
214
|
-
return options.skipInline ? void 0 : `url(${
|
|
228
|
+
if (cache.background.has(encodedUrl)) {
|
|
229
|
+
return options.skipInline ? void 0 : `url(${cache.background.get(encodedUrl)})`;
|
|
215
230
|
} else {
|
|
216
231
|
const dataUrl = yield fetchImage(encodedUrl, { useProxy: options.useProxy });
|
|
217
|
-
|
|
232
|
+
cache.background.set(encodedUrl, dataUrl);
|
|
218
233
|
return options.skipInline ? void 0 : `url("${dataUrl}")`;
|
|
219
234
|
}
|
|
220
235
|
}
|
|
@@ -236,10 +251,10 @@
|
|
|
236
251
|
if (!(el instanceof Element)) {
|
|
237
252
|
return window.getComputedStyle(el, pseudo);
|
|
238
253
|
}
|
|
239
|
-
let map =
|
|
254
|
+
let map = cache.computedStyle.get(el);
|
|
240
255
|
if (!map) {
|
|
241
256
|
map = /* @__PURE__ */ new Map();
|
|
242
|
-
|
|
257
|
+
cache.computedStyle.set(el, map);
|
|
243
258
|
}
|
|
244
259
|
if (!map.has(pseudo)) {
|
|
245
260
|
const st = window.getComputedStyle(el, pseudo);
|
|
@@ -315,13 +330,13 @@
|
|
|
315
330
|
}
|
|
316
331
|
const crossOriginValue = getCrossOriginMode(src);
|
|
317
332
|
console.log(`[SnapDOM - fetchImage] Start loading image: ${src} with crossOrigin=${crossOriginValue}`);
|
|
318
|
-
if (
|
|
333
|
+
if (cache.image.has(src)) {
|
|
319
334
|
console.log(`[SnapDOM - fetchImage] Cache hit for: ${src}`);
|
|
320
|
-
return Promise.resolve(
|
|
335
|
+
return Promise.resolve(cache.image.get(src));
|
|
321
336
|
}
|
|
322
337
|
const isDataURI = src.startsWith("data:image/");
|
|
323
338
|
if (isDataURI) {
|
|
324
|
-
|
|
339
|
+
cache.image.set(src, src);
|
|
325
340
|
return Promise.resolve(src);
|
|
326
341
|
}
|
|
327
342
|
const isSVG = /\.svg(\?.*)?$/i.test(src);
|
|
@@ -334,7 +349,7 @@
|
|
|
334
349
|
});
|
|
335
350
|
const svgText = yield response.text();
|
|
336
351
|
const encoded = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svgText)}`;
|
|
337
|
-
|
|
352
|
+
cache.image.set(src, encoded);
|
|
338
353
|
return encoded;
|
|
339
354
|
} catch (e) {
|
|
340
355
|
return fetchWithFallback(src);
|
|
@@ -358,12 +373,12 @@
|
|
|
358
373
|
const ctx = canvas.getContext("2d");
|
|
359
374
|
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
|
|
360
375
|
const dataURL = canvas.toDataURL("image/png");
|
|
361
|
-
|
|
376
|
+
cache.image.set(src, dataURL);
|
|
362
377
|
resolve(dataURL);
|
|
363
378
|
} catch (e) {
|
|
364
379
|
try {
|
|
365
380
|
const fallbackDataURL = yield fetchWithFallback(src);
|
|
366
|
-
|
|
381
|
+
cache.image.set(src, fallbackDataURL);
|
|
367
382
|
resolve(fallbackDataURL);
|
|
368
383
|
} catch (e2) {
|
|
369
384
|
reject(e2);
|
|
@@ -375,7 +390,7 @@
|
|
|
375
390
|
console.error(`[SnapDOM - fetchImage] Image failed to load: ${src}`);
|
|
376
391
|
try {
|
|
377
392
|
const fallbackDataURL = yield fetchWithFallback(src);
|
|
378
|
-
|
|
393
|
+
cache.image.set(src, fallbackDataURL);
|
|
379
394
|
resolve(fallbackDataURL);
|
|
380
395
|
} catch (e) {
|
|
381
396
|
reject(e);
|
|
@@ -439,8 +454,6 @@
|
|
|
439
454
|
}
|
|
440
455
|
|
|
441
456
|
// src/modules/styles.js
|
|
442
|
-
var snapshotCache = /* @__PURE__ */ new WeakMap();
|
|
443
|
-
var snapshotKeyCache = /* @__PURE__ */ new Map();
|
|
444
457
|
function snapshotComputedStyleFull(style) {
|
|
445
458
|
const result = {};
|
|
446
459
|
for (let i = 0; i < style.length; i++) {
|
|
@@ -453,34 +466,34 @@
|
|
|
453
466
|
}
|
|
454
467
|
return result;
|
|
455
468
|
}
|
|
456
|
-
function inlineAllStyles(source, clone,
|
|
469
|
+
function inlineAllStyles(source, clone, compress) {
|
|
457
470
|
var _a;
|
|
458
471
|
if (source.tagName === "STYLE") return;
|
|
459
|
-
if (!cache.has(source)) {
|
|
460
|
-
cache.set(source, getStyle(source));
|
|
472
|
+
if (!cache.preStyle.has(source)) {
|
|
473
|
+
cache.preStyle.set(source, getStyle(source));
|
|
461
474
|
}
|
|
462
|
-
const style = cache.get(source);
|
|
463
|
-
if (!
|
|
475
|
+
const style = cache.preStyle.get(source);
|
|
476
|
+
if (!cache.snapshot.has(source)) {
|
|
464
477
|
const snapshot2 = snapshotComputedStyleFull(style);
|
|
465
|
-
|
|
478
|
+
cache.snapshot.set(source, snapshot2);
|
|
466
479
|
}
|
|
467
|
-
const snapshot =
|
|
480
|
+
const snapshot = cache.snapshot.get(source);
|
|
468
481
|
const hash = Object.entries(snapshot).sort(([a], [b]) => a.localeCompare(b)).map(([prop, val]) => `${prop}:${val}`).join(";");
|
|
469
|
-
if (
|
|
470
|
-
|
|
482
|
+
if (cache.snapshotKey.has(hash)) {
|
|
483
|
+
cache.preStyleMap.set(clone, cache.snapshotKey.get(hash));
|
|
471
484
|
return;
|
|
472
485
|
}
|
|
473
486
|
const tagName = ((_a = source.tagName) == null ? void 0 : _a.toLowerCase()) || "div";
|
|
474
487
|
const key = getStyleKey(snapshot, tagName, compress);
|
|
475
|
-
|
|
476
|
-
|
|
488
|
+
cache.snapshotKey.set(hash, key);
|
|
489
|
+
cache.preStyleMap.set(clone, key);
|
|
477
490
|
}
|
|
478
491
|
|
|
479
492
|
// src/core/clone.js
|
|
480
493
|
function isSlotElement(node) {
|
|
481
494
|
return node.nodeType === Node.ELEMENT_NODE && node.tagName === "SLOT";
|
|
482
495
|
}
|
|
483
|
-
function deepClone(node,
|
|
496
|
+
function deepClone(node, compress, options = {}, originalRoot) {
|
|
484
497
|
var _a, _b;
|
|
485
498
|
if (node.nodeType === Node.TEXT_NODE) {
|
|
486
499
|
return node.cloneNode(true);
|
|
@@ -526,8 +539,8 @@
|
|
|
526
539
|
}
|
|
527
540
|
if (node.getAttribute("data-capture") === "placeholder") {
|
|
528
541
|
const clone2 = node.cloneNode(false);
|
|
529
|
-
|
|
530
|
-
inlineAllStyles(node, clone2,
|
|
542
|
+
cache.preNodeMap.set(clone2, node);
|
|
543
|
+
inlineAllStyles(node, clone2, compress);
|
|
531
544
|
const placeholder = document.createElement("div");
|
|
532
545
|
placeholder.textContent = node.getAttribute("data-placeholder-text") || "";
|
|
533
546
|
placeholder.style.cssText = `color: #666;font-size: 12px;text-align: center;line-height: 1.4;padding: 0.5em;box-sizing: border-box;`;
|
|
@@ -546,7 +559,7 @@
|
|
|
546
559
|
return img;
|
|
547
560
|
}
|
|
548
561
|
const clone = node.cloneNode(false);
|
|
549
|
-
|
|
562
|
+
cache.preNodeMap.set(clone, node);
|
|
550
563
|
if (node instanceof HTMLInputElement) {
|
|
551
564
|
clone.value = node.value;
|
|
552
565
|
clone.setAttribute("value", node.value);
|
|
@@ -569,13 +582,13 @@
|
|
|
569
582
|
}
|
|
570
583
|
});
|
|
571
584
|
}
|
|
572
|
-
inlineAllStyles(node, clone,
|
|
585
|
+
inlineAllStyles(node, clone, compress);
|
|
573
586
|
if (isSlotElement(node)) {
|
|
574
587
|
const assigned = ((_b = node.assignedNodes) == null ? void 0 : _b.call(node, { flatten: true })) || [];
|
|
575
588
|
const nodesToClone = assigned.length > 0 ? assigned : Array.from(node.childNodes);
|
|
576
589
|
const fragment = document.createDocumentFragment();
|
|
577
590
|
for (const child of nodesToClone) {
|
|
578
|
-
const clonedChild = deepClone(child,
|
|
591
|
+
const clonedChild = deepClone(child, compress, options, originalRoot || node);
|
|
579
592
|
if (clonedChild) fragment.appendChild(clonedChild);
|
|
580
593
|
}
|
|
581
594
|
return fragment;
|
|
@@ -583,13 +596,13 @@
|
|
|
583
596
|
} else {
|
|
584
597
|
const baseChildren = node.shadowRoot ? node.shadowRoot.childNodes : node.childNodes;
|
|
585
598
|
for (const child of baseChildren) {
|
|
586
|
-
const clonedChild = deepClone(child,
|
|
599
|
+
const clonedChild = deepClone(child, compress, options, originalRoot || node);
|
|
587
600
|
if (clonedChild) clone.appendChild(clonedChild);
|
|
588
601
|
}
|
|
589
602
|
if (node.shadowRoot && node.childNodes.length > 0 && !node.shadowRoot.querySelector("slot")) {
|
|
590
603
|
const lightDomContent = document.createDocumentFragment();
|
|
591
604
|
for (const child of node.childNodes) {
|
|
592
|
-
const clonedChild = deepClone(child,
|
|
605
|
+
const clonedChild = deepClone(child, compress, options, originalRoot || node);
|
|
593
606
|
if (clonedChild) lightDomContent.appendChild(clonedChild);
|
|
594
607
|
}
|
|
595
608
|
clone.appendChild(lightDomContent);
|
|
@@ -678,14 +691,14 @@
|
|
|
678
691
|
}
|
|
679
692
|
function embedCustomFonts() {
|
|
680
693
|
return __async(this, arguments, function* ({ preCached = false } = {}) {
|
|
681
|
-
if (
|
|
694
|
+
if (cache.resource.has("fonts-embed-css")) {
|
|
682
695
|
if (preCached) {
|
|
683
696
|
const style = document.createElement("style");
|
|
684
697
|
style.setAttribute("data-snapdom", "embedFonts");
|
|
685
|
-
style.textContent =
|
|
698
|
+
style.textContent = cache.resource.get("fonts-embed-css");
|
|
686
699
|
document.head.appendChild(style);
|
|
687
700
|
}
|
|
688
|
-
return
|
|
701
|
+
return cache.resource.get("fonts-embed-css");
|
|
689
702
|
}
|
|
690
703
|
const importRegex = /@import\s+url\(["']?([^"')]+)["']?\)/g;
|
|
691
704
|
const styleImports = [];
|
|
@@ -718,11 +731,11 @@
|
|
|
718
731
|
url = new URL(url, link.href).href;
|
|
719
732
|
}
|
|
720
733
|
if (isIconFont(url)) return null;
|
|
721
|
-
if (
|
|
722
|
-
|
|
723
|
-
return { original: match[0], inlined: `url(${
|
|
734
|
+
if (cache.resource.has(url)) {
|
|
735
|
+
cache.font.add(url);
|
|
736
|
+
return { original: match[0], inlined: `url(${cache.resource.get(url)})` };
|
|
724
737
|
}
|
|
725
|
-
if (
|
|
738
|
+
if (cache.font.has(url)) return null;
|
|
726
739
|
try {
|
|
727
740
|
const fontRes = yield fetch(url);
|
|
728
741
|
const blob = yield fontRes.blob();
|
|
@@ -731,8 +744,8 @@
|
|
|
731
744
|
reader.onload = () => resolve(reader.result);
|
|
732
745
|
reader.readAsDataURL(blob);
|
|
733
746
|
});
|
|
734
|
-
|
|
735
|
-
|
|
747
|
+
cache.resource.set(url, b64);
|
|
748
|
+
cache.font.add(url);
|
|
736
749
|
return { original: match[0], inlined: `url(${b64})` };
|
|
737
750
|
} catch (e) {
|
|
738
751
|
console.warn("[snapdom] Failed to fetch font resource:", url);
|
|
@@ -768,12 +781,12 @@
|
|
|
768
781
|
url = new URL(url, sheet.href || location.href).href;
|
|
769
782
|
}
|
|
770
783
|
if (isIconFont(url)) continue;
|
|
771
|
-
if (
|
|
772
|
-
|
|
773
|
-
inlinedSrc = inlinedSrc.replace(match[0], `url(${
|
|
784
|
+
if (cache.resource.has(url)) {
|
|
785
|
+
cache.font.add(url);
|
|
786
|
+
inlinedSrc = inlinedSrc.replace(match[0], `url(${cache.resource.get(url)})`);
|
|
774
787
|
continue;
|
|
775
788
|
}
|
|
776
|
-
if (
|
|
789
|
+
if (cache.font.has(url)) continue;
|
|
777
790
|
try {
|
|
778
791
|
const res = yield fetch(url);
|
|
779
792
|
const blob = yield res.blob();
|
|
@@ -782,8 +795,8 @@
|
|
|
782
795
|
reader.onload = () => resolve(reader.result);
|
|
783
796
|
reader.readAsDataURL(blob);
|
|
784
797
|
});
|
|
785
|
-
|
|
786
|
-
|
|
798
|
+
cache.resource.set(url, b64);
|
|
799
|
+
cache.font.add(url);
|
|
787
800
|
inlinedSrc = inlinedSrc.replace(match[0], `url(${b64})`);
|
|
788
801
|
} catch (e) {
|
|
789
802
|
console.warn("[snapdom] Failed to fetch font URL:", url);
|
|
@@ -808,10 +821,10 @@
|
|
|
808
821
|
if (isIconFont(font.family)) continue;
|
|
809
822
|
let b64 = font._snapdomSrc;
|
|
810
823
|
if (!b64.startsWith("data:")) {
|
|
811
|
-
if (
|
|
812
|
-
b64 =
|
|
813
|
-
|
|
814
|
-
} else if (!
|
|
824
|
+
if (cache.resource.has(font._snapdomSrc)) {
|
|
825
|
+
b64 = cache.resource.get(font._snapdomSrc);
|
|
826
|
+
cache.font.add(font._snapdomSrc);
|
|
827
|
+
} else if (!cache.font.has(font._snapdomSrc)) {
|
|
815
828
|
try {
|
|
816
829
|
const res = yield fetch(font._snapdomSrc);
|
|
817
830
|
const blob = yield res.blob();
|
|
@@ -820,8 +833,8 @@
|
|
|
820
833
|
reader.onload = () => resolve(reader.result);
|
|
821
834
|
reader.readAsDataURL(blob);
|
|
822
835
|
});
|
|
823
|
-
|
|
824
|
-
|
|
836
|
+
cache.resource.set(font._snapdomSrc, b64);
|
|
837
|
+
cache.font.add(font._snapdomSrc);
|
|
825
838
|
} catch (e) {
|
|
826
839
|
console.warn("[snapdom] Failed to fetch dynamic font src:", font._snapdomSrc);
|
|
827
840
|
continue;
|
|
@@ -838,7 +851,7 @@
|
|
|
838
851
|
}
|
|
839
852
|
}
|
|
840
853
|
if (finalCSS) {
|
|
841
|
-
|
|
854
|
+
cache.resource.set("fonts-embed-css", finalCSS);
|
|
842
855
|
if (preCached) {
|
|
843
856
|
const style = document.createElement("style");
|
|
844
857
|
style.setAttribute("data-snapdom", "embedFonts");
|
|
@@ -851,7 +864,7 @@
|
|
|
851
864
|
}
|
|
852
865
|
|
|
853
866
|
// src/modules/pseudo.js
|
|
854
|
-
function inlinePseudoElements(source, clone,
|
|
867
|
+
function inlinePseudoElements(source, clone, compress, embedFonts = false, useProxy) {
|
|
855
868
|
return __async(this, null, function* () {
|
|
856
869
|
if (!(source instanceof Element) || !(clone instanceof Element)) return;
|
|
857
870
|
for (const pseudo of ["::before", "::after", "::first-letter"]) {
|
|
@@ -876,7 +889,7 @@
|
|
|
876
889
|
span.dataset.snapdomPseudo = "::first-letter";
|
|
877
890
|
const snapshot = snapshotComputedStyle(style);
|
|
878
891
|
const key = getStyleKey(snapshot, "span", compress);
|
|
879
|
-
|
|
892
|
+
cache.preStyleMap.set(span, key);
|
|
880
893
|
const restNode = document.createTextNode(rest);
|
|
881
894
|
clone.replaceChild(restNode, textNode);
|
|
882
895
|
clone.insertBefore(span, restNode);
|
|
@@ -897,7 +910,7 @@
|
|
|
897
910
|
pseudoEl.dataset.snapdomPseudo = pseudo;
|
|
898
911
|
const snapshot = snapshotComputedStyle(style);
|
|
899
912
|
const key = getStyleKey(snapshot, "span", compress);
|
|
900
|
-
|
|
913
|
+
cache.preStyleMap.set(pseudoEl, key);
|
|
901
914
|
const isIconFont2 = isIconFont(fontFamily);
|
|
902
915
|
const cleanContent = parseContent(content);
|
|
903
916
|
if (isIconFont2 && cleanContent.length === 1) {
|
|
@@ -948,8 +961,6 @@
|
|
|
948
961
|
yield inlinePseudoElements(
|
|
949
962
|
sChildren[i],
|
|
950
963
|
cChildren[i],
|
|
951
|
-
styleMap,
|
|
952
|
-
styleCache,
|
|
953
964
|
compress,
|
|
954
965
|
embedFonts,
|
|
955
966
|
useProxy
|
|
@@ -992,18 +1003,15 @@
|
|
|
992
1003
|
function prepareClone(_0) {
|
|
993
1004
|
return __async(this, arguments, function* (element, compress = false, embedFonts = false, options = {}) {
|
|
994
1005
|
var _a;
|
|
995
|
-
|
|
996
|
-
const styleCache = /* @__PURE__ */ new WeakMap();
|
|
997
|
-
const nodeMap = /* @__PURE__ */ new Map();
|
|
998
|
-
let clone;
|
|
1006
|
+
let clone, classCSS;
|
|
999
1007
|
try {
|
|
1000
|
-
clone = deepClone(element,
|
|
1008
|
+
clone = deepClone(element, compress, options, element);
|
|
1001
1009
|
} catch (e) {
|
|
1002
1010
|
console.warn("deepClone failed:", e);
|
|
1003
1011
|
throw e;
|
|
1004
1012
|
}
|
|
1005
1013
|
try {
|
|
1006
|
-
yield inlinePseudoElements(element, clone,
|
|
1014
|
+
yield inlinePseudoElements(element, clone, compress, embedFonts, options.useProxy);
|
|
1007
1015
|
} catch (e) {
|
|
1008
1016
|
console.warn("inlinePseudoElements failed:", e);
|
|
1009
1017
|
}
|
|
@@ -1012,11 +1020,10 @@
|
|
|
1012
1020
|
} catch (e) {
|
|
1013
1021
|
console.warn("inlineExternalDef failed:", e);
|
|
1014
1022
|
}
|
|
1015
|
-
let classCSS = "";
|
|
1016
1023
|
if (compress) {
|
|
1017
|
-
const keyToClass = generateCSSClasses(
|
|
1024
|
+
const keyToClass = generateCSSClasses();
|
|
1018
1025
|
classCSS = Array.from(keyToClass.entries()).map(([key, className]) => `.${className}{${key}}`).join("");
|
|
1019
|
-
for (const [node, key] of
|
|
1026
|
+
for (const [node, key] of cache.preStyleMap.entries()) {
|
|
1020
1027
|
if (node.tagName === "STYLE") continue;
|
|
1021
1028
|
const className = keyToClass.get(key);
|
|
1022
1029
|
if (className) node.classList.add(className);
|
|
@@ -1025,12 +1032,12 @@
|
|
|
1025
1032
|
if (bgImage && bgImage !== "none") node.style.backgroundImage = bgImage;
|
|
1026
1033
|
}
|
|
1027
1034
|
} else {
|
|
1028
|
-
for (const [node, key] of
|
|
1035
|
+
for (const [node, key] of cache.preStyleMap.entries()) {
|
|
1029
1036
|
if (node.tagName === "STYLE") continue;
|
|
1030
1037
|
node.setAttribute("style", key.replace(/;/g, "; "));
|
|
1031
1038
|
}
|
|
1032
1039
|
}
|
|
1033
|
-
for (const [cloneNode, originalNode] of
|
|
1040
|
+
for (const [cloneNode, originalNode] of cache.preNodeMap.entries()) {
|
|
1034
1041
|
const scrollX = originalNode.scrollLeft;
|
|
1035
1042
|
const scrollY = originalNode.scrollTop;
|
|
1036
1043
|
const hasScroll = scrollX || scrollY;
|
|
@@ -1049,9 +1056,9 @@
|
|
|
1049
1056
|
cloneNode.appendChild(inner);
|
|
1050
1057
|
}
|
|
1051
1058
|
}
|
|
1052
|
-
if (element ===
|
|
1053
|
-
const computed =
|
|
1054
|
-
|
|
1059
|
+
if (element === cache.preNodeMap.get(clone)) {
|
|
1060
|
+
const computed = cache.preStyle.get(element) || window.getComputedStyle(element);
|
|
1061
|
+
cache.preStyle.set(element, computed);
|
|
1055
1062
|
const transform = stripTranslate(computed.transform);
|
|
1056
1063
|
clone.style.margin = "0";
|
|
1057
1064
|
clone.style.position = "static";
|
|
@@ -1064,13 +1071,13 @@
|
|
|
1064
1071
|
clone.style.clear = "none";
|
|
1065
1072
|
clone.style.transform = transform || "";
|
|
1066
1073
|
}
|
|
1067
|
-
for (const [cloneNode, originalNode] of
|
|
1074
|
+
for (const [cloneNode, originalNode] of cache.preNodeMap.entries()) {
|
|
1068
1075
|
if (originalNode.tagName === "PRE") {
|
|
1069
1076
|
cloneNode.style.marginTop = "0";
|
|
1070
1077
|
cloneNode.style.marginBlockStart = "0";
|
|
1071
1078
|
}
|
|
1072
1079
|
}
|
|
1073
|
-
return { clone, classCSS
|
|
1080
|
+
return { clone, classCSS };
|
|
1074
1081
|
});
|
|
1075
1082
|
}
|
|
1076
1083
|
|
|
@@ -1100,8 +1107,8 @@
|
|
|
1100
1107
|
}
|
|
1101
1108
|
|
|
1102
1109
|
// src/modules/background.js
|
|
1103
|
-
function inlineBackgroundImages(_0, _1
|
|
1104
|
-
return __async(this, arguments, function* (source, clone,
|
|
1110
|
+
function inlineBackgroundImages(_0, _1) {
|
|
1111
|
+
return __async(this, arguments, function* (source, clone, options = {}) {
|
|
1105
1112
|
const queue = [[source, clone]];
|
|
1106
1113
|
const imageProps = [
|
|
1107
1114
|
"background-image",
|
|
@@ -1115,8 +1122,8 @@
|
|
|
1115
1122
|
];
|
|
1116
1123
|
while (queue.length) {
|
|
1117
1124
|
const [srcNode, cloneNode] = queue.shift();
|
|
1118
|
-
const style =
|
|
1119
|
-
if (!
|
|
1125
|
+
const style = cache.preStyle.get(srcNode) || getStyle(srcNode);
|
|
1126
|
+
if (!cache.preStyle.has(srcNode)) cache.preStyle.set(srcNode, style);
|
|
1120
1127
|
for (const prop of imageProps) {
|
|
1121
1128
|
const val = style.getPropertyValue(prop);
|
|
1122
1129
|
if (!val || val === "none") continue;
|
|
@@ -1145,13 +1152,14 @@
|
|
|
1145
1152
|
function captureDOM(_0) {
|
|
1146
1153
|
return __async(this, arguments, function* (element, options = {}) {
|
|
1147
1154
|
if (!element) throw new Error("Element cannot be null or undefined");
|
|
1155
|
+
cache.reset();
|
|
1148
1156
|
const { compress = true, embedFonts = false, fast = true, scale = 1, useProxy = "" } = options;
|
|
1149
|
-
let clone, classCSS
|
|
1157
|
+
let clone, classCSS;
|
|
1150
1158
|
let fontsCSS = "";
|
|
1151
1159
|
let baseCSS = "";
|
|
1152
1160
|
let dataURL;
|
|
1153
1161
|
let svgString;
|
|
1154
|
-
({ clone, classCSS
|
|
1162
|
+
({ clone, classCSS } = yield prepareClone(element, compress, embedFonts, options));
|
|
1155
1163
|
yield new Promise((resolve) => {
|
|
1156
1164
|
idle(() => __async(null, null, function* () {
|
|
1157
1165
|
yield inlineImages(clone, options);
|
|
@@ -1160,7 +1168,7 @@
|
|
|
1160
1168
|
});
|
|
1161
1169
|
yield new Promise((resolve) => {
|
|
1162
1170
|
idle(() => __async(null, null, function* () {
|
|
1163
|
-
yield inlineBackgroundImages(element, clone,
|
|
1171
|
+
yield inlineBackgroundImages(element, clone, options);
|
|
1164
1172
|
resolve();
|
|
1165
1173
|
}), { fast });
|
|
1166
1174
|
});
|
|
@@ -1175,13 +1183,13 @@
|
|
|
1175
1183
|
if (compress) {
|
|
1176
1184
|
const usedTags = collectUsedTagNames(clone).sort();
|
|
1177
1185
|
const tagKey = usedTags.join(",");
|
|
1178
|
-
if (
|
|
1179
|
-
baseCSS =
|
|
1186
|
+
if (cache.baseStyle.has(tagKey)) {
|
|
1187
|
+
baseCSS = cache.baseStyle.get(tagKey);
|
|
1180
1188
|
} else {
|
|
1181
1189
|
yield new Promise((resolve) => {
|
|
1182
1190
|
idle(() => {
|
|
1183
1191
|
baseCSS = generateDedupedBaseCSS(usedTags);
|
|
1184
|
-
|
|
1192
|
+
cache.baseStyle.set(tagKey, baseCSS);
|
|
1185
1193
|
resolve();
|
|
1186
1194
|
}, { fast });
|
|
1187
1195
|
});
|
|
@@ -1423,10 +1431,7 @@
|
|
|
1423
1431
|
return __async(this, arguments, function* (root = document, options = {}) {
|
|
1424
1432
|
const { embedFonts = true, reset = false } = options;
|
|
1425
1433
|
if (reset) {
|
|
1426
|
-
|
|
1427
|
-
bgCache.clear();
|
|
1428
|
-
resourceCache.clear();
|
|
1429
|
-
baseCSSCache.clear();
|
|
1434
|
+
cache.reset();
|
|
1430
1435
|
return;
|
|
1431
1436
|
}
|
|
1432
1437
|
yield document.fonts.ready;
|
|
@@ -1439,9 +1444,9 @@
|
|
|
1439
1444
|
const promises = [];
|
|
1440
1445
|
for (const img of imgEls) {
|
|
1441
1446
|
const src = img.src;
|
|
1442
|
-
if (!
|
|
1447
|
+
if (!cache.image.has(src)) {
|
|
1443
1448
|
promises.push(
|
|
1444
|
-
fetchImage(src, { useProxy: options.useProxy }).then((dataURL) =>
|
|
1449
|
+
fetchImage(src, { useProxy: options.useProxy }).then((dataURL) => cache.image.set(src, dataURL)).catch(() => {
|
|
1445
1450
|
})
|
|
1446
1451
|
);
|
|
1447
1452
|
}
|