/** * Web pages in the browser, the pure parts: an address of a Wikipedia article read as that article * (src/core/ref.ts), what the client's second cleaning keeps (src/ui/wiki.ts cleanAttrs), a web * page's text as selectable text with its figures and links (src/ui/articleText.ts), and the words * the view writes around it (src/ui/web.ts). Page HTML comes from the mock fixtures through the * real server client, parsed with parse5 in place of the browser's DOMParser. */ import { parseFragment, serializeOuter, type DefaultTreeAdapterTypes as T } from "parse5"; import { describe, expect, it } from "vitest"; import { asOpened, wikiRefOf } from "../src/core/ref"; import { buildText, type Block, type DomLike } from "../src/ui/articleText"; import { MEDIA_ROUTE, WIKI_MEDIA_ROUTE, cleanAttrs } from "../src/ui/wiki"; import { attributionText, byline, firstSentence as clientFirstSentence, readDate, sectionsUnder, webKey } from "../src/ui/web"; import { firstSentence } from "../server/pageHtml"; import { PageClient, type WebArticle } from "../server/page"; import { MOCK_PAGES, makeMockPageFetch } from "../server/pageMock"; /** A parse5 node as the little of the DOM the walk uses, each element cleaned as cleanHtml cleans it (web text). */ function adapt(n: T.Node): DomLike | null { if (n.nodeName === "#text") return { nodeType: 3, data: (n as T.TextNode).value, textContent: (n as T.TextNode).value, childNodes: [] }; if (!("tagName" in n)) return null; const el = n as T.Element; const attrs = cleanAttrs(el.tagName, el.attrs.map((a): [string, string] => [a.name, a.value]), true); if (!attrs) return null; const clean = { ...el, attrs: attrs.map(([name, value]) => ({ name, value })) } as T.Element; const text = (x: T.Node): string => (x.nodeName === "#text" ? (x as T.TextNode).value : "childNodes" in x ? (x as T.Element).childNodes.map(text).join("") : ""); return { nodeType: 1, localName: el.tagName, textContent: text(el), childNodes: el.childNodes.map(adapt).filter((c): c is DomLike => !!c), attributes: clean.attrs, outerHTML: serializeOuter(clean) }; } const build = (html: string) => buildText({ nodeType: 11, textContent: "", childNodes: parseFragment(html).childNodes.map(adapt).filter((c): c is DomLike => !!c) }); const kinds = (bs: Block[]): string[] => bs.flatMap((b) => (b.k === "el" ? [b.tag, ...kinds(b.kids)] : [b.k])); async function page(url: string): Promise { const r = await new PageClient({ fetch: makeMockPageFetch(), spacingMs: 0, persist: false }).article(url); if (!r.ok) throw new Error(`${url}: ${r.reason}`); return r.value; } describe("an address of a Wikipedia article", () => { it("is that article, in the language of its host", () => { expect(wikiRefOf("https://en.wikipedia.org/wiki/Fixed-point_combinator")).toEqual({ kind: "wiki", title: "Fixed-point combinator", lang: "en" }); expect(wikiRefOf("https://fr.m.wikipedia.org/wiki/Phare#Histoire")).toEqual({ kind: "wiki", title: "Phare", lang: "fr" }); expect(wikiRefOf("http://de.wikipedia.org/wiki/Gezeiten")).toEqual({ kind: "wiki", title: "Gezeiten", lang: "de" }); expect(wikiRefOf("https://en.wikipedia.org/wiki/Star_Wars:_Episode_IV_%E2%80%93_A_New_Hope")).toEqual({ kind: "wiki", title: "Star Wars: Episode IV – A New Hope", lang: "en" }); }); it("is not an article outside the main namespace, with a query, or on another site", () => { for (const u of [ "https://en.wikipedia.org/wiki/File:Heron.jpg", "https://en.wikipedia.org/wiki/Talk:Tide", "https://en.wikipedia.org/wiki/User_talk:Someone", "https://en.wikipedia.org/wiki/Special:Random", "https://en.wikipedia.org/wiki/Category:Tides", "https://en.wikipedia.org/w/index.php?title=Tide&oldid=1", "https://en.wikipedia.org/wiki/Tide?action=edit", "https://www.wikipedia.org/wiki/Tide", "https://en.wikipedia.org/", "https://wikipedia.org.example.com/wiki/Tide", "https://en.wikipedia.org.example.com/wiki/Tide", "https://tracing.example.org/posts/following-a-request", "not an address", ]) { expect(wikiRefOf(u), u).toBeNull(); } }); it("is opened as the article; any other target is opened as it is", () => { expect(asOpened({ kind: "url", url: "https://en.wikipedia.org/wiki/Tide", title: "Tide - Wikipedia" })).toEqual({ kind: "wiki", title: "Tide", lang: "en" }); const page = { kind: "url" as const, url: MOCK_PAGES.tracing }; expect(asOpened(page)).toBe(page); const node = { kind: "node" as const, id: "1@1" as const }; expect(asOpened(node)).toBe(node); }); }); describe("the client's second cleaning", () => { it("leaves Wikipedia's links as they were: an article by data-wiki in the configured language, an http link in a new tab, nothing else", () => { expect(cleanAttrs("a", [["href", "https://en.wikipedia.org/wiki/Tide"], ["data-wiki", "Tide"], ["data-wiki-lang", "fr"], ["onclick", "x()"]])).toEqual([["data-wiki", "Tide"], ["href", "https://en.wikipedia.org/wiki/Tide"]]); expect(cleanAttrs("a", [["href", "https://example.org/x"], ["data-url", "https://example.org/x"], ["style", "color:red"]])).toEqual([["href", "https://example.org/x"], ["target", "_blank"], ["rel", "noopener noreferrer nofollow"]]); expect(cleanAttrs("a", [["href", "javascript:alert(1)"]])).toEqual([]); expect(cleanAttrs("td", [["colspan", "2"], ["onmouseover", "x()"], ["background", "https://evil.example.net/t.png"]])).toEqual([["colspan", "2"]]); // Wikipedia's pictures are kept, but only from Wikipedia's own image route: a web page's route, or anything else, drops the image. expect(cleanAttrs("img", [["src", `${MEDIA_ROUTE}https%3A%2F%2Fexample.org%2Fa.png`]])).toBeNull(); const src = `${WIKI_MEDIA_ROUTE}${encodeURIComponent("https://upload.wikimedia.org/x.jpg")}`; expect(cleanAttrs("img", [["src", src], ["onerror", "x()"], ["width", "500"], ["height", "400"], ["data-full", "https://en.wikipedia.org/wiki/File:X.jpg"]])).toEqual([["src", src], ["alt", ""], ["width", "500"], ["height", "400"], ["loading", "lazy"], ["decoding", "async"], ["data-full", "https://en.wikipedia.org/wiki/File:X.jpg"]]); expect(cleanAttrs("img", [["src", src]], true)).toBeNull(); }); it("in a web page's text, keeps a link to another page as data-url, and an article's link in its own language", () => { expect(cleanAttrs("a", [["data-url", "https://example.org/x"], ["href", "https://example.org/x"], ["target", "_self"], ["rel", "opener"], ["onmouseover", "x()"]], true)).toEqual([ ["data-url", "https://example.org/x"], ["href", "https://example.org/x"], ["target", "_blank"], ["rel", "noopener noreferrer nofollow"], ]); expect(cleanAttrs("a", [["href", "https://fr.wikipedia.org/wiki/Phare"], ["data-wiki", "Phare"], ["data-wiki-lang", "fr"]], true)).toEqual([["data-wiki", "Phare"], ["data-wiki-lang", "fr"], ["href", "https://fr.wikipedia.org/wiki/Phare"]]); expect(cleanAttrs("a", [["data-wiki", "Phare"], ["data-wiki-lang", "javascript:"]], true)).toEqual([["data-wiki", "Phare"], ["href", "https://en.wikipedia.org/wiki/Phare"]]); for (const bad of ["javascript:x()", "data:text/html,