fix(email-viewer): hide images that fail to load

Instead of leaving the browser's broken-image placeholder + alt text (which
reads as stray label text — e.g. a 'logo' alt — in an otherwise image-only
email), hide any image that fails to load. Sanitizer-blocked images already use
a 1x1 transparent pixel with display:none, so they're unaffected.
This commit is contained in:
Shuki Vaknin
2026-07-05 21:29:19 +02:00
committed by Linus Rath
parent 0d73cb5dfb
commit 6b74615969
+20
View File
@@ -2172,6 +2172,26 @@ export function EmailViewer({
lastBodyHeightRef.current = initialHeight;
setIframeReady(true);
// Hide images that fail to load (dead/mixed-content/unreachable external
// URLs) rather than leaving the browser's broken-image placeholder and
// alt text, which read as stray label text in an otherwise image-only
// email (e.g. a blocked "logo" alt). Blocked images already carry a 1x1
// transparent pixel (naturalWidth 1) and display:none, so they're skipped.
const hideIfBroken = (img: HTMLImageElement) => {
if (img.complete && img.naturalWidth === 0 && img.getAttribute('src')) {
img.style.display = 'none';
}
};
doc.querySelectorAll('img').forEach((el) => {
const img = el as HTMLImageElement;
if (img.complete) {
hideIfBroken(img);
} else {
img.addEventListener('error', () => { img.style.display = 'none'; }, { once: true });
img.addEventListener('load', () => hideIfBroken(img), { once: true });
}
});
// Make links open in new tab
doc.querySelectorAll('a').forEach(a => {
a.setAttribute('target', '_blank');