Watch
1
0
Fork
You've already forked packages
0

packages/ at markup-kit dd1e6eb5270e

A snapshot of packages/ in the Weft repository; the official packages are published from these commits.

Markup-Kit-Commit: dd1e6eb5270e62e0c8b6c202b3d01f0da6e00ddc
This commit is contained in:
Weft packages 2026-11-19 20:43:26 +00:00
commit 306b143104
72 changed files with 10915 additions and 0 deletions

1333
markdown/tests/cases.weft Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,47 @@
// Hostile input, one per run: `weft run tests/hostile.weft <name> <n>` renders it and prints the HTML's length.
// scripts/hostile.mjs runs them all and checks that the time each takes grows in proportion to its size.
import { log } from "std/log"
import { args } from "std/process"
import { toHtml } from "../markdown"
type Input = { name: string, make: (n: int) => string }
let inputs: Input[] = [
{ name: "nested quotes", make: n => ">".repeat(n) + " x" },
{ name: "nested lists", make: n => "- ".repeat(n) + "x" },
{ name: "nested emphasis", make: n => "*a **b ".repeat(n) + "c" + "** d*".repeat(n) },
{ name: "nested links", make: n => "[a ".repeat(n) + "](/x)".repeat(n) },
{ name: "nested images", make: n => "![".repeat(n) + "x" + "](/y)".repeat(n) },
{ name: "open emphasis", make: n => "*a ".repeat(n) },
{ name: "closed emphasis", make: n => "_a_ ".repeat(n) },
{ name: "open brackets", make: n => "[".repeat(n) },
{ name: "close brackets", make: n => "]".repeat(n) },
{ name: "unfinished links", make: n => "[a](".repeat(n) },
{ name: "backtick runs", make: n => "`a ``b ".repeat(n) },
{ name: "open tags", make: n => "<a ".repeat(n) },
{ name: "open comments", make: n => "a <!-- ".repeat(n) },
{ name: "open cdata", make: n => "a <![CDATA[ ".repeat(n) },
{ name: "open instructions", make: n => "a <? ".repeat(n) },
{ name: "entities", make: n => "&amp".repeat(n) },
{ name: "backslashes", make: n => "\\".repeat(n) },
{ name: "lines", make: n => "a\n".repeat(n) },
{ name: "reference definitions", make: n => "[a]: /b\n".repeat(n) },
{ name: "list items", make: n => "- a\n".repeat(n) },
{ name: "trailing parens", make: n => "http://a.dev/" + ")".repeat(n) },
{ name: "at signs", make: n => "a@".repeat(n) },
{ name: "table rows", make: n => "| a | b |\n|---|---|\n" + "| 1 | 2 |\n".repeat(n) },
{ name: "wide table", make: n => "| a ".repeat(n) + "\n" + "|-".repeat(n) },
]
let name = args().at(0) ?? "--list"
if name == "--list" {
log(inputs.map(i => i.name).join("\n"))
} else if name == "expansion" {
// A definition used many times can't multiply the output.
let url = "/" + "x".repeat(10000)
log(toHtml(`[a]: ${url}\n\n` + "[a] ".repeat(10000)).length)
} else {
let n = try int(args().at(1) ?? "1000") else 1000
let input = inputs.find(i => i.name == name)
log(input == null ? -1 : toHtml(input.make(n)).length)
}

View file

