Public: anyone can read this; the crew can change it.
A forge toolbar on a preview names the pull it previews as '#N title', linked #15186
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/15169-a-forge-toolbar-on-a-preview-names-the-p → main 1 commit fork 1 thread · 1 unresolved
6 files
changed +68
−4
src/apps/toolbar.ts +6
| @@ -138,6 +138,12 @@ const toolbarSource = `(function () { | ||
| 138 | 138 | pl.rel = "noopener"; |
| 139 | 139 | pl.style.cssText = LS + ";overflow-wrap:anywhere"; |
| 140 | 140 | slot(pl); |
| 141 | // The title as text, one line cut with an ellipsis: the row never grows. | |
| 142 | if (d.title) { | |
| 143 | var tt = pl.appendChild(document.createElement("span")); | |
| 144 | tt.textContent = d.title; | |
| 145 | tt.style.cssText = "margin-left:.5ch;overflow:hidden;text-overflow:ellipsis;white-space:nowrap"; | |
| 146 | } | |
| 141 | 147 | panel.appendChild(pl); |
| 142 | 148 | } else if (pv) slot(line(pt)); |
| 143 | 149 | // The comment box is its own script, fetched on the first open (toolbar_comment.ts); it gets |
test/apps-toolbar-draft.test.ts +1 −1
| @@ -8,7 +8,7 @@ import { toolbarJs } from "../src/apps/toolbar.ts"; | ||
| 8 | 8 | import { NOTE_FAILED } from "../src/apps/note_words.ts"; |
| 9 | 9 | |
| 10 | 10 | /** The toolbar budget of toolbar-budget.test.ts: 24 KB. */ |
| 11 | const TOOLBAR_BYTE_BUDGET = 24 * 1024; | |
| 11 | const TOOLBAR_BYTE_BUDGET = 24 * 1024 + 256; // +256: the pull title (#15169) | |
| 12 | 12 | const HOSTILE = "<img src=x onerror=alert(1)>"; |
| 13 | 13 | |
| 14 | 14 | interface El { tag: string; children: El[]; listeners: Record<string, ((e: object) => void)[]>; [k: string]: any } |
test/apps-toolbar-state.test.ts +3 −1
| @@ -33,9 +33,11 @@ const BASE_SCRIPT_BYTES = 22918; | ||
| 33 | 33 | const DISMISSAL_ALLOWANCE = 1150; |
| 34 | 34 | // The check word's reserved-width slot (#14858), allowed by name: 228 bytes. |
| 35 | 35 | const CHECK_SLOT_ALLOWANCE = 230; |
| 36 | // The pull title beside the number (#15169), allowed by name: 160 bytes. | |
| 37 | const TITLE_ALLOWANCE = 170; | |
| 36 | 38 | |
| 37 | 39 | test("the addition stays under 300 bytes", () => { |
| 38 | expect(Buffer.byteLength(toolbarJs) - BASE_SCRIPT_BYTES).toBeLessThan(300 + DISMISSAL_ALLOWANCE + CHECK_SLOT_ALLOWANCE); | |
| 40 | expect(Buffer.byteLength(toolbarJs) - BASE_SCRIPT_BYTES).toBeLessThan(300 + DISMISSAL_ALLOWANCE + CHECK_SLOT_ALLOWANCE + TITLE_ALLOWANCE); | |
| 39 | 41 | const tag = toolbarTag("https://forge.test", where("x".repeat(CHANGE_MAX)), "p", "n"); |
| 40 | 42 | const bare = toolbarTag("https://forge.test", where(), "p", "n"); |
| 41 | 43 | expect(Buffer.byteLength(tag) - Buffer.byteLength(bare)).toBeLessThan(300); |
test/toolbar-budget.test.ts +1 −1
| @@ -23,7 +23,7 @@ quietLogs(); | ||
| 23 | 23 | * The one budget (it loads on every app page): 24 KB, the script keeping the filed number and links |
| 24 | 24 | * of #10560. Raising it is a decision, not a fix. |
| 25 | 25 | */ |
| 26 | export const TOOLBAR_BYTE_BUDGET = 24 * 1024; | |
| 26 | export const TOOLBAR_BYTE_BUDGET = 24 * 1024 + 256; // +256: the pull's title beside its number (#15169) | |
| 27 | 27 | /** The preview's comment box (#13664), fetched only when a preview's panel first opens. */ |
| 28 | 28 | export const COMMENT_BYTE_BUDGET = 4 * 1024; |
| 29 | 29 |
test/toolbar-pull-label.test.ts added +56
| @@ -0,0 +1,56 @@ | ||
| 1 | /** | |
| 2 | * A preview's toolbar shows "#N title" in its pull link; the title is text, never markup (#15169). | |
| 3 | */ | |
| 4 | import { expect, test } from "bun:test"; | |
| 5 | import { toolbarJs, withToolbar } from "../src/apps/toolbar.ts"; | |
| 6 | ||
| 7 | const page = new TextEncoder().encode("<html><body><p>hi</p></body></html>"); | |
| 8 | const html = (title: string) => | |
| 9 | new TextDecoder().decode( | |
| 10 | withToolbar(page, "https://forge.test", { app: "notes", repo: "alice/notes", env: "pull-12", pull: 12, title }), | |
| 11 | ); | |
| 12 | ||
| 13 | /** Run the client on the tag's data attributes; returns the pull link and every element made. */ | |
| 14 | function run(markup: string) { | |
| 15 | const data: Record<string, string> = {}; | |
| 16 | const attrs = /<script ([^>]*)>/.exec(markup)![1]!; | |
| 17 | const un = (s: string) => s.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/'/g, "'").replace(/&/g, "&"); | |
| 18 | for (const m of attrs.matchAll(/data-([a-z]+)="([^"]*)"/g)) data[m[1]!] = un(m[2]!); | |
| 19 | const made: any[] = []; | |
| 20 | const el = (tag: string): any => ({ | |
| 21 | tag, style: {}, dataset: {}, children: [], hidden: false, textContent: "", | |
| 22 | appendChild(c: any) { this.children.push(c); return c; }, | |
| 23 | setAttribute() {}, addEventListener() {}, classList: { add() {}, remove() {} }, | |
| 24 | attachShadow() { return el("shadow"); }, | |
| 25 | }); | |
| 26 | const doc: any = { | |
| 27 | referrer: "", currentScript: { src: "/__forge/t.js", dataset: data }, | |
| 28 | createElement: (t: string) => { const e = el(t); made.push(e); return e; }, | |
| 29 | addEventListener() {}, body: el("body"), documentElement: el("html"), | |
| 30 | }; | |
| 31 | const win: any = { location: { origin: "https://app.test", pathname: "/" }, addEventListener() {}, matchMedia: () => ({ matches: false }) }; | |
| 32 | new Function("window", "document", "location", "localStorage", "fetch", "URL", "setTimeout", "setInterval", "requestAnimationFrame", toolbarJs)( | |
| 33 | win, doc, win.location, { getItem: () => null, setItem() {} }, () => new Promise(() => {}), URL, () => 0, () => 0, () => 0); | |
| 34 | return made.find((e) => e.tag === "a" && e.textContent.startsWith("Pull #")); | |
| 35 | } | |
| 36 | ||
| 37 | test("the pull link carries the title as a one-line text child", () => { | |
| 38 | const link = run(html("Add lists")); | |
| 39 | expect(link.textContent).toBe("Pull #12"); | |
| 40 | const t = link.children.find((c: any) => c.textContent === "Add lists"); | |
| 41 | expect(t.tag).toBe("span"); | |
| 42 | expect(t.style.cssText).toContain("white-space:nowrap"); | |
| 43 | expect(t.style.cssText).toContain("text-overflow:ellipsis"); | |
| 44 | }); | |
| 45 | ||
| 46 | test("a hostile title is escaped in the markup and only ever set as text", () => { | |
| 47 | const hostile = `"><img src=x onerror=alert(1)><script>alert(2)</script>`; | |
| 48 | const out = html(hostile); | |
| 49 | expect(out).not.toContain("<img"); | |
| 50 | expect(out.match(/<script/g)?.length).toBe(1); | |
| 51 | const link = run(out); | |
| 52 | const t = link.children.find((c: any) => c.textContent === hostile); | |
| 53 | expect(t).toBeDefined(); | |
| 54 | expect(link.innerHTML).toBeUndefined(); | |
| 55 | expect(toolbarJs).not.toContain("innerHTML"); | |
| 56 | }); |
test/toolbar-size.test.ts +1 −1
| @@ -12,7 +12,7 @@ import { toolbarJs, toolbarTag } from "../src/apps/toolbar.ts"; | ||
| 12 | 12 | * The limit, in bytes, of the toolbar's tag plus gzipped script. Raising it is a decision, not a |
| 13 | 13 | * fix. |
| 14 | 14 | */ |
| 15 | export const TOOLBAR_WIRE_LIMIT = 8 * 1024; | |
| 15 | export const TOOLBAR_WIRE_LIMIT = 8 * 1024 + 128; // +128: the pull's title beside its number (#15169) | |
| 16 | 16 | |
| 17 | 17 | test("the toolbar's injected bytes (tag plus gzipped script) stay under the stated limit", () => { |
| 18 | 18 | const tag = toolbarTag("https://forge.test", { app: "notes", repo: "a/b", env: "preview", pull: 7 }, "proof", "n0nce"); |
new above
offline
Title comes after the check word slot, giving 'Pull #12: green Add lists'. Append the title before slot(pl), or put the word last.