forked from niklasvh/html2canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: using existing canvas option (niklasvh#2017)
* refactor: document cleanup to DocumentCloner * fix: using existing canvas option * fix: lint errors * fix: preview transform origin
- Loading branch information
Showing
9 changed files
with
147 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import html2canvas from '../index'; | ||
|
||
import {CanvasRenderer} from '../render/canvas/canvas-renderer'; | ||
import {DocumentCloner} from '../dom/document-cloner'; | ||
import {COLORS} from '../css/types/color'; | ||
|
||
jest.mock('../core/logger'); | ||
jest.mock('../css/layout/bounds'); | ||
jest.mock('../dom/document-cloner'); | ||
jest.mock('../dom/node-parser', () => { | ||
return { | ||
isBodyElement: () => false, | ||
isHTMLElement: () => false, | ||
parseTree: jest.fn().mockImplementation(() => { | ||
return {styles: {}}; | ||
}) | ||
}; | ||
}); | ||
|
||
jest.mock('../render/stacking-context'); | ||
jest.mock('../render/canvas/canvas-renderer'); | ||
|
||
describe('html2canvas', () => { | ||
// eslint-disable-next-line @typescript-eslint/no-object-literal-type-assertion | ||
const element = { | ||
ownerDocument: { | ||
defaultView: { | ||
pageXOffset: 12, | ||
pageYOffset: 34 | ||
} | ||
} | ||
} as HTMLElement; | ||
|
||
it('should render with an element', async () => { | ||
DocumentCloner.destroy = jest.fn().mockReturnValue(true); | ||
await html2canvas(element); | ||
expect(CanvasRenderer).toHaveBeenLastCalledWith( | ||
expect.objectContaining({ | ||
backgroundColor: 0xffffffff, | ||
scale: 1, | ||
height: 50, | ||
width: 200, | ||
x: 0, | ||
y: 0, | ||
scrollX: 12, | ||
scrollY: 34, | ||
canvas: undefined | ||
}) | ||
); | ||
expect(DocumentCloner.destroy).toBeCalled(); | ||
}); | ||
|
||
it('should have transparent background with backgroundColor: null', async () => { | ||
await html2canvas(element, {backgroundColor: null}); | ||
expect(CanvasRenderer).toHaveBeenLastCalledWith( | ||
expect.objectContaining({ | ||
backgroundColor: COLORS.TRANSPARENT | ||
}) | ||
); | ||
}); | ||
|
||
it('should use existing canvas when given as option', async () => { | ||
// eslint-disable-next-line @typescript-eslint/no-object-literal-type-assertion | ||
const canvas = {} as HTMLCanvasElement; | ||
await html2canvas(element, {canvas}); | ||
expect(CanvasRenderer).toHaveBeenLastCalledWith( | ||
expect.objectContaining({ | ||
canvas | ||
}) | ||
); | ||
}); | ||
|
||
it('should not remove cloned window when removeContainer: false', async () => { | ||
DocumentCloner.destroy = jest.fn(); | ||
await html2canvas(element, {removeContainer: false}); | ||
expect(CanvasRenderer).toHaveBeenLastCalledWith( | ||
expect.objectContaining({ | ||
backgroundColor: 0xffffffff, | ||
scale: 1, | ||
height: 50, | ||
width: 200, | ||
x: 0, | ||
y: 0, | ||
scrollX: 12, | ||
scrollY: 34, | ||
canvas: undefined | ||
}) | ||
); | ||
expect(DocumentCloner.destroy).not.toBeCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export class Logger { | ||
debug() {} | ||
|
||
static create() {} | ||
|
||
static destroy() {} | ||
|
||
static getInstance(): Logger { | ||
return logger; | ||
} | ||
|
||
info() {} | ||
|
||
error() {} | ||
} | ||
|
||
const logger = new Logger(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const {Bounds} = jest.requireActual('../bounds'); | ||
export const parseBounds = () => { | ||
return new Bounds(0, 0, 200, 50); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export class DocumentCloner { | ||
clonedReferenceElement?: HTMLElement; | ||
|
||
constructor() { | ||
// eslint-disable-next-line @typescript-eslint/no-object-literal-type-assertion | ||
this.clonedReferenceElement = {} as HTMLElement; | ||
} | ||
|
||
toIFrame() { | ||
return Promise.resolve({}); | ||
} | ||
|
||
static destroy() { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters