Web pages: a link to any address opens as an article, as Wikipedia's do
A url link now opens in the app instead of the browser. The server fetches the page (robots.txt honoured, public addresses only, size and time caps), keeps a snapshot on disk until it is reloaded, and cuts it into a lead and sections: by its headings, else by pseudo-headings, else in chunks, or by a model when asked (s), within a daily budget. The pane has its own amber ground, the author and date in its head when the page gives them, and the source in its foot. W opens the original, R reads it again. See docs/pages.md. Pictures and videos are kept, in web pages and in Wikipedia articles: pictures through the server (only addresses seen in a page that was read), videos as a still that loads the player on a click. A section that is only a link (a target, no text, no sections) opens as its article at once, and L opens a target to the right of the node's own pane, never under it. Article text is sized to read like the reader's own. Panes: a pane that already has all its column can give beside the others' tabs no longer peeks over them on hover, and the curves of tabs under a peeking pane are no longer drawn over it. The keypress timing test judges each of its two passes on its own, so a stall from outside the app does not fail it. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
This commit is contained in:
1 parent
a3f25fbdcc
commit
f8c6576ea1
58 files changed
+8606
-367
No files matched your search
+38
-5
@@ -4,7 +4,7 @@ import { describe, expect, it } from "vitest";
|
||||
import { WikiClient } from "../server/wiki";
|
||||
import { makeMockWikiFetch, mockWikiFetch } from "../server/wikiMock";
|
||||
|
||||
const client = (f: typeof fetch = mockWikiFetch) => new WikiClient({ fetch: f, spacingMs: 0, persist: false, offlineHoldMs: 0 });
|
||||
const client = (f: typeof fetch = mockWikiFetch) => new WikiClient({ fetch: f, spacingMs: 0, mediaSpacingMs: 0, persist: false, offlineHoldMs: 0 });
|
||||
const links = (html: string) => [...html.matchAll(/data-wiki="([^"]+)"/g)].map((m) => m[1]);
|
||||
|
||||
describe("mock Wikipedia", () => {
|
||||
@@ -81,6 +81,29 @@ describe("mock Wikipedia", () => {
|
||||
expect(await w.suggest("Offline")).toMatchObject({ ok: true });
|
||||
});
|
||||
|
||||
it("shows Grey heron's pictures as figures through the image route, each credited to its File page; its flags and icon are not kept", async () => {
|
||||
const w = client();
|
||||
const r = await w.article("Grey heron", { sections: true });
|
||||
if (!r.ok) throw new Error(r.message);
|
||||
const a = r.value;
|
||||
// The infobox's picture opens the lead; the infobox does not.
|
||||
expect(a.lead).toMatch(/^<figure><img src="\/api\/wiki\/media\?u=https%3A%2F%2Fupload\.wikimedia\.org%2Fwikipedia%2Fcommons%2Fthumb%2Fa%2Fa1%2FGrey_heron_at_the_weir\.jpg%2F500px-Grey_heron_at_the_weir\.jpg" alt="A grey heron standing in shallow water" width="500" height="376" loading="lazy" decoding="async" data-full="https:\/\/en\.wikipedia\.org\/wiki\/File:Grey_heron_at_the_weir\.jpg"><figcaption>An adult at a weir<\/figcaption><\/figure>\n+<p>The <b>grey heron<\/b>/);
|
||||
expect(a.lead).not.toMatch(/Range|Flag_of_Europe|infobox|Long-legged wading bird of the heron family/);
|
||||
const [desc, gallery, behaviour] = a.sections.map((s) => s.html);
|
||||
expect(desc).toContain(`data-full="https://en.wikipedia.org/wiki/File:Heron_in_flight.jpg"><figcaption>A heron in flight, its neck folded back</figcaption></figure>`);
|
||||
expect(desc).not.toMatch(/Flag_of_Europe|Heron_icon|An icon/);
|
||||
expect(gallery.match(/<figure>/g)).toHaveLength(2);
|
||||
expect(gallery).toMatch(/^<p>Herons at the weir<\/p>/);
|
||||
expect(behaviour).toMatch(/^<figure class="video" data-video="https:\/\/upload\.wikimedia\.org\/wikipedia\/commons\/transcoded\/b\/b2\/Heron_takes_off\.webm\/Heron_takes_off\.webm\.480p\.vp9\.webm" data-width="854" data-height="480" data-file="https:\/\/en\.wikipedia\.org\/wiki\/File:Heron_takes_off\.webm"><img src="\/api\/wiki\/media\?u=[^"]+" alt="" width="854" height="480" loading="lazy" decoding="async"><figcaption>A heron taking off from the weir<\/figcaption><\/figure>/);
|
||||
// The attribution says how pictures are credited.
|
||||
expect(a.attribution.modified).toBe("Adapted: references, navigation boxes, infoboxes and edit links removed; each picture is credited on its own file page (click it).");
|
||||
// Every picture shown can be had through the route; the flag cannot.
|
||||
for (const m of (a.lead + a.sections.map((s) => s.html).join("")).matchAll(/src="\/api\/wiki\/media\?u=([^"]+)"/g)) {
|
||||
expect(await w.media(decodeURIComponent(m[1]))).toMatchObject({ ok: true, type: "image/png" });
|
||||
}
|
||||
expect(await w.media("https://upload.wikimedia.org/wikipedia/commons/thumb/f/f0/Flag_of_Europe.svg/23px-Flag_of_Europe.svg.png")).toMatchObject({ ok: false, status: 404 });
|
||||
});
|
||||
|
||||
it("is slow for the slow article", async () => {
|
||||
const w = client(makeMockWikiFetch(60));
|
||||
const t = performance.now();
|
||||
@@ -93,7 +116,8 @@ describe("mock Wikipedia", () => {
|
||||
const r = await client().article("Hostile article", { sections: true });
|
||||
if (!r.ok) throw new Error(r.message);
|
||||
const html = r.value.lead + r.value.sections.map((s) => s.html).join("");
|
||||
const allowedAttrs = new Set(["href", "data-wiki", "data-wiki-fragment", "data-wiki-anchor", "rel", "target", "colspan", "rowspan", "start", "display", "width", "height", "stretchy"]);
|
||||
// Pictures are kept now (src from the app's own route, data-full their File page), and a video as its facade's data attributes.
|
||||
const allowedAttrs = new Set(["href", "data-wiki", "data-wiki-fragment", "data-wiki-anchor", "rel", "target", "colspan", "rowspan", "start", "display", "width", "height", "stretchy", "src", "alt", "loading", "decoding", "data-full", "class", "data-video", "data-file"]);
|
||||
const seen: string[] = [];
|
||||
const walk = (nodes: T.ChildNode[]) => {
|
||||
for (const n of nodes) {
|
||||
@@ -101,7 +125,10 @@ describe("mock Wikipedia", () => {
|
||||
seen.push(n.tagName);
|
||||
for (const a of n.attrs) {
|
||||
expect(allowedAttrs.has(a.name), `${n.tagName} ${a.name}`).toBe(true);
|
||||
if (a.name === "href") expect(a.value).toMatch(/^https:\/\//);
|
||||
if (a.name === "href" || a.name === "data-full" || a.name === "data-file") expect(a.value).toMatch(/^https:\/\//);
|
||||
if (a.name === "src") expect(a.value).toMatch(/^\/api\/wiki\/media\?u=https%3A%2F%2Fupload\.wikimedia\.org%2F/);
|
||||
if (a.name === "data-video") expect(a.value).toMatch(/^https:\/\/upload\.wikimedia\.org\//);
|
||||
if (a.name === "class") expect(`${n.tagName}.${a.value}`).toBe("figure.video");
|
||||
if (a.name === "href" && !a.value.startsWith("https://en.wikipedia.org/")) {
|
||||
expect(n.attrs.find((x) => x.name === "target")?.value).toBe("_blank");
|
||||
expect(n.attrs.find((x) => x.name === "rel")?.value).toContain("noopener");
|
||||
@@ -111,12 +138,18 @@ describe("mock Wikipedia", () => {
|
||||
}
|
||||
};
|
||||
walk(parseFragment(html).childNodes);
|
||||
for (const bad of ["script", "style", "link", "meta", "base", "iframe", "svg", "form", "input", "button", "object", "embed", "video", "audio", "source", "picture", "img", "template", "noscript", "div", "span"]) {
|
||||
for (const bad of ["script", "style", "link", "meta", "base", "iframe", "svg", "form", "input", "button", "object", "embed", "video", "audio", "source", "picture", "template", "noscript", "div", "span"]) {
|
||||
expect(seen, bad).not.toContain(bad);
|
||||
}
|
||||
expect(html).not.toMatch(/on\w+=|srcset|evil\.example\/(?!page|protocol-relative)/i);
|
||||
expect(html).not.toMatch(/on\w+=|srcset|srcdoc|\sautoplay|\sloop|\spreload|href="javascript:|data:image|evil\.example\/(?!page|protocol-relative)/i);
|
||||
expect(links(html)).toContain("Octopus");
|
||||
expect(html).toContain("<script>"); // escaped text stays text
|
||||
// The forged figure keeps only its real file's picture; those with no File page, or a data: or javascript: source, are gone.
|
||||
expect(html.match(/<img /g)).toHaveLength(1);
|
||||
expect(html).toContain("<figcaption>A forged caption on a real file</figcaption>");
|
||||
expect(html).not.toMatch(/No file page|A data: picture|A javascript: picture/);
|
||||
// The autoplaying video is a facade with its upload.wikimedia.org source, and no still (its poster was javascript:).
|
||||
expect(html).toContain(`<figure class="video" data-video="https://upload.wikimedia.org/wikipedia/commons/a/aa/Autoplay.webm" data-file="https://en.wikipedia.org/wiki/File:Autoplay.webm"><figcaption>A video that tries to play by itself</figcaption></figure>`);
|
||||
}
|
||||
// Headings are plain text, never HTML: the UI must escape them.
|
||||
const a = await client().article("Hostile article");
|
||||
|
||||
Reference in new issue
Block a user