diff --git a/.eslintrc b/.eslintrc index c5efba2..9b0a890 100644 --- a/.eslintrc +++ b/.eslintrc @@ -22,5 +22,6 @@ "@typescript-eslint/no-unused-vars": "off", "@typescript-eslint/class-name-casing": "off", "prettier/prettier": "error" - } + }, + "root": true } diff --git a/src/core/__mocks__/context.ts b/src/core/__mocks__/context.ts index 3a03a8d..b5fe7dc 100644 --- a/src/core/__mocks__/context.ts +++ b/src/core/__mocks__/context.ts @@ -9,10 +9,9 @@ export class Context { constructor() { this.cache = { - addImage: jest.fn().mockImplementation((src: string): Promise => { - const result = Promise.resolve(); - this._cache[src] = result; - return result; + addImage: jest.fn().mockImplementation((src: string): boolean => { + this._cache[src] = Promise.resolve(); + return true; }) }; } diff --git a/src/core/__tests__/cache-storage.ts b/src/core/__tests__/cache-storage.ts index 7844fa0..1fa5d85 100644 --- a/src/core/__tests__/cache-storage.ts +++ b/src/core/__tests__/cache-storage.ts @@ -125,88 +125,88 @@ describe('cache-storage', () => { xhr.splice(0, xhr.length); images.splice(0, images.length); }); - it('addImage adds images to cache', async () => { + it('addImage adds images to cache', () => { const {cache} = createMockContext('http://example.com', {proxy: null}); - await cache.addImage('http://example.com/test.jpg'); - await cache.addImage('http://example.com/test2.jpg'); + cache.addImage('http://example.com/test.jpg'); + cache.addImage('http://example.com/test2.jpg'); deepStrictEqual(images.length, 2); deepStrictEqual(images[0].src, 'http://example.com/test.jpg'); deepStrictEqual(images[1].src, 'http://example.com/test2.jpg'); }); - it('addImage should not add duplicate entries', async () => { + it('addImage should not add duplicate entries', () => { const {cache} = createMockContext('http://example.com'); - await cache.addImage('http://example.com/test.jpg'); - await cache.addImage('http://example.com/test.jpg'); + cache.addImage('http://example.com/test.jpg'); + cache.addImage('http://example.com/test.jpg'); deepStrictEqual(images.length, 1); deepStrictEqual(images[0].src, 'http://example.com/test.jpg'); }); describe('svg', () => { - it('should add svg images correctly', async () => { + it('should add svg images correctly', () => { const {cache} = createMockContext('http://example.com'); - await cache.addImage('http://example.com/test.svg'); - await cache.addImage('http://example.com/test2.svg'); + cache.addImage('http://example.com/test.svg'); + cache.addImage('http://example.com/test2.svg'); deepStrictEqual(images.length, 2); deepStrictEqual(images[0].src, 'http://example.com/test.svg'); deepStrictEqual(images[1].src, 'http://example.com/test2.svg'); }); - it('should omit svg images if not supported', async () => { + it('should omit svg images if not supported', () => { setFeatures({SUPPORT_SVG_DRAWING: false}); const {cache} = createMockContext('http://example.com'); - await cache.addImage('http://example.com/test.svg'); - await cache.addImage('http://example.com/test2.svg'); + cache.addImage('http://example.com/test.svg'); + cache.addImage('http://example.com/test2.svg'); deepStrictEqual(images.length, 0); }); }); describe('cross-origin', () => { - it('addImage should not add images it cannot load/render', async () => { + it('addImage should not add images it cannot load/render', () => { const {cache} = createMockContext('http://example.com', { proxy: undefined }); - await cache.addImage('http://html2canvas.hertzen.com/test.jpg'); + cache.addImage('http://html2canvas.hertzen.com/test.jpg'); deepStrictEqual(images.length, 0); }); - it('addImage should add images if tainting enabled', async () => { + it('addImage should add images if tainting enabled', () => { const {cache} = createMockContext('http://example.com', { allowTaint: true, proxy: undefined }); - await cache.addImage('http://html2canvas.hertzen.com/test.jpg'); + cache.addImage('http://html2canvas.hertzen.com/test.jpg'); deepStrictEqual(images.length, 1); deepStrictEqual(images[0].src, 'http://html2canvas.hertzen.com/test.jpg'); deepStrictEqual(images[0].crossOrigin, undefined); }); - it('addImage should add images if cors enabled', async () => { + it('addImage should add images if cors enabled', () => { const {cache} = createMockContext('http://example.com', {useCORS: true}); - await cache.addImage('http://html2canvas.hertzen.com/test.jpg'); + cache.addImage('http://html2canvas.hertzen.com/test.jpg'); deepStrictEqual(images.length, 1); deepStrictEqual(images[0].src, 'http://html2canvas.hertzen.com/test.jpg'); deepStrictEqual(images[0].crossOrigin, 'anonymous'); }); - it('addImage should not add images if cors enabled but not supported', async () => { + it('addImage should not add images if cors enabled but not supported', () => { setFeatures({SUPPORT_CORS_IMAGES: false}); const {cache} = createMockContext('http://example.com', { useCORS: true, proxy: undefined }); - await cache.addImage('http://html2canvas.hertzen.com/test.jpg'); + cache.addImage('http://html2canvas.hertzen.com/test.jpg'); deepStrictEqual(images.length, 0); }); - it('addImage should not add images to proxy if cors enabled', async () => { + it('addImage should not add images to proxy if cors enabled', () => { const {cache} = createMockContext('http://example.com', {useCORS: true}); - await cache.addImage('http://html2canvas.hertzen.com/test.jpg'); + cache.addImage('http://html2canvas.hertzen.com/test.jpg'); deepStrictEqual(images.length, 1); deepStrictEqual(images[0].src, 'http://html2canvas.hertzen.com/test.jpg'); deepStrictEqual(images[0].crossOrigin, 'anonymous'); @@ -214,7 +214,7 @@ describe('cache-storage', () => { it('addImage should use proxy ', async () => { const {cache} = createMockContext('http://example.com'); - await cache.addImage('http://html2canvas.hertzen.com/test.jpg'); + cache.addImage('http://html2canvas.hertzen.com/test.jpg'); deepStrictEqual(xhr.length, 1); deepStrictEqual( xhr[0].url, @@ -230,7 +230,7 @@ describe('cache-storage', () => { const {cache} = createMockContext('http://example.com', { imageTimeout: 10 }); - await cache.addImage('http://html2canvas.hertzen.com/test.jpg'); + cache.addImage('http://html2canvas.hertzen.com/test.jpg'); deepStrictEqual(xhr.length, 1); deepStrictEqual( @@ -250,7 +250,7 @@ describe('cache-storage', () => { it('match should return cache entry', async () => { const {cache} = createMockContext('http://example.com'); - await cache.addImage('http://example.com/test.jpg'); + cache.addImage('http://example.com/test.jpg'); if (images[0].onload) { images[0].onload(); diff --git a/src/core/cache-storage.ts b/src/core/cache-storage.ts index d935330..13cc5c2 100644 --- a/src/core/cache-storage.ts +++ b/src/core/cache-storage.ts @@ -39,20 +39,15 @@ export class Cache { constructor(private readonly context: Context, private readonly _options: ResourceOptions) {} - addImage(src: string): Promise { - const result = Promise.resolve(); - if (this.has(src)) { - return result; - } - + addImage(src: string): boolean { + if (this.has(src)) return true; if (isBlobImage(src) || isRenderable(src)) { (this._cache[src] = this.loadImage(src)).catch(() => { // prevent unhandled rejection }); - return result; + return true; } - - return result; + return false; } // eslint-disable-next-line @typescript-eslint/no-explicit-any diff --git a/src/core/features.ts b/src/core/features.ts index 52a8c51..78286ce 100644 --- a/src/core/features.ts +++ b/src/core/features.ts @@ -157,7 +157,7 @@ export const createForeignObjectSVG = ( return svg; }; -export const serializeSvg = (svg: SVGSVGElement | SVGForeignObjectElement, encoding: string = ''): string => { +export const serializeSvg = (svg: SVGSVGElement | SVGForeignObjectElement, encoding = ''): string => { const svgPrefix = 'data:image/svg+xml'; const selializedSvg = new XMLSerializer().serializeToString(svg); const encodedSvg = encoding === 'base64' ? btoa(selializedSvg) : encodeURIComponent(selializedSvg); @@ -171,6 +171,7 @@ export const deserializeSvg = (svg: string): SVGSVGElement | SVGForeignObjectEle const document = domParser.parseFromString(encodedSvg, 'image/svg+xml'); const parserError = document.querySelector('parsererror'); if (parserError) { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore: Expected 0-1 arguments, but got 2. throw new Error('Deserialisation failed', {cause: parserError}); }