Skip to content

Commit 7e5ab00

Browse files
committed
Set compress as default
1 parent 5c21213 commit 7e5ab00

18 files changed

Lines changed: 185 additions & 190 deletions

__tests__/clone.core.test.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ const sessionCache = {
1010
nodeMap: cache.session.nodeMap
1111
}
1212

13-
function runClone(node, compress = false) {
14-
return deepClone(node, sessionCache, {...options, compress});
13+
function runClone(node) {
14+
return deepClone(node, sessionCache, {...options});
1515
}
1616

1717
describe('deepClone', () => {
1818
it('clones a simple div', () => {
1919
const el = document.createElement('div');
2020
el.textContent = 'hello';
21-
const clone = runClone(el, false);
21+
const clone = runClone(el);
2222
expect(clone).not.toBe(el);
2323
expect(clone.textContent).toBe('hello');
2424
});
@@ -32,31 +32,31 @@ describe('deepClone', () => {
3232
ctx.fillStyle = 'red';
3333
ctx.fillRect(0, 0, 10, 10);
3434
}
35-
const clone = runClone(canvas, false);
35+
const clone = runClone(canvas);
3636
expect(clone.tagName).toBe('IMG');
3737
expect(clone.src.startsWith('data:image/')).toBe(true);
3838
});
3939

4040
it('deepClone handles data-capture="exclude"', () => {
4141
const el = document.createElement('div');
4242
el.setAttribute('data-capture', 'exclude');
43-
const clone = runClone(el, true);
43+
const clone = runClone(el);
4444
expect(clone).not.toBeNull();
4545
});
4646

4747
it('deepClone handles data-capture="placeholder"', () => {
4848
const el = document.createElement('div');
4949
el.setAttribute('data-capture', 'placeholder');
5050
el.setAttribute('data-placeholder-text', 'Placeholder!');
51-
const clone = runClone(el, true);
51+
const clone = runClone(el);
5252
expect(clone.textContent).toContain('Placeholder!');
5353
});
5454

5555
it('deepClone handles iframe', () => {
5656
const iframe = document.createElement('iframe');
5757
iframe.width = 100;
5858
iframe.height = 50;
59-
const clone = runClone(iframe, true);
59+
const clone = runClone(iframe);
6060
expect(clone.tagName).toBe('DIV');
6161
});
6262

@@ -75,7 +75,7 @@ describe('deepClone', () => {
7575
select.value = 'baz';
7676

7777
[input, textarea, select].forEach(el => {
78-
const clone = runClone(el, true);
78+
const clone = runClone(el);
7979
expect(clone.value).toBe(el.value);
8080
});
8181
});
@@ -86,15 +86,15 @@ describe('deepClone', () => {
8686
const span = document.createElement('span');
8787
span.textContent = 'shadow';
8888
shadow.appendChild(span);
89-
const clone = runClone(el, true);
89+
const clone = runClone(el);
9090
expect(clone).not.toBeNull();
9191
});
9292
});
9393

9494
describe('deepClone edge cases', () => {
9595
it('clones unsupported node (Comment) as a new Comment', () => {
9696
const fake = document.createComment('not supported');
97-
const result = runClone(fake, true);
97+
const result = runClone(fake);
9898
expect(result.nodeType).toBe(Node.COMMENT_NODE);
9999
expect(result.textContent).toBe('not supported');
100100
expect(result).not.toBe(fake);
@@ -103,7 +103,7 @@ describe('deepClone edge cases', () => {
103103
it('clones attributes and children', () => {
104104
const el = document.createElement('div');
105105
el.setAttribute('data-test', '1');
106-
const result = runClone(el, true);
106+
const result = runClone(el);
107107
expect(result.getAttribute('data-test')).toBe('1');
108108
});
109109
});

__tests__/cssTools.utils.test.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@ import { describe, it, expect } from 'vitest';
22
import { getStyleKey, collectUsedTagNames, getDefaultStyleForTag } from '../src/utils';
33

44
describe('getStyleKey', () => {
5-
it('generates a non-empty style key', () => {
5+
6+
it('getStyleKey works with compress true default', () => {
67
const snapshot = { color: 'red', 'font-size': '12px' };
7-
const key = getStyleKey(snapshot, 'div',{compress:false});
8-
expect(typeof key).toBe('string');
9-
expect(key.length).toBeGreaterThan(0);
10-
});
11-
12-
it('getStyleKey works with compress true', () => {
13-
const snapshot = { color: 'red', 'font-size': '12px' };
14-
const key = getStyleKey(snapshot, 'div', {compress:true});
8+
const key = getStyleKey(snapshot, 'div');
159
expect(typeof key).toBe('string');
1610
});
1711
});

__tests__/preCache.api.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ describe('preCache', () => {
1919
text: () => Promise.resolve('<svg xmlns="http://www.w3.org/2000/svg"></svg>'),
2020
})
2121
);
22-
cache.reset()
2322
});
2423

2524

__tests__/snapdom.complex.benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ for (const size of sizes) {
100100

101101
bench('snapDOM current version', async () => {
102102
await setupContainer();
103-
await snapdom.toRaw(container, {compress: true, fast: true});
103+
await snapdom.toRaw(container);
104104
});
105105

106106
bench('snapDOM V1.9.9', async () => {

__tests__/snapdom.precache.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ for (const size of sizes) {
9898
await waitForNextFrame();
9999

100100
const start = performance.now();
101-
await snapdom.toRaw(container, { compress: true });
101+
await snapdom.toRaw(container);
102102
const end = performance.now();
103103

104104
let log = `[${size.label}] WITHOUT preCache: capture ${(end - start).toFixed(2)}ms`;
@@ -117,7 +117,7 @@ for (const size of sizes) {
117117
const endPre = performance.now();
118118

119119
const startCap = performance.now();
120-
await snapdom.toRaw(container, { compress: true });
120+
await snapdom.toRaw(container);
121121
const endCap = performance.now();
122122

123123
const precacheTime = (endPre - startPre).toFixed(2);

__tests__/snapdom.vs.htm2canvas.outputfilesize.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('Output file size snapdom vs html2canvas (cdn with averaging)', () => {
4141
it('snapdom output file size should be smaller than html2canvas', async () => {
4242

4343
// SnapDOM capture
44-
const snapdomDataURL = await snapdom.toRaw(container, {compress: true});
44+
const snapdomDataURL = await snapdom.toRaw(container);
4545
const snapdomSizeKB = (snapdomDataURL.length * 3 / 4) / 1024; // Base64 to bytes approx
4646

4747
// html2canvas capture

__tests__/snapdom.vs.modernscreenshot.outputfilesize.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('Output file size snapdom vs modern-screeenshot (cdn with averaging)',
2828
it('snapdom output file size should be smaller than modern-screenshot', async () => {
2929

3030
// SnapDOM capture
31-
const snapdomDataURL = await snapdom.toRaw(container, {compress: true});
31+
const snapdomDataURL = await snapdom.toRaw(container);
3232
const snapdomSizeKB = (snapdomDataURL.length * 3 / 4) / 1024; // Base64 to bytes approx
3333

3434
// domToDataUrl capture

__tests__/styles.module.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { cache } from '../src/core/cache.js';
44
import { createContext } from '../src/core/context.js';
55

66

7-
it('inlineAllStyles works with compress true', () => {
7+
it('inlineAllStyles works with compress true default', () => {
88
const el = document.createElement('span');
99
const clone = document.createElement('span');
1010
const options = createContext()
@@ -13,6 +13,6 @@ import { createContext } from '../src/core/context.js';
1313
styleCache: cache.session.styleCache,
1414
nodeMap: cache.session.nodeMap
1515
}
16-
inlineAllStyles(el, clone, sessionCache, {...options, compress: true});
16+
inlineAllStyles(el, clone, sessionCache, {...options});
1717
expect(sessionCache.styleMap.has(clone)).toBe(true);
1818
});

src/api/preCache.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { getStyle, inlineSingleBackgroundEntry, fetchImage, splitBackgroundImage, precacheCommonTags, isSafari } from '../utils';
44
import { embedCustomFonts, collectUsedFontVariants, collectUsedCodepoints, ensureFontsReady } from '../modules/fonts.js';
5-
import { cache } from '../core/cache.js';
5+
import { cache, applyReset } from '../core/cache.js';
66

77
/**
88
* Preloads images, background images, and (optionally) fonts into cache before DOM capture.
@@ -22,21 +22,11 @@ import { cache } from '../core/cache.js';
2222
export async function preCache(root = document, options = {}) {
2323
const {
2424
embedFonts = true,
25-
reset = false,
25+
reset = 'hard',
2626
useProxy = "",
2727
} = options;
2828

29-
if (reset) {
30-
// Soft reset: clear all caches and computed styles
31-
cache.image.clear();
32-
cache.background.clear();
33-
cache.resource.clear();
34-
cache.defaultStyle.clear();
35-
cache.baseStyle.clear();
36-
cache.font.clear();
37-
cache.computedStyle = new WeakMap();
38-
return;
39-
}
29+
applyReset(reset)
4030

4131
// Fonts readiness: don't crash in test/headless environments
4232
try { await document.fonts.ready; } catch {}
@@ -103,8 +93,7 @@ if (isSafari) {
10393
await embedCustomFonts({
10494
required,
10595
usedCodepoints,
106-
exclude: options.fontExclude, // { families?, domains?, subsets? }
107-
preCached: true,
96+
exclude: options.excludeFonts, // { families?, domains?, subsets? }
10897
localFonts: options.localFonts,
10998
useProxy: options.useProxy ?? useProxy,
11099
});

src/core/cache.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* Caches for images, backgrounds, resources, and computed styles used during DOM capture.
33
* @module cache
44
*/
5-
65
export const cache = {
76
image: new Map(),
87
background: new Map(),
@@ -15,15 +14,31 @@ export const cache = {
1514
styleMap: new Map(),
1615
styleCache: new WeakMap(),
1716
nodeMap: new Map(),
18-
},
19-
reset: resetCache
17+
}
2018
};
2119

22-
function resetCache() {
20+
21+
// Mantener solo estos públicos:
22+
export function softReset() {
2323
cache.computedStyle = new WeakMap();
24-
cache.session.styleMap = new Map()
25-
cache.session.styleCache = new WeakMap()
26-
cache.session.nodeMap = new Map()
27-
cache.defaultStyle = new Map()
28-
cache.baseStyle = new Map()
24+
cache.session.styleMap.clear();
25+
cache.session.styleCache = new WeakMap();
26+
cache.session.nodeMap.clear();
27+
cache.defaultStyle.clear();
28+
cache.baseStyle.clear();
2929
}
30+
31+
export function hardReset() {
32+
softReset();
33+
cache.image.clear();
34+
cache.background.clear();
35+
cache.resource.clear();
36+
cache.font.clear();
37+
}
38+
39+
// Mapea nivel → acción
40+
export function applyReset(level) {
41+
if (level === "soft") return softReset();
42+
if (level === "hard") return hardReset();
43+
// 'none' → no-op
44+
}

0 commit comments

Comments
 (0)