/** Hauteur utile du corps d’un iframe d’aperçu mail (évite le scrollHeight fantôme). */ export const EMAIL_PREVIEW_MIN_IFRAME_HEIGHT = 120 const CONTENT_MEASURE_SELECTORS = "p,div,span,td,th,li,h1,h2,h3,h4,h5,h6,table,img,blockquote,pre,hr,a,section,article,footer,header" export function measureEmailPreviewIframeHeight(doc: Document): number { const body = doc.body const view = doc.defaultView if (!body || !view) return EMAIL_PREVIEW_MIN_IFRAME_HEIGHT const scrollEstimate = Math.max( body.scrollHeight, body.offsetHeight, doc.documentElement?.scrollHeight ?? 0, doc.documentElement?.offsetHeight ?? 0 ) const bodyTop = body.getBoundingClientRect().top let contentBottom = bodyTop for (const el of body.querySelectorAll(CONTENT_MEASURE_SELECTORS)) { const cs = view.getComputedStyle(el) if (cs.display === "none" || cs.visibility === "hidden") continue const rect = el.getBoundingClientRect() if (rect.height < 1 && rect.width < 1) continue contentBottom = Math.max(contentBottom, rect.bottom) } const hasVisibleLayout = contentBottom > bodyTop + 8 if (hasVisibleLayout) { const boundEstimate = Math.ceil(contentBottom - bodyTop) + 4 if (boundEstimate < scrollEstimate * 0.92) { return Math.max(EMAIL_PREVIEW_MIN_IFRAME_HEIGHT, boundEstimate) } } return Math.max(EMAIL_PREVIEW_MIN_IFRAME_HEIGHT, scrollEstimate + 2) }