mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
Remove promise from addmage
This commit is contained in:
parent
357a9a029f
commit
d07e440127
@ -22,5 +22,6 @@
|
|||||||
"@typescript-eslint/no-unused-vars": "off",
|
"@typescript-eslint/no-unused-vars": "off",
|
||||||
"@typescript-eslint/class-name-casing": "off",
|
"@typescript-eslint/class-name-casing": "off",
|
||||||
"prettier/prettier": "error"
|
"prettier/prettier": "error"
|
||||||
}
|
},
|
||||||
|
"root": true
|
||||||
}
|
}
|
||||||
|
@ -9,10 +9,9 @@ export class Context {
|
|||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.cache = {
|
this.cache = {
|
||||||
addImage: jest.fn().mockImplementation((src: string): Promise<void> => {
|
addImage: jest.fn().mockImplementation((src: string): boolean => {
|
||||||
const result = Promise.resolve();
|
this._cache[src] = Promise.resolve();
|
||||||
this._cache[src] = result;
|
return true;
|
||||||
return result;
|
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -125,88 +125,88 @@ describe('cache-storage', () => {
|
|||||||
xhr.splice(0, xhr.length);
|
xhr.splice(0, xhr.length);
|
||||||
images.splice(0, images.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});
|
const {cache} = createMockContext('http://example.com', {proxy: null});
|
||||||
await cache.addImage('http://example.com/test.jpg');
|
cache.addImage('http://example.com/test.jpg');
|
||||||
await cache.addImage('http://example.com/test2.jpg');
|
cache.addImage('http://example.com/test2.jpg');
|
||||||
|
|
||||||
deepStrictEqual(images.length, 2);
|
deepStrictEqual(images.length, 2);
|
||||||
deepStrictEqual(images[0].src, 'http://example.com/test.jpg');
|
deepStrictEqual(images[0].src, 'http://example.com/test.jpg');
|
||||||
deepStrictEqual(images[1].src, 'http://example.com/test2.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');
|
const {cache} = createMockContext('http://example.com');
|
||||||
await cache.addImage('http://example.com/test.jpg');
|
cache.addImage('http://example.com/test.jpg');
|
||||||
await cache.addImage('http://example.com/test.jpg');
|
cache.addImage('http://example.com/test.jpg');
|
||||||
|
|
||||||
deepStrictEqual(images.length, 1);
|
deepStrictEqual(images.length, 1);
|
||||||
deepStrictEqual(images[0].src, 'http://example.com/test.jpg');
|
deepStrictEqual(images[0].src, 'http://example.com/test.jpg');
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('svg', () => {
|
describe('svg', () => {
|
||||||
it('should add svg images correctly', async () => {
|
it('should add svg images correctly', () => {
|
||||||
const {cache} = createMockContext('http://example.com');
|
const {cache} = createMockContext('http://example.com');
|
||||||
await cache.addImage('http://example.com/test.svg');
|
cache.addImage('http://example.com/test.svg');
|
||||||
await cache.addImage('http://example.com/test2.svg');
|
cache.addImage('http://example.com/test2.svg');
|
||||||
|
|
||||||
deepStrictEqual(images.length, 2);
|
deepStrictEqual(images.length, 2);
|
||||||
deepStrictEqual(images[0].src, 'http://example.com/test.svg');
|
deepStrictEqual(images[0].src, 'http://example.com/test.svg');
|
||||||
deepStrictEqual(images[1].src, 'http://example.com/test2.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});
|
setFeatures({SUPPORT_SVG_DRAWING: false});
|
||||||
const {cache} = createMockContext('http://example.com');
|
const {cache} = createMockContext('http://example.com');
|
||||||
await cache.addImage('http://example.com/test.svg');
|
cache.addImage('http://example.com/test.svg');
|
||||||
await cache.addImage('http://example.com/test2.svg');
|
cache.addImage('http://example.com/test2.svg');
|
||||||
|
|
||||||
deepStrictEqual(images.length, 0);
|
deepStrictEqual(images.length, 0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('cross-origin', () => {
|
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', {
|
const {cache} = createMockContext('http://example.com', {
|
||||||
proxy: undefined
|
proxy: undefined
|
||||||
});
|
});
|
||||||
await cache.addImage('http://html2canvas.hertzen.com/test.jpg');
|
cache.addImage('http://html2canvas.hertzen.com/test.jpg');
|
||||||
deepStrictEqual(images.length, 0);
|
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', {
|
const {cache} = createMockContext('http://example.com', {
|
||||||
allowTaint: true,
|
allowTaint: true,
|
||||||
proxy: undefined
|
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.length, 1);
|
||||||
deepStrictEqual(images[0].src, 'http://html2canvas.hertzen.com/test.jpg');
|
deepStrictEqual(images[0].src, 'http://html2canvas.hertzen.com/test.jpg');
|
||||||
deepStrictEqual(images[0].crossOrigin, undefined);
|
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});
|
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.length, 1);
|
||||||
deepStrictEqual(images[0].src, 'http://html2canvas.hertzen.com/test.jpg');
|
deepStrictEqual(images[0].src, 'http://html2canvas.hertzen.com/test.jpg');
|
||||||
deepStrictEqual(images[0].crossOrigin, 'anonymous');
|
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});
|
setFeatures({SUPPORT_CORS_IMAGES: false});
|
||||||
|
|
||||||
const {cache} = createMockContext('http://example.com', {
|
const {cache} = createMockContext('http://example.com', {
|
||||||
useCORS: true,
|
useCORS: true,
|
||||||
proxy: undefined
|
proxy: undefined
|
||||||
});
|
});
|
||||||
await cache.addImage('http://html2canvas.hertzen.com/test.jpg');
|
cache.addImage('http://html2canvas.hertzen.com/test.jpg');
|
||||||
deepStrictEqual(images.length, 0);
|
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});
|
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.length, 1);
|
||||||
deepStrictEqual(images[0].src, 'http://html2canvas.hertzen.com/test.jpg');
|
deepStrictEqual(images[0].src, 'http://html2canvas.hertzen.com/test.jpg');
|
||||||
deepStrictEqual(images[0].crossOrigin, 'anonymous');
|
deepStrictEqual(images[0].crossOrigin, 'anonymous');
|
||||||
@ -214,7 +214,7 @@ describe('cache-storage', () => {
|
|||||||
|
|
||||||
it('addImage should use proxy ', async () => {
|
it('addImage should use proxy ', async () => {
|
||||||
const {cache} = createMockContext('http://example.com');
|
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.length, 1);
|
||||||
deepStrictEqual(
|
deepStrictEqual(
|
||||||
xhr[0].url,
|
xhr[0].url,
|
||||||
@ -230,7 +230,7 @@ describe('cache-storage', () => {
|
|||||||
const {cache} = createMockContext('http://example.com', {
|
const {cache} = createMockContext('http://example.com', {
|
||||||
imageTimeout: 10
|
imageTimeout: 10
|
||||||
});
|
});
|
||||||
await cache.addImage('http://html2canvas.hertzen.com/test.jpg');
|
cache.addImage('http://html2canvas.hertzen.com/test.jpg');
|
||||||
|
|
||||||
deepStrictEqual(xhr.length, 1);
|
deepStrictEqual(xhr.length, 1);
|
||||||
deepStrictEqual(
|
deepStrictEqual(
|
||||||
@ -250,7 +250,7 @@ describe('cache-storage', () => {
|
|||||||
|
|
||||||
it('match should return cache entry', async () => {
|
it('match should return cache entry', async () => {
|
||||||
const {cache} = createMockContext('http://example.com');
|
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) {
|
if (images[0].onload) {
|
||||||
images[0].onload();
|
images[0].onload();
|
||||||
|
@ -39,20 +39,15 @@ export class Cache {
|
|||||||
|
|
||||||
constructor(private readonly context: Context, private readonly _options: ResourceOptions) {}
|
constructor(private readonly context: Context, private readonly _options: ResourceOptions) {}
|
||||||
|
|
||||||
addImage(src: string): Promise<void> {
|
addImage(src: string): boolean {
|
||||||
const result = Promise.resolve();
|
if (this.has(src)) return true;
|
||||||
if (this.has(src)) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isBlobImage(src) || isRenderable(src)) {
|
if (isBlobImage(src) || isRenderable(src)) {
|
||||||
(this._cache[src] = this.loadImage(src)).catch(() => {
|
(this._cache[src] = this.loadImage(src)).catch(() => {
|
||||||
// prevent unhandled rejection
|
// prevent unhandled rejection
|
||||||
});
|
});
|
||||||
return result;
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
@ -157,7 +157,7 @@ export const createForeignObjectSVG = (
|
|||||||
return svg;
|
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 svgPrefix = 'data:image/svg+xml';
|
||||||
const selializedSvg = new XMLSerializer().serializeToString(svg);
|
const selializedSvg = new XMLSerializer().serializeToString(svg);
|
||||||
const encodedSvg = encoding === 'base64' ? btoa(selializedSvg) : encodeURIComponent(selializedSvg);
|
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 document = domParser.parseFromString(encodedSvg, 'image/svg+xml');
|
||||||
const parserError = document.querySelector('parsererror');
|
const parserError = document.querySelector('parsererror');
|
||||||
if (parserError) {
|
if (parserError) {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
// @ts-ignore: Expected 0-1 arguments, but got 2.
|
// @ts-ignore: Expected 0-1 arguments, but got 2.
|
||||||
throw new Error('Deserialisation failed', {cause: parserError});
|
throw new Error('Deserialisation failed', {cause: parserError});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user