Skip to content

Commit

Permalink
fix: correctly handle allowTaint canvas cloning (#2649)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasvh authored Aug 13, 2021
1 parent 2b4de68 commit c378e22
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/dom/document-cloner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {Context} from '../core/context';
export interface CloneOptions {
ignoreElements?: (element: Element) => boolean;
onclone?: (document: Document, element: HTMLElement) => void;
allowTaint?: boolean;
}

export interface WindowOptions {
Expand Down Expand Up @@ -201,14 +202,16 @@ export class DocumentCloner {
const ctx = canvas.getContext('2d');
const clonedCtx = clonedCanvas.getContext('2d');
if (clonedCtx) {
if (ctx) {
if (!this.options.allowTaint && ctx) {
clonedCtx.putImageData(ctx.getImageData(0, 0, canvas.width, canvas.height), 0, 0);
} else {
clonedCtx.drawImage(canvas, 0, 0);
}
}
return clonedCanvas;
} catch (e) {}
} catch (e) {
this.context.logger.info(`Unable to clone canvas as it is tainted`);
}

return clonedCanvas;
}
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const renderElement = async (element: HTMLElement, opts: Partial<Options>): Prom
const foreignObjectRendering = opts.foreignObjectRendering ?? false;

const cloneOptions: CloneConfigurations = {
allowTaint: opts.allowTaint ?? false,
onclone: opts.onclone,
ignoreElements: opts.ignoreElements,
inlineImages: foreignObjectRendering,
Expand Down

0 comments on commit c378e22

Please sign in to comment.