@@ -49,36 +49,56 @@ export const createArgosScreenshotCommand = (
4949 // and we screenshot the iframe's `<body>`. Anything overflowing the iframe box
5050 // is not painted, so the screenshot gets cut. `setViewportSize` grows the iframe
5151 // *before* `argosCSS` (which injects `fitToContent`'s `zoom`) is applied, so it
52- // can't account for the final content size. Re-fit the iframe height here, after
52+ // can't account for the final content size. Re-fit the iframe here, after
5353 // stabilization has injected `argosCSS`, so the whole content is painted.
5454 const userBeforeScreenshot = options ?. beforeScreenshot ;
5555 const optionsWithFit : ArgosScreenshotOptions = {
5656 ...options ,
5757 beforeScreenshot : async ( api ) => {
5858 await userBeforeScreenshot ?.( api ) ;
59- await ctx . page . evaluate ( ( ) => {
60- const iframe = document . querySelector ( 'iframe[data-vitest="true"]' ) ;
59+ await ctx . page . evaluate (
60+ ( { fitWidth } ) => {
61+ const iframe = document . querySelector ( 'iframe[data-vitest="true"]' ) ;
6162
62- if (
63- ! ( iframe instanceof HTMLIFrameElement ) ||
64- ! iframe . contentDocument
65- ) {
66- return ;
67- }
63+ if (
64+ ! ( iframe instanceof HTMLIFrameElement ) ||
65+ ! iframe . contentDocument
66+ ) {
67+ return ;
68+ }
6869
69- const { body, documentElement } = iframe . contentDocument ;
70- const contentHeight = Math . max (
71- body . scrollHeight ,
72- body . offsetHeight ,
73- documentElement . scrollHeight ,
74- ) ;
70+ const { body, documentElement } = iframe . contentDocument ;
71+ const contentHeight = Math . max (
72+ body . scrollHeight ,
73+ body . offsetHeight ,
74+ documentElement . scrollHeight ,
75+ ) ;
76+
77+ // Only grow, never shrink: the iframe must contain the full content
78+ // so it's painted, but we don't want to collapse an intentionally
79+ // sized viewport.
80+ if ( contentHeight > iframe . clientHeight ) {
81+ iframe . style . height = `${ contentHeight } px` ;
82+ }
7583
76- // Only grow, never shrink: the iframe must contain the full content so
77- // it's painted, but we don't want to collapse an intentionally sized viewport.
78- if ( contentHeight > iframe . clientHeight ) {
79- iframe . style . height = `${ contentHeight } px` ;
80- }
81- } ) ;
84+ // `fitToContent` fits the content in both dimensions, so the iframe
85+ // must also grow horizontally to paint content wider than the
86+ // viewport. Without `fitToContent` we keep the viewport width to
87+ // match Playwright's `fullPage` semantics (full height, viewport
88+ // width).
89+ if ( fitWidth ) {
90+ const contentWidth = Math . max (
91+ body . scrollWidth ,
92+ body . offsetWidth ,
93+ documentElement . scrollWidth ,
94+ ) ;
95+ if ( contentWidth > iframe . clientWidth ) {
96+ iframe . style . width = `${ contentWidth } px` ;
97+ }
98+ }
99+ } ,
100+ { fitWidth : Boolean ( fitToContent ) } ,
101+ ) ;
82102 } ,
83103 } ;
84104
0 commit comments