// `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
\n") assertEq(toHtml(""), "cat
\n") assertEq( toHtml(""), "hi
") assertEq(sanitize("hi"), "hi") assertEq(sanitize("hi"), "hi") assertEq(sanitize("hi"), "hi") assertEq(sanitize("hi"), "hi") assertEq(sanitize("a"), "
a
") assertEq(sanitize("1 < 2 & 3 > 2 & ok"), "1 < 2 & 3 > 2 & ok") assertEq(sanitize("x"), "x") assertEq( sanitize("x"), "x", ) assertEq(sanitize("x"), "x") } test "a policy narrows what is kept" { assertEq(sanitize("xy", { tags: ["b"] }), "xy") assertEq(sanitize("y", { schemes: ["ftp"] }), "y") assertEq(sanitize("y", { schemes: ["ftp"] }), "y") let attributes = Mapa b
\n", ) assertEq(toHtml("\n\nok", { html: "sanitize" }), "\nok
\n") assertEq( toHtml("- [x] done", { html: "sanitize" }), "| a | \nb | \n
|---|---|
| 1 | \nx|y | \n
gone
see https://x.dev/a_b.
\n", ) assertEq(toHtml("www.x.dev"), "\n") assertEq(toHtml("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 }), "c
\n") } test "toText for previews and emails" { assertEq( toText(parse("# Hi\n\nRead **this** [guide](https://x.dev).\n\n\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("\n\n> **\n\n|  |\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
\nhello
\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(""), "") }