diff --git a/apps/web/src/components/files/FilePreviewPanel.test.ts b/apps/web/src/components/files/FilePreviewPanel.test.ts index 0b4d7dc46b49f..4713e37c80b19 100644 --- a/apps/web/src/components/files/FilePreviewPanel.test.ts +++ b/apps/web/src/components/files/FilePreviewPanel.test.ts @@ -217,6 +217,19 @@ describe("document preview contents", () => { expect(html.replaceAll(/<[^>]*>/g, "")).toBe("Hello boldUnicode: αvisible"); expect(html).toContain(""); expect(html).not.toContain("https:"); + const styled = openRtf( + bytes( + "{\\rtf1\\ansi\\b \\uc0\\u55357 \\u56960 Launch\\b0 \\i\\uc1\\u-10187?\\u-8904?\\i0}", + ), + ); + if (styled.type !== "document") throw new Error("Expected document"); + expect(styled.pages[0]!.html).toBe("

🚀 Launch 𝔸

"); + const fallbacks = openRtf(bytes("{\\rtf1\\b\\uc1\\u-10179\\_\\uc0\\u-8576 }")); + if (fallbacks.type !== "document") throw new Error("Expected document"); + expect(fallbacks.pages[0]!.html).toBe("

🚀

"); + const escapedBreak = openRtf(bytes("{\\rtf1\\b A\\u66\\\nC\\par D}")); + if (escapedBreak.type !== "document") throw new Error("Expected document"); + expect(escapedBreak.pages[0]!.html).toBe("

ABC
D

"); }); it("breaks lines at the paragraph and line breaks TextEdit writes", () => { diff --git a/apps/web/src/components/files/extendedDocumentPreview.ts b/apps/web/src/components/files/extendedDocumentPreview.ts index f57d9c59a101d..bb37d1ca30656 100644 --- a/apps/web/src/components/files/extendedDocumentPreview.ts +++ b/apps/web/src/components/files/extendedDocumentPreview.ts @@ -598,13 +598,15 @@ export function openRtf(bytes: ArrayBuffer): OfficeWorkerResult { const hidden = () => state.skip || state.deleted || state.hiddenText; const encoded = new Uint8Array(bytes.byteLength); let encodedLength = 0; + const decodeBytes = () => { + if (!encodedLength) return; + run += escape( + (fonts.get(state.font) ?? state.decoder).decode(encoded.subarray(0, encodedLength)), + ); + encodedLength = 0; + }; const flush = () => { - if (encodedLength) { - run += escape( - (fonts.get(state.font) ?? state.decoder).decode(encoded.subarray(0, encodedLength)), - ); - encodedLength = 0; - } + decodeBytes(); if (completedLength + html.length + run.length > 8 * 1024 * 1024) throw new PreviewError({ code: "rtf-too-much-content" }); if (!run) return; @@ -621,6 +623,7 @@ export function openRtf(bytes: ArrayBuffer): OfficeWorkerResult { return; } if (skip) return; + decodeBytes(); run += escape(value); if (completedLength + html.length + run.length > 8 * 1024 * 1024) throw new PreviewError({ code: "rtf-too-much-content" }); @@ -651,7 +654,9 @@ export function openRtf(bytes: ArrayBuffer): OfficeWorkerResult { emitByte(Number.parseInt(hex, 16)); continue; } - flush(); + // \u, \uc and skipped fallbacks change no formatting, so a character written as two \u + // words stays in one run. + if (fallback === 0 && !/^uc?-?\d/.test(source.slice(i, i + 4))) flush(); if (next === "\\" || next === "{" || next === "}") { i++; emit(next); @@ -667,7 +672,10 @@ export function openRtf(bytes: ArrayBuffer): OfficeWorkerResult { const character = RTF_CHARACTERS.get(next!); if (character) emit(character); // A backslash before a line break is \par; TextEdit ends paragraphs this way. - else if ((next === "\n" || next === "\r") && !hidden()) html += "
"; + else if (next === "\n" || next === "\r") { + if (fallback > 0) fallback--; + else if (!hidden()) html += "
"; + } i++; continue; } @@ -768,7 +776,10 @@ export function openRtf(bytes: ArrayBuffer): OfficeWorkerResult { fallback = 0; // TextEdit writes a soft line break as U+2028, where Chromium does not break the line. if (arg !== 0x2028) emit(String.fromCharCode(arg < 0 ? arg + 65536 : arg)); - else if (!hidden()) html += "
"; + else if (!hidden()) { + flush(); + html += "
"; + } fallback = state.uc; } else if (!hidden() && word === "page") finishPage(); else if (word === "cell") emit("\t", state.skip);