Public: anyone can read this; the crew can change it.
Every pop-out link opens in a new tab: one helper draws the icon and the attributes together #16353
Check · Review · Land · Live done — Landed and live in production.
ui/popout-new-tab → main 0 commits
9 files
changed +101
−27
src/web/comments.ts +2 −7
| @@ -9,7 +9,7 @@ import type { Mark } from "../core/marks.ts"; | ||
| 9 | 9 | import { emptyNote } from "./empty_words.ts"; |
| 10 | 10 | import { html, when, type Html } from "./html.ts"; |
| 11 | 11 | import { renderMarkdown } from "./markdown.ts"; |
| 12 | import { icon, originTag, time, userLink, type IconName } from "./ui.ts"; | |
| 12 | import { originTag, popout, time, userLink, type IconName } from "./ui.ts"; | |
| 13 | 13 | |
| 14 | 14 | export interface CommentCard { |
| 15 | 15 | /** "" for the opener (the issue/PR body), which has no comment row. */ |
| @@ -46,12 +46,7 @@ export function commentCard(card: CommentCard): Html { | ||
| 46 | 46 | ${when( |
| 47 | 47 | card.originUrl != null && card.originUrl !== "", |
| 48 | 48 | () => html`<span class="spacer"></span> |
| 49 | <a | |
| 50 | class="bar muted small" | |
| 51 | href="${card.originUrl ?? ""}" | |
| 52 | rel="noreferrer noopener" | |
| 53 | >${icon("external")}view on origin</a | |
| 54 | >`, | |
| 49 | ${popout(card.originUrl ?? "", "view on origin", "bar muted small")}`, | |
| 55 | 50 | )} |
| 56 | 51 | </div> |
| 57 | 52 | <div class="prose comment-body">${bodyHtml}</div> |
src/web/pages/browse.ts +2 −3
| @@ -77,6 +77,7 @@ import { | ||
| 77 | 77 | tlComment, |
| 78 | 78 | userTag, |
| 79 | 79 | type IconName, |
| 80 | popout, | |
| 80 | 81 | } from "../ui.ts"; |
| 81 | 82 | import { browseCommit, browseCommits } from "./commits.ts"; |
| 82 | 83 | import { blankDraft, editorPage, runContribute } from "./edit.ts"; |
| @@ -728,9 +729,7 @@ async function browseThread( | ||
| 728 | 729 | ${when( |
| 729 | 730 | detail.issue.originUrl, |
| 730 | 731 | (url) => |
| 731 | html`<a class="bar muted small" href="${url}" rel="noreferrer noopener" | |
| 732 | >${icon("external")}view on origin</a | |
| 733 | >`, | |
| 732 | popout(url, "view on origin", "bar muted small"), | |
| 734 | 733 | )} |
| 735 | 734 | ${when( |
| 736 | 735 | detail.pull, |
src/web/scope.ts +2 −8
| @@ -45,7 +45,7 @@ import { repoSteps } from "./repo_steps.ts"; | ||
| 45 | 45 | import { icon, type IconName } from "./icons.ts"; |
| 46 | 46 | import type { PaletteItem } from "./layout.ts"; |
| 47 | 47 | import { originIcon } from "./pages/origins.ts"; |
| 48 | import { time, timeAgo } from "./ui.ts"; | |
| 48 | import { popout, time, timeAgo } from "./ui.ts"; | |
| 49 | 49 | |
| 50 | 50 | /** Which side of the world a page is reading. */ |
| 51 | 51 | export type ScopeKind = "local" | "origin"; |
| @@ -634,13 +634,7 @@ export function browseActions(scope: RepoScope): Html { | ||
| 634 | 634 | const away = |
| 635 | 635 | scope.originUrl === "" |
| 636 | 636 | ? null |
| 637 | : html`<a | |
| 638 | class="menu-item" | |
| 639 | href="${scope.originUrl}" | |
| 640 | target="_blank" | |
| 641 | rel="noopener noreferrer" | |
| 642 | >${icon("external", 16)}<span class="grow">Open on ${host}</span></a | |
| 643 | >`; | |
| 637 | : popout(scope.originUrl, html`<span class="grow">Open on ${host}</span>`, "menu-item"); | |
| 644 | 638 | if (fork.text.trim() === "" && away === null) return primary; |
| 645 | 639 | return html`${primary}<details class="menu" data-align="end"> |
| 646 | 640 | <summary class="btn" data-variant="ghost" data-size="sm" aria-label="More actions">More</summary> |
src/web/studio/change.ts +2 −3
| @@ -65,6 +65,7 @@ import { | ||
| 65 | 65 | userLink, |
| 66 | 66 | verdictBadge, |
| 67 | 67 | type Tone, |
| 68 | popout, | |
| 68 | 69 | } from "../ui.ts"; |
| 69 | 70 | import { previewBlock } from "../pages/apps.ts"; |
| 70 | 71 | import { filmstrip, filmstripJs } from "../pages/filmstrip.ts"; |
| @@ -293,9 +294,7 @@ export function changeHead( | ||
| 293 | 294 | ${when( |
| 294 | 295 | pull.originUrl, |
| 295 | 296 | (url) => |
| 296 | html`<a class="bar muted small" href="${url}" rel="noreferrer noopener" | |
| 297 | >${icon("external")}view on origin</a | |
| 298 | >`, | |
| 297 | popout(url, "view on origin", "bar muted small"), | |
| 299 | 298 | )} |
| 300 | 299 | ${workSpent(ctx, repo, pull, "pull")}${when( |
| 301 | 300 | proposal, |
src/web/studio/tell.ts +2 −3
| @@ -73,6 +73,7 @@ import { | ||
| 73 | 73 | stateBadge, |
| 74 | 74 | time, |
| 75 | 75 | userLink, |
| 76 | popout, | |
| 76 | 77 | } from "../ui.ts"; |
| 77 | 78 | import { previewBlock } from "../pages/apps.ts"; |
| 78 | 79 | import { |
| @@ -560,9 +561,7 @@ async function studioTell( | ||
| 560 | 561 | ${when( |
| 561 | 562 | issue.originUrl, |
| 562 | 563 | (url) => |
| 563 | html`<a class="bar muted small" href="${url}" rel="noreferrer noopener" | |
| 564 | >${icon("external")}view on origin</a | |
| 565 | >`, | |
| 564 | popout(url, "view on origin", "bar muted small"), | |
| 566 | 565 | )} |
| 567 | 566 | ${when( |
| 568 | 567 | issue.sealed, |
src/web/ui.ts +10 −1
| @@ -177,13 +177,22 @@ function btnAttrs(o: BtnOptions): Html { | ||
| 177 | 177 | ${o.pressed !== undefined ? html`aria-pressed="${o.pressed ? "true" : "false"}"` : null}${o.current ? raw('aria-current="page"') : null}`; |
| 178 | 178 | } |
| 179 | 179 | |
| 180 | /** | |
| 181 | * A link that leaves the page, drawn with the pop-out icon: it always opens in a new tab, with no | |
| 182 | * opener or referrer. Every pop-out link goes through here or through `btn` with that icon, so | |
| 183 | * none can forget it (and in a frame a same-tab link to an app is refused by the app's policy). | |
| 184 | */ | |
| 185 | export function popout(href: string, content: Html | string, cls = ""): Html { | |
| 186 | return html`<a${cls === "" ? null : html` class="${cls}"`} href="${href}" target="_blank" rel="noopener noreferrer">${icon("external")}${content}</a>`; | |
| 187 | } | |
| 188 | ||
| 180 | 189 | /** A button or a link that looks like one. Content is the visible label. */ |
| 181 | 190 | export function btn(content: Html | string, o: BtnOptions = {}): Html { |
| 182 | 191 | const inner = html`${o.icon ? icon(o.icon) : null}${content}`; |
| 183 | 192 | if (o.href !== undefined) { |
| 184 | 193 | return html`<a |
| 185 | 194 | href="${o.href}" |
| 186 | ${o.external ? raw('target="_blank" rel="noopener noreferrer"') : o.rel ? html`rel="${o.rel}"` : null} | |
| 195 | ${o.external || o.icon === "external" ? raw('target="_blank" rel="noopener noreferrer"') : o.rel ? html`rel="${o.rel}"` : null} | |
| 187 | 196 | ${btnAttrs(o)} |
| 188 | 197 | >${inner}</a |
| 189 | 198 | >`; |
test/popout-new-tab.test.ts added +79
| @@ -0,0 +1,79 @@ | ||
| 1 | /** | |
| 2 | * A link drawn with the pop-out icon opens in a new tab, never in the page it is on: in the | |
| 3 | * Talk pane a same-tab link to an app is refused by the app's frame policy and looks broken. | |
| 4 | * `popout` and `btn` with the icon draw the attributes with it; this holds every rendered | |
| 5 | * page, and the source, to that. | |
| 6 | */ | |
| 7 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 8 | import { mkdtemp, readdir, readFile, rm } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { openIssue } from "../src/actions/issues.ts"; | |
| 13 | import { createRepo } from "../src/actions/repos.ts"; | |
| 14 | import { createSession } from "../src/core/sessions.ts"; | |
| 15 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 16 | import { btn, popout } from "../src/web/ui.ts"; | |
| 17 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 18 | import { quietLogs } from "./helpers/globals.ts"; | |
| 19 | ||
| 20 | quietLogs(); | |
| 21 | ||
| 22 | let dir = ""; | |
| 23 | let app: App, servers: Servers, cookie = "", issuePath = ""; | |
| 24 | ||
| 25 | beforeAll(async () => { | |
| 26 | dir = await mkdtemp(join(tmpdir(), "forge-popout-")); | |
| 27 | app = createApp({ dataDir: dir, port: 0 }); | |
| 28 | servers = await serve(app); | |
| 29 | const made = await seedAdmin(app, { username: "alice", email: "[email protected]", password: "a-good-passphrase" }); | |
| 30 | if (!made.ok) throw new Error(made.error); | |
| 31 | cookie = `forge_session=${createSession(app.db, made.value.id).token}`; | |
| 32 | const repo = await createRepo(app, made.value, { name: "vault" }); | |
| 33 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 34 | // An issue that came from another site carries a "view on origin" link. | |
| 35 | const issue = openIssue(app, made.value, repo.value, { title: "From elsewhere" }); | |
| 36 | if (!issue.ok) throw new Error(String(issue.error)); | |
| 37 | app.db.query("UPDATE issue SET origin_html_url = 'https://elsewhere.test/1' WHERE id = ?").run(issue.value.id); | |
| 38 | issuePath = `/alice/vault/issues/${issue.value.number}`; | |
| 39 | }); | |
| 40 | ||
| 41 | afterAll(async () => { | |
| 42 | servers.stop(); | |
| 43 | app.close(); | |
| 44 | await rm(dir, { recursive: true, force: true }); | |
| 45 | }); | |
| 46 | ||
| 47 | /** Every anchor in `html` that carries the pop-out icon, as its opening tag. */ | |
| 48 | const popoutLinks = (html: string): string[] => | |
| 49 | [...html.matchAll(/<a\b[^>]*>(?:(?!<\/a\s*>)[\s\S])*?ico-external/g)].map((m) => m[0].slice(0, m[0].indexOf(">") + 1)); | |
| 50 | ||
| 51 | test("the helpers draw the icon and the new-tab attributes together", () => { | |
| 52 | for (const html of [String(popout("https://x.test", "Open")), String(btn("Open", { href: "/a", icon: "external" })), String(btn("Open", { href: "/a", external: true }))]) { | |
| 53 | expect(html).toContain('target="_blank"'); | |
| 54 | expect(html).toContain('rel="noopener noreferrer"'); | |
| 55 | } | |
| 56 | expect(String(popout("https://x.test", "Open"))).toContain("ico-external"); | |
| 57 | }); | |
| 58 | ||
| 59 | test("every rendered link with the pop-out icon opens in a new tab", async () => { | |
| 60 | let seen = 0; | |
| 61 | for (const path of ["/", "/apps", "/talk", "/alice/vault", "/alice/vault/pulls", "/alice/vault/issues", issuePath]) { | |
| 62 | const html = await (await fetch(`${servers.url}${path}`, { headers: { cookie } })).text(); | |
| 63 | for (const tag of popoutLinks(html)) { | |
| 64 | seen++; | |
| 65 | expect(tag, path).toContain('target="_blank"'); | |
| 66 | expect(tag, path).toContain("noopener"); | |
| 67 | } | |
| 68 | } | |
| 69 | expect(seen).toBeGreaterThan(0); | |
| 70 | }); | |
| 71 | ||
| 72 | test("no source draws the pop-out icon by hand beside a link", async () => { | |
| 73 | const files = (await readdir("src/web", { recursive: true })).filter((f) => f.endsWith(".ts") && f !== "ui.ts" && f !== "icons.ts"); | |
| 74 | const stray: string[] = []; | |
| 75 | for (const f of files) | |
| 76 | for (const [i, line] of (await readFile(join("src/web", f), "utf8")).split("\n").entries()) | |
| 77 | if (/icon\("external"/.test(line) && !/btn\(|popout\(/.test(line)) stray.push(`src/web/${f}:${i + 1}`); | |
| 78 | expect(stray).toEqual([]); | |
| 79 | }); |
test/web-origin-link.test.ts +1 −1
| @@ -20,7 +20,7 @@ quietLogs(); | ||
| 20 | 20 | |
| 21 | 21 | const URL_AT_ORIGIN = "https://origin.example/octo/widgets/pulls/3?a=1&b=2"; |
| 22 | 22 | const anchor = |
| 23 | /<a\s+class="bar muted small"\s+href="https:\/\/origin\.example\/octo\/widgets\/pulls\/3\?a=1&b=2"\s+rel="noreferrer noopener"/; | |
| 23 | /<a\s+class="bar muted small"\s+href="https:\/\/origin\.example\/octo\/widgets\/pulls\/3\?a=1&b=2"\s+target="_blank"\s+rel="noopener noreferrer"/; | |
| 24 | 24 | |
| 25 | 25 | let dataDir: string; |
| 26 | 26 | let app: App; |
test/web-origins.test.ts +1 −1
| @@ -557,7 +557,7 @@ describe("origins on the web", () => { | ||
| 557 | 557 | // §5's muted small text, not the retired .origin-link. |
| 558 | 558 | expect(page).not.toContain("origin-link"); |
| 559 | 559 | expect(page).toMatch( |
| 560 | /<a class="bar muted small" href="[^"]+" rel="noreferrer noopener"/, | |
| 560 | /<a class="bar muted small" href="[^"]+" target="_blank" rel="noopener noreferrer"/, | |
| 561 | 561 | ); |
| 562 | 562 | }); |
| 563 | 563 |
new above
offline