Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions apps/web/src/components/files/FilePreviewPanel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,19 @@ describe("document preview contents", () => {
expect(html.replaceAll(/<[^>]*>/g, "")).toBe("Hello boldUnicode: αvisible");
expect(html).toContain("<strong>");
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("<p><strong>🚀 Launch</strong> <em>𝔸</em></p>");
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("<p><strong>🚀</strong></p>");
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("<p><strong>ABC</strong><br><strong>D</strong></p>");
});

it("breaks lines at the paragraph and line breaks TextEdit writes", () => {
Expand Down
29 changes: 20 additions & 9 deletions apps/web/src/components/files/extendedDocumentPreview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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" });
Expand Down Expand Up @@ -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();
Comment thread
Marve10s marked this conversation as resolved.
Resolved
if (next === "\\" || next === "{" || next === "}") {
i++;
emit(next);
Expand All @@ -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 += "<br>";
else if (next === "\n" || next === "\r") {
if (fallback > 0) fallback--;
else if (!hidden()) html += "<br>";
}
i++;
continue;
}
Expand Down Expand Up @@ -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 += "<br>";
else if (!hidden()) {
flush();
html += "<br>";
}
fallback = state.uc;
} else if (!hidden() && word === "page") finishPage();
else if (word === "cell") emit("\t", state.skip);
Expand Down
Loading