Skip to content

Commit 6fe7cde

Browse files
authored
[PER-5758] fix canvas-to-image width handling (#1995)
* fix canvas-to-image width * add test cases
1 parent 88c987a commit 6fe7cde

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

packages/dom/src/serialize-canvas.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ function createAndInsertImageElement(canvas, clone, percyElementId, imageUrl) {
1313
img.setAttribute('data-percy-canvas-serialized', '');
1414
img.setAttribute('data-percy-serialized-attribute-src', imageUrl);
1515

16-
// set a default max width to account for canvases that might resize with JS
17-
img.style.maxWidth = img.style.maxWidth || '100%';
16+
// set the maxWidth only if it was set on the canvas
17+
if (canvas.style.maxWidth) {
18+
img.style.maxWidth = canvas.style.maxWidth;
19+
}
1820

1921
// insert the image into the cloned DOM and remove the cloned canvas element
2022
let cloneEl = clone.querySelector(`[data-percy-element-id=${percyElementId}]`);

packages/dom/test/serialize-canvas.test.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ describe('serializeCanvas', () => {
1414
height="150px"
1515
style="border: 5px solid black;"
1616
></canvas>
17+
<canvas
18+
id="canvas-with-maxwidth"
19+
width="200px"
20+
height="100px"
21+
style="border: 2px solid red; max-width: 150px;"
22+
></canvas>
1723
<canvas
1824
id="empty"
1925
width="0px"
@@ -37,6 +43,14 @@ describe('serializeCanvas', () => {
3743
ctx.stroke();
3844

3945
cache[plat].dataURL = canvas.toDataURL();
46+
47+
// Draw on canvas with maxWidth
48+
let canvasWithMaxWidth = dom.getElementById('canvas-with-maxwidth');
49+
let ctxWithMaxWidth = canvasWithMaxWidth.getContext('2d');
50+
ctxWithMaxWidth.fillStyle = 'blue';
51+
ctxWithMaxWidth.fillRect(10, 10, 50, 30);
52+
53+
cache[plat].dataURLWithMaxWidth = canvasWithMaxWidth.toDataURL();
4054
});
4155

4256
serialized = serializeDOM();
@@ -54,7 +68,7 @@ describe('serializeCanvas', () => {
5468
expect($canvas[0].getAttribute('width')).toBe('150px');
5569
expect($canvas[0].getAttribute('height')).toBe('150px');
5670
expect($canvas[0].getAttribute('src')).toMatch('/__serialized__/\\w+\\.png');
57-
expect($canvas[0].getAttribute('style')).toBe('border: 5px solid black; max-width: 100%;');
71+
expect($canvas[0].getAttribute('style')).toBe('border: 5px solid black;');
5872
expect($canvas[0].matches('[data-percy-canvas-serialized]')).toBe(true);
5973

6074
expect(serialized.resources).toContain(jasmine.objectContaining({
@@ -64,6 +78,22 @@ describe('serializeCanvas', () => {
6478
}));
6579
});
6680

81+
it(`${platform}: preserves maxWidth from canvas to image element`, () => {
82+
let $canvas = $('#canvas-with-maxwidth');
83+
expect($canvas[0].tagName).toBe('IMG');
84+
expect($canvas[0].getAttribute('width')).toBe('200px');
85+
expect($canvas[0].getAttribute('height')).toBe('100px');
86+
expect($canvas[0].getAttribute('style')).toBe('border: 2px solid red; max-width: 150px;');
87+
expect($canvas[0].style.maxWidth).toBe('150px');
88+
expect($canvas[0].matches('[data-percy-canvas-serialized]')).toBe(true);
89+
90+
expect(serialized.resources).toContain(jasmine.objectContaining({
91+
url: $canvas[0].getAttribute('src'),
92+
content: cache[platform].dataURLWithMaxWidth.split(',')[1],
93+
mimetype: 'image/png'
94+
}));
95+
});
96+
6797
it(`${platform}: does not serialize canvas elements when JS is enabled`, () => {
6898
serialized = serializeDOM({ enableJavaScript: true });
6999
$ = parseDOM(serialized.html, platform);

0 commit comments

Comments
 (0)