The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 14:45 UTC
fix(web): RTF previews keep named spaces, hyphens and joiners (#36767)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Wout Stiens committed
Nov 14, 2026 at 15:27 UTC
10a44f5553a52ff29ceffe2a70304315aadf3d4b
3 files changed
+80
-11
apps/web/src/components/files/FilePreviewPanel.test.ts
+15
-1
@@ -134,6 +134,20 @@ describe("workspaceAssetResource", () => {
134
describe("document preview contents", () => {
135
const bytes = (text: string) => new TextEncoder().encode(text).buffer;
136
137
+ it.each([
138
+ ["control symbols", String.raw`{\rtf1 Jean\_Luc\-X\~Y}`, "Jean\u2011Luc\u00adX\u00a0Y"],
139
+ [
140
+ "named spaces, joiners and direction marks",
141
+ String.raw`{\rtf1 A\tab B\emspace C\enspace D\qmspace E\zwj F\zwnj G\zwbo H\ltrmark I\rtlmark J}`,
142
+ "A\tB\u2003C\u2002D\u2005E\u200dF\u200cG\u200bH\u200eI\u200fJ",
143
+ ],
144
+ ["a Unicode fallback", String.raw`{\rtf1 Jean\u8209\_Luc}`, "Jean\u2011Luc"],
145
+ ])("keeps the text of %s", (_name, source, expected) => {
146
+ const result = openRtf(bytes(source));
147
+ if (result.type !== "document") throw new Error("Expected document");
148
+ expect(result.pages[0]!.html.replaceAll(/<[^>]*>/g, "")).toBe(expected);
149
+ });
150
+
151
it.each(["?", String.raw`\'3f`, String.raw`\\`])(
152
"consumes skipped rich text Unicode fallback %s",
153
(fallback) => {
@@ -162,7 +176,7 @@ describe("document preview contents", () => {
176
it("decodes rich text formatting and Unicode without showing hidden destinations", () => {
177
const result = openRtf(
178
bytes(
165
- String.raw`{\rtf1\ansi{\fonttbl{\f0 Hidden font;}}Hello \b bold\b0\par Unicode: \u945?{\*\unknown hidden}{\field{\*\fldinst HYPERLINK "https://example.com"}{\fldrslt visible}}}`,
179
+ String.raw`{\rtf1\ansi{\fonttbl{\f0 Hidden font;}}Hello \b bold\b0\par Unicode: \u945?{\*\unknown hidden\_\emspace\zwj}{\field{\*\fldinst HYPERLINK "https://example.com"}{\fldrslt visible}}}`,
180
),
181
);
182
expect(result.type).toBe("document");
apps/web/src/components/files/extendedDocumentPreview.ts
+31
-10
@@ -492,6 +492,31 @@ export function openNotebook(bytes: ArrayBuffer): OfficeWorkerResult {
492
return { type: "notebook", language, cells };
493
}
494
495
+// Characters RTF writes as control symbols (`\~`) and control words (`\emspace`).
496
+const RTF_CHARACTERS = new Map([
497
+ ["~", "\u00a0"],
498
+ ["_", "\u2011"],
499
+ ["-", "\u00ad"],
500
+ ["tab", "\t"],
501
+ ["cell", "\t"],
502
+ ["endash", "–"],
503
+ // oxlint-disable-next-line t3code/no-em-dash-copy -- Preserve the RTF document's punctuation.
504
+ ["emdash", "—"],
505
+ ["bullet", "•"],
506
+ ["lquote", "‘"],
507
+ ["rquote", "’"],
508
+ ["ldblquote", "“"],
509
+ ["rdblquote", "”"],
510
+ ["enspace", "\u2002"],
511
+ ["emspace", "\u2003"],
512
+ ["qmspace", "\u2005"],
513
+ ["zwbo", "\u200b"],
514
+ ["zwnj", "\u200c"],
515
+ ["zwj", "\u200d"],
516
+ ["ltrmark", "\u200e"],
517
+ ["rtlmark", "\u200f"],
518
+]);
519
+
520
export function openRtf(bytes: ArrayBuffer): OfficeWorkerResult {
521
if (bytes.byteLength > 8 * 1024 * 1024)
522
throw new Error("Rich text preview limited to files under 8 MB.");
@@ -593,7 +618,8 @@ export function openRtf(bytes: ArrayBuffer): OfficeWorkerResult {
618
}
619
const match = /^([a-z]+)(-?\d+)? ?/i.exec(source.slice(i));
620
if (!match) {
596
- if (next === "~") emit("\u00a0");
621
+ const character = RTF_CHARACTERS.get(next!);
622
+ if (character) emit(character);
623
// A backslash before a line break is \par; TextEdit ends paragraphs this way.
624
else if ((next === "\n" || next === "\r") && !state.skip) html += "<br>";
625
i++;
@@ -686,15 +712,10 @@ export function openRtf(bytes: ArrayBuffer): OfficeWorkerResult {
712
fallback = state.uc;
713
} else if (!state.skip && word === "page") finishPage();
714
else if (!state.skip && (word === "par" || word === "line" || word === "row")) html += "<br>";
689
- else if (word === "tab" || word === "cell") emit("\t");
690
- else if (word === "endash") emit("–");
691
- // oxlint-disable-next-line t3code/no-em-dash-copy -- Preserve the RTF document's punctuation.
692
- else if (word === "emdash") emit("—");
693
- else if (word === "bullet") emit("•");
694
- else if (word === "lquote") emit("‘");
695
- else if (word === "rquote") emit("’");
696
- else if (word === "ldblquote") emit("“");
697
- else if (word === "rdblquote") emit("”");
715
+ else {
716
+ const character = RTF_CHARACTERS.get(word!);
717
+ if (character) emit(character);
718
+ }
719
} else if (char !== "\r" && char !== "\n" && char) emitByte(char.charCodeAt(0));
720
}
721
if (stack.length) throw new Error("This rich text document is damaged.");
scripts/scenarios/rtf-preview-keeps-named-text-characters.e2e.ts
new
+34
@@ -0,0 +1,34 @@
1
+import { expect } from "e2e";
2
+
3
+import { test, WEB } from "./t3.ts";
4
+
5
+// Java's RTFEditorKit writing "Account<em space>Name<en space>Jean<nonbreaking hyphen>Luc".
6
+const JDK_RTF = String.raw`{\rtf1\ansi
7
+{\fonttbl\f0\fnil Monospaced;}
8
+
9
+Account\emspace Name\enspace Jean\_ Luc\par
10
+}
11
+`;
12
+
13
+test(
14
+ "An RTF preview keeps the spaces and hyphens written as named control words",
15
+ WEB,
16
+ async ({ t3, app, screen, browser }) => {
17
+ const project = await t3.project("demo", { "contacts.rtf": JDK_RTF });
18
+ const thread = await t3.thread({ project, title: "Review contacts", messages: ["Start"] });
19
+ await t3.open(thread);
20
+ await expect(screen.getByText("fixture turn complete", { exact: false })).toBeVisible();
21
+
22
+ await browser.keyboard.press("Control+p");
23
+ await screen.getByPlaceholder("Search files…").fill("contacts.rtf");
24
+ await screen.getByRole("option", "contacts.rtf", { exact: false }).tap();
25
+
26
+ // The first document preview loads its worker, which a cold Vite cache optimizes on demand.
27
+ await expect(screen.getByText("1 page", { exact: false })).toBeVisible({ timeout: 120_000 });
28
+ const document = browser.frameLocator('iframe[title^="contacts.rtf"]');
29
+ const body = document.locator("body");
30
+ await expect(body).toContainText("Account");
31
+ await app.screenshot("text");
32
+ await expect(body).toHaveText("Account Name Jean‑ Luc");
33
+ },
34
+);