html2canvas-pro 2.1.0 → 2.2.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.
Files changed (266) hide show
  1. package/dist/html2canvas-pro.esm.js +10226 -10526
  2. package/dist/html2canvas-pro.esm.js.map +1 -1
  3. package/dist/html2canvas-pro.js +10869 -11171
  4. package/dist/html2canvas-pro.js.map +1 -1
  5. package/dist/html2canvas-pro.min.js +8 -8
  6. package/dist/lib/config.js +0 -22
  7. package/dist/lib/core/cache-storage.js +3 -40
  8. package/dist/lib/core/constants.js +25 -0
  9. package/dist/lib/core/context.js +1 -0
  10. package/dist/lib/core/features.js +3 -2
  11. package/dist/lib/core/validator.js +3 -3
  12. package/dist/lib/css/grouped/background-styles.js +36 -0
  13. package/dist/lib/css/grouped/border-styles.js +75 -0
  14. package/dist/lib/css/grouped/font-styles.js +93 -0
  15. package/dist/lib/css/grouped/layout-styles.js +127 -0
  16. package/dist/lib/css/index.js +74 -46
  17. package/dist/lib/css/layout/text.js +7 -6
  18. package/dist/lib/css/property-descriptors/background-blend-mode.js +41 -0
  19. package/dist/lib/css/property-descriptors/border-image-repeat.js +42 -0
  20. package/dist/lib/css/property-descriptors/border-image-slice.js +45 -0
  21. package/dist/lib/css/property-descriptors/border-image-source.js +21 -0
  22. package/dist/lib/css/property-descriptors/border-radius.js +1 -1
  23. package/dist/lib/css/property-descriptors/box-decoration-break.js +18 -0
  24. package/dist/lib/css/property-descriptors/counter-increment.js +17 -12
  25. package/dist/lib/css/property-descriptors/counter-reset.js +4 -12
  26. package/dist/lib/css/property-descriptors/filter.js +76 -0
  27. package/dist/lib/css/property-descriptors/font-variant-ligatures.js +34 -0
  28. package/dist/lib/css/property-descriptors/object-fit.js +1 -1
  29. package/dist/lib/css/property-descriptors/object-position.js +42 -0
  30. package/dist/lib/css/property-descriptors/visibility.js +1 -1
  31. package/dist/lib/css/property-descriptors/zoom.js +18 -0
  32. package/dist/lib/css/syntax/parser.js +0 -1
  33. package/dist/lib/css/types/color.js +5 -1
  34. package/dist/lib/css/types/functions/repeating-linear-gradient.js +9 -0
  35. package/dist/lib/css/types/image.js +12 -2
  36. package/dist/lib/css/types/length-percentage.js +6 -2
  37. package/dist/lib/css/types/safe-eval.js +80 -0
  38. package/dist/lib/dom/document-cloner.js +23 -163
  39. package/dist/lib/dom/slot-cloner.js +176 -0
  40. package/dist/lib/index.js +1 -17
  41. package/dist/lib/render/canvas/background-renderer.js +169 -30
  42. package/dist/lib/render/canvas/border-image-renderer.js +153 -0
  43. package/dist/lib/render/canvas/canvas-renderer.js +39 -190
  44. package/dist/lib/render/canvas/content-renderer.js +202 -0
  45. package/dist/lib/render/canvas/effects-renderer.js +3 -0
  46. package/dist/lib/render/canvas/foreignobject-renderer.js +5 -1
  47. package/dist/lib/render/canvas/text/text-decoration-renderer.js +99 -0
  48. package/dist/lib/render/canvas/text-renderer.js +100 -224
  49. package/dist/lib/render/effects.js +38 -3
  50. package/dist/lib/render/object-fit.js +19 -15
  51. package/dist/lib/render/stacking-context.js +11 -0
  52. package/dist/types/config.d.ts +0 -10
  53. package/dist/types/core/cache-storage.d.ts +0 -24
  54. package/dist/types/core/constants.d.ts +22 -0
  55. package/dist/types/core/context.d.ts +3 -0
  56. package/dist/types/core/performance-monitor.d.ts +4 -4
  57. package/dist/types/core/validator.d.ts +6 -8
  58. package/dist/types/css/grouped/background-styles.d.ts +16 -0
  59. package/dist/types/css/grouped/border-styles.d.ts +31 -0
  60. package/dist/types/css/grouped/font-styles.d.ts +35 -0
  61. package/dist/types/css/grouped/layout-styles.d.ts +46 -0
  62. package/dist/types/css/index.d.ts +30 -0
  63. package/dist/types/css/property-descriptors/background-blend-mode.d.ts +23 -0
  64. package/dist/types/css/property-descriptors/border-image-repeat.d.ts +12 -0
  65. package/dist/types/css/property-descriptors/border-image-slice.d.ts +10 -0
  66. package/dist/types/css/property-descriptors/border-image-source.d.ts +4 -0
  67. package/dist/types/css/property-descriptors/box-decoration-break.d.ts +6 -0
  68. package/dist/types/css/property-descriptors/counter-increment.d.ts +3 -0
  69. package/dist/types/css/property-descriptors/filter.d.ts +3 -0
  70. package/dist/types/css/property-descriptors/font-variant-ligatures.d.ts +14 -0
  71. package/dist/types/css/property-descriptors/object-position.d.ts +4 -0
  72. package/dist/types/css/property-descriptors/zoom.d.ts +3 -0
  73. package/dist/types/css/types/functions/repeating-linear-gradient.d.ts +4 -0
  74. package/dist/types/css/types/image.d.ts +4 -2
  75. package/dist/types/css/types/safe-eval.d.ts +8 -0
  76. package/dist/types/dom/document-cloner.d.ts +3 -44
  77. package/dist/types/dom/slot-cloner.d.ts +66 -0
  78. package/dist/types/index.d.ts +3 -7
  79. package/dist/types/options.d.ts +11 -0
  80. package/dist/types/render/canvas/background-renderer.d.ts +23 -0
  81. package/dist/types/render/canvas/border-image-renderer.d.ts +18 -0
  82. package/dist/types/render/canvas/canvas-renderer.d.ts +1 -0
  83. package/dist/types/render/canvas/content-renderer.d.ts +44 -0
  84. package/dist/types/render/canvas/text/text-decoration-renderer.d.ts +18 -0
  85. package/dist/types/render/canvas/text-renderer.d.ts +12 -1
  86. package/dist/types/render/effects.d.ts +12 -2
  87. package/dist/types/render/object-fit.d.ts +2 -1
  88. package/dist/types/render/renderer-interface.d.ts +11 -9
  89. package/package.json +7 -20
  90. package/dist/lib/dom/replaced-elements/pseudo-elements.js +0 -0
  91. package/dist/lib/invariant.js +0 -9
  92. package/dist/types/dom/replaced-elements/pseudo-elements.d.ts +0 -0
  93. package/dist/types/invariant.d.ts +0 -1
  94. package/src/__tests__/index.ts +0 -99
  95. package/src/config.ts +0 -107
  96. package/src/core/__mocks__/cache-storage.ts +0 -1
  97. package/src/core/__mocks__/context.ts +0 -19
  98. package/src/core/__mocks__/features.ts +0 -8
  99. package/src/core/__mocks__/logger.ts +0 -17
  100. package/src/core/__tests__/cache-storage.test.ts +0 -205
  101. package/src/core/__tests__/cache-storage.ts +0 -278
  102. package/src/core/__tests__/logger.ts +0 -29
  103. package/src/core/__tests__/validator.ts +0 -359
  104. package/src/core/bitwise.ts +0 -1
  105. package/src/core/cache-storage.ts +0 -315
  106. package/src/core/context.ts +0 -31
  107. package/src/core/debugger.ts +0 -32
  108. package/src/core/features.ts +0 -222
  109. package/src/core/logger.ts +0 -64
  110. package/src/core/origin-checker.ts +0 -57
  111. package/src/core/performance-monitor.ts +0 -241
  112. package/src/core/render-element.ts +0 -272
  113. package/src/core/util.ts +0 -1
  114. package/src/core/validator.ts +0 -593
  115. package/src/css/index.ts +0 -427
  116. package/src/css/layout/__mocks__/bounds.ts +0 -6
  117. package/src/css/layout/bounds.ts +0 -79
  118. package/src/css/layout/text.ts +0 -161
  119. package/src/css/property-descriptor.ts +0 -49
  120. package/src/css/property-descriptors/__tests__/background-tests.ts +0 -65
  121. package/src/css/property-descriptors/__tests__/clip-path.test.ts +0 -280
  122. package/src/css/property-descriptors/__tests__/font-family.ts +0 -25
  123. package/src/css/property-descriptors/__tests__/image-rendering-integration.test.ts +0 -153
  124. package/src/css/property-descriptors/__tests__/image-rendering-performance.test.ts +0 -175
  125. package/src/css/property-descriptors/__tests__/image-rendering.test.ts +0 -72
  126. package/src/css/property-descriptors/__tests__/paint-order.ts +0 -87
  127. package/src/css/property-descriptors/__tests__/text-shadow.ts +0 -94
  128. package/src/css/property-descriptors/__tests__/transform-tests.ts +0 -18
  129. package/src/css/property-descriptors/background-clip.ts +0 -30
  130. package/src/css/property-descriptors/background-color.ts +0 -9
  131. package/src/css/property-descriptors/background-image.ts +0 -27
  132. package/src/css/property-descriptors/background-origin.ts +0 -31
  133. package/src/css/property-descriptors/background-position.ts +0 -38
  134. package/src/css/property-descriptors/background-repeat.ts +0 -44
  135. package/src/css/property-descriptors/background-size.ts +0 -27
  136. package/src/css/property-descriptors/border-color.ts +0 -13
  137. package/src/css/property-descriptors/border-radius.ts +0 -19
  138. package/src/css/property-descriptors/border-style.ts +0 -34
  139. package/src/css/property-descriptors/border-width.ts +0 -20
  140. package/src/css/property-descriptors/box-shadow.ts +0 -60
  141. package/src/css/property-descriptors/clip-path.ts +0 -271
  142. package/src/css/property-descriptors/color.ts +0 -9
  143. package/src/css/property-descriptors/content.ts +0 -26
  144. package/src/css/property-descriptors/counter-increment.ts +0 -43
  145. package/src/css/property-descriptors/counter-reset.ts +0 -36
  146. package/src/css/property-descriptors/direction.ts +0 -23
  147. package/src/css/property-descriptors/display.ts +0 -117
  148. package/src/css/property-descriptors/duration.ts +0 -14
  149. package/src/css/property-descriptors/float.ts +0 -29
  150. package/src/css/property-descriptors/font-family.ts +0 -38
  151. package/src/css/property-descriptors/font-size.ts +0 -9
  152. package/src/css/property-descriptors/font-style.ts +0 -25
  153. package/src/css/property-descriptors/font-variant.ts +0 -12
  154. package/src/css/property-descriptors/font-weight.ts +0 -26
  155. package/src/css/property-descriptors/image-rendering.ts +0 -33
  156. package/src/css/property-descriptors/letter-spacing.ts +0 -25
  157. package/src/css/property-descriptors/line-break.ts +0 -22
  158. package/src/css/property-descriptors/line-height.ts +0 -22
  159. package/src/css/property-descriptors/list-style-image.ts +0 -19
  160. package/src/css/property-descriptors/list-style-position.ts +0 -22
  161. package/src/css/property-descriptors/list-style-type.ts +0 -179
  162. package/src/css/property-descriptors/margin.ts +0 -13
  163. package/src/css/property-descriptors/mix-blend-mode.ts +0 -35
  164. package/src/css/property-descriptors/object-fit.ts +0 -39
  165. package/src/css/property-descriptors/opacity.ts +0 -15
  166. package/src/css/property-descriptors/overflow-wrap.ts +0 -22
  167. package/src/css/property-descriptors/overflow.ts +0 -34
  168. package/src/css/property-descriptors/padding.ts +0 -14
  169. package/src/css/property-descriptors/paint-order.ts +0 -42
  170. package/src/css/property-descriptors/position.ts +0 -30
  171. package/src/css/property-descriptors/quotes.ts +0 -57
  172. package/src/css/property-descriptors/rotate.ts +0 -34
  173. package/src/css/property-descriptors/text-align.ts +0 -26
  174. package/src/css/property-descriptors/text-decoration-color.ts +0 -9
  175. package/src/css/property-descriptors/text-decoration-line.ts +0 -38
  176. package/src/css/property-descriptors/text-decoration-style.ts +0 -32
  177. package/src/css/property-descriptors/text-decoration-thickness.ts +0 -30
  178. package/src/css/property-descriptors/text-overflow.ts +0 -23
  179. package/src/css/property-descriptors/text-shadow.ts +0 -52
  180. package/src/css/property-descriptors/text-transform.ts +0 -27
  181. package/src/css/property-descriptors/text-underline-offset.ts +0 -27
  182. package/src/css/property-descriptors/transform-origin.ts +0 -29
  183. package/src/css/property-descriptors/transform.ts +0 -74
  184. package/src/css/property-descriptors/visibility.ts +0 -25
  185. package/src/css/property-descriptors/webkit-line-clamp.ts +0 -30
  186. package/src/css/property-descriptors/webkit-text-stroke-color.ts +0 -8
  187. package/src/css/property-descriptors/webkit-text-stroke-width.ts +0 -15
  188. package/src/css/property-descriptors/word-break.ts +0 -25
  189. package/src/css/property-descriptors/writing-mode.ts +0 -37
  190. package/src/css/property-descriptors/z-index.ts +0 -27
  191. package/src/css/syntax/__tests__/tokernizer-tests.ts +0 -29
  192. package/src/css/syntax/parser.ts +0 -188
  193. package/src/css/syntax/tokenizer.ts +0 -822
  194. package/src/css/type-descriptor.ts +0 -7
  195. package/src/css/types/__tests__/color-tests.ts +0 -147
  196. package/src/css/types/__tests__/image-tests.ts +0 -239
  197. package/src/css/types/angle.ts +0 -86
  198. package/src/css/types/color-math.ts +0 -22
  199. package/src/css/types/color-spaces/a98.ts +0 -86
  200. package/src/css/types/color-spaces/p3.ts +0 -92
  201. package/src/css/types/color-spaces/pro-photo.ts +0 -87
  202. package/src/css/types/color-spaces/rec2020.ts +0 -90
  203. package/src/css/types/color-spaces/srgb.ts +0 -87
  204. package/src/css/types/color-utilities.ts +0 -452
  205. package/src/css/types/color.ts +0 -485
  206. package/src/css/types/functions/-prefix-linear-gradient.ts +0 -35
  207. package/src/css/types/functions/-prefix-radial-gradient.ts +0 -106
  208. package/src/css/types/functions/-webkit-gradient.ts +0 -69
  209. package/src/css/types/functions/__tests__/radial-gradient.ts +0 -69
  210. package/src/css/types/functions/counter.ts +0 -511
  211. package/src/css/types/functions/gradient.ts +0 -206
  212. package/src/css/types/functions/linear-gradient.ts +0 -28
  213. package/src/css/types/functions/radial-gradient.ts +0 -101
  214. package/src/css/types/image.ts +0 -120
  215. package/src/css/types/index.ts +0 -1
  216. package/src/css/types/length-percentage.ts +0 -137
  217. package/src/css/types/length.ts +0 -7
  218. package/src/css/types/time.ts +0 -20
  219. package/src/dom/__mocks__/document-cloner.ts +0 -22
  220. package/src/dom/__tests__/dom-normalizer.test.ts +0 -133
  221. package/src/dom/__tests__/element-container.test.ts +0 -129
  222. package/src/dom/document-cloner.ts +0 -929
  223. package/src/dom/dom-normalizer.ts +0 -133
  224. package/src/dom/element-container.ts +0 -75
  225. package/src/dom/elements/li-element-container.ts +0 -10
  226. package/src/dom/elements/ol-element-container.ts +0 -12
  227. package/src/dom/elements/select-element-container.ts +0 -10
  228. package/src/dom/elements/textarea-element-container.ts +0 -9
  229. package/src/dom/node-parser.ts +0 -177
  230. package/src/dom/node-type-guards.ts +0 -70
  231. package/src/dom/replaced-elements/canvas-element-container.ts +0 -15
  232. package/src/dom/replaced-elements/iframe-element-container.ts +0 -55
  233. package/src/dom/replaced-elements/image-element-container.ts +0 -16
  234. package/src/dom/replaced-elements/index.ts +0 -5
  235. package/src/dom/replaced-elements/input-element-container.ts +0 -105
  236. package/src/dom/replaced-elements/pseudo-elements.ts +0 -0
  237. package/src/dom/replaced-elements/svg-element-container.ts +0 -23
  238. package/src/dom/text-container.ts +0 -42
  239. package/src/global.d.ts +0 -19
  240. package/src/index.ts +0 -82
  241. package/src/invariant.ts +0 -5
  242. package/src/options.ts +0 -55
  243. package/src/render/__tests__/object-fit.test.ts +0 -85
  244. package/src/render/background.ts +0 -298
  245. package/src/render/bezier-curve.ts +0 -47
  246. package/src/render/border.ts +0 -165
  247. package/src/render/bound-curves.ts +0 -388
  248. package/src/render/box-sizing.ts +0 -31
  249. package/src/render/canvas/__tests__/background-renderer.test.ts +0 -72
  250. package/src/render/canvas/__tests__/border-renderer.test.ts +0 -24
  251. package/src/render/canvas/__tests__/effects-renderer.test.ts +0 -32
  252. package/src/render/canvas/__tests__/text-renderer.test.ts +0 -471
  253. package/src/render/canvas/background-renderer.ts +0 -271
  254. package/src/render/canvas/border-renderer.ts +0 -224
  255. package/src/render/canvas/canvas-path.ts +0 -31
  256. package/src/render/canvas/canvas-renderer.ts +0 -641
  257. package/src/render/canvas/effects-renderer.ts +0 -130
  258. package/src/render/canvas/foreignobject-renderer.ts +0 -53
  259. package/src/render/canvas/text-renderer.ts +0 -700
  260. package/src/render/effects.ts +0 -75
  261. package/src/render/font-metrics.ts +0 -72
  262. package/src/render/object-fit.ts +0 -100
  263. package/src/render/path.ts +0 -37
  264. package/src/render/renderer-interface.ts +0 -28
  265. package/src/render/stacking-context.ts +0 -386
  266. package/src/render/vector.ts +0 -19
