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

Merged opened this

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

Merged

ui/popout-new-tab → main 0 commits

  • Merged. ui/popout-new-tab was merged into main as 2a4d9c8.

Review

Sign in to review this change.
9 files changed +101 −27
src/web/comments.ts +2 −7
@@ -9,7 +9,7 @@ import type { Mark } from "../core/marks.ts";
99 import { emptyNote } from "./empty_words.ts";
1010 import { html, when, type Html } from "./html.ts";
1111 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";
1313
1414 export interface CommentCard {
1515 /** "" for the opener (the issue/PR body), which has no comment row. */
@@ -46,12 +46,7 @@ export function commentCard(card: CommentCard): Html {
4646 ${when(
4747 card.originUrl != null && card.originUrl !== "",
4848 () => 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")}`,
5550 )}
5651 </div>
5752 <div class="prose comment-body">${bodyHtml}</div>
src/web/pages/browse.ts +2 −3
@@ -77,6 +77,7 @@ import {
7777 tlComment,
7878 userTag,
7979 type IconName,
80 popout,
8081 } from "../ui.ts";
8182 import { browseCommit, browseCommits } from "./commits.ts";
8283 import { blankDraft, editorPage, runContribute } from "./edit.ts";
@@ -728,9 +729,7 @@ async function browseThread(
728729 ${when(
729730 detail.issue.originUrl,
730731 (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"),
734733 )}
735734 ${when(
736735 detail.pull,
src/web/scope.ts +2 −8
@@ -45,7 +45,7 @@ import { repoSteps } from "./repo_steps.ts";
4545 import { icon, type IconName } from "./icons.ts";
4646 import type { PaletteItem } from "./layout.ts";
4747 import { originIcon } from "./pages/origins.ts";
48 import { time, timeAgo } from "./ui.ts";
48 import { popout, time, timeAgo } from "./ui.ts";
4949
5050 /** Which side of the world a page is reading. */
5151 export type ScopeKind = "local" | "origin";
@@ -634,13 +634,7 @@ export function browseActions(scope: RepoScope): Html {
634634 const away =
635635 scope.originUrl === ""
636636 ? 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");
644638 if (fork.text.trim() === "" && away === null) return primary;
645639 return html`${primary}<details class="menu" data-align="end">
646640 <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 {
6565 userLink,
6666 verdictBadge,
6767 type Tone,
68 popout,
6869 } from "../ui.ts";
6970 import { previewBlock } from "../pages/apps.ts";
7071 import { filmstrip, filmstripJs } from "../pages/filmstrip.ts";
@@ -293,9 +294,7 @@ export function changeHead(
293294 ${when(
294295 pull.originUrl,
295296 (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"),
299298 )}
300299 ${workSpent(ctx, repo, pull, "pull")}${when(
301300 proposal,
src/web/studio/tell.ts +2 −3
@@ -73,6 +73,7 @@ import {
7373 stateBadge,
7474 time,
7575 userLink,
76 popout,
7677 } from "../ui.ts";
7778 import { previewBlock } from "../pages/apps.ts";
7879 import {
@@ -560,9 +561,7 @@ async function studioTell(
560561 ${when(
561562 issue.originUrl,
562563 (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"),
566565 )}
567566 ${when(
568567 issue.sealed,
src/web/ui.ts +10 −1
@@ -177,13 +177,22 @@ function btnAttrs(o: BtnOptions): Html {
177177 ${o.pressed !== undefined ? html`aria-pressed="${o.pressed ? "true" : "false"}"` : null}${o.current ? raw('aria-current="page"') : null}`;
178178 }
179179
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
180189 /** A button or a link that looks like one. Content is the visible label. */
181190 export function btn(content: Html | string, o: BtnOptions = {}): Html {
182191 const inner = html`${o.icon ? icon(o.icon) : null}${content}`;
183192 if (o.href !== undefined) {
184193 return html`<a
185194 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}
187196 ${btnAttrs(o)}
188197 >${inner}</a
189198 >`;
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-origins.test.ts +1 −1
@@ -557,7 +557,7 @@ describe("origins on the web", () => {
557557 // §5's muted small text, not the retired .origin-link.
558558 expect(page).not.toContain("origin-link");
559559 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"/,
561561 );
562562 });
563563

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs