diff --git a/web/src/lib/__tests__/htmlAlternative.test.ts b/web/src/lib/__tests__/htmlAlternative.test.ts new file mode 100644 index 0000000..0fa86c6 --- /dev/null +++ b/web/src/lib/__tests__/htmlAlternative.test.ts @@ -0,0 +1,54 @@ +import { describe, expect, it } from "vitest"; +import { hasHtmlAlternative } from "../html"; + +/* + * The rule: `htmlBody` is derived, so its presence proves nothing. Only the + * part's own type says whether there is an HTML alternative to render. + * + * The shapes below are what Stalwart 0.16.21 actually returned for one thread + * on 2026-09-10, read back through `Email/get` with + * `bodyProperties: ["partId", "type"]`. A plain-text message named the *same* + * part in both lists; a message with a real alternative named two. + * + * Getting this wrong is not a rendering nicety. Plain text went to the HTML + * path, which places the body under `white-space: normal`, so every line break + * collapsed: hard-wrapped mail arrived as one paragraph, and the signature and + * the quoted reply ran into the prose. + */ +describe("deciding whether a message has an HTML alternative", () => { + it("says no to a plain-text message, whose htmlBody holds the text part", () => { + // As returned for mo@bunkus.online: htmlBody[0] and textBody[0] are the + // same part, typed text/plain. + expect(hasHtmlAlternative({ type: "text/plain" }, "Hey,\n\nmy earlier response was before\n")).toBe(false); + }); + + it("says yes to a real multipart/alternative", () => { + expect(hasHtmlAlternative({ type: "text/html" }, "

Hello

")).toBe(true); + }); + + it("keeps the parameters that follow a media type", () => { + // `type` arrives bare in practice, but a charset must not turn a real HTML + // part into a plain-text one. + expect(hasHtmlAlternative({ type: "text/html; charset=utf-8" }, "

Hi

")).toBe(true); + }); + + it("is not fooled by a type that merely starts with the right letters", () => { + expect(hasHtmlAlternative({ type: "text/htmlish" }, "

Hi

")).toBe(false); + }); + + it("matches the type case-insensitively, since a header may be capitalised", () => { + expect(hasHtmlAlternative({ type: "TEXT/HTML" }, "

Hi

")).toBe(true); + }); + + it("says no when the part is HTML but its value never arrived", () => { + // maxBodyValueBytes can leave a part named with nothing fetched; falling + // through to the text body is the useful answer, not an empty pane. + expect(hasHtmlAlternative({ type: "text/html" }, undefined)).toBe(false); + expect(hasHtmlAlternative({ type: "text/html" }, "")).toBe(false); + }); + + it("says no when there is no part at all", () => { + expect(hasHtmlAlternative(undefined, undefined)).toBe(false); + expect(hasHtmlAlternative({}, "something")).toBe(false); + }); +}); diff --git a/web/src/lib/html.ts b/web/src/lib/html.ts index 23a9d51..88ff172 100644 --- a/web/src/lib/html.ts +++ b/web/src/lib/html.ts @@ -350,6 +350,26 @@ export function markKeptSurfaces(root: ParentNode): number { return kept; } +/** + * Whether a message really has an HTML alternative to render. + * + * `htmlBody` is a *derived* list, not a filter: RFC 8621 ยง4.1.4 says a message + * with no HTML alternative still gets one, and it holds the text/plain part. + * Confirmed live against Stalwart 0.16.21 (2026-09-10) -- a plain-text mail + * comes back with `htmlBody` and `textBody` naming the same part, typed + * `text/plain`, while a real multipart/alternative names two different parts. + * + * So "is there a body value under htmlBody" is not the question; the part's own + * type is. Answering the first one sent every plain-text message down the HTML + * path, where the body is placed in `.ihm-email-root` under + * `white-space: normal` and every line break collapses -- hard-wrapped mail + * arrived as a single paragraph with the signature and the quoted reply run + * into the prose. + */ +export function hasHtmlAlternative(part: { type?: string } | undefined, value: string | undefined): boolean { + return /^text\/html\b/i.test(part?.type ?? "") && Boolean(value); +} + export const TEXT_EMAIL_CSS = ` :host { display:block; } .ihm-text-root { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-size: 13.5px; line-height:1.55; white-space: pre-wrap; overflow-wrap: anywhere; color: inherit; } diff --git a/web/src/views/mail/MessageView.tsx b/web/src/views/mail/MessageView.tsx index 2840059..0beec3d 100644 --- a/web/src/views/mail/MessageView.tsx +++ b/web/src/views/mail/MessageView.tsx @@ -18,7 +18,7 @@ import { internalDomains, isExternalSender, linkVerdict } from "@/lib/warnings"; import { spamReport, type SpamReport } from "@/lib/spamScore"; import { formatFullDate, formatListDate, formatSize } from "@/lib/format"; import { displayName, domainOf, formatAddress } from "@/lib/address"; -import { EMAIL_BASE_CSS, TEXT_EMAIL_CSS, htmlDeclaresColors, markKeptSurfaces, sanitizeEmailHtml } from "@/lib/html"; +import { EMAIL_BASE_CSS, TEXT_EMAIL_CSS, hasHtmlAlternative, htmlDeclaresColors, markKeptSurfaces, sanitizeEmailHtml } from "@/lib/html"; import { openableInTab, previewKind } from "@/lib/preview"; import { FilePreviewDialog } from "@/ui/filepreview"; import { findQuoteStart, htmlToText, textToHtml } from "@/lib/text"; @@ -137,7 +137,9 @@ export const MessageView = memo(function MessageView({ email: e, expanded, wasUn const textPart = e.textBody?.[0]; const htmlRaw = htmlPart?.partId ? e.bodyValues?.[htmlPart.partId]?.value : undefined; const textRaw = textPart?.partId ? e.bodyValues?.[textPart.partId]?.value : undefined; - const showHtml = Boolean(htmlRaw); + // Not `Boolean(htmlRaw)`: `htmlBody` carries the text part when there is no + // HTML alternative. See hasHtmlAlternative(). + const showHtml = hasHtmlAlternative(htmlPart, htmlRaw); const themeMessageBody = settings.themeMessageBody; const themeStyledMessages = settings.themeStyledMessages;