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:
commit
306b143104
72 changed files with 10915 additions and 0 deletions
1145
markdown/lib/blocks.weft
Normal file
1145
markdown/lib/blocks.weft
Normal file
File diff suppressed because it is too large
Load diff
242
markdown/lib/chars.weft
Normal file
242
markdown/lib/chars.weft
Normal file
|
|
@ -0,0 +1,242 @@
|
|||
// Character classes and the text transformations CommonMark defines: escapes, entities, URLs.
|
||||
import { entity } from "./entities"
|
||||
import { PUNCTUATION } from "./unicode"
|
||||
|
||||
/// The code unit at `i`, or -1 past the end.
|
||||
export fn unit(s: string, i: int): int {
|
||||
return s.codeUnitAt(i) ?? -1
|
||||
}
|
||||
|
||||
export fn isSpaceOrTab(c: float): boolean {
|
||||
return c == 32 || c == 9
|
||||
}
|
||||
|
||||
export fn isDigit(c: float): boolean {
|
||||
return c >= 48 && c <= 57
|
||||
}
|
||||
|
||||
export fn isAlpha(c: float): boolean {
|
||||
return (c >= 65 && c <= 90) || (c >= 97 && c <= 122)
|
||||
}
|
||||
|
||||
export fn isAlnum(c: float): boolean {
|
||||
return isAlpha(c) || isDigit(c)
|
||||
}
|
||||
|
||||
export fn isHex(c: float): boolean {
|
||||
return isDigit(c) || (c >= 65 && c <= 70) || (c >= 97 && c <= 102)
|
||||
}
|
||||
|
||||
/// ASCII punctuation: what a backslash can escape.
|
||||
export fn isAsciiPunct(c: float): boolean {
|
||||
return (c >= 33 && c <= 47) ||
|
||||
(c >= 58 && c <= 64) ||
|
||||
(c >= 91 && c <= 96) ||
|
||||
(c >= 123 && c <= 126)
|
||||
}
|
||||
|
||||
/// Unicode whitespace: the `Zs` category, tab, line feed, form feed and carriage return.
|
||||
export fn isUnicodeSpace(c: float): boolean {
|
||||
if c < 128 {
|
||||
return c == 32 || c == 9 || c == 10 || c == 12 || c == 13
|
||||
}
|
||||
return c == 0xa0 ||
|
||||
c == 0x1680 ||
|
||||
(c >= 0x2000 && c <= 0x200a) ||
|
||||
c == 0x202f ||
|
||||
c == 0x205f ||
|
||||
c == 0x3000
|
||||
}
|
||||
|
||||
/// Unicode punctuation: the `P` and `S` categories.
|
||||
export fn isUnicodePunct(c: float): boolean {
|
||||
if c < 128 {
|
||||
return isAsciiPunct(c)
|
||||
}
|
||||
let mut lo = 0
|
||||
let mut hi = PUNCTUATION.length.div(2) - 1
|
||||
while lo <= hi {
|
||||
let mid = (lo + hi) >> 1
|
||||
if c < PUNCTUATION[mid * 2] {
|
||||
hi = mid - 1
|
||||
} else if c > PUNCTUATION[mid * 2 + 1] {
|
||||
lo = mid + 1
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/// Whether `s` holds only spaces and tabs (or nothing).
|
||||
export fn isBlank(s: string): boolean {
|
||||
for i of 0..s.length {
|
||||
let c = s.codeUnitAt(i) ?? 32
|
||||
if c != 32 && c != 9 && c != 10 && c != 13 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/// The code point ending just before `pos`, or a line feed at the start of `s`.
|
||||
export fn codePointBefore(s: string, pos: int): int {
|
||||
if pos <= 0 {
|
||||
return 10
|
||||
}
|
||||
let low = s.codeUnitAt(pos - 1) ?? 10
|
||||
if low >= 0xdc00 && low <= 0xdfff && pos >= 2 {
|
||||
let high = s.codeUnitAt(pos - 2) ?? 0
|
||||
if high >= 0xd800 && high <= 0xdbff {
|
||||
return (high - 0xd800) * 0x400 + (low - 0xdc00) + 0x10000
|
||||
}
|
||||
}
|
||||
return low
|
||||
}
|
||||
|
||||
/// The character reference starting at the `&` at `pos`: its text and where it ends, or `null` when there is none.
|
||||
export fn scanEntity(s: string, pos: int): [string, int]? {
|
||||
let mut i = pos + 1
|
||||
if s.codeUnitAt(i) == 35 {
|
||||
i += 1
|
||||
let c = s.codeUnitAt(i) ?? -1
|
||||
let hex = c == 120 || c == 88
|
||||
if hex {
|
||||
i += 1
|
||||
}
|
||||
let start = i
|
||||
while i - start < (hex ? 6 : 7) &&
|
||||
(hex ? isHex(s.codeUnitAt(i) ?? -1) : isDigit(s.codeUnitAt(i) ?? -1)) {
|
||||
i += 1
|
||||
}
|
||||
if i == start || s.codeUnitAt(i) != 59 {
|
||||
return null
|
||||
}
|
||||
let n = try int(s.slice(start, i), hex ? 16 : 10) else 0
|
||||
let valid = n != 0 && n <= 0x10ffff && !(n >= 0xd800 && n <= 0xdfff)
|
||||
return [String.fromCodePoint(valid ? n : 0xfffd), i + 1]
|
||||
}
|
||||
let start = i
|
||||
while i - start < 32 && isAlnum(s.codeUnitAt(i) ?? -1) {
|
||||
i += 1
|
||||
}
|
||||
if i == start || s.codeUnitAt(i) != 59 {
|
||||
return null
|
||||
}
|
||||
let text = entity(s.slice(start, i))
|
||||
if text == null {
|
||||
return null
|
||||
}
|
||||
return [text, i + 1]
|
||||
}
|
||||
|
||||
/// `s` with its backslash escapes and character references replaced by what they stand for.
|
||||
export fn unescape(s: string): string {
|
||||
if !s.includes("\\") && !s.includes("&") {
|
||||
return s
|
||||
}
|
||||
let mut parts: string[] = []
|
||||
let mut from = 0
|
||||
let mut i = 0
|
||||
while i < s.length {
|
||||
let c = s.codeUnitAt(i) ?? 0
|
||||
if c == 92 && isAsciiPunct(s.codeUnitAt(i + 1) ?? 0) {
|
||||
parts.push(s.slice(from, i))
|
||||
from = i + 1
|
||||
i += 2
|
||||
} else if c == 38 {
|
||||
let found = scanEntity(s, i)
|
||||
if found != null {
|
||||
parts.push(s.slice(from, i))
|
||||
parts.push(found[0])
|
||||
i = found[1]
|
||||
from = i
|
||||
} else {
|
||||
i += 1
|
||||
}
|
||||
} else {
|
||||
i += 1
|
||||
}
|
||||
}
|
||||
parts.push(s.slice(from))
|
||||
return parts.join("")
|
||||
}
|
||||
|
||||
/// `s` escaped for HTML text and attribute values.
|
||||
export fn escapeHtml(s: string): string {
|
||||
let mut i = 0
|
||||
while i < s.length {
|
||||
let c = s.codeUnitAt(i) ?? 0
|
||||
if c == 38 || c == 60 || c == 62 || c == 34 {
|
||||
break
|
||||
}
|
||||
i += 1
|
||||
}
|
||||
if i == s.length {
|
||||
return s
|
||||
}
|
||||
let mut parts: string[] = [s.slice(0, i)]
|
||||
let mut from = i
|
||||
while i < s.length {
|
||||
let c = s.codeUnitAt(i) ?? 0
|
||||
let rep = c == 38 ? "&" : c == 60 ? "<" : c == 62 ? ">" : c == 34 ? """ : ""
|
||||
if rep != "" {
|
||||
parts.push(s.slice(from, i))
|
||||
parts.push(rep)
|
||||
from = i + 1
|
||||
}
|
||||
i += 1
|
||||
}
|
||||
parts.push(s.slice(from))
|
||||
return parts.join("")
|
||||
}
|
||||
|
||||
fn keptInUrl(c: int): boolean {
|
||||
// Letters, digits and what URLs use as syntax; everything else is percent-encoded.
|
||||
return isAlnum(c) || (c < 128 && ";/?:@&=+$,-_.!~*'()#".includes(String.fromCharCode(c)))
|
||||
}
|
||||
|
||||
/// `url` percent-encoded for an `href`: what is already encoded stays, and a lone surrogate becomes U+FFFD.
|
||||
export fn normalizeUrl(url: string): string {
|
||||
let mut parts: string[] = []
|
||||
let mut from = 0
|
||||
let mut i = 0
|
||||
while i < url.length {
|
||||
let c = url.codeUnitAt(i) ?? 0
|
||||
if c == 37 && isHex(url.codeUnitAt(i + 1) ?? -1) && isHex(url.codeUnitAt(i + 2) ?? -1) {
|
||||
i += 3
|
||||
} else if keptInUrl(c) {
|
||||
i += 1
|
||||
} else {
|
||||
parts.push(url.slice(from, i))
|
||||
let point = url.codePointAt(i) ?? 0xfffd
|
||||
let width = point > 0xffff ? 2 : 1
|
||||
let safe = point >= 0xd800 && point <= 0xdfff ? 0xfffd : point
|
||||
for b of Bytes.fromUtf8(String.fromCodePoint(safe)).toList() {
|
||||
parts.push("%" + str(b, { radix: 16 }).toUpperCase().padStart(2, "0"))
|
||||
}
|
||||
i += width
|
||||
from = i
|
||||
}
|
||||
}
|
||||
parts.push(url.slice(from))
|
||||
return parts.join("")
|
||||
}
|
||||
|
||||
/// A link label as references are matched: case-folded, with whitespace collapsed.
|
||||
export fn normalizeLabel(label: string): string {
|
||||
let mut out: string[] = []
|
||||
let mut space = false
|
||||
for ch of label.trim() {
|
||||
if ch == " " || ch == "\t" || ch == "\n" || ch == "\r" {
|
||||
space = true
|
||||
} else {
|
||||
if space {
|
||||
out.push(" ")
|
||||
space = false
|
||||
}
|
||||
out.push(ch)
|
||||
}
|
||||
}
|
||||
return out.join("").toLowerCase().toUpperCase()
|
||||
}
|
||||
23
markdown/lib/entities.weft
Normal file
23
markdown/lib/entities.weft
Normal file
File diff suppressed because one or more lines are too long
1397
markdown/lib/inlines.weft
Normal file
1397
markdown/lib/inlines.weft
Normal file
File diff suppressed because it is too large
Load diff
255
markdown/lib/render.weft
Normal file
255
markdown/lib/render.weft
Normal file
|
|
@ -0,0 +1,255 @@
|
|||
// The HTML renderer: the output CommonMark's and GFM's spec tests expect.
|
||||
import type {
|
||||
Align,
|
||||
Block,
|
||||
Document,
|
||||
HtmlMode,
|
||||
Inline,
|
||||
ListItem,
|
||||
Options,
|
||||
TableCell,
|
||||
UrlKind,
|
||||
} from "../ast"
|
||||
import { escapeHtml } from "./chars"
|
||||
import { DEFAULT_SCHEMES, isSafeUrl, sanitize } from "./sanitize"
|
||||
|
||||
type Out = {
|
||||
parts: string[],
|
||||
atLineStart: boolean,
|
||||
mode: HtmlMode,
|
||||
schemes: string[],
|
||||
offset: float,
|
||||
target: string?,
|
||||
}
|
||||
|
||||
fn put(mut o: Out, text: string) {
|
||||
if text != "" {
|
||||
o.parts.push(text)
|
||||
o.atLineStart = text.endsWith("\n")
|
||||
}
|
||||
}
|
||||
|
||||
fn targetOf(o: Out): string? {
|
||||
return o.target
|
||||
}
|
||||
|
||||
fn isEscaping(o: Out): boolean {
|
||||
return o.mode == "escape"
|
||||
}
|
||||
|
||||
fn isAtLineStart(o: Out): boolean {
|
||||
return o.atLineStart
|
||||
}
|
||||
|
||||
fn cr(mut o: Out) {
|
||||
if !isAtLineStart(o) {
|
||||
put(&o, "\n")
|
||||
}
|
||||
}
|
||||
|
||||
fn rawHtml(o: Out, html: string): string {
|
||||
return o.mode == "escape" ? escapeHtml(html) : html
|
||||
}
|
||||
|
||||
fn plainText(inlines: Inline[]): string {
|
||||
let mut out: string[] = []
|
||||
for n of inlines {
|
||||
match n {
|
||||
Str { text } => out.push(text),
|
||||
SoftBreak | HardBreak => out.push(" "),
|
||||
CodeSpan { code } => out.push(code),
|
||||
Emphasis { children } | Strong { children } | Strikethrough { children } => out.push(
|
||||
plainText(children),
|
||||
),
|
||||
Link { children } => out.push(plainText(children)),
|
||||
Picture { alt } => out.push(plainText(alt)),
|
||||
HtmlInline { html } => out.push(html),
|
||||
}
|
||||
}
|
||||
return out.join("")
|
||||
}
|
||||
|
||||
fn wrap(mut o: Out, tag: string, children: Inline[], options: Options): void {
|
||||
put(&o, `<${tag}>`)
|
||||
renderInlines(&o, children, options)
|
||||
put(&o, `</${tag}>`)
|
||||
}
|
||||
|
||||
/// The URL to render, or `null` when it isn't safe to.
|
||||
fn finalUrl(url: string, kind: UrlKind, o: Out, options: Options): string? {
|
||||
let rewrite = options.rewriteUrl
|
||||
let rewritten = rewrite != null ? rewrite(url, kind) ?? url : url
|
||||
return o.mode == "trust" || isSafeUrl(rewritten, o.schemes, kind == "image") ? rewritten : null
|
||||
}
|
||||
|
||||
fn renderInlines(mut o: Out, inlines: Inline[], options: Options): void {
|
||||
for n of inlines {
|
||||
match n {
|
||||
Str { text } => put(&o, escapeHtml(text)),
|
||||
SoftBreak => put(&o, "\n"),
|
||||
HardBreak => put(&o, "<br />\n"),
|
||||
CodeSpan { code } => put(&o, `<code>${escapeHtml(code)}</code>`),
|
||||
Emphasis { children } => wrap(&o, "em", children, options),
|
||||
Strong { children } => wrap(&o, "strong", children, options),
|
||||
Strikethrough { children } => wrap(&o, "del", children, options),
|
||||
HtmlInline { html } => {
|
||||
let raw = rawHtml(o, html)
|
||||
put(&o, raw)
|
||||
},
|
||||
Link { url, title, children } => {
|
||||
let href = finalUrl(url, "link", o, options)
|
||||
if href == null {
|
||||
renderInlines(&o, children, options)
|
||||
} else {
|
||||
let titled = title != null && title != "" ? ` title="${escapeHtml(title)}"` : ""
|
||||
let linkTarget = targetOf(o)
|
||||
let target = linkTarget != null ? ` target="${escapeHtml(linkTarget)}"` : ""
|
||||
let rel = linkTarget == "_blank" ? " rel=\"noopener noreferrer\"" : ""
|
||||
put(&o, `<a href="${escapeHtml(href)}"${titled}${target}${rel}>`)
|
||||
renderInlines(&o, children, options)
|
||||
put(&o, "</a>")
|
||||
}
|
||||
},
|
||||
Picture { url, title, alt } => {
|
||||
let src = finalUrl(url, "image", o, options)
|
||||
if src == null {
|
||||
put(&o, escapeHtml(plainText(alt)))
|
||||
} else {
|
||||
let titled = title != null && title != "" ? ` title="${escapeHtml(title)}"` : ""
|
||||
put(&o, `<img src="${escapeHtml(src)}" alt="${escapeHtml(plainText(alt))}"${titled} />`)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn renderItem(mut o: Out, item: ListItem, tight: boolean, options: Options): void {
|
||||
put(&o, "<li>")
|
||||
if item.checked != null {
|
||||
put(
|
||||
&o,
|
||||
item.checked
|
||||
? "<input checked=\"\" disabled=\"\" type=\"checkbox\"> "
|
||||
: "<input disabled=\"\" type=\"checkbox\"> ",
|
||||
)
|
||||
}
|
||||
renderBlocks(&o, item.children, tight, options)
|
||||
put(&o, "</li>")
|
||||
cr(&o)
|
||||
}
|
||||
|
||||
fn renderCells(mut o: Out, tag: string, cells: TableCell[], align: Align[], options: Options) {
|
||||
put(&o, "<tr>\n")
|
||||
for [i, cell] of cells.entries() {
|
||||
let a = align.at(i) ?? null
|
||||
put(&o, a != null ? `<${tag} align="${a}">` : `<${tag}>`)
|
||||
renderInlines(&o, cell.children, options)
|
||||
put(&o, `</${tag}>\n`)
|
||||
}
|
||||
put(&o, "</tr>\n")
|
||||
}
|
||||
|
||||
fn renderBlocks(mut o: Out, blocks: Block[], tight: boolean, options: Options): void {
|
||||
for block of blocks {
|
||||
match block {
|
||||
Paragraph { children } => {
|
||||
if tight {
|
||||
renderInlines(&o, children, options)
|
||||
} else {
|
||||
cr(&o)
|
||||
wrap(&o, "p", children, options)
|
||||
cr(&o)
|
||||
}
|
||||
},
|
||||
Heading { level, children } => {
|
||||
let shifted = level + o.offset
|
||||
let shown = shifted > 6 ? 6 : shifted < 1 ? 1 : shifted
|
||||
cr(&o)
|
||||
wrap(&o, `h${shown}`, children, options)
|
||||
cr(&o)
|
||||
},
|
||||
ThematicBreak => {
|
||||
cr(&o)
|
||||
put(&o, "<hr />")
|
||||
cr(&o)
|
||||
},
|
||||
CodeBlock { info, code } => {
|
||||
cr(&o)
|
||||
let language = info.trim().split(" ")[0].split("\t")[0]
|
||||
put(
|
||||
&o,
|
||||
language != "" ? `<pre><code class="language-${escapeHtml(language)}">` : "<pre><code>",
|
||||
)
|
||||
put(&o, escapeHtml(code))
|
||||
put(&o, "</code></pre>")
|
||||
cr(&o)
|
||||
},
|
||||
HtmlBlock { html } => {
|
||||
cr(&o)
|
||||
if isEscaping(o) {
|
||||
put(&o, `<p>${escapeHtml(html)}</p>`)
|
||||
} else {
|
||||
put(&o, html)
|
||||
}
|
||||
cr(&o)
|
||||
},
|
||||
Blockquote { children } => {
|
||||
cr(&o)
|
||||
put(&o, "<blockquote>\n")
|
||||
renderBlocks(&o, children, false, options)
|
||||
cr(&o)
|
||||
put(&o, "</blockquote>")
|
||||
cr(&o)
|
||||
},
|
||||
BulletList { tight: itemsTight, items } => {
|
||||
cr(&o)
|
||||
put(&o, "<ul>\n")
|
||||
for item of items {
|
||||
renderItem(&o, item, itemsTight, options)
|
||||
}
|
||||
put(&o, "</ul>")
|
||||
cr(&o)
|
||||
},
|
||||
OrderedList { tight: itemsTight, start, items } => {
|
||||
cr(&o)
|
||||
put(&o, start != 1 ? `<ol start="${start}">\n` : "<ol>\n")
|
||||
for item of items {
|
||||
renderItem(&o, item, itemsTight, options)
|
||||
}
|
||||
put(&o, "</ol>")
|
||||
cr(&o)
|
||||
},
|
||||
Table { align, head, rows } => {
|
||||
cr(&o)
|
||||
put(&o, "<table>\n<thead>\n")
|
||||
renderCells(&o, "th", head, align, options)
|
||||
put(&o, "</thead>\n")
|
||||
if rows.length > 0 {
|
||||
put(&o, "<tbody>\n")
|
||||
for row of rows {
|
||||
renderCells(&o, "td", row, align, options)
|
||||
}
|
||||
put(&o, "</tbody>\n")
|
||||
}
|
||||
put(&o, "</table>")
|
||||
cr(&o)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Renders a document to HTML.
|
||||
export fn renderDocument(doc: Document, options: Options): string {
|
||||
let mode = options.html ?? "escape"
|
||||
let mut o: Out = {
|
||||
parts: [],
|
||||
atLineStart: true,
|
||||
mode,
|
||||
schemes: options.schemes ?? DEFAULT_SCHEMES,
|
||||
offset: options.headingOffset ?? 0,
|
||||
target: options.linkTarget,
|
||||
}
|
||||
renderBlocks(&o, doc.children, false, options)
|
||||
let html = o.parts.join("")
|
||||
return mode == "sanitize" ? sanitize(html, options.policy) : html
|
||||
}
|
||||
403
markdown/lib/sanitize.weft
Normal file
403
markdown/lib/sanitize.weft
Normal file
|
|
@ -0,0 +1,403 @@
|
|||
// The HTML sanitiser: an allowlist over a tokenizer. What comes out is well formed and holds only allowed tags and
|
||||
// attributes, every text escaped, so nothing a browser parses differently can bring a removed element back.
|
||||
import type { Policy } from "../ast"
|
||||
import { escapeHtml, isAlpha, scanEntity } from "./chars"
|
||||
|
||||
let DEFAULT_TAGS = [
|
||||
"a",
|
||||
"abbr",
|
||||
"b",
|
||||
"blockquote",
|
||||
"br",
|
||||
"code",
|
||||
"dd",
|
||||
"del",
|
||||
"details",
|
||||
"div",
|
||||
"dl",
|
||||
"dt",
|
||||
"em",
|
||||
"figcaption",
|
||||
"figure",
|
||||
"h1",
|
||||
"h2",
|
||||
"h3",
|
||||
"h4",
|
||||
"h5",
|
||||
"h6",
|
||||
"hr",
|
||||
"i",
|
||||
"img",
|
||||
"input",
|
||||
"ins",
|
||||
"kbd",
|
||||
"li",
|
||||
"mark",
|
||||
"ol",
|
||||
"p",
|
||||
"pre",
|
||||
"q",
|
||||
"s",
|
||||
"samp",
|
||||
"small",
|
||||
"span",
|
||||
"strong",
|
||||
"sub",
|
||||
"summary",
|
||||
"sup",
|
||||
"table",
|
||||
"tbody",
|
||||
"td",
|
||||
"tfoot",
|
||||
"th",
|
||||
"thead",
|
||||
"tr",
|
||||
"u",
|
||||
"ul",
|
||||
"var",
|
||||
]
|
||||
|
||||
let DEFAULT_ATTRIBUTES = Map<string, string[]>(
|
||||
[
|
||||
["a", ["href", "title", "target"]],
|
||||
["img", ["src", "alt", "title", "width", "height"]],
|
||||
["ol", ["start"]],
|
||||
["code", ["class"]],
|
||||
["td", ["align", "colspan", "rowspan"]],
|
||||
["th", ["align", "colspan", "rowspan", "scope"]],
|
||||
["input", ["type", "checked", "disabled"]],
|
||||
["details", ["open"]],
|
||||
],
|
||||
)
|
||||
|
||||
/// URL schemes allowed unless the options say otherwise.
|
||||
export let DEFAULT_SCHEMES = ["http", "https", "mailto", "tel"]
|
||||
|
||||
let VOID_TAGS = Set(
|
||||
[
|
||||
"br",
|
||||
"hr",
|
||||
"img",
|
||||
"input",
|
||||
"wbr",
|
||||
"area",
|
||||
"base",
|
||||
"col",
|
||||
"embed",
|
||||
"link",
|
||||
"meta",
|
||||
"param",
|
||||
"source",
|
||||
"track",
|
||||
],
|
||||
)
|
||||
|
||||
/// Elements removed with everything inside them.
|
||||
let DROPPED = Set(
|
||||
[
|
||||
"script",
|
||||
"style",
|
||||
"template",
|
||||
"iframe",
|
||||
"object",
|
||||
"embed",
|
||||
"applet",
|
||||
"noscript",
|
||||
"noembed",
|
||||
"noframes",
|
||||
"title",
|
||||
"textarea",
|
||||
"xmp",
|
||||
"plaintext",
|
||||
"svg",
|
||||
"math",
|
||||
"select",
|
||||
"frameset",
|
||||
"head",
|
||||
],
|
||||
)
|
||||
|
||||
fn isDataImage(url: string): boolean {
|
||||
let lower = url.slice(0, 16).toLowerCase()
|
||||
return lower.startsWith("data:image/png") ||
|
||||
lower.startsWith("data:image/gif") ||
|
||||
lower.startsWith("data:image/jpeg") ||
|
||||
lower.startsWith("data:image/jpg") ||
|
||||
lower.startsWith("data:image/webp")
|
||||
}
|
||||
|
||||
/// Whether a URL is relative or uses one of `schemes`. Browsers ignore tabs, line breaks and leading control characters
|
||||
/// in a scheme, so they are ignored here too.
|
||||
export fn isSafeUrl(url: string, schemes: string[], image: boolean): boolean {
|
||||
let mut compact: string[] = []
|
||||
for ch of url {
|
||||
let c = ch.codeUnitAt(0) ?? 0
|
||||
if c > 32 && c != 127 {
|
||||
compact.push(ch)
|
||||
}
|
||||
}
|
||||
let text = compact.join("")
|
||||
let mut colon = -1
|
||||
for i of 0..text.length {
|
||||
let c = text.codeUnitAt(i) ?? 0
|
||||
if c == 58 {
|
||||
colon = i
|
||||
break
|
||||
}
|
||||
if c == 47 || c == 63 || c == 35 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if colon < 0 {
|
||||
return true
|
||||
}
|
||||
if image && isDataImage(text) {
|
||||
return true
|
||||
}
|
||||
return schemes.includes(text.slice(0, colon).toLowerCase())
|
||||
}
|
||||
|
||||
/// Text decoded from an attribute value: its character references replaced.
|
||||
fn decodeAttribute(value: string): string {
|
||||
if !value.includes("&") {
|
||||
return value
|
||||
}
|
||||
let mut parts: string[] = []
|
||||
let mut from = 0
|
||||
let mut i = value.indexOf("&")
|
||||
while i >= 0 {
|
||||
let found = scanEntity(value, i)
|
||||
if found != null {
|
||||
parts.push(value.slice(from, i))
|
||||
parts.push(found[0])
|
||||
from = found[1]
|
||||
i = value.indexOf("&", from)
|
||||
} else {
|
||||
i = value.indexOf("&", i + 1)
|
||||
}
|
||||
}
|
||||
parts.push(value.slice(from))
|
||||
return parts.join("")
|
||||
}
|
||||
|
||||
/// Text as it may appear between tags: a character reference stays, anything else that means something is escaped.
|
||||
fn cleanText(text: string): string {
|
||||
if !text.includes("&") && !text.includes("<") && !text.includes(">") {
|
||||
return text
|
||||
}
|
||||
let mut parts: string[] = []
|
||||
let mut from = 0
|
||||
let mut i = 0
|
||||
while i < text.length {
|
||||
let c = text.codeUnitAt(i) ?? 0
|
||||
if c == 38 {
|
||||
let found = scanEntity(text, i)
|
||||
if found != null {
|
||||
i = found[1]
|
||||
} else {
|
||||
parts.push(text.slice(from, i))
|
||||
parts.push("&")
|
||||
i += 1
|
||||
from = i
|
||||
}
|
||||
} else if c == 60 || c == 62 {
|
||||
parts.push(text.slice(from, i))
|
||||
parts.push(c == 60 ? "<" : ">")
|
||||
i += 1
|
||||
from = i
|
||||
} else {
|
||||
i += 1
|
||||
}
|
||||
}
|
||||
parts.push(text.slice(from))
|
||||
return parts.join("")
|
||||
}
|
||||
|
||||
type Attr = { name: string, value: string }
|
||||
|
||||
type Tag = { name: string, attrs: Attr[], end: int }
|
||||
|
||||
fn isSpace(c: int): boolean {
|
||||
return c == 32 || c == 9 || c == 10 || c == 12 || c == 13
|
||||
}
|
||||
|
||||
/// Reads the tag starting at `<` at `pos` (its name begins at `start`).
|
||||
fn readTag(html: string, start: int): Tag {
|
||||
let mut i = start
|
||||
while i < html.length {
|
||||
let c = html.codeUnitAt(i) ?? 32
|
||||
if isSpace(c) || c == 62 || c == 47 {
|
||||
break
|
||||
}
|
||||
i += 1
|
||||
}
|
||||
let name = html.slice(start, i).toLowerCase()
|
||||
let mut attrs: Attr[] = []
|
||||
while i < html.length {
|
||||
let c = html.codeUnitAt(i) ?? 32
|
||||
if c == 62 {
|
||||
return { name, attrs, end: i + 1 }
|
||||
}
|
||||
if isSpace(c) || c == 47 {
|
||||
i += 1
|
||||
continue
|
||||
}
|
||||
let nameStart = i
|
||||
i += 1
|
||||
while i < html.length {
|
||||
let u = html.codeUnitAt(i) ?? 32
|
||||
if isSpace(u) || u == 61 || u == 62 || u == 47 {
|
||||
break
|
||||
}
|
||||
i += 1
|
||||
}
|
||||
let attr = html.slice(nameStart, i).toLowerCase()
|
||||
while isSpace(html.codeUnitAt(i) ?? 0) {
|
||||
i += 1
|
||||
}
|
||||
let mut value = ""
|
||||
if html.codeUnitAt(i) == 61 {
|
||||
i += 1
|
||||
while isSpace(html.codeUnitAt(i) ?? 0) {
|
||||
i += 1
|
||||
}
|
||||
let q = html.codeUnitAt(i) ?? -1
|
||||
if q == 34 || q == 39 {
|
||||
let close = html.indexOf(String.fromCharCode(q), i + 1)
|
||||
let stop = close < 0 ? html.length : close
|
||||
value = html.slice(i + 1, stop)
|
||||
i = close < 0 ? html.length : close + 1
|
||||
} else {
|
||||
let from = i
|
||||
while i < html.length {
|
||||
let u = html.codeUnitAt(i) ?? 32
|
||||
if isSpace(u) || u == 62 {
|
||||
break
|
||||
}
|
||||
i += 1
|
||||
}
|
||||
value = html.slice(from, i)
|
||||
}
|
||||
}
|
||||
attrs.push({ name: attr, value: decodeAttribute(value) })
|
||||
}
|
||||
return { name, attrs, end: html.length }
|
||||
}
|
||||
|
||||
/// Where the content of the dropped element `name` ends: after its closing tag, or at the end of the text.
|
||||
fn skipElement(html: string, name: string, from: int): int {
|
||||
let mut i = html.indexOf("</", from)
|
||||
while i >= 0 {
|
||||
if html.slice(i + 2, i + 2 + name.length).toLowerCase() == name {
|
||||
let after = html.codeUnitAt(i + 2 + name.length) ?? 62
|
||||
if isSpace(after) || after == 62 || after == 47 {
|
||||
let close = html.indexOf(">", i)
|
||||
return close < 0 ? html.length : close + 1
|
||||
}
|
||||
}
|
||||
i = html.indexOf("</", i + 2)
|
||||
}
|
||||
return html.length
|
||||
}
|
||||
|
||||
fn keepAttribute(tag: string, a: Attr, schemes: string[]): boolean {
|
||||
if a.name == "href" || a.name == "src" {
|
||||
return isSafeUrl(a.value, schemes, tag == "img")
|
||||
}
|
||||
if a.name == "target" {
|
||||
return a.value == "_blank"
|
||||
}
|
||||
if a.name == "type" && tag == "input" {
|
||||
return a.value.toLowerCase() == "checkbox"
|
||||
}
|
||||
if a.name == "class" {
|
||||
return a.value.startsWith("language-") && !a.value.includes(" ")
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/// The only input kept is a task list's checkbox, always disabled: a text field in rendered content can phish.
|
||||
fn isCheckbox(tag: Tag): boolean {
|
||||
return tag.attrs.some(a => a.name == "type" && a.value.toLowerCase() == "checkbox")
|
||||
}
|
||||
|
||||
fn openTag(tag: Tag, allowed: string[], schemes: string[]): string {
|
||||
let mut out: string[] = ["<", tag.name]
|
||||
let mut blank = false
|
||||
let mut seen = Set<string>()
|
||||
for a of tag.attrs {
|
||||
if allowed.includes(a.name) && !seen.has(a.name) && keepAttribute(tag.name, a, schemes) {
|
||||
seen.add(a.name)
|
||||
out.push(` ${a.name}="${escapeHtml(a.value)}"`)
|
||||
blank = blank || (a.name == "target" && a.value == "_blank")
|
||||
}
|
||||
}
|
||||
if blank {
|
||||
out.push(" rel=\"noopener noreferrer\"")
|
||||
}
|
||||
if tag.name == "input" && !seen.has("disabled") {
|
||||
out.push(" disabled=\"\"")
|
||||
}
|
||||
out.push(VOID_TAGS.has(tag.name) ? " />" : ">")
|
||||
return out.join("")
|
||||
}
|
||||
|
||||
/// Removes everything `policy` doesn't allow from `html`: scripts, styles, event handlers and unsafe URLs by default.
|
||||
/// The result is well formed: stray closing tags are dropped and open ones closed.
|
||||
///
|
||||
/// ```weft
|
||||
/// sanitize("<p onclick=\"x()\">hi<script>bad()</script></p>") // "<p>hi</p>"
|
||||
/// ```
|
||||
export fn sanitize(html: string, policy?: Policy): string {
|
||||
let tags = Set(policy?.tags ?? DEFAULT_TAGS)
|
||||
let attributes = policy?.attributes ?? DEFAULT_ATTRIBUTES
|
||||
let schemes = policy?.schemes ?? DEFAULT_SCHEMES
|
||||
let mut out: string[] = []
|
||||
let mut open: string[] = []
|
||||
let mut i = 0
|
||||
while i < html.length {
|
||||
let lt = html.indexOf("<", i)
|
||||
if lt < 0 {
|
||||
out.push(cleanText(html.slice(i)))
|
||||
break
|
||||
}
|
||||
if lt > i {
|
||||
out.push(cleanText(html.slice(i, lt)))
|
||||
}
|
||||
let next = html.codeUnitAt(lt + 1) ?? -1
|
||||
if html.startsWith("<!--", lt) {
|
||||
let end = html.indexOf("-->", lt + 4)
|
||||
i = end < 0 ? html.length : end + 3
|
||||
} else if next == 33 || next == 63 {
|
||||
let end = html.indexOf(">", lt + 2)
|
||||
i = end < 0 ? html.length : end + 1
|
||||
} else if next == 47 && isAlpha(html.codeUnitAt(lt + 2) ?? -1) {
|
||||
let tag = readTag(html, lt + 2)
|
||||
i = tag.end
|
||||
let at = open.lastIndexOf(tag.name)
|
||||
if at >= 0 {
|
||||
while open.length > at {
|
||||
out.push(`</${open.pop() ?? ""}>`)
|
||||
}
|
||||
}
|
||||
} else if isAlpha(next) {
|
||||
let tag = readTag(html, lt + 1)
|
||||
i = tag.end
|
||||
if DROPPED.has(tag.name) {
|
||||
i = VOID_TAGS.has(tag.name) ? i : skipElement(html, tag.name, i)
|
||||
} else if tags.has(tag.name) && (tag.name != "input" || isCheckbox(tag)) {
|
||||
out.push(openTag(tag, attributes.get(tag.name) ?? [], schemes))
|
||||
if !VOID_TAGS.has(tag.name) {
|
||||
open.push(tag.name)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
out.push("<")
|
||||
i = lt + 1
|
||||
}
|
||||
}
|
||||
while open.length > 0 {
|
||||
out.push(`</${open.pop() ?? ""}>`)
|
||||
}
|
||||
return out.join("")
|
||||
}
|
||||
50
markdown/lib/unicode.weft
Normal file
50
markdown/lib/unicode.weft
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
// Generated by scripts/gen-tables.mjs from the engine's Unicode tables (general categories P and S). Don't edit it.
|
||||
|
||||
/// Sorted, inclusive ranges of code points: start, end, start, end, …
|
||||
export let PUNCTUATION: int[] = [
|
||||
33, 47, 58, 64, 91, 96, 123, 126, 161, 169, 171, 172, 174, 177, 180, 180,
|
||||
182, 184, 187, 187, 191, 191, 215, 215, 247, 247, 706, 709, 722, 735, 741, 747,
|
||||
749, 749, 751, 767, 885, 885, 894, 894, 900, 901, 903, 903, 1014, 1014, 1154, 1154,
|
||||
1370, 1375, 1417, 1418, 1421, 1423, 1470, 1470, 1472, 1472, 1475, 1475, 1478, 1478, 1523, 1524,
|
||||
1542, 1551, 1563, 1563, 1565, 1567, 1642, 1645, 1748, 1748, 1758, 1758, 1769, 1769, 1789, 1790,
|
||||
1792, 1805, 2038, 2041, 2046, 2047, 2096, 2110, 2142, 2142, 2184, 2184, 2404, 2405, 2416, 2416,
|
||||
2546, 2547, 2554, 2555, 2557, 2557, 2678, 2678, 2800, 2801, 2928, 2928, 3059, 3066, 3191, 3191,
|
||||
3199, 3199, 3204, 3204, 3407, 3407, 3449, 3449, 3572, 3572, 3647, 3647, 3663, 3663, 3674, 3675,
|
||||
3841, 3863, 3866, 3871, 3892, 3892, 3894, 3894, 3896, 3896, 3898, 3901, 3973, 3973, 4030, 4037,
|
||||
4039, 4044, 4046, 4058, 4170, 4175, 4254, 4255, 4347, 4347, 4960, 4968, 5008, 5017, 5120, 5120,
|
||||
5741, 5742, 5787, 5788, 5867, 5869, 5941, 5942, 6100, 6102, 6104, 6107, 6144, 6154, 6464, 6464,
|
||||
6468, 6469, 6622, 6655, 6686, 6687, 6816, 6822, 6824, 6829, 6990, 6991, 7002, 7018, 7028, 7039,
|
||||
7164, 7167, 7227, 7231, 7294, 7295, 7360, 7367, 7379, 7379, 8125, 8125, 8127, 8129, 8141, 8143,
|
||||
8157, 8159, 8173, 8175, 8189, 8190, 8208, 8231, 8240, 8286, 8314, 8318, 8330, 8334, 8352, 8385,
|
||||
8448, 8449, 8451, 8454, 8456, 8457, 8468, 8468, 8470, 8472, 8478, 8483, 8485, 8485, 8487, 8487,
|
||||
8489, 8489, 8494, 8494, 8506, 8507, 8512, 8516, 8522, 8525, 8527, 8527, 8586, 8587, 8592, 9257,
|
||||
9280, 9290, 9372, 9449, 9472, 10101, 10132, 11123, 11126, 11263, 11493, 11498, 11513, 11516, 11518, 11519,
|
||||
11632, 11632, 11776, 11822, 11824, 11869, 11904, 11929, 11931, 12019, 12032, 12245, 12272, 12287, 12289, 12292,
|
||||
12296, 12320, 12336, 12336, 12342, 12343, 12349, 12351, 12443, 12444, 12448, 12448, 12539, 12539, 12688, 12689,
|
||||
12694, 12703, 12736, 12773, 12783, 12783, 12800, 12830, 12842, 12871, 12880, 12880, 12896, 12927, 12938, 12976,
|
||||
12992, 13311, 19904, 19967, 42128, 42182, 42238, 42239, 42509, 42511, 42611, 42611, 42622, 42622, 42738, 42743,
|
||||
42752, 42774, 42784, 42785, 42889, 42890, 43048, 43051, 43062, 43065, 43124, 43127, 43214, 43215, 43256, 43258,
|
||||
43260, 43260, 43310, 43311, 43359, 43359, 43457, 43469, 43486, 43487, 43612, 43615, 43639, 43641, 43742, 43743,
|
||||
43760, 43761, 43867, 43867, 43882, 43883, 44011, 44011, 64297, 64297, 64434, 64466, 64830, 64847, 64912, 64913,
|
||||
64968, 64975, 65020, 65023, 65040, 65049, 65072, 65106, 65108, 65126, 65128, 65131, 65281, 65295, 65306, 65312,
|
||||
65339, 65344, 65371, 65381, 65504, 65510, 65512, 65518, 65532, 65533, 65792, 65794, 65847, 65855, 65913, 65929,
|
||||
65932, 65934, 65936, 65948, 65952, 65952, 66000, 66044, 66463, 66463, 66512, 66512, 66927, 66927, 67671, 67671,
|
||||
67703, 67704, 67871, 67871, 67903, 67903, 68176, 68184, 68223, 68223, 68296, 68296, 68336, 68342, 68409, 68415,
|
||||
68505, 68508, 68974, 68974, 69006, 69007, 69293, 69293, 69328, 69336, 69461, 69465, 69510, 69513, 69703, 69709,
|
||||
69819, 69820, 69822, 69825, 69952, 69955, 70004, 70005, 70085, 70088, 70093, 70093, 70107, 70107, 70109, 70111,
|
||||
70200, 70205, 70313, 70313, 70612, 70613, 70615, 70616, 70731, 70735, 70746, 70747, 70749, 70749, 70854, 70854,
|
||||
71105, 71127, 71233, 71235, 71264, 71276, 71353, 71353, 71484, 71487, 71739, 71739, 72004, 72006, 72162, 72162,
|
||||
72255, 72262, 72346, 72348, 72350, 72354, 72448, 72457, 72673, 72673, 72769, 72773, 72816, 72817, 73463, 73464,
|
||||
73539, 73551, 73685, 73713, 73727, 73727, 74864, 74868, 77809, 77810, 92782, 92783, 92917, 92917, 92983, 92991,
|
||||
92996, 92997, 93549, 93551, 93847, 93850, 94178, 94178, 113820, 113820, 113823, 113823, 117760, 117999, 118010, 118012,
|
||||
118016, 118451, 118458, 118480, 118496, 118512, 118608, 118723, 118784, 119029, 119040, 119078, 119081, 119140, 119146, 119148,
|
||||
119171, 119172, 119180, 119209, 119214, 119274, 119296, 119361, 119365, 119365, 119552, 119638, 120513, 120513, 120539, 120539,
|
||||
120571, 120571, 120597, 120597, 120629, 120629, 120655, 120655, 120687, 120687, 120713, 120713, 120745, 120745, 120771, 120771,
|
||||
120832, 121343, 121399, 121402, 121453, 121460, 121462, 121475, 121477, 121483, 123215, 123215, 123647, 123647, 124415, 124415,
|
||||
125278, 125279, 126124, 126124, 126128, 126128, 126254, 126254, 126704, 126705, 126976, 127019, 127024, 127123, 127136, 127150,
|
||||
127153, 127167, 127169, 127183, 127185, 127221, 127245, 127405, 127462, 127490, 127504, 127547, 127552, 127560, 127568, 127569,
|
||||
127584, 127589, 127744, 128728, 128732, 128748, 128752, 128764, 128768, 128985, 128992, 129003, 129008, 129008, 129024, 129035,
|
||||
129040, 129095, 129104, 129113, 129120, 129159, 129168, 129197, 129200, 129211, 129216, 129217, 129232, 129240, 129280, 129623,
|
||||
129632, 129645, 129648, 129660, 129664, 129674, 129678, 129734, 129736, 129736, 129741, 129756, 129759, 129770, 129775, 129784,
|
||||
129792, 129938, 129940, 130031, 130042, 130042,
|
||||
]
|
||||
Loading…
Reference in a new issue