// `weft test`: the package's API and its safety. The spec's examples are in tests/spec.weft, hostile input in // tests/hostile.weft (both are programs: they run past a test's deadline). import { assert, assertEq } from "std/testing" import { parse, render, sanitize, toHtml, toText, visit } from "../markdown" // ---- safe by default ---- test "raw HTML is shown as text" { assertEq(toHtml(""), "

<script>alert(1)</script>

\n") assertEq( toHtml("a b"), "

a <b onclick="x()">b</b>

\n", ) } test "unsafe link schemes render as their text" { let attacks = [ "[x](javascript:alert(1))", "[x](JaVaScRiPt:alert(1))", "[x](vbscript:msgbox(1))", "[x](data:text/html;base64,PHNjcmlwdD4=)", "[x](javascript:alert(1))", "[x](javascript:alert(1))", "[x](file:///etc/passwd)", ] for a of attacks { assertEq(toHtml(a), "

x

\n") } assertEq(toHtml(""), "

javascript:alert(1)

\n") } test "unsafe images render as their alt text" { assertEq(toHtml("![cat](javascript:alert(1))"), "

cat

\n") assertEq(toHtml("![cat](data:image/svg+xml;base64,PHN2Zz4=)"), "

cat

\n") assertEq( toHtml("![cat](data:image/png;base64,AAAA)"), "

\"cat\"

\n", ) } test "safe URLs are kept" { assertEq( toHtml("[a](https://x.dev/?q=1&r=2)"), "

a

\n", ) assertEq(toHtml("[a](/docs/x:y)"), "

a

\n") assertEq(toHtml("[a](#top)"), "

a

\n") assertEq(toHtml("[a](mailto:me@x.dev)"), "

a

\n") assertEq(toHtml("[a](tel:+38640123456)"), "

a

\n") } test "titles, code and attributes are escaped" { assertEq( toHtml("[a](/x \"\\\">

"), "

hi

") assertEq(sanitize("hi"), "hi") assertEq(sanitize("hi"), "hi") assertEq(sanitize("hi"), "hi") assertEq(sanitize("hi"), "hi") assertEq(sanitize(""), "") assertEq(sanitize(""), "") assertEq(sanitize("ok"), "ok") assertEq(sanitize("xok"), "ok") assertEq(sanitize("ok"), "ok") assertEq(sanitize("ok"), "ok") assertEq(sanitize("ok"), "ok") assertEq(sanitize("", { html: "sanitize" }), "

a b

\n", ) assertEq(toHtml("\n\nok", { html: "sanitize" }), "\n

ok

\n") assertEq( toHtml("- [x] done", { html: "sanitize" }), "\n", ) } test "html: trust passes raw HTML through" { assertEq( toHtml("
a
", { html: "trust" }), "
a
\n", ) } // ---- GFM ---- test "tables" { let html = toHtml("| a | b |\n|:--|--:|\n| 1 | `x\\|y` |") assertEq( html, "\n\n\n\n\n\n\n\n\n\n\n\n\n
ab
1x|y
\n", ) } test "task lists" { let doc = parse("- [ ] todo\n- [x] done\n- plain") match doc.children[0] { BulletList { items } => { let checked: boolean?[] = items.map(i => i.checked) let expected: boolean?[] = [false, true, null] assertEq(checked, expected) }, _ => assert(false, "a list"), } } test "strikethrough and bare links" { assertEq(toHtml("~~gone~~"), "

gone

\n") assertEq( toHtml("see https://x.dev/a_b."), "

see https://x.dev/a_b.

\n", ) assertEq(toHtml("www.x.dev"), "

www.x.dev

\n") assertEq(toHtml("me@x.dev"), "

me@x.dev

\n") } test "gfm: false is plain CommonMark" { assertEq(toHtml("~~a~~ https://x.dev", { gfm: false }), "

~~a~~ https://x.dev

\n") assertEq(toHtml("| a |\n|---|", { gfm: false }), "

| a |\n|---|

\n") } // ---- what Vidni's briefs need ---- test "heading offset, link target and URL rewriting" { assertEq(toHtml("# T", { headingOffset: 1 }), "

T

\n") assertEq(toHtml("###### T", { headingOffset: 3 }), "
T
\n") assertEq( toHtml("[a](https://x.dev)", { linkTarget: "_blank" }), "

a

\n", ) let resolve = (url: string, kind: "link" | "image") => url.startsWith("upload:") ? `https://cdn.x.dev/${url.slice(7)}` : null assertEq( toHtml("![c](upload:abc)", { rewriteUrl: resolve }), "

\"c\"

\n", ) // Not resolved: `upload:` isn't an allowed scheme, so the caption stays. assertEq(toHtml("![c](upload:abc)"), "

c

\n") } test "toText for previews and emails" { assertEq( toText(parse("# Hi\n\nRead **this** [guide](https://x.dev).\n\n![cat](a.png)\n\n- one\n- two")), "Hi\n\nRead this guide (https://x.dev).\n\n[image: cat]\n\n- one\n\n- two", ) } test "visit finds the images of a document" { let mut urls: string[] = [] visit(parse("![a](upload:1)\n\n> *![b](upload:2)*\n\n| ![c](upload:3) |\n|---|"), n => { if let Picture { url } = n { urls.push(url) } }) assertEq(urls, ["upload:1", "upload:2", "upload:3"]) } test "render uses a tree that was changed" { let doc = parse("hello") assertEq(render({ children: [...doc.children, ...doc.children] }), "

hello

\n

hello

\n") } test "strange characters survive" { assertEq(toHtml("a\u{0}b"), "

a\u{fffd}b

\n") assertEq(toHtml("line\r\nnext\rlast"), "

line\nnext\nlast

\n") assertEq(toHtml("😀 *é*"), "

😀 é

\n") assertEq(toHtml(""), "") }