Public: anyone can read this; the crew can change it.
Three more file kinds open readable: .ini/.properties, .rtf and .xml #14179
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14044-three-more-file-kinds-open-readable-ini → main 7 commits fork 5 threads · 5 unresolved
docs/design/areas/render.md +4
| @@ -80,3 +80,7 @@ A .zip, .tar or .tar.gz is drawn by `src/web/render/archive-view.ts` as the exis | ||
| 80 | 80 | ## Diagrams (.drawio, .excalidraw, .mmd) |
| 81 | 81 | |
| 82 | 82 | `src/web/render/diagram.ts` draws a draw.io or Excalidraw file as one inline SVG in `.diagram-view` (same rules as `.dxf-view`: `currentColor`, no accent), inside `.diagram-scroll`, a frame that never lets the SVG exceed it (no fixed width, so no rule fixes a width over 390 px). A Mermaid file shows its diagram type and its source in a native `<details>` (`.diagram-source`, its summary a 44px tap target), folded, whose `pre` scrolls sideways inside its own box. |
| 83 | ||
| 84 | ## XML file (.xml) | |
| 85 | ||
| 86 | `src/web/render/xml.ts` draws the element tree as nested `ul.xml-tree` (`src/web/style/content.ts`): no bullets, 8px of indent per level with a `--border` rule beside it, so the 24 levels it draws at most still fit 360px, and names, attributes and text wrap (`overflow-wrap: anywhere`) instead of scrolling the page. Tokens only, both themes. An `.rtf` file's text is plain `p` inside the existing `.prose`; an `.ini` is the existing `.table`; neither adds a class. |
docs/features/README.md +1
| @@ -305,6 +305,7 @@ Read with the guide page [Your apps](../guide-your-apps.md). | ||
| 305 | 305 | - [](file-workflow.md) |
| 306 | 306 | - [](file-graphql.md) |
| 307 | 307 | - [](file-mbox-proto.md) |
| 308 | - [](file-ini-rtf-xml.md) | |
| 308 | 309 | - [](file-junit.md) |
| 309 | 310 | - [](file-eml.md) |
| 310 | 311 | - [](file-lcov.md) |
docs/features/file-ini-rtf-xml.md added +14
| @@ -0,0 +1,14 @@ | ||
| 1 | # `.ini`/`.properties`, `.rtf` and `.xml` files open readable | |
| 2 | ||
| 3 | Each opens as a drawn view instead of raw text. Source is one tap away (`?view=source`). | |
| 4 | ||
| 5 | - **`.ini` and `.properties`:** one table per `[section]` with a key and value per row; comments dropped, properties continuation lines joined. A value whose key ends in key, secret, token or password is shown as `•••`. 500 settings, then `more not shown`. A file with no settings, or over 1 MB, opens as text. | |
| 6 | - **`.rtf`:** its text as paragraphs, formatting dropped. Font and colour tables, pictures, headers, footers and other destinations are skipped; nothing embedded is fetched or run. 100,000 characters, then a note. A `.rtf` not starting with `{\rtf` is plain text. Over 5 MB opens as text. | |
| 7 | - **`.xml`:** the element tree with attributes and leaf text. Prolog, comments, processing instructions and the doctype are skipped and no entity is expanded (only the five named ones and numeric references are decoded). 1,000 elements, 24 levels deep, then `more elements not shown`; at most 100,000 elements are read, a file with more is counted as that many. Indented with a small step per level and wrapped, so a deep tree fits a phone. JUnit, OPML and feed files keep their own views. Over 5 MB opens as text. | |
| 8 | - A file's bytes must agree with its name; the media route still sends all three as `application/octet-stream`. Every value is escaped, drawn on the server; no script, no dependency. | |
| 9 | ||
| 10 | `.conf` stays a config file. | |
| 11 | ||
| 12 | **In the terminal** (the file browser, `src/tui/screens/workspace.ts`, the same parsers): `.ini` is a Section/Key/Value table, `.rtf` a table of its paragraphs, `.xml` a table with a row per element (indented name, attributes, text). Same masking and caps. | |
| 13 | ||
| 14 | Code: `src/web/render/ini.ts`, `rtf.ts`, `xml.ts`, `kinds.ts`, `renderers.ts`. Test: `test/render-ini-rtf-xml.test.ts`. |
docs/features/file-kinds.md +4 −1
| @@ -21,7 +21,7 @@ The forge draws a file on its page when its kind is below and it is within the s | ||
| 21 | 21 | - **Layered image**, drawn up to 20 MB: `.ora` |
| 22 | 22 | - **Font**, drawn up to 20 MB: `.ttf`, `.otf`, `.woff`, `.woff2` |
| 23 | 23 | - **Photoshop**, drawn up to 4 GB: `.psd` |
| 24 | - **Config**, drawn up to 1 MB: `.json`, `.jsonc`, `.yaml`, `.yml`, `.toml`, `.ini`, `.conf` | |
| 24 | - **Config**, drawn up to 1 MB: `.json`, `.jsonc`, `.yaml`, `.yml`, `.toml`, `.conf` | |
| 25 | 25 | - **Markdown**, drawn up to 1 MB: `.md`, `.markdown`, `.mdx` |
| 26 | 26 | - **Notebook**, drawn up to 1 MB: `.ipynb` |
| 27 | 27 | - **Patch**, drawn up to 1 MB: `.patch`, `.diff` |
| @@ -59,6 +59,9 @@ The forge draws a file on its page when its kind is below and it is within the s | ||
| 59 | 59 | - **GraphQL**, drawn up to 1 MB: `.graphql`, `.gql` |
| 60 | 60 | - **Mailbox**, drawn up to 5 MB: `.mbox` |
| 61 | 61 | - **Protocol Buffers**, drawn up to 1 MB: `.proto` |
| 62 | - **INI or properties**, drawn up to 1 MB: `.ini`, `.properties` | |
| 63 | - **Rich text**, drawn up to 5 MB: `.rtf` | |
| 64 | - **XML**, drawn up to 5 MB: `.xml` | |
| 62 | 65 | - **Other text**, drawn up to 1 MB: any other text |
| 63 | 66 | - **Other binary**, none: download only: any other bytes |
| 64 | 67 |
scripts/kinds-guide.ts +1 −1
| @@ -18,7 +18,7 @@ export const NAMES: Readonly<Record<FileKind, string>> = { | ||
| 18 | 18 | excalidraw: "Excalidraw", step: "STEP model", sarif: "SARIF report", lcov: "Coverage (lcov)", junit: "JUnit report", |
| 19 | 19 | opml: "OPML", feed: "RSS or Atom feed", env: "Env example", openapi: "OpenAPI", package: "package.json", makefile: "Makefile", |
| 20 | 20 | dockerfile: "Dockerfile", workflow: "GitHub workflow", compose: "Compose file", sql: "SQL", graphql: "GraphQL", |
| 21 | mbox: "Mailbox", proto: "Protocol Buffers", text: "Other text", binary: "Other binary", | |
| 21 | mbox: "Mailbox", proto: "Protocol Buffers", ini: "INI or properties", rtf: "Rich text", xml: "XML", text: "Other text", binary: "Other binary", | |
| 22 | 22 | }; |
| 23 | 23 | |
| 24 | 24 | /** 1_000_000 as `1 MB`; 4_000_000_000 as `4 GB`; no cap as `none: download only`. */ |
src/tui/screens/workspace.ts +45
| @@ -77,6 +77,9 @@ import { parseWorkflow } from "../../web/render/workflow.ts"; | ||
| 77 | 77 | import { parseGraphql } from "../../web/render/graphql.ts"; |
| 78 | 78 | import { parseMbox } from "../../web/render/mbox.ts"; |
| 79 | 79 | import { parseProto } from "../../web/render/proto.ts"; |
| 80 | import { parseIni } from "../../web/render/ini.ts"; | |
| 81 | import { parseRtf } from "../../web/render/rtf.ts"; | |
| 82 | import { parseXml, type XmlNode } from "../../web/render/xml.ts"; | |
| 80 | 83 | import { parseSql } from "../../web/render/sql.ts"; |
| 81 | 84 | import { RESULT_CAP, parseSarif } from "../../web/render/sarif.ts"; |
| 82 | 85 | import { RECORD_CAP, parseLcov, worstFirst, type Ratio } from "../../web/render/lcov.ts"; |
| @@ -723,6 +726,42 @@ function protoTable(source: string): Table | string { | ||
| 723 | 726 | return { header: ["Block", "Name", "Type", "Number"], rows, more: doc.total - doc.blocks.length, delimiter: ",", columns: 4 }; |
| 724 | 727 | } |
| 725 | 728 | |
| 729 | /** Settings as a table: a row per key, its section beside it. Shares the web's parser. */ | |
| 730 | function iniTable(path: string, source: Uint8Array): Table | string { | |
| 731 | if (source.length > RENDER_CAP.ini) return "not drawn as settings: it is over 1 MB"; | |
| 732 | const doc = parseIni(new TextDecoder().decode(source), /\.properties$/i.test(path)); | |
| 733 | if (doc === null) return "not drawn as settings: this file holds no setting"; | |
| 734 | const rows = doc.sections.flatMap((sec) => sec.rows.map((r) => [sec.name === "" ? "-" : sec.name, r.key, r.value])); | |
| 735 | return { header: ["Section", "Key", "Value"], rows, more: doc.total - rows.length, delimiter: ",", columns: 3 }; | |
| 736 | } | |
| 737 | ||
| 738 | /** A rich text file as a table of its paragraphs. Shares the web's parser. */ | |
| 739 | function rtfTable(source: Uint8Array): Table | string { | |
| 740 | if (source.length > RENDER_CAP.rtf) return "not drawn as text: it is over 5 MB"; | |
| 741 | const doc = parseRtf(new TextDecoder("latin1").decode(source)); | |
| 742 | if (doc === null) return "not drawn as rich text: this file does not start with `{\\rtf`"; | |
| 743 | const rows = doc.paragraphs.filter((p) => p.trim() !== "").map((p) => [p]); | |
| 744 | if (rows.length === 0) return "this document holds no text"; | |
| 745 | return { header: ["Text"], rows, more: doc.truncated ? 1 : 0, delimiter: ",", columns: 1 }; | |
| 746 | } | |
| 747 | ||
| 748 | /** | |
| 749 | * An XML file as a table: a row per element in document order, indented by depth. Shares the web's | |
| 750 | * parser. | |
| 751 | */ | |
| 752 | function xmlTable(source: Uint8Array): Table | string { | |
| 753 | if (source.length > RENDER_CAP.xml) return "not drawn as a tree: it is over 5 MB"; | |
| 754 | const doc = parseXml(new TextDecoder().decode(source)); | |
| 755 | if (doc === null) return "not drawn as a tree: this file holds no XML element"; | |
| 756 | const rows: string[][] = []; | |
| 757 | const walk = (n: XmlNode, depth: number): void => { | |
| 758 | rows.push([`${" ".repeat(depth)}${n.name}`, n.attrs.map(([k, v]) => `${k}="${v}"`).join(" "), n.text]); | |
| 759 | for (const c of n.children) walk(c, depth + 1); | |
| 760 | }; | |
| 761 | walk(doc.root, 0); | |
| 762 | return { header: ["Element", "Attributes", "Text"], rows, more: doc.total - rows.length, delimiter: ",", columns: 3 }; | |
| 763 | } | |
| 764 | ||
| 726 | 765 | /** A JUnit report, by the web's own rule (file name, or root element). */ |
| 727 | 766 | function isJunitPath(path: string, source: Uint8Array): boolean { |
| 728 | 767 | return /\.xml$/i.test(path) && fileKind(path, source.subarray(0, 4096)) === "junit"; |
| @@ -1113,6 +1152,12 @@ class Tab { | ||
| 1113 | 1152 | this.table = typeof feed === "string" ? null : feed; |
| 1114 | 1153 | this.fault = typeof feed === "string" ? feed : ""; |
| 1115 | 1154 | if (this.table !== null) this.grid = layoutTable(this.table); |
| 1155 | } else if (["ini", "rtf", "xml"].includes(fileKind(this.path, source.subarray(0, 4096)))) { | |
| 1156 | const kind = fileKind(this.path, source.subarray(0, 4096)); | |
| 1157 | const made = kind === "ini" ? iniTable(this.path, source) : kind === "rtf" ? rtfTable(source) : xmlTable(source); | |
| 1158 | this.table = typeof made === "string" ? null : made; | |
| 1159 | this.fault = typeof made === "string" ? made : ""; | |
| 1160 | if (this.table !== null) this.grid = layoutTable(this.table); | |
| 1116 | 1161 | } else if (isGcodePath(this.path)) { |
| 1117 | 1162 | this.table = gcodeTable(new TextDecoder().decode(source)); |
| 1118 | 1163 | if (this.table !== null) this.grid = layoutTable(this.table); |
src/web/render/ini.ts added +80
| @@ -0,0 +1,80 @@ | ||
| 1 | /** | |
| 2 | * An `.ini` or `.properties` file as its settings: one table per `[section]` | |
| 3 | * (properties files have only the unnamed one), a row per key and value. | |
| 4 | * Comments (`;`, `#`, and `!` in properties) are dropped. A value whose key | |
| 5 | * looks secret is shown as `•••`. Properties continuation lines (a trailing | |
| 6 | * `\`) are joined. Pure; never throws; every cell is escaped by the template. | |
| 7 | * At most `ROW_CAP` settings are drawn, the rest counted. | |
| 8 | */ | |
| 9 | ||
| 10 | import { html, type Html } from "../html.ts"; | |
| 11 | import { fileSize } from "../ui.ts"; | |
| 12 | import type { Rendered, RenderInput } from "./renderers.ts"; | |
| 13 | ||
| 14 | export const ROW_CAP = 500; | |
| 15 | const VALUE_CAP = 300; | |
| 16 | export const MASK = "•••"; | |
| 17 | ||
| 18 | export interface IniSection { | |
| 19 | /** Empty for the settings before any `[section]`. */ | |
| 20 | readonly name: string; | |
| 21 | readonly rows: readonly { readonly key: string; readonly value: string }[]; | |
| 22 | } | |
| 23 | ||
| 24 | export interface IniDoc { | |
| 25 | readonly sections: readonly IniSection[]; | |
| 26 | /** Settings in the file, drawn or not. */ | |
| 27 | readonly total: number; | |
| 28 | } | |
| 29 | ||
| 30 | const looksSecret = (key: string): boolean => /(^|[._\-\s])(key|secret|token|passw(or)?d|pwd)$|password/i.test(key); | |
| 31 | const clip = (s: string): string => (s.length > VALUE_CAP ? `${s.slice(0, VALUE_CAP)}…` : s); | |
| 32 | ||
| 33 | /** Null when the text holds no setting at all. */ | |
| 34 | export function parseIni(source: string, properties = false): IniDoc | null { | |
| 35 | const lines = source.replace(/^/, "").split(/\r\n|\r|\n/); | |
| 36 | const sections: { name: string; rows: { key: string; value: string }[] }[] = []; | |
| 37 | let cur: { name: string; rows: { key: string; value: string }[] } | undefined; | |
| 38 | let total = 0; | |
| 39 | for (let i = 0; i < lines.length; i++) { | |
| 40 | let line = lines[i]!.trim(); | |
| 41 | if (line === "" || /^[;#]/.test(line) || (properties && line.startsWith("!"))) continue; | |
| 42 | if (properties) { | |
| 43 | while (/(^|[^\\])(\\\\)*\\$/.test(line) && i + 1 < lines.length) line = line.slice(0, -1) + lines[++i]!.trim(); | |
| 44 | } | |
| 45 | const head = !properties ? /^\[([^\]]*)\]/.exec(line) : null; | |
| 46 | if (head) { | |
| 47 | cur = { name: clip(head[1]!.trim()), rows: [] }; | |
| 48 | sections.push(cur); | |
| 49 | continue; | |
| 50 | } | |
| 51 | const m = properties ? /^((?:\\.|[^=:\s\\])+)\s*[=:\s]\s*(.*)$/.exec(line) ?? /^((?:\\.|[^=:\s\\])+)$/.exec(line) : /^([^=:]+?)\s*[=:]\s*(.*)$/.exec(line); | |
| 52 | if (!m) continue; | |
| 53 | if (!cur) { | |
| 54 | cur = { name: "", rows: [] }; | |
| 55 | sections.push(cur); | |
| 56 | } | |
| 57 | total++; | |
| 58 | if (total <= ROW_CAP) { | |
| 59 | const key = clip(m[1]!.trim()); | |
| 60 | cur.rows.push({ key, value: looksSecret(key) ? MASK : clip((m[2] ?? "").trim().replace(/^"(.*)"$/, "$1")) }); | |
| 61 | } | |
| 62 | } | |
| 63 | return total === 0 ? null : { sections: sections.filter((s) => s.rows.length > 0 || s.name !== ""), total }; | |
| 64 | } | |
| 65 | ||
| 66 | export function ini(input: RenderInput): Rendered { | |
| 67 | const properties = /\.properties$/i.test(input.path); | |
| 68 | const doc = parseIni(new TextDecoder().decode(input.body), properties); | |
| 69 | if (doc === null) return { content: html``, lines: 0, fallback: "This file holds no settings, so it is not drawn as one." }; | |
| 70 | const tables: Html[] = doc.sections.map((s) => { | |
| 71 | const rows = s.rows.map((r) => html`<tr><th scope="row"><code>${r.key}</code></th><td>${r.value}</td></tr>`); | |
| 72 | const head = s.name === "" ? html`` : html`<h3><code>${s.name}</code></h3>`; | |
| 73 | return html`${head}<div class="table-scroll"><table class="table" data-dense aria-label="${s.name === "" ? "Settings" : s.name}"><thead><tr><th scope="col">Key</th><th scope="col">Value</th></tr></thead><tbody>${rows}</tbody></table></div>`; | |
| 74 | }); | |
| 75 | const shown = doc.sections.reduce((n, s) => n + s.rows.length, 0); | |
| 76 | const more = doc.total - shown; | |
| 77 | const foot = more > 0 ? html`<p class="muted small">${more.toLocaleString()} more not shown</p>` : html``; | |
| 78 | const n = doc.total; | |
| 79 | return { content: html`${tables}${foot}`, lines: 0, summary: `${n.toLocaleString()} ${n === 1 ? "setting" : "settings"} · ${fileSize(input.size)}` }; | |
| 80 | } |
src/web/render/kinds.ts +12 −1
| @@ -69,6 +69,9 @@ export type FileKind = | ||
| 69 | 69 | | "graphql" |
| 70 | 70 | | "mbox" |
| 71 | 71 | | "proto" |
| 72 | | "ini" | |
| 73 | | "rtf" | |
| 74 | | "xml" | |
| 72 | 75 | | "text" |
| 73 | 76 | | "binary"; |
| 74 | 77 | |
| @@ -149,6 +152,9 @@ export const RENDER_CAP: Readonly<Record<FileKind, number>> = { | ||
| 149 | 152 | graphql: 1 * MB, |
| 150 | 153 | mbox: 5 * MB, |
| 151 | 154 | proto: 1 * MB, |
| 155 | ini: 1 * MB, | |
| 156 | rtf: 5 * MB, | |
| 157 | xml: 5 * MB, | |
| 152 | 158 | text: TEXT_CAP, |
| 153 | 159 | binary: 0, |
| 154 | 160 | }; |
| @@ -253,7 +259,10 @@ const TEXTUAL: Readonly<Record<string, FileKind>> = { | ||
| 253 | 259 | yaml: "config", |
| 254 | 260 | yml: "config", |
| 255 | 261 | toml: "config", |
| 256 | ini: "config", | |
| 262 | ini: "ini", | |
| 263 | properties: "ini", | |
| 264 | rtf: "rtf", | |
| 265 | xml: "xml", | |
| 257 | 266 | conf: "config", |
| 258 | 267 | md: "markdown", |
| 259 | 268 | markdown: "markdown", |
| @@ -470,6 +479,8 @@ export function fileKind(path: string, head: Uint8Array): FileKind { | ||
| 470 | 479 | if (isComposeName(path)) return "compose"; |
| 471 | 480 | const ext = extension(path); |
| 472 | 481 | if (ext === "step" || ext === "stp") return isStepHead(head) ? "step" : "text"; |
| 482 | // RTF opens on `{\rtf`: a `.rtf` whose bytes say otherwise is only text. | |
| 483 | if (ext === "rtf") return /^\uFEFF?\s*\{\\rtf/.test(new TextDecoder().decode(head.subarray(0, 16))) ? "rtf" : "text"; | |
| 473 | 484 | return TEXTUAL[ext] ?? (isDxfHead(head) ? "dxf" : "text"); |
| 474 | 485 | } |
| 475 | 486 |
src/web/render/renderers.ts +6
| @@ -15,6 +15,9 @@ import { sqlView } from "./sql.ts"; | ||
| 15 | 15 | import { graphql } from "./graphql.ts"; |
| 16 | 16 | import { mbox } from "./mbox.ts"; |
| 17 | 17 | import { proto } from "./proto.ts"; |
| 18 | import { ini } from "./ini.ts"; | |
| 19 | import { rtf } from "./rtf.ts"; | |
| 20 | import { xml } from "./xml.ts"; | |
| 18 | 21 | import { eml } from "./eml.ts"; |
| 19 | 22 | import { csv } from "./csv.ts"; |
| 20 | 23 | import { geojson } from "./geojson.ts"; |
| @@ -159,6 +162,9 @@ export const RENDERERS: Partial<Readonly<Record<FileKind, Renderer>>> = { | ||
| 159 | 162 | graphql, |
| 160 | 163 | mbox, |
| 161 | 164 | proto, |
| 165 | ini, | |
| 166 | rtf, | |
| 167 | xml, | |
| 162 | 168 | openapi, |
| 163 | 169 | package: packageJson, |
| 164 | 170 | makefile, |
src/web/render/rtf.ts added +136
| @@ -0,0 +1,136 @@ | ||
| 1 | /** | |
| 2 | * A `.rtf` file as its text: paragraphs, with the formatting dropped. The | |
| 3 | * reader walks control words: `\par` ends a paragraph, `\tab` is a tab, | |
| 4 | * `\'hh` and `\uN` are characters, and groups that hold no document text | |
| 5 | * (font and colour tables, stylesheet, info, pictures, `\*` destinations) | |
| 6 | * are skipped. Nothing is executed and nothing is fetched; embedded objects | |
| 7 | * and pictures are not drawn. A file not starting with `{\rtf` is not drawn. | |
| 8 | * Pure; never throws; the text is escaped by the template; at most | |
| 9 | * `CHAR_CAP` characters are drawn. | |
| 10 | */ | |
| 11 | ||
| 12 | import { html } from "../html.ts"; | |
| 13 | import { fileSize } from "../ui.ts"; | |
| 14 | import type { Rendered, RenderInput } from "./renderers.ts"; | |
| 15 | ||
| 16 | export const CHAR_CAP = 100_000; | |
| 17 | const DEPTH_CAP = 200; | |
| 18 | ||
| 19 | const SKIP = new Set([ | |
| 20 | "fonttbl", "colortbl", "stylesheet", "info", "pict", "object", "header", "footer", "headerl", "headerr", "headerf", | |
| 21 | "footerl", "footerr", "footerf", "footnote", "themedata", "colorschememapping", "latentstyles", "datastore", | |
| 22 | "generator", "listtable", "listoverridetable", "rsidtbl", "fldinst", "bkmkstart", "bkmkend", "xmlnstbl", "private", | |
| 23 | ]); | |
| 24 | ||
| 25 | /** Windows-1252 for the high half, as RTF's `\'hh` writes it. */ | |
| 26 | const CP1252 = "€\u0081‚ƒ„…†‡ˆ‰Š‹Œ\u008dŽ\u008f\u0090‘’“”•–—˜™š›œ\u009džŸ"; | |
| 27 | const cp = (n: number): string => (n >= 0x80 && n < 0xa0 ? CP1252[n - 0x80]! : String.fromCharCode(n)); | |
| 28 | ||
| 29 | export interface RtfDoc { | |
| 30 | readonly paragraphs: readonly string[]; | |
| 31 | readonly truncated: boolean; | |
| 32 | } | |
| 33 | ||
| 34 | /** Null when the text is not RTF. */ | |
| 35 | export function parseRtf(source: string): RtfDoc | null { | |
| 36 | if (!source.startsWith("{\\rtf")) return null; | |
| 37 | const paras: string[] = []; | |
| 38 | let cur = ""; | |
| 39 | let count = 0; | |
| 40 | let truncated = false; | |
| 41 | // One entry per open group: whether it is skipped, and its `\uc` fallback length. | |
| 42 | const stack: { skip: boolean; uc: number }[] = []; | |
| 43 | let skip = false; | |
| 44 | let uc = 1; | |
| 45 | let pendingSkip = 0; | |
| 46 | const push = (s: string): void => { | |
| 47 | if (skip || s === "") return; | |
| 48 | if (count + s.length > CHAR_CAP) { | |
| 49 | s = s.slice(0, Math.max(0, CHAR_CAP - count)); | |
| 50 | truncated = true; | |
| 51 | } | |
| 52 | count += s.length; | |
| 53 | cur += s; | |
| 54 | }; | |
| 55 | let i = 0; | |
| 56 | const n = source.length; | |
| 57 | while (i < n && !truncated) { | |
| 58 | const c = source[i]!; | |
| 59 | if (c === "{") { | |
| 60 | if (stack.length >= DEPTH_CAP) return { paragraphs: [], truncated: true }; | |
| 61 | stack.push({ skip, uc }); | |
| 62 | i++; | |
| 63 | // `{\*\dest` is an optional destination: skipped whole. | |
| 64 | if (source.startsWith("\\*", i)) skip = true; | |
| 65 | } else if (c === "}") { | |
| 66 | const top = stack.pop(); | |
| 67 | if (top) ({ skip, uc } = top); | |
| 68 | i++; | |
| 69 | } else if (c === "\\") { | |
| 70 | const next = source[i + 1] ?? ""; | |
| 71 | if (next === "'") { | |
| 72 | const hex = source.slice(i + 2, i + 4); | |
| 73 | if (/^[0-9a-fA-F]{2}$/.test(hex)) { | |
| 74 | if (pendingSkip > 0) pendingSkip--; | |
| 75 | else push(cp(parseInt(hex, 16))); | |
| 76 | } | |
| 77 | i += 4; | |
| 78 | } else if (/[A-Za-z]/.test(next)) { | |
| 79 | const m = /^\\([A-Za-z]+)(-?\d+)? ?/.exec(source.slice(i, i + 40)); | |
| 80 | if (!m) { i++; continue; } | |
| 81 | i += m[0].length; | |
| 82 | const word = m[1]!; | |
| 83 | const arg = m[2] === undefined ? undefined : Number(m[2]); | |
| 84 | if (SKIP.has(word)) skip = true; | |
| 85 | else if (word === "par" || word === "sect" || word === "page") { | |
| 86 | if (!skip) { paras.push(cur); cur = ""; } | |
| 87 | } else if (word === "line") push("\n"); | |
| 88 | else if (word === "tab") push("\t"); | |
| 89 | else if (word === "emdash") push("—"); | |
| 90 | else if (word === "endash") push("–"); | |
| 91 | else if (word === "lquote") push("‘"); | |
| 92 | else if (word === "rquote") push("’"); | |
| 93 | else if (word === "ldblquote") push("“"); | |
| 94 | else if (word === "rdblquote") push("”"); | |
| 95 | else if (word === "bullet") push("•"); | |
| 96 | else if (word === "uc" && arg !== undefined) uc = Math.max(0, Math.min(8, arg)); | |
| 97 | else if (word === "u" && arg !== undefined) { | |
| 98 | push(String.fromCharCode(arg < 0 ? arg + 65536 : arg)); | |
| 99 | pendingSkip = uc; | |
| 100 | } | |
| 101 | } else if (next === "~") { push(" "); i += 2; } | |
| 102 | else if (next === "-") { i += 2; } | |
| 103 | else if (next === "_") { push("-"); i += 2; } | |
| 104 | else if (next === "\\" || next === "{" || next === "}") { push(next); i += 2; } | |
| 105 | else if (next === "\n" || next === "\r") { if (!skip) { paras.push(cur); cur = ""; } i += 2; } | |
| 106 | else i += 2; | |
| 107 | } else if (c === "\n" || c === "\r") { | |
| 108 | i++; | |
| 109 | } else { | |
| 110 | // a run of plain text, up to the next control character | |
| 111 | let j = i; | |
| 112 | while (j < n && source[j] !== "\\" && source[j] !== "{" && source[j] !== "}" && source[j] !== "\n" && source[j] !== "\r") j++; | |
| 113 | if (pendingSkip > 0) { | |
| 114 | const take = Math.min(pendingSkip, j - i); | |
| 115 | pendingSkip -= take; | |
| 116 | i += take; | |
| 117 | } | |
| 118 | push(source.slice(i, j)); | |
| 119 | i = j; | |
| 120 | } | |
| 121 | } | |
| 122 | if (cur !== "") paras.push(cur); | |
| 123 | const clean = paras.map((p) => p.replace(/[\u0000-\u0008\u000b-\u001f\u007f]/g, "")); | |
| 124 | while (clean.length > 0 && clean[clean.length - 1]!.trim() === "") clean.pop(); | |
| 125 | return { paragraphs: clean, truncated }; | |
| 126 | } | |
| 127 | ||
| 128 | export function rtf(input: RenderInput): Rendered { | |
| 129 | const doc = parseRtf(new TextDecoder("latin1").decode(input.body)); | |
| 130 | if (doc === null) return { content: html``, lines: 0, fallback: "This file does not start with `{\\rtf`, so it is not drawn as a rich text document." }; | |
| 131 | if (doc.paragraphs.length === 0) return { content: html``, lines: 0, fallback: "This document holds no text." }; | |
| 132 | const paras = doc.paragraphs.map((p) => (p.trim() === "" ? html`<p> </p>` : html`<p>${p}</p>`)); | |
| 133 | const foot = doc.truncated ? html`<p class="muted small">The rest of the text is not shown</p>` : html``; | |
| 134 | const words = doc.paragraphs.join(" ").split(/\s+/).filter(Boolean).length; | |
| 135 | return { content: html`<article class="prose">${paras}</article>${foot}`, lines: 0, summary: `${words.toLocaleString()} ${words === 1 ? "word" : "words"}${doc.truncated ? "+" : ""} · ${fileSize(input.size)}` }; | |
outdated
|
||
| 136 | } |
src/web/render/xml.ts added +187
| @@ -0,0 +1,187 @@ | ||
| 1 | /** | |
| 2 | * An `.xml` file (one no other kind claimed) as its element tree: each | |
| 3 | * element with its attributes and, for a leaf, its text. The reader is its | |
| 4 | * own and small: it skips the prolog, comments, processing instructions and | |
| 5 | * the doctype (never expanding an entity or fetching anything), reads CDATA | |
| 6 | * as text, and decodes only the five named entities and numeric references. | |
| 7 | * A file with no element is not drawn. Pure; never throws; every name and | |
| 8 | * value is escaped by the template; at most `NODE_CAP` elements, `DEPTH_CAP` | |
| 9 | * levels and `ATTR_CAP` attributes per element are drawn. | |
| 10 | */ | |
| 11 | ||
| 12 | import { html, type Html } from "../html.ts"; | |
| 13 | import { fileSize } from "../ui.ts"; | |
| 14 | import type { Rendered, RenderInput } from "./renderers.ts"; | |
| 15 | ||
| 16 | export const NODE_CAP = 1000; | |
| 17 | export const DEPTH_CAP = 24; | |
| 18 | const ATTR_CAP = 20; | |
| 19 | const TEXT_CAP = 200; | |
| 20 | const ATTR_SCAN = 4000; | |
| 21 | /** | |
| 22 | * Elements read at most; a file with more is counted as this many and the rest is not looked at. | |
| 23 | */ | |
| 24 | export const SCAN_CAP = 100_000; | |
| 25 | ||
| 26 | export interface XmlNode { | |
| 27 | readonly name: string; | |
| 28 | readonly attrs: readonly (readonly [string, string])[]; | |
| 29 | readonly text: string; | |
| 30 | readonly children: XmlNode[]; | |
| 31 | } | |
| 32 | ||
| 33 | export interface XmlDoc { | |
| 34 | readonly root: XmlNode; | |
| 35 | /** Elements in the file, drawn or not. */ | |
| 36 | readonly total: number; | |
| 37 | readonly drawn: number; | |
| 38 | } | |
| 39 | ||
| 40 | const ENT: Record<string, string> = { lt: "<", gt: ">", amp: "&", quot: '"', apos: "'" }; | |
| 41 | const decode = (s: string): string => | |
| 42 | s.replace(/&(?:#(\d{1,7})|#x([0-9a-fA-F]{1,6})|([a-z]+));/g, (all, d, h, name) => { | |
outdated
Quadratic backtracking: an unterminated |
||
| 43 | if (name !== undefined) return ENT[name] ?? all; | |
| 44 | const cpt = d !== undefined ? Number(d) : parseInt(h, 16); | |
| 45 | return cpt > 0x1f && cpt <= 0x10ffff && !(cpt >= 0xd800 && cpt <= 0xdfff) ? String.fromCodePoint(cpt) : ""; | |
| 46 | }); | |
| 47 | const clip = (s: string): string => (s.length > TEXT_CAP ? `${s.slice(0, TEXT_CAP)}…` : s); | |
| 48 | ||
| 49 | const isNameStart = (c: number): boolean => (c >= 65 && c <= 90) || (c >= 97 && c <= 122) || c === 95; | |
| 50 | const ATTR = /([^\s=]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g; | |
| 51 | ||
| 52 | /** Stands for an element past the caps: it takes no text. */ | |
| 53 | const SINK: XmlNode = { name: "", attrs: [], text: "x".repeat(TEXT_CAP), children: [] }; | |
| 54 | ||
| 55 | /** Null when the text holds no element. */ | |
| 56 | export function parseXml(source: string): XmlDoc | null { | |
| 57 | const text = source.replace(/^/, ""); | |
| 58 | const stack: XmlNode[] = []; | |
| 59 | let root: XmlNode | undefined; | |
| 60 | let total = 0; | |
| 61 | let drawn = 0; | |
| 62 | const attach = (node: XmlNode): void => { | |
| 63 | const parent = stack[stack.length - 1]; | |
| 64 | if (parent === undefined) { | |
| 65 | root ??= node; | |
| 66 | } else if (stack.length <= DEPTH_CAP && drawn <= NODE_CAP) { | |
| 67 | parent.children.push(node); | |
| 68 | drawn++; | |
| 69 | } | |
| 70 | }; | |
| 71 | const open = (name: string, attrText: string, selfClosing: boolean): boolean => { | |
| 72 | total++; | |
| 73 | if (root !== undefined && (stack.length > DEPTH_CAP || drawn > NODE_CAP)) { | |
| 74 | // Counted, never drawn: no attributes read, nothing allocated, so a file of millions of | |
| 75 | // elements stays cheap. | |
| 76 | if (!selfClosing) stack.push(SINK); | |
| 77 | return false; | |
| 78 | } | |
| 79 | const attrs: [string, string][] = []; | |
| 80 | ATTR.lastIndex = 0; | |
| 81 | let a: RegExpExecArray | null; | |
| 82 | // A tag's attribute text is cut before the pattern runs, so a hostile tag costs a bounded | |
| 83 | // amount. | |
| 84 | const scan = attrText.slice(0, ATTR_SCAN); | |
| 85 | while (attrs.length < ATTR_CAP && (a = ATTR.exec(scan)) !== null) attrs.push([clip(a[1]!), clip(decode(a[2] ?? a[3] ?? ""))]); | |
| 86 | const node: XmlNode = { name: clip(name), attrs, text: "", children: [] }; | |
| 87 | if (root === undefined) drawn = 1; | |
| 88 | attach(node); | |
| 89 | if (!selfClosing) stack.push(node); | |
| 90 | return selfClosing && stack.length === 0; | |
| 91 | }; | |
| 92 | const addText = (t: string): void => { | |
| 93 | const top = stack[stack.length - 1]; | |
| 94 | if (top !== undefined && t.trim() !== "" && top.text.length < TEXT_CAP) (top as { text: string }).text += `${top.text === "" ? "" : " "}${t.trim().replace(/\s+/g, " ")}`; | |
| 95 | }; | |
| 96 | // One left-to-right pass: every scan moves forward or ends the file, so the work is linear in its | |
| 97 | // size. | |
| 98 | let i = 0; | |
| 99 | while (i < text.length && total < SCAN_CAP) { | |
outdated
Principle 7/3 and phone: nested |
||
| 100 | if (text.charCodeAt(i) !== 60) { | |
| 101 | let end = text.indexOf("<", i); | |
| 102 | if (end < 0) end = text.length; | |
| 103 | addText(decode(text.slice(i, end))); | |
| 104 | i = end; | |
| 105 | } else if (text.charCodeAt(i + 1) !== 33 && text.charCodeAt(i + 1) !== 63 && !isNameStart(text.charCodeAt(i + 1 + (text.charCodeAt(i + 1) === 47 ? 1 : 0)))) { | |
| 106 | // A bare `<`: not markup, so the span up to the next `<` is dropped. | |
| 107 | const next = text.indexOf("<", i + 1); | |
| 108 | i = next < 0 ? text.length : next; | |
| 109 | } else if (text.startsWith("<!--", i)) { | |
| 110 | const end = text.indexOf("-->", i + 4); | |
| 111 | if (end < 0) break; | |
| 112 | i = end + 3; | |
| 113 | } else if (text.startsWith("<![CDATA[", i)) { | |
| 114 | const end = text.indexOf("]]>", i + 9); | |
| 115 | if (end < 0) break; | |
| 116 | addText(text.slice(i + 9, end)); | |
| 117 | i = end + 3; | |
| 118 | } else if (text.startsWith("<?", i)) { | |
| 119 | const end = text.indexOf("?>", i + 2); | |
| 120 | if (end < 0) break; | |
| 121 | i = end + 2; | |
| 122 | } else if (text.startsWith("<!", i)) { | |
| 123 | // A doctype, with its internal subset in brackets; anything else with `<!` is skipped to its | |
| 124 | // `>`. | |
| 125 | const bracket = text.indexOf("[", i); | |
| 126 | const gt = text.indexOf(">", i); | |
| 127 | let end = gt; | |
| 128 | if (bracket >= 0 && (gt < 0 || bracket < gt)) { | |
| 129 | const close = text.indexOf("]", bracket); | |
| 130 | end = close < 0 ? -1 : text.indexOf(">", close); | |
| 131 | } | |
| 132 | if (end < 0) break; | |
| 133 | i = end + 1; | |
| 134 | } else { | |
| 135 | const closing = text.charCodeAt(i + 1) === 47; | |
| 136 | const nameAt = i + (closing ? 2 : 1); | |
| 137 | let j = nameAt; | |
| 138 | while (j < text.length) { | |
| 139 | const c = text.charCodeAt(j); | |
| 140 | if (c === 62 || c === 47 || c === 32 || (c >= 9 && c <= 13)) break; | |
| 141 | j++; | |
| 142 | } | |
| 143 | const name = text.slice(nameAt, j); | |
| 144 | let quote = 0; | |
| 145 | let k = j; | |
| 146 | for (; k < text.length; k++) { | |
| 147 | const c = text.charCodeAt(k); | |
| 148 | if (quote !== 0) { | |
| 149 | if (c === quote) quote = 0; | |
| 150 | } else if (c === 34 || c === 39) quote = c; | |
| 151 | else if (c === 62) break; | |
| 152 | } | |
| 153 | if (k >= text.length) break; | |
| 154 | let attrText = text.slice(j, k); | |
| 155 | i = k + 1; | |
| 156 | if (closing) { | |
| 157 | if (stack.length > 0) stack.pop(); | |
| 158 | if (stack.length === 0 && root !== undefined) break; | |
| 159 | continue; | |
| 160 | } | |
| 161 | const selfClosing = attrText.endsWith("/"); | |
| 162 | if (selfClosing) attrText = attrText.slice(0, -1); | |
| 163 | if (open(name, attrText, selfClosing)) break; | |
| 164 | } | |
| 165 | } | |
| 166 | return root === undefined ? null : { root, total, drawn }; | |
| 167 | } | |
| 168 | ||
| 169 | function tree(n: XmlNode): Html { | |
| 170 | const attrs = n.attrs.map(([k, v]) => html` <span class="muted">${k}=</span>"${v}"`); | |
| 171 | const text = n.text === "" ? html`` : html` <span>${clip(n.text.replace(/[\u0000-\u001f\u007f]/g, " "))}</span>`; | |
| 172 | const kids = n.children.length === 0 ? html`` : html`<ul>${n.children.map(tree)}</ul>`; | |
| 173 | return html`<li><code>${n.name}</code>${attrs}${text}${kids}</li>`; | |
| 174 | } | |
| 175 | ||
| 176 | export function xml(input: RenderInput): Rendered { | |
| 177 | const doc = parseXml(new TextDecoder().decode(input.body)); | |
| 178 | if (doc === null) return { content: html``, lines: 0, fallback: "This file holds no XML element, so it is not drawn as a tree." }; | |
| 179 | const more = doc.total - doc.drawn; | |
| 180 | const foot = more > 0 ? html`<p class="muted small">${more.toLocaleString()} more elements not shown</p>` : html``; | |
| 181 | const n = doc.total; | |
| 182 | return { | |
| 183 | content: html`<ul class="xml-tree" aria-label="Elements">${tree(doc.root)}</ul>${foot}`, | |
| 184 | lines: 0, | |
| 185 | summary: `${n.toLocaleString()} ${n === 1 ? "element" : "elements"} · ${fileSize(input.size)}`, | |
| 186 | }; | |
| 187 | } |
src/web/style/content.ts +5
| @@ -410,6 +410,11 @@ form.inline { display: inline; margin-left: auto; } | ||
| 410 | 410 | .bar > .menu > summary { max-width: 100%; min-width: 0; } |
| 411 | 411 | .bar > .menu > summary .truncate { min-width: 0; } |
| 412 | 412 | .prose img { max-width: 100%; height: auto; } |
| 413 | /* An XML file's element tree: a small indent per level (24 levels at 360 px stay on screen), long | |
| 414 | names and values wrap, never the page. */ | |
| 415 | .xml-tree, .xml-tree ul { list-style: none; margin: 0; padding: 0; min-width: 0; overflow-wrap: anywhere; } | |
| 416 | .xml-tree ul { padding-left: 8px; border-left: 1px solid var(--border); } | |
Non-blocking, principle 6: a border-left per nesting level draws a line per level. Indent alone, or one rule on the outer list, is quieter. |
||
| 417 | .xml-tree li { margin: 2px 0; } | |
| 413 | 418 | .prose pre { max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; overscroll-behavior-x: contain; } |
| 414 | 419 | |
| 415 | 420 | /* The generic two-column page: content, then a sidebar that sticks. */ |
test/config-yaml-toml.test.ts +1 −1
| @@ -51,7 +51,7 @@ describe("which files open as a form", () => { | ||
| 51 | 51 | const blob = { kind: "text" as const, size: body.length, body } as never; |
| 52 | 52 | expect(rendererFor("x/a.yml", blob)).toBeDefined(); |
| 53 | 53 | expect(rendererFor("x/a.toml", blob)).toBeDefined(); |
| 54 | expect(rendererFor("x/a.ini", blob)).toBeUndefined(); | |
| 54 | expect(rendererFor("x/a.ini", blob)).toBeDefined(); // .ini opens as settings tables (#14044) | |
| 55 | 55 | }); |
| 56 | 56 | }); |
| 57 | 57 |
test/fuzz-renderers.test.ts +12
| @@ -248,6 +248,18 @@ export const SAMPLES: Readonly<Record<string, { path: string; files: readonly st | ||
| 248 | 248 | "TITLE: <b>T</b>\nFCM: NON-DROP FRAME\n\n001 AX V C 00:00:00:00 00:00:02:00 01:00:00:00 01:00:02:00\n* FROM CLIP NAME: a.mov\n", |
| 249 | 249 | ], |
| 250 | 250 | }, |
| 251 | ini: { | |
| 252 | path: "app.ini", | |
| 253 | files: ["; c\n[core]\nname = <b>x</b>\nurl=javascript:alert(1)\n[other]\nk: v\n"], | |
| 254 | }, | |
| 255 | rtf: { | |
| 256 | path: "note.rtf", | |
| 257 | files: ["{\\rtf1\\ansi{\\fonttbl{\\f0 Arial;}}\\pard Hello <b>x</b>\\par {\\b bold} \\'e9\\u233? end\\par}"], | |
| 258 | }, | |
| 259 | xml: { | |
| 260 | path: "data.xml", | |
| 261 | files: ['<?xml version="1.0"?><root a="<b>"><!-- c --><item id="1">x & <i>y</i></item><e/></root>'], | |
| 262 | }, | |
| 251 | 263 | fcpxml: { |
| 252 | 264 | path: "cut.fcpxml", |
| 253 | 265 | files: [ |
test/render-feed-xml.test.ts +1 −1
| @@ -23,7 +23,7 @@ describe("feed-xml", () => { | ||
| 23 | 23 | expect(fileKind("a.atom", enc("<feed/>"))).toBe("feed"); |
| 24 | 24 | expect(fileKind("a.xml", enc('<?xml version="1.0"?><opml/>'))).toBe("opml"); |
| 25 | 25 | expect(fileKind("a.xml", enc("<rss/>"))).toBe("feed"); |
| 26 | expect(fileKind("a.xml", enc("<project/>"))).toBe("text"); | |
| 26 | expect(fileKind("a.xml", enc("<project/>"))).toBe("xml"); | |
| 27 | 27 | expect(RENDERERS.opml).toBe(feedXml); |
| 28 | 28 | expect(RENDERERS.feed).toBe(feedXml); |
| 29 | 29 | }); |
test/render-ini-rtf-xml.test.ts added +124
| @@ -0,0 +1,124 @@ | ||
| 1 | /** | |
| 2 | * .ini/.properties, .rtf and .xml open readable: shape, caps, hostile text escaped, the registry. | |
| 3 | */ | |
| 4 | import { describe, expect, test } from "bun:test"; | |
| 5 | import { ROW_CAP, ini, parseIni } from "../src/web/render/ini.ts"; | |
| 6 | import { parseRtf, rtf } from "../src/web/render/rtf.ts"; | |
| 7 | import { NODE_CAP, parseXml, xml } from "../src/web/render/xml.ts"; | |
| 8 | import { fileKind, kindByName, mediaType, RENDER_CAP } from "../src/web/render/kinds.ts"; | |
| 9 | import { RENDERERS } from "../src/web/render/renderers.ts"; | |
| 10 | ||
| 11 | const enc = (s: string): Uint8Array => new TextEncoder().encode(s); | |
| 12 | const input = (path: string, text: string) => ({ path, size: text.length, body: enc(text), media: "/m", download: "/d" }); | |
| 13 | const BAD = `<script>alert(1)</script>`; | |
| 14 | ||
| 15 | describe("registry", () => { | |
| 16 | test("registered, and bytes must agree with the name", () => { | |
| 17 | expect(kindByName("a.ini")).toBe("ini"); | |
| 18 | expect(kindByName("a.properties")).toBe("ini"); | |
| 19 | expect(RENDERERS.ini).toBe(ini); | |
| 20 | expect(RENDERERS.rtf).toBe(rtf); | |
| 21 | expect(RENDERERS.xml).toBe(xml); | |
| 22 | expect(RENDER_CAP.xml).toBe(5_000_000); | |
| 23 | expect(fileKind("a.rtf", enc("{\\rtf1 hi}"))).toBe("rtf"); | |
| 24 | expect(fileKind("a.rtf", enc("just words"))).toBe("text"); | |
| 25 | expect(fileKind("a.rtf", new Uint8Array([0, 1, 2]))).toBe("binary"); | |
| 26 | for (const f of ["a.ini", "a.rtf", "a.xml", "a.properties"]) expect(mediaType(f, enc("{\\rtf1}"))).toBe("application/octet-stream"); | |
| 27 | }); | |
| 28 | test("an xml file another kind claims keeps that kind", () => { | |
| 29 | expect(fileKind("a.xml", enc("<rss/>"))).toBe("feed"); | |
| 30 | expect(fileKind("a.xml", enc("<testsuite/>"))).toBe("junit"); | |
| 31 | expect(fileKind("a.xml", enc("<a/>"))).toBe("xml"); | |
| 32 | }); | |
| 33 | }); | |
| 34 | ||
| 35 | describe("ini", () => { | |
| 36 | test("sections, comments, masks", () => { | |
| 37 | const doc = parseIni("; c\nroot = 1\n[db]\nhost=localhost\n# x\npassword = hunter2\napi_key: abc\n")!; | |
| 38 | expect(doc.total).toBe(4); | |
| 39 | expect(doc.sections[0]).toEqual({ name: "", rows: [{ key: "root", value: "1" }] }); | |
| 40 | expect(doc.sections[1]!.rows.map((r) => r.value)).toEqual(["localhost", "•••", "•••"]); | |
| 41 | expect(String(ini(input("a.ini", "[db]\npassword=hunter2\n")).content)).not.toContain("hunter2"); | |
| 42 | }); | |
| 43 | test("properties: ! comments, continuation, colon and space separators", () => { | |
| 44 | const doc = parseIni("! c\na.b=one \\\n two\nc: 3\nd 4\n", true)!; | |
| 45 | expect(doc.sections[0]!.rows).toEqual([{ key: "a.b", value: "one two" }, { key: "c", value: "3" }, { key: "d", value: "4" }]); | |
| 46 | }); | |
| 47 | test("no settings: fallback; cap counted", () => { | |
| 48 | expect(ini(input("a.ini", "; nothing\n")).fallback).toBeDefined(); | |
| 49 | const r = ini(input("a.ini", Array.from({ length: ROW_CAP + 3 }, (_, n) => `k${n}=v`).join("\n"))); | |
| 50 | expect(String(r.content)).toContain("3 more not shown"); | |
| 51 | }); | |
| 52 | test("hostile text is escaped", () => { | |
| 53 | const out = String(ini(input("a.ini", `[${BAD}]\n${BAD}=<img src=x onerror=1>\n`)).content); | |
| 54 | expect(out).not.toContain("<script>"); | |
| 55 | expect(out).not.toContain("<img"); | |
| 56 | expect(out).toContain("<script>"); | |
| 57 | }); | |
| 58 | }); | |
| 59 | ||
| 60 | describe("rtf", () => { | |
| 61 | const SAMPLE = "{\\rtf1\\ansi{\\fonttbl{\\f0 Arial;}}{\\colortbl;\\red0;}\\pard Hello \\b world\\b0\\par Caf\\'e9 \\u8364? end{\\*\\generator X;}\\par}"; | |
| 62 | test("text, paragraphs, no tables", () => { | |
| 63 | const doc = parseRtf(SAMPLE)!; | |
| 64 | expect(doc.paragraphs).toEqual(["Hello world", "Café € end"]); | |
| 65 | }); | |
| 66 | test("not rtf: fallback", () => { | |
| 67 | expect(parseRtf("hello")).toBeNull(); | |
| 68 | expect(rtf(input("a.rtf", "hello")).fallback).toBeDefined(); | |
| 69 | }); | |
| 70 | test("hostile text is escaped and pictures are skipped", () => { | |
| 71 | const out = String(rtf(input("a.rtf", `{\\rtf1 ${BAD}{\\pict 89504e47}\\par}`)).content); | |
| 72 | expect(out).not.toContain("<script>"); | |
| 73 | expect(out).toContain("<script>"); | |
| 74 | expect(out).not.toContain("89504e47"); | |
| 75 | }); | |
| 76 | test("deep nesting never throws", () => { | |
| 77 | expect(() => rtf(input("a.rtf", "{\\rtf1" + "{".repeat(5000) + "x"))).not.toThrow(); | |
| 78 | }); | |
| 79 | }); | |
| 80 | ||
| 81 | describe("xml", () => { | |
| 82 | test("tree with attributes, text, entities and CDATA", () => { | |
| 83 | const doc = parseXml(`<?xml version="1.0"?><!DOCTYPE a [<!ENTITY x "y">]><!-- c --><a id="1"><b>t & u</b><c><![CDATA[<raw>]]></c><d/></a>`)!; | |
| 84 | expect(doc.root.name).toBe("a"); | |
| 85 | expect(doc.root.attrs).toEqual([["id", "1"]]); | |
| 86 | expect(doc.root.children.map((c) => c.name)).toEqual(["b", "c", "d"]); | |
| 87 | expect(doc.root.children[0]!.text).toBe("t & u"); | |
| 88 | expect(doc.root.children[1]!.text).toBe("<raw>"); | |
| 89 | expect(doc.total).toBe(4); | |
| 90 | }); | |
| 91 | test("no element: fallback; node cap counted", () => { | |
| 92 | expect(xml(input("a.xml", "plain")).fallback).toBeDefined(); | |
| 93 | const big = `<r>${"<i/>".repeat(NODE_CAP + 20)}</r>`; | |
| 94 | expect(String(xml(input("a.xml", big)).content)).toContain("more elements not shown"); | |
| 95 | }); | |
| 96 | test("hostile names, attributes and text are escaped", () => { | |
| 97 | const out = String(xml(input("a.xml", `<r a=""><img src=x onerror=1>" b='"><img src=x>'><script>alert(1)</script></r>`)).content); | |
| 98 | expect(out).not.toContain("<script>"); | |
| 99 | expect(out).not.toContain("<img"); | |
| 100 | expect(out).toContain("<script>"); | |
| 101 | }); | |
| 102 | }); | |
| 103 | ||
| 104 | describe("xml: hostile repeated input costs a bounded amount", () => { | |
| 105 | const MB5 = 5 * 1024 * 1024; | |
| 106 | const cases: Record<string, string> = { | |
| 107 | "unterminated quoted starts": '<r>' + '<a "'.repeat(MB5 / 4), | |
| 108 | "unterminated doctype subsets": "<!DOCTYPE a [".repeat(MB5 / 13), | |
| 109 | "one huge attribute run": "<a " + "x".repeat(MB5) + "/>", | |
| 110 | "unterminated comments": "<r>" + "<!-- ".repeat(MB5 / 5), | |
| 111 | "bare angle brackets": "<r>" + "< ".repeat(MB5 / 2), | |
| 112 | "deep opens": "<a>".repeat(MB5 / 3), | |
| 113 | }; | |
| 114 | for (const [name, text] of Object.entries(cases)) | |
| 115 | test(name, () => { | |
| 116 | const t = performance.now(); | |
| 117 | parseXml(text); | |
| 118 | xml(input("x.xml", text)); | |
| 119 | expect(performance.now() - t).toBeLessThan(500); | |
| 120 | }); | |
| 121 | test("a tag after a hostile prefix is still read", () => { | |
| 122 | expect(parseXml('<!-- a --><r k="v"><c/></r>')?.root.children.length).toBe(1); | |
| 123 | }); | |
| 124 | }); |
test/render-junit.test.ts +1 −1
| @@ -24,7 +24,7 @@ describe("junit", () => { | ||
| 24 | 24 | expect(fileKind("x/TEST-foo.xml", enc("<a/>"))).toBe("junit"); |
| 25 | 25 | expect(fileKind("x/report.xml", enc('<?xml version="1.0"?><!-- c --><testsuite name="a"/>'))).toBe("junit"); |
| 26 | 26 | expect(fileKind("x/report.xml", enc("<testsuites/>"))).toBe("junit"); |
| 27 | expect(fileKind("x/pom.xml", enc("<project><testsuite/></project>"))).toBe("text"); | |
| 27 | expect(fileKind("x/pom.xml", enc("<project><testsuite/></project>"))).toBe("xml"); | |
| 28 | 28 | expect(fileKind("x/report.txt", enc("<testsuite/>"))).toBe("text"); |
| 29 | 29 | expect(RENDERERS.junit).toBe(junit); |
| 30 | 30 | }); |
test/render-kinds.test.ts +4 −1
| @@ -56,7 +56,7 @@ const SAMPLES: Record<string, [Uint8Array, FileKind, string]> = { | ||
| 56 | 56 | yaml: [enc("a: 1\n"), "config", "application/octet-stream"], |
| 57 | 57 | yml: [enc("a: 1\n"), "config", "application/octet-stream"], |
| 58 | 58 | toml: [enc("a = 1\n"), "config", "application/octet-stream"], |
| 59 | ini: [enc("[a]\n"), "config", "application/octet-stream"], | |
| 59 | ini: [enc("[a]\n"), "ini", "application/octet-stream"], | |
| 60 | 60 | conf: [enc("a 1\n"), "config", "application/octet-stream"], |
| 61 | 61 | ttf: [of(0, 1, 0, 0, 0, 0), "font", "font/ttf"], |
| 62 | 62 | otf: [enc("OTTO\0\0"), "font", "font/otf"], |
| @@ -104,6 +104,9 @@ const SAMPLES: Record<string, [Uint8Array, FileKind, string]> = { | ||
| 104 | 104 | graphql: [enc("type A { b: Int }\n"), "graphql", "application/octet-stream"], |
| 105 | 105 | gql: [enc("type A { b: Int }\n"), "graphql", "application/octet-stream"], |
| 106 | 106 | mbox: [enc("From [email protected] Mon Jan 1 00:00:00 2024\nSubject: x\n\nhi\n"), "mbox", "application/octet-stream"], |
| 107 | properties: [enc("a=b\n"), "ini", "application/octet-stream"], | |
| 108 | rtf: [enc("{\\rtf1 hi}"), "rtf", "application/octet-stream"], | |
| 109 | xml: [enc("<a/>"), "xml", "application/octet-stream"], | |
| 107 | 110 | proto: [enc('syntax = "proto3";\nmessage A {}\n'), "proto", "application/octet-stream"], |
| 108 | 111 | sarif: [enc('{"runs":[]}'), "sarif", "application/octet-stream"], |
| 109 | 112 | opml: [enc("<opml/>"), "opml", "application/octet-stream"], |
test/tui-workspace-formats.test.ts +30
| @@ -645,6 +645,36 @@ describe("the terminal workspace, by file type", () => { | ||
| 645 | 645 | } |
| 646 | 646 | }); |
| 647 | 647 | |
| 648 | test("an .ini, .rtf and plain .xml open as settings, paragraphs and an element tree", async () => { | |
| 649 | const made = await createRepo(forge, alice, { name: "inirepo", visibility: "public", description: "i" }); | |
| 650 | if (!made.ok) throw new Error("repo failed"); | |
| 651 | const files = [ | |
| 652 | { path: "a.ini", content: "[core]\nname = Widget\napi_token = hunter2\n" }, | |
| 653 | { path: "n.rtf", content: "{\\rtf1\\ansi Hello rich world\\par}" }, | |
| 654 | { path: "d.xml", content: '<config><item id="7">first value</item></config>' }, | |
| 655 | ]; | |
| 656 | await commitToBranch(forge, alice, made.value, { message: "seed", files }); | |
| 657 | const h = boot(); | |
| 658 | try { | |
| 659 | await h.settle(); | |
| 660 | await open(h, "inirepo"); | |
| 661 | await openFile(h, "a.ini"); | |
| 662 | expect(h.text()).toContain("Widget"); | |
| 663 | expect(h.text()).toContain("•••"); | |
| 664 | expect(h.text()).not.toContain("hunter2"); | |
| 665 | await h.press("escape"); | |
| 666 | await openFile(h, "n.rtf"); | |
| 667 | expect(h.text()).toContain("Hello rich world"); | |
| 668 | await h.press("escape"); | |
| 669 | await openFile(h, "d.xml"); | |
| 670 | expect(h.text()).toContain("config"); | |
| 671 | expect(h.text()).toContain('id="7"'); | |
| 672 | expect(h.text()).toContain("first value"); | |
| 673 | } finally { | |
| 674 | h.stop(); | |
| 675 | } | |
| 676 | }); | |
| 677 | ||
| 648 | 678 | test("a JUnit report opens as its totals and failing cases, sharing the web parser", async () => { |
| 649 | 679 | const made = await createRepo(forge, alice, { name: "junitrepo", visibility: "public", description: "tests" }); |
| 650 | 680 | if (!made.ok) throw new Error("repo failed"); |
'Not done: the terminal still shows these as source' is the both-doors miss: a screen in src/tui/ is required, not deferred.