@@ -1,205 +0,0 @@
1
- import { strictEqual, ok, deepStrictEqual } from 'assert';
2
- import { Cache, ResourceOptions } from '../cache-storage';
3
- import { Context } from '../context';
4
- import { Bounds } from '../../css/layout/bounds';
5
- import { Html2CanvasConfig } from '../../config';
6
-
7
- describe('Cache LRU', () => {
8
- let context: Context;
9
- let cache: Cache;
10
-
11
- beforeEach(() => {
12
- const mockWindow = {
13
- document: {
14
- createElement: (_name: string) => {
15
- let _href = '';
16
- return {
17
- set href(value: string) {
18
- _href = value;
19
- },
20
- get href() {
21
- return _href;
22
- },
23
- get protocol() {
24
- return 'http:';
25
- },
26
- get hostname() {
27
- return 'localhost';
28
- },
29
- get port() {
30
- return '';
31
- }
32
- };
33
- }
34
- },
35
- location: { href: 'http://localhost/' }
36
- } as unknown as Window;
37
-
38
- const config = new Html2CanvasConfig({ window: mockWindow });
39
- context = new Context(
40
- {
41
- logging: false,
42
- imageTimeout: 15000,
43
- useCORS: false,
44
- allowTaint: false
45
- },
46
- new Bounds(0, 0, 800, 600),
47
- config
48
- );
49
-
50
- const options: ResourceOptions = {
51
- imageTimeout: 15000,
52
- useCORS: false,
53
- allowTaint: false,
54
- maxCacheSize: 3 // Small size for testing
55
- };
56
-
57
- cache = new Cache(context, options);
58
- });
59
-
60
- it('should enforce maximum cache size', () => {
61
- strictEqual(cache.getMaxSize(), 3);
62
- strictEqual(cache.size(), 0);
63
- });
64
-
65
- it('should add images to cache', () => {
66
- const blob1 = 'blob:http://localhost/image1';
67
- const blob2 = 'blob:http://localhost/image2';
68
-
69
- cache.addImage(blob1);
70
- strictEqual(cache.size(), 1);
71
-
72
- cache.addImage(blob2);
73
- strictEqual(cache.size(), 2);
74
- });
75
-
76
- it('should not exceed max cache size', () => {
77
- const blobs = [
78
- 'blob:http://localhost/image1',
79
- 'blob:http://localhost/image2',
80
- 'blob:http://localhost/image3',
81
- 'blob:http://localhost/image4' // This should trigger eviction
82
- ];
83
-
84
- blobs.forEach((blob) => cache.addImage(blob));
85
-
86
- // Should not exceed max size (3)
87
- strictEqual(cache.size(), 3);
88
- });
89
-
90
- it('should evict least recently used entry when full', async () => {
91
- const blob1 = 'blob:http://localhost/image1';
92
- const blob2 = 'blob:http://localhost/image2';
93
- const blob3 = 'blob:http://localhost/image3';
94
- const blob4 = 'blob:http://localhost/image4';
95
-
96
- // Add 3 images (fill cache)
97
- cache.addImage(blob1);
98
- await new Promise((resolve) => setTimeout(resolve, 10));
99
-
100
- cache.addImage(blob2);
101
- await new Promise((resolve) => setTimeout(resolve, 10));
102
-
103
- cache.addImage(blob3);
104
- await new Promise((resolve) => setTimeout(resolve, 10));
105
-
106
- strictEqual(cache.size(), 3);
107
-
108
- // Access blob2 and blob3 (update their access time)
109
- cache.match(blob2);
110
- await new Promise((resolve) => setTimeout(resolve, 10));
111
-
112
- cache.match(blob3);
113
- await new Promise((resolve) => setTimeout(resolve, 10));
114
-
115
- // Add blob4 - should evict blob1 (oldest, never accessed)
116
- cache.addImage(blob4);
117
-
118
- strictEqual(cache.size(), 3);
119
-
120
- // blob1 should be evicted
121
- strictEqual(cache.match(blob1), undefined);
122
-
123
- // blob2, blob3, blob4 should exist
124
- ok(cache.match(blob2) !== undefined);
125
- ok(cache.match(blob3) !== undefined);
126
- ok(cache.match(blob4) !== undefined);
127
- });
128
-
129
- it('should update access time on repeated access', async () => {
130
- const blob1 = 'blob:http://localhost/image1';
131
- const blob2 = 'blob:http://localhost/image2';
132
- const blob3 = 'blob:http://localhost/image3';
133
-
134
- cache.addImage(blob1);
135
- await new Promise((resolve) => setTimeout(resolve, 10));
136
-
137
- cache.addImage(blob2);
138
- await new Promise((resolve) => setTimeout(resolve, 10));
139
-
140
- cache.addImage(blob3);
141
- strictEqual(cache.size(), 3);
142
-
143
- // Access blob1 multiple times (update access time)
144
- cache.match(blob1);
145
- await new Promise((resolve) => setTimeout(resolve, 10));
146
-
147
- cache.addImage(blob1); // Should update access time
148
- await new Promise((resolve) => setTimeout(resolve, 10));
149
-
150
- // Add 4th image - should evict blob2 (oldest unaccessed)
151
- const blob4 = 'blob:http://localhost/image4';
152
- cache.addImage(blob4);
153
-
154
- strictEqual(cache.size(), 3);
155
- strictEqual(cache.match(blob2), undefined); // Evicted
156
- ok(cache.match(blob1) !== undefined); // Still exists
157
- });
158
-
159
- it('should clear all cache entries', () => {
160
- cache.addImage('blob:http://localhost/image1');
161
- cache.addImage('blob:http://localhost/image2');
162
- strictEqual(cache.size(), 2);
163
-
164
- cache.clear();
165
- strictEqual(cache.size(), 0);
166
- });
167
-
168
- it('should return cache keys', async () => {
169
- const blob1 = 'blob:http://localhost/image1';
170
- const blob2 = 'blob:http://localhost/image2';
171
-
172
- cache.addImage(blob1);
173
- cache.addImage(blob2);
174
-
175
- deepStrictEqual(await cache.keys(), [blob1, blob2]);
176
- });
177
-
178
- it('should throw error for invalid max size', () => {
179
- const options: ResourceOptions = {
180
- imageTimeout: 15000,
181
- useCORS: false,
182
- allowTaint: false,
183
- maxCacheSize: 0 // Invalid
184
- };
185
-
186
- try {
187
- new Cache(context, options);
188
- ok(false, 'Should have thrown error');
189
- } catch (error: any) {
190
- ok(error.message.includes('at least 1'));
191
- }
192
- });
193
-
194
- it('should use default max size if not specified', () => {
195
- const options: ResourceOptions = {
196
- imageTimeout: 15000,
197
- useCORS: false,
198
- allowTaint: false
199
- // maxCacheSize not specified
200
- };
201
-
202
- const cacheWithDefault = new Cache(context, options);
203
- strictEqual(cacheWithDefault.getMaxSize(), 100); // Default
204
- });
205
- });
@@ -1,278 +0,0 @@
1
- import { deepStrictEqual, fail } from 'assert';
2
- import { FEATURES } from '../features';
3
- import { CacheStorage } from '../cache-storage';
4
- import { Context } from '../context';
5
- import { Bounds } from '../../css/layout/bounds';
6
- import { Html2CanvasConfig } from '../../config';
7
-
8
- const proxy = 'http://example.com/proxy';
9
-
10
- const createMockContext = (origin: string, opts = {}) => {
11
- const mockWindow = {
12
- location: {
13
- href: origin
14
- },
15
- document: {
16
- createElement(_name: string) {
17
- let _href = '';
18
- return {
19
- set href(value: string) {
20
- _href = value;
21
- },
22
- get href() {
23
- return _href;
24
- },
25
- get protocol() {
26
- return new URL(_href).protocol;
27
- },
28
- get hostname() {
29
- return new URL(_href).hostname;
30
- },
31
- get port() {
32
- return new URL(_href).port;
33
- }
34
- };
35
- }
36
- }
37
- };
38
-
39
- // For backward compatibility, also initialize static CacheStorage
40
- CacheStorage.setContext(mockWindow as Window);
41
-
42
- const config = new Html2CanvasConfig({ window: mockWindow as Window });
43
-
44
- return new Context(
45
- {
46
- logging: false,
47
- imageTimeout: 0,
48
- useCORS: false,
49
- allowTaint: false,
50
- proxy,
51
- ...opts
52
- },
53
- new Bounds(0, 0, 0, 0),
54
- config
55
- );
56
- };
57
-
58
- const images: ImageMock[] = [];
59
- const xhr: XMLHttpRequestMock[] = [];
60
- const sleep = async (timeout: number) => await new Promise((resolve) => setTimeout(resolve, timeout));
61
-
62
- class ImageMock {
63
- src?: string;
64
- crossOrigin?: string;
65
- onload?: () => void;
66
- constructor() {
67
- images.push(this);
68
- }
69
- }
70
-
71
- class XMLHttpRequestMock {
72
- sent: boolean;
73
- status: number;
74
- timeout: number;
75
- method?: string;
76
- url?: string;
77
- response?: string;
78
- onload?: () => void;
79
- ontimeout?: () => void;
80
- constructor() {
81
- this.sent = false;
82
- this.status = 500;
83
- this.timeout = 5000;
84
- xhr.push(this);
85
- }
86
-
87
- async load(status: number, response: string) {
88
- this.response = response;
89
- this.status = status;
90
- if (this.onload) {
91
- this.onload();
92
- }
93
- await sleep(0);
94
- }
95
-
96
- open(method: string, url: string) {
97
- this.method = method;
98
- this.url = url;
99
- }
100
- send() {
101
- this.sent = true;
102
- }
103
- }
104
-
105
- Object.defineProperty(global, 'Image', { value: ImageMock, writable: true });
106
- Object.defineProperty(global, 'XMLHttpRequest', {
107
- value: XMLHttpRequestMock,
108
- writable: true
109
- });
110
-
111
- const setFeatures = (opts: { [key: string]: boolean } = {}) => {
112
- const defaults: { [key: string]: boolean } = {
113
- SUPPORT_SVG_DRAWING: true,
114
- SUPPORT_CORS_IMAGES: true,
115
- SUPPORT_CORS_XHR: true,
116
- SUPPORT_RESPONSE_TYPE: false
117
- };
118
-
119
- Object.keys(defaults).forEach((key) => {
120
- Object.defineProperty(FEATURES, key, {
121
- value: typeof opts[key] === 'boolean' ? opts[key] : defaults[key],
122
- writable: true
123
- });
124
- });
125
- };
126
-
127
- describe('cache-storage', () => {
128
- beforeEach(() => setFeatures());
129
- afterEach(() => {
130
- xhr.splice(0, xhr.length);
131
- images.splice(0, images.length);
132
- });
133
- it('addImage adds images to cache', async () => {
134
- const { cache } = createMockContext('http://example.com', { proxy: null });
135
- await cache.addImage('http://example.com/test.jpg');
136
- await cache.addImage('http://example.com/test2.jpg');
137
-
138
- deepStrictEqual(images.length, 2);
139
- deepStrictEqual(images[0].src, 'http://example.com/test.jpg');
140
- deepStrictEqual(images[1].src, 'http://example.com/test2.jpg');
141
- });
142
-
143
- it('addImage should not add duplicate entries', async () => {
144
- const { cache } = createMockContext('http://example.com');
145
- await cache.addImage('http://example.com/test.jpg');
146
- await cache.addImage('http://example.com/test.jpg');
147
-
148
- deepStrictEqual(images.length, 1);
149
- deepStrictEqual(images[0].src, 'http://example.com/test.jpg');
150
- });
151
-
152
- describe('svg', () => {
153
- it('should add svg images correctly', async () => {
154
- const { cache } = createMockContext('http://example.com');
155
- await cache.addImage('http://example.com/test.svg');
156
- await cache.addImage('http://example.com/test2.svg');
157
-
158
- deepStrictEqual(images.length, 2);
159
- deepStrictEqual(images[0].src, 'http://example.com/test.svg');
160
- deepStrictEqual(images[1].src, 'http://example.com/test2.svg');
161
- });
162
-
163
- it('should omit svg images if not supported', async () => {
164
- setFeatures({ SUPPORT_SVG_DRAWING: false });
165
- const { cache } = createMockContext('http://example.com');
166
- await cache.addImage('http://example.com/test.svg');
167
- await cache.addImage('http://example.com/test2.svg');
168
-
169
- deepStrictEqual(images.length, 0);
170
- });
171
- });
172
-
173
- describe('cross-origin', () => {
174
- it('addImage should not add images it cannot load/render', async () => {
175
- const { cache } = createMockContext('http://example.com', {
176
- proxy: undefined
177
- });
178
- await cache.addImage('http://html2canvas.hertzen.com/test.jpg');
179
- deepStrictEqual(images.length, 0);
180
- });
181
-
182
- it('addImage should add images if tainting enabled', async () => {
183
- const { cache } = createMockContext('http://example.com', {
184
- allowTaint: true,
185
- proxy: undefined
186
- });
187
- await cache.addImage('http://html2canvas.hertzen.com/test.jpg');
188
- deepStrictEqual(images.length, 1);
189
- deepStrictEqual(images[0].src, 'http://html2canvas.hertzen.com/test.jpg');
190
- deepStrictEqual(images[0].crossOrigin, undefined);
191
- });
192
-
193
- it('addImage should add images if cors enabled', async () => {
194
- const { cache } = createMockContext('http://example.com', { useCORS: true });
195
- await cache.addImage('http://html2canvas.hertzen.com/test.jpg');
196
- deepStrictEqual(images.length, 1);
197
- deepStrictEqual(images[0].src, 'http://html2canvas.hertzen.com/test.jpg');
198
- deepStrictEqual(images[0].crossOrigin, 'anonymous');
199
- });
200
-
201
- it('addImage should not add images if cors enabled but not supported', async () => {
202
- setFeatures({ SUPPORT_CORS_IMAGES: false });
203
-
204
- const { cache } = createMockContext('http://example.com', {
205
- useCORS: true,
206
- proxy: undefined
207
- });
208
- await cache.addImage('http://html2canvas.hertzen.com/test.jpg');
209
- deepStrictEqual(images.length, 0);
210
- });
211
-
212
- it('addImage should not add images to proxy if cors enabled', async () => {
213
- const { cache } = createMockContext('http://example.com', { useCORS: true });
214
- await cache.addImage('http://html2canvas.hertzen.com/test.jpg');
215
- deepStrictEqual(images.length, 1);
216
- deepStrictEqual(images[0].src, 'http://html2canvas.hertzen.com/test.jpg');
217
- deepStrictEqual(images[0].crossOrigin, 'anonymous');
218
- });
219
-
220
- it('addImage should use proxy ', async () => {
221
- const { cache } = createMockContext('http://example.com');
222
- await cache.addImage('http://html2canvas.hertzen.com/test.jpg');
223
- deepStrictEqual(xhr.length, 1);
224
- deepStrictEqual(
225
- xhr[0].url,
226
- `${proxy}?url=${encodeURIComponent('http://html2canvas.hertzen.com/test.jpg')}&responseType=text`
227
- );
228
- await xhr[0].load(200, '<data response>');
229
-
230
- deepStrictEqual(images.length, 1);
231
- deepStrictEqual(images[0].src, '<data response>');
232
- });
233
-
234
- it('proxy should respect imageTimeout', async () => {
235
- const { cache } = createMockContext('http://example.com', {
236
- imageTimeout: 10
237
- });
238
- await cache.addImage('http://html2canvas.hertzen.com/test.jpg');
239
-
240
- deepStrictEqual(xhr.length, 1);
241
- deepStrictEqual(
242
- xhr[0].url,
243
- `${proxy}?url=${encodeURIComponent('http://html2canvas.hertzen.com/test.jpg')}&responseType=text`
244
- );
245
- deepStrictEqual(xhr[0].timeout, 10);
246
- if (xhr[0].ontimeout) {
247
- xhr[0].ontimeout();
248
- }
249
- try {
250
- await cache.match('http://html2canvas.hertzen.com/test.jpg');
251
- fail('Expected result to timeout');
252
- } catch (e) {}
253
- });
254
- });
255
-
256
- it('match should return cache entry', async () => {
257
- const { cache } = createMockContext('http://example.com');
258
- await cache.addImage('http://example.com/test.jpg');
259
-
260
- if (images[0].onload) {
261
- images[0].onload();
262
- }
263
-
264
- const response = await cache.match('http://example.com/test.jpg');
265
-
266
- deepStrictEqual((response as HTMLImageElement).src, 'http://example.com/test.jpg');
267
- });
268
-
269
- it('image should respect imageTimeout', async () => {
270
- const { cache } = createMockContext('http://example.com', { imageTimeout: 10 });
271
- cache.addImage('http://example.com/test.jpg');
272
-
273
- try {
274
- await cache.match('http://example.com/test.jpg');
275
- fail('Expected result to timeout');
276
- } catch (e) {}
277
- });
278
- });
@@ -1,29 +0,0 @@
1
- import { Logger } from '../logger';
2
-
3
- describe('logger', () => {
4
- let infoSpy: any;
5
-
6
- beforeEach(() => {
7
- infoSpy = vi.spyOn(console, 'info').mockImplementation(() => {
8
- // do nothing
9
- });
10
- });
11
-
12
- afterEach(() => {
13
- infoSpy.mockRestore();
14
- });
15
-
16
- it('should call console.info when logger enabled', () => {
17
- const id = Math.random().toString();
18
- const logger = new Logger({ id, enabled: true });
19
- logger.info('testing');
20
- expect(infoSpy).toHaveBeenLastCalledWith(id, expect.stringMatching(/\d+ms/), 'testing');
21
- });
22
-
23
- it("shouldn't call console.info when logger disabled", () => {
24
- const id = Math.random().toString();
25
- const logger = new Logger({ id, enabled: false });
26
- logger.info('testing');
27
- expect(infoSpy).not.toHaveBeenCalled();
28
- });
29
- });