Render plain-text mail with its line breaks

`htmlBody` is a derived list, not a filter: RFC 8621 §4.1.4 gives a message
with no HTML alternative one anyway, holding the text/plain part. Testing
`Boolean(htmlRaw)` therefore answered "this is HTML" for every plain-text
mail, sending it to HtmlBody and `.ihm-email-root`, which is
`white-space: normal` and collapses every line break. Hard-wrapped mail
arrived as a single paragraph with the signature and the quoted reply run
into the prose.

Confirmed live against Stalwart 0.16.21 (2026-09-10): a plain-text message
comes back with `htmlBody` and `textBody` naming the same part, typed
text/plain, while a real multipart/alternative names two different parts.
`type` was already in BODY_PROPS; nothing looked at it.

TextBody was written for exactly these messages and was simply unreachable,
so this also restores what it does -- pre-wrap, quote-depth colouring and the
collapsible quoted block, none of which had ever fired on plain-text mail.
This commit is contained in:
2026-09-10 13:32:37 -07:00
parent a9923c48d9
commit 73a1bad29f
3 changed files with 78 additions and 2 deletions
@@ -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 [email protected]: 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" }, "<p>Hello</p>")).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" }, "<p>Hi</p>")).toBe(true);
});
it("is not fooled by a type that merely starts with the right letters", () => {
expect(hasHtmlAlternative({ type: "text/htmlish" }, "<p>Hi</p>")).toBe(false);
});
it("matches the type case-insensitively, since a header may be capitalised", () => {
expect(hasHtmlAlternative({ type: "TEXT/HTML" }, "<p>Hi</p>")).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);
});
});
+20
View File
@@ -350,6 +350,26 @@ export function markKeptSurfaces(root: ParentNode): number {
return kept; 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 = ` export const TEXT_EMAIL_CSS = `
:host { display:block; } :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; } .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; }
+4 -2
View File
@@ -18,7 +18,7 @@ import { internalDomains, isExternalSender, linkVerdict } from "@/lib/warnings";
import { spamReport, type SpamReport } from "@/lib/spamScore"; import { spamReport, type SpamReport } from "@/lib/spamScore";
import { formatFullDate, formatListDate, formatSize } from "@/lib/format"; import { formatFullDate, formatListDate, formatSize } from "@/lib/format";
import { displayName, domainOf, formatAddress } from "@/lib/address"; 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 { openableInTab, previewKind } from "@/lib/preview";
import { FilePreviewDialog } from "@/ui/filepreview"; import { FilePreviewDialog } from "@/ui/filepreview";
import { findQuoteStart, htmlToText, textToHtml } from "@/lib/text"; 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 textPart = e.textBody?.[0];
const htmlRaw = htmlPart?.partId ? e.bodyValues?.[htmlPart.partId]?.value : undefined; const htmlRaw = htmlPart?.partId ? e.bodyValues?.[htmlPart.partId]?.value : undefined;
const textRaw = textPart?.partId ? e.bodyValues?.[textPart.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 themeMessageBody = settings.themeMessageBody;
const themeStyledMessages = settings.themeStyledMessages; const themeStyledMessages = settings.themeStyledMessages;