Public: anyone can read this; the crew can change it.
A .ics calendar file opens as a list of its events with date, title and place #14145
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/14123-a-ics-calendar-file-opens-as-a-list-of-i → main 2 commits fork
3 files
changed +66
−10
src/web/render/ics.ts +9 −2
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * A calendar file (iCalendar `.ics`, RFC 5545) as a list of its events: start, |
| 3 | * end (or "all day"), summary and location, in the file's own order. Lines are unfolded | |
| 3 | * end (or "all day"), summary and location, sorted by start as written (the | |
| 4 | * zone is named, never converted; equal starts keep the file's order). Lines are unfolded | |
| 4 | 5 | * (§3.1), VEVENT blocks read, DTSTART/DTEND taken with or without TZID, and |
| 5 | 6 | * TEXT escapes (`\,` `\;` `\\` `\n`) undone. Every value is escaped on output. |
| 6 | 7 | * A file that is not a calendar falls back to its source; an event that cannot |
| @@ -12,7 +13,7 @@ import { fileSize } from "../ui.ts"; | ||
| 12 | 13 | import type { Rendered, RenderInput } from "./renderers.ts"; |
| 13 | 14 | |
| 14 | 15 | /** Events drawn, whatever the file holds. */ |
| 15 | export const EVENT_CAP = 500; | |
| 16 | export const EVENT_CAP = 200; | |
| 16 | 17 | |
| 17 | 18 | export interface IcsEvent { |
| 18 | 19 | /** `2026-03-01 09:00`, `2026-03-01`, with a zone note when the file gave one. */ |
| @@ -133,6 +134,12 @@ export function parseIcs(source: string): Calendar { | ||
| 133 | 134 | } |
| 134 | 135 | } |
| 135 | 136 | if (props !== undefined) skipped++; |
| 137 | // `2026-03-01` or `2026-03-01 09:00`: the first 16 characters order as written. | |
| 138 | events.sort((a, b) => { | |
| 139 | const x = a.start.slice(0, 16); | |
| 140 | const y = b.start.slice(0, 16); | |
| 141 | return x < y ? -1 : x > y ? 1 : 0; | |
| 142 | }); | |
| 136 | 143 | return { events, skipped }; |
| 137 | 144 | } |
| 138 | 145 |
test/ics-render.test.ts added +49
| @@ -0,0 +1,49 @@ | ||
| 1 | /** The calendar view: sorted by start as written, folded lines, a hostile summary, the 200 cap. */ | |
| 2 | import { expect, test } from "bun:test"; | |
| 3 | import { EVENT_CAP, ics, parseIcs } from "../src/web/render/ics.ts"; | |
| 4 | ||
| 5 | const draw = (text: string) => | |
| 6 | ics({ path: "cal.ics", size: text.length, body: new TextEncoder().encode(text), media: "/m", download: "/d" }); | |
| 7 | const wrap = (...events: string[]) => | |
| 8 | `BEGIN:VCALENDAR\r\n${events.map((e) => `BEGIN:VEVENT\r\n${e}\r\nEND:VEVENT\r\n`).join("")}END:VCALENDAR\r\n`; | |
| 9 | ||
| 10 | test("events are sorted by start as written; the zone is named, not converted", () => { | |
| 11 | const cal = parseIcs( | |
| 12 | wrap( | |
| 13 | "DTSTART;TZID=Europe/Paris:20260302T140000\r\nSUMMARY:c", | |
| 14 | "DTSTART:20260301T090000Z\r\nSUMMARY:b", | |
| 15 | "DTSTART;VALUE=DATE:20260301\r\nSUMMARY:a", | |
| 16 | ), | |
| 17 | ); | |
| 18 | if ("error" in cal) throw new Error(cal.error); | |
| 19 | expect(cal.events.map((e) => e.summary)).toEqual(["a", "b", "c"]); | |
| 20 | expect(cal.events[1]!.start).toBe("2026-03-01 09:00 UTC"); | |
| 21 | expect(cal.events[2]!.start).toBe("2026-03-02 14:00 Europe/Paris"); | |
| 22 | }); | |
| 23 | ||
| 24 | test("folded lines and escaped commas read as one value", () => { | |
| 25 | const cal = parseIcs(wrap("DTSTART:20260301T090000Z\r\nSUMMARY:Plan\\, build and a long\r\n title\r\nLOCATION:Room\\, 1")); | |
| 26 | if ("error" in cal) throw new Error(cal.error); | |
| 27 | expect(cal.events[0]!.summary).toBe("Plan, build and a long title"); | |
| 28 | expect(cal.events[0]!.location).toBe("Room, 1"); | |
| 29 | }); | |
| 30 | ||
| 31 | test("a hostile summary and location are escaped", () => { | |
| 32 | const out = String(draw(wrap("DTSTART:20260301T090000Z\r\nSUMMARY:<script>alert(1)</script>\r\nLOCATION:\"><img src=x onerror=y>")).content); | |
| 33 | expect(out).not.toContain("<script>"); | |
| 34 | expect(out).not.toContain("<img"); | |
| 35 | expect(out).toContain("<script>"); | |
| 36 | }); | |
| 37 | ||
| 38 | test("more than 200 events show the first 200 sorted, with a count", () => { | |
| 39 | expect(EVENT_CAP).toBe(200); | |
| 40 | const many = Array.from({ length: 205 }, (_, i) => `DTSTART:2026${String(12 - (i % 12)).padStart(2, "0")}01T090000Z\r\nSUMMARY:e${i}`); | |
| 41 | const r = draw(wrap(...many)); | |
| 42 | const out = String(r.content); | |
| 43 | expect(out.match(/<tr><td>/g)?.length).toBe(200); | |
| 44 | expect(out).toContain("Showing 200 of 205 events, 5 more"); | |
| 45 | }); | |
| 46 | ||
| 47 | test("a file that is not a calendar falls back with one reason", () => { | |
| 48 | expect(draw("hello").fallback).toContain("not a calendar"); | |
| 49 | }); |
test/render-ics.test.ts +8 −8
| @@ -40,8 +40,8 @@ describe("calendar files", () => { | ||
| 40 | 40 | ); |
| 41 | 41 | if ("error" in cal) throw new Error(cal.error); |
| 42 | 42 | expect(cal.events.map((e) => [e.allDay, e.start, e.end])).toEqual([ |
| 43 | [true, "2026-03-01", ""], | |
| 44 | 43 | [true, "2026-02-28", "2026-03-02"], |
| 44 | [true, "2026-03-01", ""], | |
| 45 | 45 | ]); |
| 46 | 46 | const out = String(draw(wrap("DTSTART;VALUE=DATE:20260301\r\nSUMMARY:X")).content); |
| 47 | 47 | expect(out).toContain("all day"); |
| @@ -62,14 +62,14 @@ describe("calendar files", () => { | ||
| 62 | 62 | expect(r.summary).toContain("1 event"); |
| 63 | 63 | }); |
| 64 | 64 | |
| 65 | test("events keep the file's own order; non-VEVENT parts are ignored", () => { | |
| 65 | test("events sort by start; non-VEVENT parts are ignored", () => { | |
| 66 | 66 | const cal = parseIcs( |
| 67 | 67 | "BEGIN:VCALENDAR\r\nBEGIN:VTODO\r\nDTSTART:20250101T000000Z\r\nSUMMARY:todo\r\nEND:VTODO\r\n" + |
| 68 | 68 | "BEGIN:VEVENT\r\nDTSTART:20260305T090000Z\r\nSUMMARY:late\r\nEND:VEVENT\r\n" + |
| 69 | 69 | "BEGIN:VEVENT\r\nDTSTART;VALUE=DATE:20260301\r\nSUMMARY:early\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n", |
| 70 | 70 | ); |
| 71 | 71 | if ("error" in cal) throw new Error(cal.error); |
| 72 | expect(cal.events.map((e) => e.summary)).toEqual(["late", "early"]); | |
| 72 | expect(cal.events.map((e) => e.summary)).toEqual(["early", "late"]); | |
| 73 | 73 | }); |
| 74 | 74 | |
| 75 | 75 | test("a malformed file shows the rows it could read and one line about the rest", () => { |
| @@ -85,12 +85,12 @@ describe("calendar files", () => { | ||
| 85 | 85 | expect(String(open.content)).toContain("1 event could not be read"); |
| 86 | 86 | }); |
| 87 | 87 | |
| 88 | test("events in different zones stay in file order, with no note", () => { | |
| 88 | test("events in different zones sort as written, never converted", () => { | |
| 89 | 89 | const out = String( |
| 90 | 90 | draw(wrap("DTSTART:20260301T090000Z\r\nSUMMARY:a", "DTSTART;TZID=Europe/Paris:20260301T080000\r\nSUMMARY:b")).content, |
| 91 | 91 | ); |
| 92 | 92 | expect(out).not.toContain("time zones"); |
| 93 | expect(out.indexOf(">a<")).toBeLessThan(out.indexOf(">b<")); | |
| 93 | expect(out.indexOf(">b<")).toBeLessThan(out.indexOf(">a<")); | |
| 94 | 94 | }); |
| 95 | 95 | |
| 96 | 96 | test("malformed files fall back to text", () => { |
| @@ -101,11 +101,11 @@ describe("calendar files", () => { | ||
| 101 | 101 | expect(draw("BEGIN:VCALENDAR\r\nEND:VCALENDAR\r\n").fallback).toContain("no events"); |
| 102 | 102 | }); |
| 103 | 103 | |
| 104 | test("capped at 500 events, saying how many more", () => { | |
| 104 | test("capped at 200 events, saying how many more", () => { | |
| 105 | 105 | const many = Array.from({ length: EVENT_CAP + 7 }, (_, i) => `DTSTART:20260301T090000Z\r\nSUMMARY:e${i}`); |
| 106 | 106 | const out = String(draw(wrap(...many)).content); |
| 107 | expect(out).toContain("e499<"); | |
| 108 | expect(out).not.toContain("e500<"); | |
| 107 | expect(out).toContain("e199<"); | |
| 108 | expect(out).not.toContain("e200<"); | |
| 109 | 109 | expect(out).toContain("7 more"); |
| 110 | 110 | }); |
| 111 | 111 | }); |
new above
offline