Public: anyone can read this; the crew can change it.

Office documents and layered images open as facts: .docx text outline, .pptx slide titles, .ora layer list #12903

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/12769-office-documents-and-layered-images-open → main 5 commits fork 5 threads · 5 unresolved

  • Merged. scout/12769-office-documents-and-layered-images-open was merged into main as fe6e692.

Review

Sign in to review this change.
14 files changed +849 −1
docs/features/README.md +1
@@ -247,6 +247,7 @@ Read with the guide page [Your apps](../guide-your-apps.md).
247247 - [Shift a subtitle file](subtitle-shift.md)
248248 - [SQLite files as tables](sqlite-view.md)
249249 - [Excel workbooks (.xlsx) as a table](xlsx.md)
250 - [Office documents and layered images (.docx, .pptx, .ora)](office.md)
250251 - [Subtitles as a transcript](subtitles.md)
251252 - [Terminal logs in colour](ansi-log.md)
252253
docs/features/office.md added +13
@@ -0,0 +1,13 @@
1 # Office documents and layered images (.docx, .pptx, .ora)
2
3 Each is a zip of XML; the forge lists facts from it with no library and shows them instead of a download link.
4
5 - **.docx:** its headings as an outline (styles `Heading1`…`Heading6`, `Title`, or an outline level) and the first 200 non-empty paragraphs as plain text. Formatting, images and tables are not drawn.
6 - **.pptx:** each slide's number and title, in presentation order (the title placeholder's text; `(no title)` otherwise).
7 - **.ora:** layers (name, pixel size from the layer's PNG, visible or hidden), top first, from `stack.xml`, and the merged preview. The preview is inlined as a `data:image/png` only when `mergedimage.png` is really a PNG of at most 4 MB.
8 - **Zip bombs:** a file declaring more than 1,000 entries or 64 MB unpacked is refused before any part is read; one inflation budget of 64 MB is spent across every part read, and one part may not inflate past 16 MB (2 MB per slide). Sources: `src/web/render/office-zip.ts`.
9 - **Fallback:** a damaged, encrypted, zip64 or wrong-format file keeps the source view with a sentence saying why; never an error page.
10 - **Hostile XML:** parts are walked in one forward pass (`elements` and `tags` in `office-zip.ts`), never with a regex that can rescan; an unclosed tag ends the walk, and each walk stops at a cap on matches.
outdated

Stale: the terminal workspace does draw these now (src/tui/screens/workspace_office.ts). Fix this line and say how the terminal shows each kind.

11 - **Terminal:** the workspace opens the same files with the same readers and caps (`src/tui/screens/workspace_office.ts`), as a table of plain text: a .docx is its first 200 paragraphs, each once, with `H1`…`H6` beside the headings; a .pptx is slide number and title; an .ora is layer name, size and visibility (no preview). A file over the page's cap, or one that cannot be read, shows a sentence saying why.
12 - **Left out:** tables, comments and notes in documents; layer blend modes and offsets; the .ora preview in the terminal.
13 - **Code:** `src/web/render/docx.ts`, `pptx.ts`, `ora.ts`; kinds `document`, `slides`, `layers` in `kinds.ts`. Served as `application/octet-stream` always.
src/tui/screens/workspace.ts +2 −1
@@ -77,6 +77,7 @@ import { CARD_CAP, parseVcf } from "../../web/render/vcf-parse.ts";
7777 import { CUE_CAP, parseSubtitles } from "../../web/render/subtitles.ts";
7878 import { isJsonlPath, kindView, type KindLines } from "./workspace_kinds.ts";
7979 import { sheetView } from "./workspace_sheet.ts";
80 import { officeView } from "./workspace_office.ts";
8081 import { layoutTable, type Grid } from "../ui/grid.ts";
8182 import type { Ctx, Screen } from "../app/app.ts";
8283 import { stripAnsi } from "../term/ansi.ts";
@@ -1269,7 +1270,7 @@ export function workspaceScreen(
12691270 // A binary blob whose *name* says picture is the one binary we do
12701271 // something with. Everything else keeps the honest note: showing
12711272 // bytes as text is how a terminal ends up beeping at you.
1272 const book = looksLikeImage(t.path) ? null : sheetView(t.path, read.value.body);
1273 const book = looksLikeImage(t.path) ? null : (sheetView(t.path, read.value.body) ?? officeView(t.path, read.value.body));
12731274 if (looksLikeImage(t.path)) picture = read.value.body;
12741275 else if (book !== null && typeof book !== "string") sheet = book;
12751276 else if (typeof book === "string") note = book;
src/tui/screens/workspace_office.ts added +52
@@ -0,0 +1,52 @@
1 /**
2 * A .docx, .pptx or .ora in the terminal workspace, read the way the page
3 * reads it: the same `readDocument`, `readSlides` and `readStack`, drawn as a
4 * table of plain text. The same gate too: the name and the bytes must say so
5 * (`fileKind`), and a file that cannot be read answers its reason.
6 *
7 * `officeView(path, body)` answers a table, a sentence saying why not (the
8 * workspace shows that as its note), or `null` for a file that is not one of
9 * the three: the workspace keeps its binary note.
10 */
11
12 import { fileKind, HEAD_BYTES, RENDER_CAP } from "../../web/render/kinds.ts";
13 import { readDocument } from "../../web/render/docx.ts";
14 import { readSlides } from "../../web/render/pptx.ts";
15 import { readStack } from "../../web/render/ora.ts";
16 import type { Table } from "../../text/csv.ts";
17
18 const table = (header: string[], rows: string[][]): Table => ({
19 header,
20 rows,
21 more: 0,
22 delimiter: ",",
23 columns: header.length,
24 });
25
26 export function officeView(path: string, body: Uint8Array): Table | string | null {
27 const kind = fileKind(path, body.subarray(0, HEAD_BYTES));
28 if (kind !== "document" && kind !== "slides" && kind !== "layers") return null;
29 // The page's cap: past it the page keeps the source view, and so does this.
30 if (body.length > RENDER_CAP[kind])
31 return `This file is over ${RENDER_CAP[kind] / 1_000_000} MB, too large to open here. Download it instead.`;
outdated

Headings are printed once up front and again in the text, with an empty spacer row. Show the paragraphs once, with the heading level on the heading rows.

32 if (kind === "document") {
33 const doc = readDocument(body);
34 if ("error" in doc) return doc.error;
35 // Each paragraph once; a heading carries its level in place.
36 return table(["Level", "Text"], doc.paragraphs.map((p, i) => [doc.levels[i]! > 0 ? `H${doc.levels[i]}` : "", p]));
37 }
38 if (kind === "slides") {
39 const deck = readSlides(body);
40 if ("error" in deck) return deck.error;
41 return table(["Slide", "Title"], deck.slides.map((s) => [String(s.number), s.title === "" ? "(no title)" : s.title]));
42 }
43 if (kind === "layers") {
44 const stack = readStack(body);
45 if ("error" in stack) return stack.error;
46 return table(
47 ["Layer", "Size", "Visibility"],
48 stack.layers.map((l) => [l.name, l.size === null ? "—" : `${l.size.w} × ${l.size.h}`, l.visible ? "visible" : "hidden"]),
49 );
50 }
51 return null;
52 }
src/web/render/docx.ts added +117
@@ -0,0 +1,117 @@
1 /**
2 * A .docx as facts: its headings as an outline and the first paragraphs as
3 * text, read from `word/document.xml` only. No formatting is drawn, no
4 * macro, field or embedded object is touched, every string is escaped.
5 * Anything damaged, hostile or over a cap is `{ error }`: the source view.
6 */
7
8 import { escapeHtml, html, raw } from "../html.ts";
9 import { fileSize } from "../ui.ts";
10 import { elements, nameEnds, openPackage, tags, tidy, unescapeXml, xmlAttr } from "./office-zip.ts";
11 import type { Rendered, RenderInput } from "./renderers.ts";
12
13 export const PARAGRAPHS = 200;
14 export const HEADINGS = 500;
15
16 export interface Heading {
17 readonly level: number;
18 readonly text: string;
19 }
20
21 export interface Document {
22 readonly headings: readonly Heading[];
23 /** The first `PARAGRAPHS` non-empty paragraphs. */
24 readonly paragraphs: readonly string[];
25 /** Each shown paragraph's heading level, 1 to 6, or 0 for body text. */
26 readonly levels: readonly number[];
27 /** Non-empty paragraphs the document holds. */
28 readonly total: number;
29 }
30
31 /** 1 to 6 for a heading paragraph (style `Heading1`…, `Title`, or an outline level), else 0. */
32 function levelOf(props: string): number {
33 let id: string | undefined;
34 for (const a of tags(props, "w:pStyle", 1)) id = xmlAttr(a, "w:val");
35 const h = id === undefined ? null : /^heading\s*([1-9])$/i.exec(id);
36 if (h !== null) return Math.min(6, Number(h[1]));
37 if (id !== undefined && /^title$/i.test(id)) return 1;
38 let n = NaN;
39 for (const a of tags(props, "w:outlineLvl", 1)) n = Number(xmlAttr(a, "w:val"));
40 return Number.isInteger(n) && n >= 0 && n <= 8 ? Math.min(6, n + 1) : 0;
41 }
42
43 /**
44 * The text of a paragraph's runs: `<w:t>` text, `<w:tab/>` as a tab and
45 * `<w:br/>` as a space, in order, in one forward pass, at most ~4,000 characters.
46 */
47 function runText(body: string): string {
48 let text = "";
49 let i = 0;
outdated

Quadratic on an unclosed <w:p>: each opening scans to the end of the part, and a 16 MB part allows a million openings. Replace with a linear scan (indexOf for the open and close tags) and a cap on matches. Add a test.

50 while (text.length <= 4_000) {
51 const j = body.indexOf("<w:", i);
52 if (j < 0) break;
53 i = j + 3;
54 if (body.startsWith("t", i) && nameEnds(body.charCodeAt(i + 1))) {
55 const gt = body.indexOf(">", i);
56 if (gt < 0) break;
57 if (body.charCodeAt(gt - 1) === 0x2f) {
58 i = gt + 1;
59 continue;
60 }
61 const stop = body.indexOf("</w:t>", gt + 1);
62 if (stop < 0) break;
63 text += unescapeXml(body.slice(gt + 1, stop));
64 i = stop + 6;
65 } else if (body.startsWith("tab", i) && nameEnds(body.charCodeAt(i + 3))) text += "\t";
66 else if (body.startsWith("br", i) && nameEnds(body.charCodeAt(i + 2))) text += " ";
67 }
68 return text;
69 }
70
71 export function readDocument(zip: Uint8Array): Document | { readonly error: string } {
72 const pkg = openPackage(zip);
73 if ("error" in pkg) return pkg;
74 const xml = pkg.text("word/document.xml");
outdated

Principles 6 and 7: a card box around the outline and text. Use plain rows, no box; and one sentence for the page, not a disclaimer plus a summary. Same in pptx.ts and ora.ts.

75 if (xml === null || !xml.includes("<w:body")) return { error: "This file is damaged or not a Word document this page can read." };
76 const headings: Heading[] = [];
77 const paragraphs: string[] = [];
78 let total = 0;
79 const levels: number[] = [];
80 for (const p of elements(xml, "w:p")) {
81 const text = tidy(runText(p.body).replace(/\s+/g, " ").trim());
82 if (text === "") continue;
83 total++;
84 let props = "";
85 for (const pr of elements(p.body, "w:pPr", 1)) props = pr.body;
86 const level = levelOf(props);
87 if (level > 0 && headings.length < HEADINGS) headings.push({ level, text });
88 if (paragraphs.length < PARAGRAPHS) {
89 paragraphs.push(text);
90 levels.push(level);
91 }
92 }
93 return { headings, paragraphs, levels, total };
94 }
95
96 export function docx(input: RenderInput): Rendered {
97 const doc = readDocument(input.body);
98 if ("error" in doc) return { content: html``, lines: 0, fallback: doc.error };
99 const outline = doc.headings
100 .map((h) => `<tr><td class="num">H${h.level}</td><td>${escapeHtml(h.text)}</td></tr>`)
101 .join("");
102 const paras = doc.paragraphs.map((p) => `<tr><td>${escapeHtml(p)}</td></tr>`).join("");
103 const table = (label: string, head: string, rows: string): string =>
104 rows === ""
105 ? ""
106 : `<table class="table" data-dense aria-label="${label}"><thead><tr>${head}</tr></thead><tbody>${rows}</tbody></table>`;
107 const cut = doc.total > doc.paragraphs.length ? `<p class="muted small">The first ${PARAGRAPHS} paragraphs are shown.</p>` : "";
108 const content = html`${raw(table("Headings in this document", `<th scope="col">Level</th><th scope="col">Heading</th>`, outline))}
109 ${raw(table("Text of this document", `<th scope="col">Text</th>`, paras))}
110 ${raw(cut)}`;
111 const n = doc.total;
112 return {
113 content,
114 lines: 0,
115 summary: `${n.toLocaleString("en-US")} ${n === 1 ? "paragraph" : "paragraphs"} · ${doc.headings.length.toLocaleString("en-US")} ${doc.headings.length === 1 ? "heading" : "headings"} · ${fileSize(input.size)}`,
116 };
117 }
src/web/render/kinds.ts +20
@@ -25,6 +25,9 @@ export type FileKind =
2525 | "model3d"
2626 | "archive"
2727 | "sheet"
28 | "document"
29 | "slides"
30 | "layers"
2831 | "font"
2932 | "config"
3033 | "markdown"
@@ -75,6 +78,9 @@ export const RENDER_CAP: Readonly<Record<FileKind, number>> = {
7578 model3d: 100 * MB,
7679 archive: 50 * MB,
7780 sheet: 10 * MB,
81 document: 10 * MB,
82 slides: 10 * MB,
83 layers: 20 * MB,
7884 font: 20 * MB,
7985 config: 1 * MB,
8086 markdown: 1 * MB,
@@ -290,6 +296,16 @@ const isSheet = (path: string, head: Uint8Array): boolean =>
290296 */
291297 const isWorkbookHead = (head: string): boolean =>
292298 head.includes("xl/workbook.xml") || head.slice(30, 49) === "[Content_Types].xml";
299 /** Office-class zips named by extension; the bytes must open as a zip, else the reader refuses. */
300 const OFFICE: Readonly<Record<string, FileKind>> = {
301 docx: "document",
302 pptx: "slides",
303 ora: "layers",
304 };
305 const isOffice = (path: string, head: Uint8Array): FileKind | undefined => {
306 const kind = OFFICE[extension(path)];
307 return kind !== undefined && bytes(0x50, 0x4b, 0x03, 0x04)(head) ? kind : undefined;
308 };
293309
294310 /** An ASCII DXF opens on group code 0, then `SECTION`, whatever the file is named. */
295311 const isDxfHead = (head: Uint8Array): boolean =>
@@ -341,6 +357,8 @@ export function fileKind(path: string, head: Uint8Array): FileKind {
341357 const media = confirmed(path, head);
342358 if (media !== undefined) return media.kind;
343359 if (isSheet(path, head)) return "sheet";
360 const office = isOffice(path, head);
361 if (office !== undefined) return office;
344362 if (isArchive(path, head)) return "archive";
345363 if (looksBinary(head)) return "binary";
346364 if (isLcovName(path)) return "lcov";
@@ -374,6 +392,7 @@ export function kindByName(path: string): FileKind {
374392 if (isWorkflowName(path)) return "workflow";
375393 if (isComposeName(path)) return "compose";
376394 if (ext === "xlsx") return "sheet";
395 if (OFFICE[ext] !== undefined) return OFFICE[ext]!;
377396 return MEDIA[ext]?.kind ?? TEXTUAL[ext] ?? "text";
378397 }
379398
@@ -405,4 +424,5 @@ export const KNOWN_EXTENSIONS: readonly string[] = [
405424 "tar",
406425 "tgz",
407426 "xlsx",
427 ...Object.keys(OFFICE),
408428 ];
src/web/render/office-zip.ts added +137
@@ -0,0 +1,137 @@
1 /**
2 * The shared floor of the zip-of-XML formats (.docx, .pptx, .ora): a guard
3 * that refuses a zip bomb before any part is read, and a reader that spends
4 * one inflation budget across every part it hands out. Nothing is written
5 * or unpacked to disk; a part is inflated in memory and decoded as text.
6 */
7
8 import { listArchive, zipEntry } from "./archive.ts";
9
10 /** Entries a zip may declare before it is refused. */
11 export const MAX_ENTRIES = 1_000;
12 /** Unpacked bytes a zip may declare, and the most all parts read from it may inflate to. */
13 export const MAX_UNPACKED = 64 * 1024 * 1024;
14 /** The most one part may inflate to. */
15 export const PART_CAP = 16 * 1024 * 1024;
16
17 export interface Package {
18 /** Bytes of a named part, or null: absent, damaged, over `cap`, or past the zip's budget. */
19 readonly bytes: (name: string, cap?: number) => Uint8Array | null;
20 /** The same, as UTF-8 text. */
21 readonly text: (name: string, cap?: number) => string | null;
22 }
23
24 export interface Refused {
25 readonly error: string;
26 }
27
28 const UTF8 = new TextDecoder();
29
30 /** Open a zip for reading parts, or say in a sentence why not. */
31 export function openPackage(zip: Uint8Array): Package | Refused {
32 const list = listArchive(zip, { entries: 0 });
33 if ("error" in list) return { error: list.error };
34 if (list.format !== "zip") return { error: "This file is not a zip package." };
35 if (list.count > MAX_ENTRIES)
36 return { error: `This file holds more than ${MAX_ENTRIES.toLocaleString("en-US")} parts, so it is not opened here.` };
37 if (list.bytes > MAX_UNPACKED)
38 return { error: `This file unpacks to more than ${MAX_UNPACKED / 1024 / 1024} MB, so it is not opened here.` };
39 let budget = MAX_UNPACKED;
40 const bytes = (name: string, cap = PART_CAP): Uint8Array | null => {
41 if (name.includes("..") || name.startsWith("/")) return null;
42 const got = zipEntry(zip, name, Math.min(cap, budget));
43 if (got !== null) budget -= got.length;
44 return got;
45 };
46 return {
47 bytes,
48 text: (name, cap) => {
49 const b = bytes(name, cap);
50 return b === null ? null : UTF8.decode(b);
51 },
52 };
53 }
54
55 const ENTITY: Record<string, string> = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'" };
56
57 /** XML character data as text; an unknown entity stays as written. */
58 export const unescapeXml = (s: string): string =>
59 s.replace(/&(?:#(\d{1,7})|#x([0-9a-fA-F]{1,6})|(amp|lt|gt|quot|apos));/g, (m, d, x, n) => {
60 if (n) return ENTITY[n]!;
61 const code = d ? Number(d) : parseInt(x, 16);
62 return code > 0 && code <= 0x10ffff && !(code >= 0xd800 && code < 0xe000) ? String.fromCodePoint(code) : m;
63 });
64
65 /** An attribute of a tag's text, unescaped; `name` is a literal, never user text. */
66 export const xmlAttr = (tag: string, name: string): string | undefined => {
67 const m = new RegExp(`(?:^|\\s)${name}="([^"]*)"`).exec(tag);
68 return m === null ? undefined : unescapeXml(m[1]!);
69 };
70
71 /** Text with control characters dropped and length bounded, for a line of the page. */
72 export const tidy = (s: string, max = 2_000): string =>
73 // eslint-disable-next-line no-control-regex
74 s.replace(/[\u0000-\u0008\u000b-\u001f\u007f]/g, "").slice(0, max);
75
76 /** The most elements `elements` hands out from one part. */
77 export const ELEMENTS = 100_000;
78
79 /**
80 * Each `<tag …>body</tag>` (or `<tag …/>`, body "") in document order, in one
81 * forward pass with `indexOf`: the scan never moves back, so an unclosed tag
82 * repeated a million times costs one read of the part, not a million. An
83 * opening with no closing after it ends the walk. Nesting of the same tag is
84 * not followed; `<w:p>` and `<p:sp>` do not nest in the parts read here.
85 * `tag` is a literal, never user text.
86 */
87 export function* elements(xml: string, tag: string, cap = ELEMENTS): Generator<{ readonly attrs: string; readonly body: string }> {
88 const open = `<${tag}`;
89 const close = `</${tag}>`;
90 let at = 0;
91 for (let n = 0; n < cap; ) {
92 const start = xml.indexOf(open, at);
93 if (start < 0) return;
94 // `<w:p` must not match `<w:pPr`: the name ends at space, `>` or `/`.
95 if (!nameEnds(xml.charCodeAt(start + open.length))) {
96 at = start + open.length;
97 continue;
98 }
99 const end = xml.indexOf(">", start + open.length);
100 if (end < 0) return;
101 n++;
102 const attrs = xml.slice(start + open.length, end);
103 if (xml.charCodeAt(end - 1) === 0x2f) {
104 at = end + 1;
105 yield { attrs: attrs.slice(0, -1), body: "" };
106 continue;
107 }
108 const stop = xml.indexOf(close, end + 1);
109 if (stop < 0) return;
110 at = stop + close.length;
111 yield { attrs, body: xml.slice(end + 1, stop) };
112 }
113 }
114
115 /**
116 * The attributes of each opening `<tag …>` or `<tag …/>`, in one forward pass;
117 * bodies and closing tags are not looked for. `tag` is a literal.
118 */
119 export function* tags(xml: string, tag: string, cap = ELEMENTS): Generator<string> {
120 const open = `<${tag}`;
121 let at = 0;
122 for (let n = 0; n < cap; ) {
123 const start = xml.indexOf(open, at);
124 if (start < 0) return;
125 at = start + open.length;
126 if (!nameEnds(xml.charCodeAt(at))) continue;
127 const end = xml.indexOf(">", at);
128 if (end < 0) return;
129 n++;
130 yield xml.slice(at, xml.charCodeAt(end - 1) === 0x2f ? end - 1 : end);
131 at = end + 1;
132 }
133 }
134
135 /** True when the character after `<name` ends the name: space, `>` or `/`. */
136 export const nameEnds = (c: number): boolean =>
137 c === 0x3e || c === 0x2f || c === 0x20 || c === 0x09 || c === 0x0a || c === 0x0d;
src/web/render/ora.ts added +105
@@ -0,0 +1,105 @@
1 /**
2 * An OpenRaster (.ora) image as facts: its layers (name, size, visibility)
3 * from `stack.xml` and its merged preview. The preview is shown only when
4 * `mergedimage.png` really is a PNG under a size cap, inlined as a `data:`
5 * image with the one media type this file hardcodes; no script, no other
6 * type. A layer's size is the pixel size in its own PNG header.
7 */
8
9 import { escapeHtml, html, raw } from "../html.ts";
10 import { fileSize } from "../ui.ts";
11 import { openPackage, tags, tidy, xmlAttr } from "./office-zip.ts";
12 import type { Rendered, RenderInput } from "./renderers.ts";
13
14 export const LAYERS = 200;
15 /** The largest merged preview inlined. */
16 export const PREVIEW_CAP = 4 * 1024 * 1024;
17 /** The most one layer part may inflate to. */
18 export const LAYER_CAP = 16 * 1024 * 1024;
19
20 export interface Layer {
21 readonly name: string;
22 /** Pixels, from the layer's PNG header; null when it could not be read. */
23 readonly size: { readonly w: number; readonly h: number } | null;
24 readonly visible: boolean;
25 }
26
27 export interface Stack {
28 readonly width: number | null;
29 readonly height: number | null;
30 /** Top layer first, as the file lists them. */
31 readonly layers: readonly Layer[];
32 readonly total: number;
33 /** A `data:image/png` address, or null. */
34 readonly preview: string | null;
35 }
36
37 const PNG = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
38 const isPng = (b: Uint8Array): boolean => b.length >= 24 && PNG.every((v, i) => b[i] === v);
39 const pixels = (b: Uint8Array): { w: number; h: number } | null => {
40 if (!isPng(b)) return null;
41 const v = new DataView(b.buffer, b.byteOffset, b.byteLength);
42 const w = v.getUint32(16);
43 const h = v.getUint32(20);
44 return w > 0 && h > 0 ? { w, h } : null;
45 };
46 const dim = (s: string | undefined): number | null => {
47 const n = Number(s);
48 return Number.isInteger(n) && n > 0 && n < 1_000_000 ? n : null;
49 };
50
51 export function readStack(zip: Uint8Array): Stack | { readonly error: string } {
52 const pkg = openPackage(zip);
53 if ("error" in pkg) return pkg;
54 const xml = pkg.text("stack.xml", 1024 * 1024);
55 let image: string | null = null;
56 for (const a of xml === null ? [] : tags(xml, "image", 1)) image = a;
57 if (xml === null || image === null) return { error: "This file is damaged or not an OpenRaster image this page can read." };
58 const layers: Layer[] = [];
59 let total = 0;
60 for (const a of tags(xml, "layer")) {
61 total++;
62 if (layers.length >= LAYERS) continue;
63 const src = xmlAttr(a, "src");
64 const part = src === undefined ? null : pkg.bytes(src, LAYER_CAP);
65 layers.push({
66 name: tidy(xmlAttr(a, "name") ?? "", 200) || "(unnamed)",
67 size: part === null ? null : pixels(part),
68 visible: (xmlAttr(a, "visibility") ?? "visible") !== "hidden",
69 });
70 }
71 const merged = pkg.bytes("mergedimage.png", PREVIEW_CAP);
72 const preview = merged !== null && isPng(merged) ? `data:image/png;base64,${Buffer.from(merged).toString("base64")}` : null;
73 return { width: dim(xmlAttr(image, "w")), height: dim(xmlAttr(image, "h")), layers, total, preview };
74 }
75
76 export function ora(input: RenderInput): Rendered {
77 const stack = readStack(input.body);
78 if ("error" in stack) return { content: html``, lines: 0, fallback: stack.error };
79 const rows = stack.layers
80 .map(
81 (l) =>
82 `<tr><td>${escapeHtml(l.name)}</td><td class="num">${l.size === null ? "—" : `${l.size.w} × ${l.size.h}`}</td><td>${l.visible ? "visible" : '<span class="muted">hidden</span>'}</td></tr>`,
83 )
84 .join("");
85 const w = stack.width;
86 const h = stack.height;
87 const box = w !== null && h !== null ? ` width="${w}" height="${h}"` : "";
88 const picture =
89 stack.preview === null
90 ? `<p class="muted small">No merged preview in this file.</p>`
91 : `<img class="nb-img" src="${stack.preview}" alt="Merged preview of ${escapeHtml(input.path.slice(input.path.lastIndexOf("/") + 1))}"${box}>`;
92 const more = stack.total > stack.layers.length ? `<p class="muted small">The first ${LAYERS} layers are shown.</p>` : "";
93 const content = html`${raw(picture)}
94 <table class="table" data-dense aria-label="Layers, top first">
95 <thead><tr><th scope="col">Layer</th><th scope="col">Size</th><th scope="col">Visibility</th></tr></thead>
96 <tbody>${raw(rows)}</tbody>
97 </table>
98 ${raw(more)}`;
99 const n = stack.total;
100 return {
101 content,
102 lines: 0,
103 summary: `${n.toLocaleString("en-US")} ${n === 1 ? "layer" : "layers"}${w !== null && h !== null ? ` · ${w} × ${h}` : ""} · ${fileSize(input.size)}`,
104 };
105 }
src/web/render/pptx.ts added +77
@@ -0,0 +1,77 @@
1 /**
2 * A .pptx as facts: each slide's number and title, in presentation order,
3 * from `ppt/presentation.xml` and the slides it names. Titles are the text
4 * of a title placeholder; nothing is drawn, run or followed.
5 */
6
7 import { escapeHtml, html, raw } from "../html.ts";
8 import { fileSize } from "../ui.ts";
9 import { elements, openPackage, tags, tidy, unescapeXml, xmlAttr } from "./office-zip.ts";
10 import type { Rendered, RenderInput } from "./renderers.ts";
11
12 export const SLIDES = 500;
13 /** The most one slide part may inflate to. */
14 export const SLIDE_CAP = 2 * 1024 * 1024;
15
16 export interface Slide {
17 readonly number: number;
18 /** Empty when the slide has no title placeholder text. */
19 readonly title: string;
20 }
21
22 const titleOf = (xml: string): string => {
23 for (const sp of elements(xml, "p:sp", 5_000)) {
outdated

Same lazy [\s\S]*? scan over <p:sp>, up to 500 slides of 2 MB each. Use a linear scan with a match cap.

24 let title = false;
25 for (const a of tags(sp.body, "p:ph", 1)) title = /^(?:title|ctrTitle)$/.test(xmlAttr(a, "type") ?? "");
26 if (!title) continue;
27 let text = "";
28 for (const t of elements(sp.body, "a:t", 2_000)) text += unescapeXml(t.body);
29 return tidy(text.replace(/\s+/g, " ").trim(), 300);
30 }
31 return "";
32 };
33
34 export function readSlides(zip: Uint8Array): { readonly slides: readonly Slide[]; readonly total: number } | { readonly error: string } {
35 const bad = { error: "This file is damaged or not a presentation this page can read." };
36 const pkg = openPackage(zip);
37 if ("error" in pkg) return pkg;
38 const pres = pkg.text("ppt/presentation.xml");
39 const rels = pkg.text("ppt/_rels/presentation.xml.rels");
40 if (pres === null || rels === null) return bad;
41 const target = new Map<string, string>();
42 for (const a of tags(rels, "Relationship", 10_000)) {
43 const id = xmlAttr(a, "Id");
44 const to = xmlAttr(a, "Target");
45 if (id !== undefined && to !== undefined) target.set(id, to.startsWith("/") ? to.slice(1) : `ppt/${to}`);
46 }
47 const order: string[] = [];
48 for (const a of tags(pres, "p:sldId", 10_000)) {
49 const to = target.get(xmlAttr(a, "r:id") ?? "");
50 if (to !== undefined) order.push(to);
51 }
52 if (order.length === 0) return bad;
53 const slides = order.slice(0, SLIDES).map((path, i) => {
54 const xml = pkg.text(path, SLIDE_CAP);
55 return { number: i + 1, title: xml === null ? "" : titleOf(xml) };
56 });
57 return { slides, total: order.length };
58 }
59
60 export function pptx(input: RenderInput): Rendered {
61 const deck = readSlides(input.body);
62 if ("error" in deck) return { content: html``, lines: 0, fallback: deck.error };
63 const rows = deck.slides
64 .map((s) => `<tr><td class="num">${s.number}</td><td>${s.title === "" ? '<span class="muted">(no title)</span>' : escapeHtml(s.title)}</td></tr>`)
65 .join("");
66 const more = deck.total > deck.slides.length ? `<p class="muted small">The first ${SLIDES} slides are shown.</p>` : "";
67 const content = html`<table class="table" data-dense aria-label="Slides in this presentation">
68 <thead><tr><th scope="col">Slide</th><th scope="col">Title</th></tr></thead>
69 <tbody>${raw(rows)}</tbody>
70 </table>
71 ${raw(more)}`;
72 return {
73 content,
74 lines: 0,
75 summary: `${deck.total.toLocaleString("en-US")} ${deck.total === 1 ? "slide" : "slides"} · ${fileSize(input.size)}`,
76 };
77 }
src/web/render/renderers.ts +6
@@ -42,6 +42,9 @@ import { openapi } from "./openapi.ts";
4242 import { lcov } from "./lcov.ts";
4343 import { sarif } from "./sarif.ts";
4444 import { sheet } from "./xlsx.ts";
45 import { docx } from "./docx.ts";
46 import { pptx } from "./pptx.ts";
47 import { ora } from "./ora.ts";
4548 import { sqlite } from "./sqlite.ts";
4649 import { subtitles } from "./subtitles.ts";
4750 import { video } from "./video.ts";
@@ -125,6 +128,9 @@ export const RENDERERS: Partial<Readonly<Record<FileKind, Renderer>>> = {
125128 mail: eml,
126129 archive,
127130 sheet,
131 document: docx,
132 slides: pptx,
133 layers: ora,
128134 font,
129135 };
130136
test/render-kinds.test.ts +3
@@ -64,6 +64,9 @@ const SAMPLES: Record<string, [Uint8Array, FileKind, string]> = {
6464 tar: [enc("x".repeat(257) + "ustar\0"), "archive", "application/octet-stream"],
6565 tgz: [of(0x1f, 0x8b, 8), "archive", "application/octet-stream"],
6666 xlsx: [enc("PK\x03\x04....xl/workbook.xml"), "sheet", "application/octet-stream"],
67 docx: [of(0x50, 0x4b, 3, 4, 0), "document", "application/octet-stream"],
68 pptx: [of(0x50, 0x4b, 3, 4, 0), "slides", "application/octet-stream"],
69 ora: [of(0x50, 0x4b, 3, 4, 0), "layers", "application/octet-stream"],
6770 ipynb: [enc("{}"), "notebook", "application/octet-stream"],
6871 md: [enc("# a\n"), "markdown", "application/octet-stream"],
6972 markdown: [enc("# a\n"), "markdown", "application/octet-stream"],
test/render-office.test.ts added +226
@@ -0,0 +1,226 @@
1 /** .docx, .pptx and .ora as facts: registry, readers, pages, and the zip-bomb caps. */
2 import { describe, expect, test } from "bun:test";
3 import { zip } from "./helpers/zip.ts";
4 import { fileKind, mediaType } from "../src/web/render/kinds.ts";
5 import { RENDERERS, rendererFor } from "../src/web/render/renderers.ts";
6 import { MAX_ENTRIES, MAX_UNPACKED, elements, openPackage } from "../src/web/render/office-zip.ts";
7 import { PARAGRAPHS, docx, readDocument } from "../src/web/render/docx.ts";
8 import { pptx, readSlides } from "../src/web/render/pptx.ts";
9 import { ora, readStack } from "../src/web/render/ora.ts";
10 import { content as CONTENT_CSS } from "../src/web/style/content.ts";
11
12 const draw = (f: typeof docx, path: string, body: Uint8Array) =>
13 f({ path, size: body.length, body, media: "/m", download: "/d" });
14
15 const para = (text: string, style?: string) =>
16 `<w:p>${style ? `<w:pPr><w:pStyle w:val="${style}"/></w:pPr>` : ""}<w:r><w:t xml:space="preserve">${text}</w:t></w:r></w:p>`;
17 const doc = (...ps: string[]) => zip({ "[Content_Types].xml": "<Types/>", "word/document.xml": `<w:document><w:body>${ps.join("")}</w:body></w:document>` });
18
19 describe("office kinds", () => {
20 test("the three are their own kinds, served as octet-stream, and need zip bytes", () => {
21 const pk = new Uint8Array([0x50, 0x4b, 3, 4, 0]);
22 for (const [name, kind] of [["a.docx", "document"], ["a.pptx", "slides"], ["a.ora", "layers"]] as const) {
23 expect(fileKind(name, pk)).toBe(kind);
24 expect(mediaType(name, pk)).toBe("application/octet-stream");
25 expect(fileKind(name, new TextEncoder().encode("plain"))).toBe("text");
26 }
27 expect(RENDERERS.document).toBe(docx);
28 expect(RENDERERS.slides).toBe(pptx);
29 expect(RENDERERS.layers).toBe(ora);
30 });
31
32 test("the blob page picks the renderer for a docx", () => {
33 const body = doc(para("Hi"));
34 const got = rendererFor("a.docx", { kind: "binary", body, size: body.length });
35 expect(got?.render).toBe(docx);
36 });
37 });
38
39 describe("docx", () => {
40 test("headings make an outline and paragraphs show as escaped text", () => {
41 const body = doc(
42 para("Intro", "Heading1"),
43 para("Some &lt;b&gt;text&lt;/b&gt; &amp; more"),
44 para("Detail", "Heading2"),
45 "<w:p/>",
46 `<w:p><w:pPr><w:outlineLvl w:val="2"/></w:pPr><w:r><w:t>Deep</w:t></w:r><w:r><w:tab/><w:t>x</w:t></w:r></w:p>`,
47 );
48 const d = readDocument(body);
49 if ("error" in d) throw new Error(d.error);
50 expect(d.headings).toEqual([
51 { level: 1, text: "Intro" },
52 { level: 2, text: "Detail" },
53 { level: 3, text: "Deep x" },
54 ]);
55 expect(d.total).toBe(4);
56 const page = draw(docx, "a.docx", body);
57 const text = String(page.content);
58 expect(text).toContain("Headings in this document");
59 expect(text).toContain("Some &lt;b&gt;text&lt;/b&gt; &amp; more");
60 expect(text).not.toContain("<b>text");
61 expect(page.summary).toContain("4 paragraphs · 3 headings");
62 });
63
64 test("only the first 200 paragraphs are shown, and the page says so", () => {
65 const body = doc(...Array.from({ length: PARAGRAPHS + 5 }, (_, i) => para(`p${i}`)));
66 const d = readDocument(body);
67 if ("error" in d) throw new Error(d.error);
68 expect(d.paragraphs.length).toBe(PARAGRAPHS);
69 expect(d.total).toBe(PARAGRAPHS + 5);
70 expect(String(draw(docx, "a.docx", body).content)).toContain("first 200 paragraphs");
71 });
72
73 test("a malformed file falls back to the source view, never throws", () => {
74 for (const body of [zip({ "a.txt": "hi" }), zip({ "word/document.xml": "<nope/>" }), new Uint8Array([0x50, 0x4b, 3, 4, 1, 2, 3]), new Uint8Array(0)]) {
75 const page = draw(docx, "a.docx", body);
76 expect(page.fallback).toBeTruthy();
77 }
78 });
79 });
80
81 describe("pptx", () => {
82 const deck = zip({
83 "ppt/presentation.xml": `<p:presentation><p:sldIdLst><p:sldId id="256" r:id="rId2"/><p:sldId id="257" r:id="rId1"/><p:sldId id="258" r:id="rId3"/></p:sldIdLst></p:presentation>`,
84 "ppt/_rels/presentation.xml.rels": `<Relationships><Relationship Id="rId1" Target="slides/slide1.xml"/><Relationship Id="rId2" Target="slides/slide2.xml"/><Relationship Id="rId3" Target="slides/slide3.xml"/></Relationships>`,
85 "ppt/slides/slide1.xml": `<p:sld><p:sp><p:nvSpPr><p:nvPr><p:ph type="body"/></p:nvPr></p:nvSpPr><p:txBody><a:t>not a title</a:t></p:txBody></p:sp></p:sld>`,
86 "ppt/slides/slide2.xml": `<p:sld><p:sp><p:nvSpPr><p:nvPr><p:ph type="ctrTitle"/></p:nvPr></p:nvSpPr><p:txBody><a:p><a:r><a:t>Q3 &lt;Plan&gt;</a:t></a:r></a:p></p:txBody></p:sp></p:sld>`,
87 "ppt/slides/slide3.xml": `<p:sld><p:sp><p:nvSpPr><p:nvPr><p:ph type="title"/></p:nvPr></p:nvSpPr><p:txBody><a:r><a:t>Wrap</a:t></a:r><a:r><a:t>-up</a:t></a:r></p:txBody></p:sp></p:sld>`,
88 });
89
90 test("each slide has its number and title, in presentation order", () => {
91 const d = readSlides(deck);
92 if ("error" in d) throw new Error(d.error);
93 expect(d.slides).toEqual([
94 { number: 1, title: "Q3 <Plan>" },
95 { number: 2, title: "" },
96 { number: 3, title: "Wrap-up" },
97 ]);
98 const text = String(draw(pptx, "a.pptx", deck).content);
99 expect(text).toContain("Q3 &lt;Plan&gt;");
100 expect(text).toContain("(no title)");
101 expect(draw(pptx, "a.pptx", deck).summary).toContain("3 slides");
102 });
103
104 test("a malformed file falls back", () => {
105 expect(draw(pptx, "a.pptx", zip({ "a.txt": "x" })).fallback).toBeTruthy();
106 expect(draw(pptx, "a.pptx", new Uint8Array(10)).fallback).toBeTruthy();
107 });
108 });
109
110 describe("ora", () => {
111 const png = (w: number, h: number): Uint8Array => {
112 const b = new Uint8Array(33);
113 b.set([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 13, 0x49, 0x48, 0x44, 0x52]);
114 new DataView(b.buffer).setUint32(16, w);
115 new DataView(b.buffer).setUint32(20, h);
116 return b;
117 };
118 const stack = `<image w="64" h="32"><stack><layer name="Top &amp; &lt;i&gt;" src="data/top.png" visibility="hidden"/><stack><layer name="Base" src="data/base.png"/></stack></stack></image>`;
119 const file = (extra: Record<string, string | Uint8Array> = {}) =>
120 zip({ mimetype: "image/openraster", "stack.xml": stack, "data/top.png": png(10, 20), "data/base.png": png(64, 32), "mergedimage.png": png(64, 32), ...extra });
121
122 test("layers show name, size and visibility, with the merged preview", () => {
123 const s = readStack(file());
124 if ("error" in s) throw new Error(s.error);
125 expect(s.layers).toEqual([
126 { name: "Top & <i>", size: { w: 10, h: 20 }, visible: false },
127 { name: "Base", size: { w: 64, h: 32 }, visible: true },
128 ]);
129 const page = draw(ora, "a.ora", file());
130 const text = String(page.content);
131 expect(text).toContain("Top &amp; &lt;i&gt;");
132 expect(text).toContain("10 × 20");
133 expect(text).toContain("hidden");
134 expect(text).toContain('src="data:image/png;base64,');
135 expect(page.summary).toContain("2 layers · 64 × 32");
136 });
137
138 test("a merged image that is not a PNG is never inlined", () => {
139 const text = String(draw(ora, "a.ora", file({ "mergedimage.png": "<svg onload=alert(1)>" })).content);
140 expect(text).not.toContain("data:");
141 expect(text).toContain("No merged preview");
142 });
143
144 test("a malformed file falls back", () => {
145 expect(draw(ora, "a.ora", zip({ "stack.xml": "<nope/>" })).fallback).toBeTruthy();
146 expect(draw(ora, "a.ora", zip({ "a.txt": "x" })).fallback).toBeTruthy();
147 });
148 });
149
150 describe("zip bombs", () => {
151 test("a package with more entries than the cap is refused", () => {
152 const parts: Record<string, string> = { "word/document.xml": "<w:body/>" };
153 for (let i = 0; i < MAX_ENTRIES; i++) parts[`f${i}`] = "";
154 const r = openPackage(zip(parts));
155 expect("error" in r && r.error).toContain("parts");
156 const page = draw(docx, "a.docx", zip(parts));
157 expect(page.fallback).toContain("parts");
158 expect("error" in openPackage(zip({ a: "", b: "" }))).toBe(false);
159 });
160
161 test("a package declaring more unpacked bytes than the cap is refused", () => {
162 const bomb = zip({ "word/document.xml": "<w:body/>" }, MAX_UNPACKED + 1);
163 const r = openPackage(bomb);
164 expect("error" in r && r.error).toContain("unpacks to more than");
165 expect(draw(docx, "a.docx", bomb).fallback).toContain("unpacks");
166 });
167
168 test("a part that inflates past the cap, whatever it declares, is not read", () => {
169 const big = "<w:body>" + "a".repeat(20 * 1024 * 1024);
170 const pkg = openPackage(zip({ "word/document.xml": big }, 100));
171 if ("error" in pkg) throw new Error(pkg.error);
172 expect(pkg.text("word/document.xml")).toBeNull();
173 expect(draw(docx, "a.docx", zip({ "word/document.xml": big }, 100)).fallback).toBeTruthy();
174 });
175 });
176
177 describe("hostile XML is read in one pass", () => {
178 // A million unclosed openings: a lazy regex to a closing tag rescans the
179 // rest of the part from each one; the linear walk reads it once.
180 const flood = (open: string, n = 1_000_000) => open.repeat(n);
181
182 test("a .docx of unclosed <w:p> openings finishes quickly", () => {
183 const body = zip({ "[Content_Types].xml": "<Types/>", "word/document.xml": `<w:document><w:body>${para("First")}${flood("<w:p>")}</w:body></w:document>` });
184 const t0 = performance.now();
185 const d = readDocument(body);
186 expect(performance.now() - t0).toBeLessThan(1_000);
187 if ("error" in d) throw new Error(d.error);
188 expect(d.paragraphs).toEqual(["First"]);
189 });
190
191 test("a paragraph of unclosed <w:t> openings, and of tags with no >, finishes quickly", () => {
192 for (const inner of [flood("<w:t>", 500_000), flood("<w:pPr><w:pStyle ", 300_000)]) {
193 const body = zip({ "[Content_Types].xml": "<Types/>", "word/document.xml": `<w:document><w:body><w:p>${inner}</w:p></w:body></w:document>` });
194 const t0 = performance.now();
195 expect("error" in readDocument(body)).toBe(false);
196 expect(performance.now() - t0).toBeLessThan(1_000);
197 }
198 });
199
200 test("a .pptx slide of unclosed <p:sp> and <p:ph openings finishes quickly", () => {
201 for (const slide of [flood("<p:sp>", 300_000), `<p:sp>${flood("<p:ph ", 300_000)}</p:sp>`]) {
202 const body = zip({
203 "[Content_Types].xml": "<Types/>",
204 "ppt/presentation.xml": `<p:presentation><p:sldIdLst><p:sldId id="256" r:id="rId1"/></p:sldIdLst></p:presentation>`,
205 "ppt/_rels/presentation.xml.rels": `<Relationships><Relationship Id="rId1" Target="slides/slide1.xml"/></Relationships>`,
206 "ppt/slides/slide1.xml": `<p:sld>${slide}</p:sld>`,
207 });
208 const t0 = performance.now();
209 const d = readSlides(body);
210 expect(performance.now() - t0).toBeLessThan(1_000);
211 if ("error" in d) throw new Error(d.error);
212 expect(d.slides).toEqual([{ number: 1, title: "" }]);
213 }
214 });
215
216 test("the walk stops at its cap and does not take <w:pPr> for <w:p>", () => {
217 const got = [...elements(`<w:p>a</w:p><w:pPr>x</w:pPr><w:p/>${"<w:p>b</w:p>".repeat(10)}`, "w:p", 3)];
218 expect(got.map((e) => e.body)).toEqual(["a", "", "b"]);
219 });
220 });
221
222 describe("a large canvas does not scroll the phone sideways", () => {
223 test("the merged preview is an .nb-img, which is held to the column", () => {
224 expect(CONTENT_CSS).toMatch(/\.nb-img\s*\{[^}]*max-width:\s*100%[^}]*height:\s*auto/);
225 });
226 });
test/tui-office.test.ts added +70
@@ -0,0 +1,70 @@
1 /** The terminal reads .docx, .pptx and .ora with the page's readers and the page's gate. */
2 import { describe, expect, test } from "bun:test";
3 import { zip } from "./helpers/zip.ts";
4 import { RENDER_CAP } from "../src/web/render/kinds.ts";
5 import { officeView } from "../src/tui/screens/workspace_office.ts";
6 import { docx } from "../src/web/render/docx.ts";
7 import { pptx } from "../src/web/render/pptx.ts";
8 import { ora } from "../src/web/render/ora.ts";
9
10 const para = (t: string, style?: string) =>
11 `<w:p>${style ? `<w:pPr><w:pStyle w:val="${style}"/></w:pPr>` : ""}<w:r><w:t>${t}</w:t></w:r></w:p>`;
12 const word = zip({
13 "[Content_Types].xml": "<Types/>",
14 "word/document.xml": `<w:document><w:body>${para("Intro", "Heading1")}${para("Body text")}</w:body></w:document>`,
15 });
16 const deck = zip({
17 "[Content_Types].xml": "<Types/>",
18 "ppt/presentation.xml": `<p:presentation><p:sldIdLst><p:sldId id="256" r:id="rId1"/></p:sldIdLst></p:presentation>`,
19 "ppt/_rels/presentation.xml.rels": `<Relationships><Relationship Id="rId1" Target="slides/slide1.xml"/></Relationships>`,
20 "ppt/slides/slide1.xml": `<p:sld><p:sp><p:nvSpPr><p:nvPr><p:ph type="title"/></p:nvPr></p:nvSpPr><p:txBody><a:p><a:r><a:t>Hello</a:t></a:r></a:p></p:txBody></p:sp></p:sld>`,
21 });
22 const image = zip({
23 "[Content_Types].xml": "<Types/>",
24 "stack.xml": `<image w="4" h="4"><stack><layer name="Ink" src="data/a.png" visibility="hidden"/></stack></image>`,
25 });
26
27 describe("the terminal's office view", () => {
28 test("a .docx is its text once, each heading marked in place with its level", () => {
29 const t = officeView("a.docx", word);
30 if (typeof t === "string" || t === null) throw new Error("no table");
31 expect(t.rows).toEqual([["H1", "Intro"], ["", "Body text"]]);
32 });
33 test("past the page's cap the file is not read, and the note says why", () => {
34 const big = new Uint8Array(RENDER_CAP.document + 1);
35 big.set(word);
36 const t = officeView("a.docx", big);
37 expect(typeof t).toBe("string");
38 expect(String(t)).toContain("too large");
39 });
40 test("a .pptx is its slide titles", () => {
41 const t = officeView("a.pptx", deck);
42 if (typeof t === "string" || t === null) throw new Error("no table");
43 expect(t.rows).toEqual([["1", "Hello"]]);
44 });
45 test("an .ora is its layers", () => {
46 const t = officeView("a.ora", image);
47 if (typeof t === "string" || t === null) throw new Error("no table");
48 expect(t.rows).toEqual([["Ink", "—", "hidden"]]);
49 });
50 test("the gate is the page's: a name with text bytes, or another name, is not read", () => {
51 expect(officeView("a.docx", new TextEncoder().encode("plain"))).toBeNull();
52 expect(officeView("a.zip", word)).toBeNull();
53 });
54 test("a damaged file answers its reason", () => {
55 const bad = zip({ "[Content_Types].xml": "<Types/>" });
56 expect(typeof officeView("a.docx", bad)).toBe("string");
57 });
58 });
59
60 describe("the pages are rows, not a box", () => {
61 const draw = (f: typeof docx, path: string, body: Uint8Array) =>
62 f({ path, size: body.length, body, media: "/m", download: "/d" }).content.toString();
63 test("no card and no heading around the facts", () => {
64 for (const out of [draw(docx, "a.docx", word), draw(pptx, "a.pptx", deck), draw(ora, "a.ora", image)]) {
65 expect(out).not.toContain('class="card"');
66 expect(out).not.toContain("<h3");
67 expect(out).not.toContain("not drawn");
68 }
69 });
70 });
test/tui-workspace-formats.test.ts +20
@@ -714,4 +714,24 @@ describe("the terminal workspace, by file type", () => {
714714 h.stop();
715715 }
716716 });
717 test("a .pptx opens as its slide titles", async () => {
718 const made = await createRepo(forge, alice, { name: "decks", visibility: "public", description: "slides" });
719 if (!made.ok) throw new Error("repo failed");
720 const deck = zip({
721 "[Content_Types].xml": "<Types/>",
722 "ppt/presentation.xml": `<p:presentation><p:sldIdLst><p:sldId id="256" r:id="rId1"/></p:sldIdLst></p:presentation>`,
723 "ppt/_rels/presentation.xml.rels": `<Relationships><Relationship Id="rId1" Target="slides/slide1.xml"/></Relationships>`,
724 "ppt/slides/slide1.xml": `<p:sld><p:sp><p:nvSpPr><p:nvPr><p:ph type="title"/></p:nvPr></p:nvSpPr><p:txBody><a:p><a:r><a:t>Launch plan</a:t></a:r></a:p></p:txBody></p:sp></p:sld>`,
725 });
726 await commitToBranch(forge, alice, made.value, { message: "seed", files: [{ path: "talk.pptx", content: deck }] });
727 const h = boot();
728 try {
729 await h.settle();
730 await open(h, "decks");
731 await openFile(h, "talk.pptx");
732 expect(h.text()).toContain("Launch plan");
733 } finally {
734 h.stop();
735 }
736 });
717737 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs