@@ -20,6 +20,12 @@ describe('serializeCanvas', () => {
2020 height="100px"
2121 style="border: 2px solid red; max-width: 150px;"
2222 ></canvas>
23+ <canvas
24+ id="canvas-with-static-width"
25+ width="180px"
26+ height="120px"
27+ style="border: 1px solid green; width: 180px;"
28+ ></canvas>
2329 <canvas
2430 id="empty"
2531 width="0px"
@@ -51,6 +57,14 @@ describe('serializeCanvas', () => {
5157 ctxWithMaxWidth . fillRect ( 10 , 10 , 50 , 30 ) ;
5258
5359 cache [ plat ] . dataURLWithMaxWidth = canvasWithMaxWidth . toDataURL ( ) ;
60+
61+ // Draw on canvas with static width
62+ let canvasWithStaticWidth = dom . getElementById ( 'canvas-with-static-width' ) ;
63+ let ctxWithStaticWidth = canvasWithStaticWidth . getContext ( '2d' ) ;
64+ ctxWithStaticWidth . fillStyle = 'green' ;
65+ ctxWithStaticWidth . fillRect ( 5 , 5 , 40 , 25 ) ;
66+
67+ cache [ plat ] . dataURLWithStaticWidth = canvasWithStaticWidth . toDataURL ( ) ;
5468 } ) ;
5569
5670 serialized = serializeDOM ( ) ;
@@ -68,8 +82,10 @@ describe('serializeCanvas', () => {
6882 expect ( $canvas [ 0 ] . getAttribute ( 'width' ) ) . toBe ( '150px' ) ;
6983 expect ( $canvas [ 0 ] . getAttribute ( 'height' ) ) . toBe ( '150px' ) ;
7084 expect ( $canvas [ 0 ] . getAttribute ( 'src' ) ) . toMatch ( '/__serialized__/\\w+\\.png' ) ;
71- expect ( $canvas [ 0 ] . getAttribute ( 'style' ) ) . toBe ( 'border: 5px solid black;' ) ;
85+ expect ( $canvas [ 0 ] . getAttribute ( 'style' ) ) . toBe ( 'border: 5px solid black; max-width: 100%; ' ) ;
7286 expect ( $canvas [ 0 ] . matches ( '[data-percy-canvas-serialized]' ) ) . toBe ( true ) ;
87+ // Verify maxWidth is applied since canvas has no style.width (only attribute width)
88+ expect ( $canvas [ 0 ] . style . maxWidth ) . toBe ( '100%' ) ;
7389
7490 expect ( serialized . resources ) . toContain ( jasmine . objectContaining ( {
7591 url : $canvas [ 0 ] . getAttribute ( 'src' ) ,
@@ -86,6 +102,8 @@ describe('serializeCanvas', () => {
86102 expect ( $canvas [ 0 ] . getAttribute ( 'style' ) ) . toBe ( 'border: 2px solid red; max-width: 150px;' ) ;
87103 expect ( $canvas [ 0 ] . style . maxWidth ) . toBe ( '150px' ) ;
88104 expect ( $canvas [ 0 ] . matches ( '[data-percy-canvas-serialized]' ) ) . toBe ( true ) ;
105+ // Verify original maxWidth is preserved (not overridden to 100%)
106+ // since canvas already has maxWidth and no static pixel width
89107
90108 expect ( serialized . resources ) . toContain ( jasmine . objectContaining ( {
91109 url : $canvas [ 0 ] . getAttribute ( 'src' ) ,
@@ -94,6 +112,23 @@ describe('serializeCanvas', () => {
94112 } ) ) ;
95113 } ) ;
96114
115+ it ( `${ platform } : does not add maxWidth when canvas has static pixel width` , ( ) => {
116+ let $canvas = $ ( '#canvas-with-static-width' ) ;
117+ expect ( $canvas [ 0 ] . tagName ) . toBe ( 'IMG' ) ;
118+ expect ( $canvas [ 0 ] . getAttribute ( 'width' ) ) . toBe ( '180px' ) ;
119+ expect ( $canvas [ 0 ] . getAttribute ( 'height' ) ) . toBe ( '120px' ) ;
120+ expect ( $canvas [ 0 ] . getAttribute ( 'style' ) ) . toBe ( 'border: 1px solid green; width: 180px;' ) ;
121+ expect ( $canvas [ 0 ] . matches ( '[data-percy-canvas-serialized]' ) ) . toBe ( true ) ;
122+ // Verify maxWidth is NOT added since canvas has static pixel width (width: 180px)
123+ expect ( $canvas [ 0 ] . style . maxWidth ) . toBe ( '' ) ;
124+
125+ expect ( serialized . resources ) . toContain ( jasmine . objectContaining ( {
126+ url : $canvas [ 0 ] . getAttribute ( 'src' ) ,
127+ content : cache [ platform ] . dataURLWithStaticWidth . split ( ',' ) [ 1 ] ,
128+ mimetype : 'image/png'
129+ } ) ) ;
130+ } ) ;
131+
97132 it ( `${ platform } : does not serialize canvas elements when JS is enabled` , ( ) => {
98133 serialized = serializeDOM ( { enableJavaScript : true } ) ;
99134 $ = parseDOM ( serialized . html , platform ) ;
0 commit comments