@ -0,0 +1,220 @@
// `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>"), "<p>&lt;script&gt;alert(1)&lt;/script&gt;</p>\n")
assertEq(
toHtml("a <b onclick=\"x()\">b</b>"),
"<p>a &lt;b onclick=&quot;x()&quot;&gt;b&lt;/b&gt;</p>\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](&#106;avascript:alert(1))",
"[x](javascript&colon;alert(1))",
"[x](file:///etc/passwd)",
]
for a of attacks {
assertEq(toHtml(a), "<p>x</p>\n")
}
assertEq(toHtml("<javascript:alert(1)>"), "<p>javascript:alert(1)</p>\n")
}
test "unsafe images render as their alt text" {
assertEq(toHtml("![cat](javascript:alert(1))"), "<p>cat</p>\n")
assertEq(toHtml("![cat](data:image/svg+xml;base64,PHN2Zz4=)"), "<p>cat</p>\n")
assertEq(
toHtml("![cat](data:image/png;base64,AAAA)"),
"<p><img src=\"data:image/png;base64,AAAA\" alt=\"cat\" /></p>\n",
)
}
test "safe URLs are kept" {
assertEq(
toHtml("[a](https://x.dev/?q=1&r=2)"),
"<p><a href=\"https://x.dev/?q=1&amp;r=2\">a</a></p>\n",
)
assertEq(toHtml("[a](/docs/x:y)"), "<p><a href=\"/docs/x:y\">a</a></p>\n")
assertEq(toHtml("[a](#top)"), "<p><a href=\"#top\">a</a></p>\n")
assertEq(toHtml("[a](mailto:me@x.dev)"), "<p><a href=\"mailto:me@x.dev\">a</a></p>\n")
assertEq(toHtml("[a](tel:+38640123456)"), "<p><a href=\"tel:+38640123456\">a</a></p>\n")
}
test "titles, code and attributes are escaped" {
assertEq(
toHtml("[a](/x \"\\\"><script>\")"),
"<p><a href=\"/x\" title=\"&quot;&gt;&lt;script&gt;\">a</a></p>\n",
)
assertEq(toHtml("`<b>`"), "<p><code>&lt;b&gt;</code></p>\n")
assertEq(
toHtml("```\"><script>\nx\n```"),
"<pre><code class=\"language-&quot;&gt;&lt;script&gt;\">x\n</code></pre>\n",
)
}
// ---- the sanitiser ----
test "sanitize removes scripts, handlers and unsafe URLs" {
assertEq(sanitize("<p onclick=\"x()\">hi<script>bad()</script></p>"), "<p>hi</p>")
assertEq(sanitize("<a href=\"javascript:x()\" title=\"t\">hi</a>"), "<a title=\"t\">hi</a>")
assertEq(sanitize("<a href=\"java&#115;cript:x()\">hi</a>"), "<a>hi</a>")
assertEq(sanitize("<a href=\"java&#9;script:x()\">hi</a>"), "<a>hi</a>")
assertEq(sanitize("<a href=\" &#14; javascript:x()\">hi</a>"), "<a>hi</a>")
assertEq(sanitize("<img src=x onerror=alert(1)>"), "<img src=\"x\" />")
assertEq(sanitize("<IMG SRC=\"JAVASCRIPT:x\">"), "<img />")
assertEq(sanitize("<svg><script>x</script></svg>ok"), "ok")
assertEq(sanitize("<math><mi>x</mi></math>ok"), "ok")
assertEq(sanitize("<style>*{display:none}</style>ok"), "ok")
assertEq(sanitize("<iframe src=\"https://evil\"></iframe>ok"), "ok")
assertEq(sanitize("<ScRiPt >x</sCrIpT >ok"), "ok")
assertEq(sanitize("<script>never closed"), "")
assertEq(sanitize("<!-- <script>x</script> -->ok"), "ok")
assertEq(sanitize("<form action=\"/x\"><button>go</button></form>"), "go")
}
test "sanitize keeps only a disabled checkbox" {
assertEq(sanitize("<input type=\"text\" value=\"pw\">"), "")
assertEq(sanitize("<input>"), "")
assertEq(
sanitize("<input type=\"checkbox\" checked>"),
"<input type=\"checkbox\" checked=\"\" disabled=\"\" />",
)
}
test "sanitize gives well-formed output" {
assertEq(sanitize("<b><i>x</b>y"), "<b><i>x</i></b>y")
assertEq(sanitize("</div>x"), "x")
assertEq(sanitize("<p>a"), "<p>a</p>")
assertEq(sanitize("1 < 2 & 3 > 2 &amp; ok"), "1 &lt; 2 &amp; 3 &gt; 2 &amp; ok")
assertEq(sanitize("<a title=\"a&quot;b\">x</a>"), "<a title=\"a&quot;b\">x</a>")
assertEq(
sanitize("<a target=\"_blank\" href=\"/x\">x</a>"),
"<a target=\"_blank\" href=\"/x\" rel=\"noopener noreferrer\">x</a>",
)
assertEq(sanitize("<a target=\"evil\" href=\"/x\">x</a>"), "<a href=\"/x\">x</a>")
}
test "a policy narrows what is kept" {
assertEq(sanitize("<b>x</b><i>y</i>", { tags: ["b"] }), "<b>x</b>y")
assertEq(sanitize("<a href=\"ftp://x\">y</a>", { schemes: ["ftp"] }), "<a href=\"ftp://x\">y</a>")
assertEq(sanitize("<a href=\"https://x\">y</a>", { schemes: ["ftp"] }), "<a>y</a>")
let attributes = Map<string, string[]>([["b", ["title"]]])
assertEq(
sanitize("<b title=\"t\" id=\"i\">x</b>", { tags: ["b"], attributes }),
"<b title=\"t\">x</b>",
)
}
test "html: sanitize keeps safe raw HTML in Markdown" {
assertEq(
toHtml("<div onclick=\"x()\">\n*hi*\n</div>", { html: "sanitize" }),
"<div>\n*hi*\n</div>\n",
)
assertEq(
toHtml("a <kbd>b</kbd> <script>c</script>", { html: "sanitize" }),
"<p>a <kbd>b</kbd> </p>\n",
)
assertEq(toHtml("<script>\nbad()\n\n</script>\n\nok", { html: "sanitize" }), "\n<p>ok</p>\n")
assertEq(
toHtml("- [x] done", { html: "sanitize" }),
"<ul>\n<li><input checked=\"\" disabled=\"\" type=\"checkbox\" /> done</li>\n</ul>\n",
)
}
test "html: trust passes raw HTML through" {
assertEq(
toHtml("<div onclick=\"x()\">a</div>", { html: "trust" }),
"<div onclick=\"x()\">a</div>\n",
)
}
// ---- GFM ----
test "tables" {
let html = toHtml("| a | b |\n|:--|--:|\n| 1 | `x\\|y` |")
assertEq(
html,
"<table>\n<thead>\n<tr>\n<th align=\"left\">a</th>\n<th align=\"right\">b</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td align=\"left\">1</td>\n<td align=\"right\"><code>x|y</code></td>\n</tr>\n</tbody>\n</table>\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~~"), "<p><del>gone</del></p>\n")
assertEq(
toHtml("see https://x.dev/a_b."),
"<p>see <a href=\"https://x.dev/a_b\">https://x.dev/a_b</a>.</p>\n",
)
assertEq(toHtml("www.x.dev"), "<p><a href=\"http://www.x.dev\">www.x.dev</a></p>\n")
assertEq(toHtml("me@x.dev"), "<p><a href=\"mailto:me@x.dev\">me@x.dev</a></p>\n")
}
test "gfm: false is plain CommonMark" {
assertEq(toHtml("~~a~~ https://x.dev", { gfm: false }), "<p>~~a~~ https://x.dev</p>\n")
assertEq(toHtml("| a |\n|---|", { gfm: false }), "<p>| a |\n|---|</p>\n")
}
// ---- what Vidni's briefs need ----
test "heading offset, link target and URL rewriting" {
assertEq(toHtml("# T", { headingOffset: 1 }), "<h2>T</h2>\n")
assertEq(toHtml("###### T", { headingOffset: 3 }), "<h6>T</h6>\n")
assertEq(
toHtml("[a](https://x.dev)", { linkTarget: "_blank" }),
"<p><a href=\"https://x.dev\" target=\"_blank\" rel=\"noopener noreferrer\">a</a></p>\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 }),
"<p><img src=\"https://cdn.x.dev/abc\" alt=\"c\" /></p>\n",
)
// Not resolved: `upload:` isn't an allowed scheme, so the caption stays.
assertEq(toHtml("![c](upload:abc)"), "<p>c</p>\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] }), "<p>hello</p>\n<p>hello</p>\n")
}
test "strange characters survive" {
assertEq(toHtml("a\u{0}b"), "<p>a\u{fffd}b</p>\n")
assertEq(toHtml("line\r\nnext\rlast"), "<p>line\nnext\nlast</p>\n")
assertEq(toHtml("😀 *é*"), "<p>😀 <em>é</em></p>\n")
assertEq(toHtml(""), "")
}

54
markdown/tests/spec.weft Normal file
View file

@ -0,0 +1,54 @@
// Runs the spec's examples: `weft run tests/spec.weft [commonmark|gfm] [-v]` prints every failure and a summary.
import { args, exit } from "std/process"
import { log } from "std/log"
import { toHtml } from "../markdown"
import { COMMONMARK, GFM } from "./cases"
import type { Case } from "./cases"
/// Where the GFM spec (written against CommonMark 0.29) and CommonMark 0.31.2 give different HTML for the same text,
/// CommonMark wins: cmark-gfm's renderer folds nested `<strong>`, which CommonMark's examples keep.
fn followsCommonMark(c: Case, got: string): boolean {
return COMMONMARK.some(o => o.markdown == c.markdown && o.html == got)
}
fn runAll(name: string, cases: Case[], verbose: boolean): int {
let mut failed = 0
let mut disagreements = 0
let mut bySection = Map<string, int>()
for c of cases {
let got = toHtml(c.markdown, { gfm: c.gfm, html: "trust" })
if got != c.html && followsCommonMark(c, got) {
disagreements += 1
} else if got != c.html {
failed += 1
bySection.set(c.section, (bySection.get(c.section) ?? 0) + 1)
if verbose {
log(`--- ${name} example ${c.id} (${c.section})`)
log(`markdown: ${c.markdown}`)
log(`expected: ${c.html}`)
log(`got: ${got}`)
}
}
}
log(
`${name}: ${cases.length - failed - disagreements} of ${cases.length} pass, ${disagreements} follow CommonMark 0.31.2 where the specs disagree, ${failed} fail`,
)
for [section, n] of bySection {
log(` ${n} failing in ${section}`)
}
return failed
}
let argv = args()
let verbose = argv.includes("-v")
let only = argv.filter(a => a != "-v").at(0)
let mut failures = 0
if only == null || only == "commonmark" {
failures += runAll("CommonMark", COMMONMARK, verbose)
}
if only == null || only == "gfm" {
failures += runAll("GFM", GFM, verbose)
}
if failures > 0 {
exit(1)
}