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, 16:00 UTC
fix(web): preserve literal entities in EPUB CDATA (#36595)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Wout Stiens committed
Nov 14, 2026 at 10:33 UTC
d6244ac5a337b6c71154715d197b3d915b8584a8
2 files changed
+14
-5
apps/web/src/components/files/FilePreviewPanel.test.ts
+4
-3
@@ -221,12 +221,12 @@ Third}`;
221
it("resolves scoped ODF namespaces and explicit spreadsheet whitespace", async () => {
222
const archive = new JSZip().file(
223
"content.xml",
224
- '<o:document-content xmlns:o="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:t="urn:oasis:names:tc:opendocument:xmlns:table:1.0"><o:body><o:spreadsheet><t:table t:name="Accounts & names"><t:table-row><t:table-cell><p xmlns="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:x="urn:oasis:names:tc:opendocument:xmlns:text:1.0">Account<s/>Name<s x:c="3"/>A<tab/>B<line-break/>C</p></t:table-cell></t:table-row></t:table></o:spreadsheet></o:body></o:document-content>',
224
+ '<o:document-content xmlns:o="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:t="urn:oasis:names:tc:opendocument:xmlns:table:1.0"><o:body><o:spreadsheet><t:table t:name="Accounts & names"><t:table-row><t:table-cell><p xmlns="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:x="urn:oasis:names:tc:opendocument:xmlns:text:1.0">Account<s/>Name<s x:c="3"/>A<tab/>B<line-break/>C & <![CDATA[&]]></p></t:table-cell></t:table-row></t:table></o:spreadsheet></o:body></o:document-content>',
225
);
226
const result = await openDocument(archive, "ods");
227
if (result.type !== "ods") throw new Error("Expected workbook");
228
expect(result.sheets[0]!.name).toBe("Accounts & names");
229
- expect(result.sheets[0]!.rows[0]).toEqual(["Account Name A\tB\nC"]);
229
+ expect(result.sheets[0]!.rows[0]).toEqual(["Account Name A\tB\nC & &"]);
230
});
231
232
it("stops repeated slide images before encoding beyond the output budget", async () => {
@@ -439,7 +439,7 @@ Third}`;
439
)
440
.file(
441
"Book/two&&&.xhtml",
442
- '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html><head><title>Second</title></head><body><h1>Second chapter</h1><p>A B © — ≂̸ &lt;</p><img src="cover.png" alt="© ≂̸"/></body></html>',
442
+ '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html><head><title>Second</title></head><body><h1>Second chapter</h1><p>A B © — ≂̸ &lt;</p><pre><![CDATA[Use & and < literally]]></pre><img src="cover.png" alt="© ≂̸"/></body></html>',
443
)
444
.file(
445
"Book/cover.png",
@@ -453,6 +453,7 @@ Third}`;
453
if (result.type !== "document") throw new Error("Expected document");
454
expect(result.pages.map((page) => page.title)).toEqual(["Second", "First"]);
455
expect(result.pages[0]!.html).toContain("A\u00a0B © — ≂̸ &lt;");
456
+ expect(result.pages[0]!.html).toContain("<pre>Use &amp; and &lt; literally</pre>");
457
expect(result.pages[0]!.html).toContain('alt="© ≂̸"');
458
expect(result.pages[1]!.html).toContain('src="data:image/png;base64,');
459
expect(result.pages[1]!.html).not.toMatch(/https:|script|alert/);
apps/web/src/components/files/extendedDocumentPreview.ts
+10
-2
@@ -12,6 +12,7 @@ import { type OfficeWorkerResult, type NotebookCell } from "./officePreviewProto
12
const ODS_PREVIEW_MAX_ROWS = 500;
13
const ODS_PREVIEW_MAX_COLUMNS = 50;
14
15
+// `text` and `attrs` hold decoded values: escaped text is decoded once, CDATA stays literal.
16
type XmlNode = { name: string; attrs: Record<string, string>; children: XmlNode[]; text: string };
17
type OrderedXml = { [key: string]: OrderedXml[] | Record<string, string> | string };
18
const escape = (value: string) =>
@@ -47,6 +48,7 @@ function parseXml(source: string): XmlNode {
48
processEntities: false,
49
trimValues: false,
50
parseTagValue: false,
51
+ cdataPropName: "#cdata",
52
});
53
const ordered = parser.parse(source) as OrderedXml[];
54
let count = 0;
@@ -60,6 +62,12 @@ function parseXml(source: string): XmlNode {
62
if (++count > 100_000) throw new Error("This document has too much content to preview.");
63
const name = Object.keys(item).find((key) => key !== ":@") ?? "#text";
64
const value = item[name];
65
+ if (name === "#cdata") {
66
+ const literal = Array.isArray(value)
67
+ ? value.map((part) => part["#text"] ?? "").join("")
68
+ : "";
69
+ return { name: "#text", attrs: {}, children: [], text: literal };
70
+ }
71
const attrs = Object.fromEntries(
72
Object.entries((item[":@"] ?? {}) as Record<string, string>).map(([key, value]) => [
73
key,
@@ -91,7 +99,7 @@ function parseXml(source: string): XmlNode {
99
Object.entries(attrs).map(([key, value]) => [expanded(key, true), value]),
100
),
101
children: Array.isArray(value) ? convert(value, depth + 1, namespaces) : [],
94
- text: typeof value === "string" ? value : "",
102
+ text: typeof value === "string" ? decodeEntities(value) : "",
103
};
104
});
105
}
@@ -116,7 +124,7 @@ function text(node: XmlNode): string {
124
if (node.name === "text:s") return " ".repeat(repeat(node.attrs["text:c"], 100));
125
if (node.name === "text:tab") return "\t";
126
if (node.name === "text:line-break") return "\n";
119
- return node.name === "#text" ? decodeEntities(node.text) : node.children.map(text).join("");
127
+ return node.name === "#text" ? node.text : node.children.map(text).join("");
128
}
129
function archivePath(base: string, href: string): string | null {
130
if (/^(?:[a-z][a-z\d+.-]*:|\/|\\)/i.test(href)) return null;