-
Notifications
You must be signed in to change notification settings - Fork 3
/
mod.ts
379 lines (359 loc) · 11.8 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// @deno-types="./pkg/ammonia_wasm.d.ts"
import {
AmmoniaBuilder as AmmoniaBuilderWASM,
clean as cleanWASM,
clean_text as cleanTextWASM,
default as initWASM,
} from "./pkg/ammonia_wasm.js";
import { source } from "./wasm.js";
let inited: true | Promise<true> | undefined;
export async function init(): Promise<void> {
if (inited !== undefined) {
await inited;
}
inited = initWASM(source)
.then(() => true);
await inited;
inited = true;
}
/**
* Clean HTML with a conservative set of defaults.
*
* * [tags](https://docs.rs/ammonia/3.1.2/ammonia/https://docs.rs/ammonia/3.1.2/ammonia/struct.Builder.html#defaults)
* * [attributes on specific tags](https://docs.rs/ammonia/3.1.2/ammonia/struct.Builder.html#defaults-1)
* * [attributes on all tags](https://docs.rs/ammonia/3.1.2/ammonia/struct.Builder.html#defaults-2)
* * [url schemes](https://docs.rs/ammonia/3.1.2/ammonia/struct.Builder.html#defaults-3)
* * [relative URLs are passed through, unchanged, by default](https://docs.rs/ammonia/3.1.2/ammonia/struct.Builder.html#defaults-4)
* * [links are marked `noopener noreferrer` by default](https://docs.rs/ammonia/3.1.2/ammonia/struct.Builder.html#defaults-5)
* * [all `class=""` settings are blocked by default](https://docs.rs/ammonia/3.1.2/ammonia/struct.Builder.html#defaults-6)
* * [comments are stripped by default](https://docs.rs/ammonia/3.1.2/ammonia/struct.Builder.html#defaults-7)
*
* [opener]: https://mathiasbynens.github.io/rel-noopener/
* [referrer]: https://en.wikipedia.org/wiki/HTTP_referer
*/
export function clean(src: string): string {
return cleanWASM(src);
}
/**
* Turn an arbitrary string into unformatted HTML.
*
* This function is roughly equivalent to PHP's `htmlspecialchars` and
* `htmlentities`. It is as strict as possible, encoding every character that
* has special meaning to the HTML parser.
*
* # Warnings
*
* This function cannot be used to package strings into a `<script>` or
* `<style>` tag; you need a JavaScript or CSS escaper to do that.
*
* `<textarea>` tags will strip the first newline, if present, even if that
* newline is encoded. If you want to build an editor that works the way most
* folks expect them to, you should put a newline at the beginning of the tag.
*
* It also does not make user text safe for HTML attribute microsyntaxes such
* as `class` or `id`. Only use this function for places where HTML accepts
* unrestricted text such as `title` attributes and paragraph contents.
*/
export function cleanText(src: string): string {
return cleanTextWASM(src);
}
export class AmmoniaBuilder {
/**
* The tags that are allowed.
*
* # Defaults
*
* ```text
* a, abbr, acronym, area, article, aside, b, bdi,
* bdo, blockquote, br, caption, center, cite, code,
* col, colgroup, data, dd, del, details, dfn, div,
* dl, dt, em, figcaption, figure, footer, h1, h2,
* h3, h4, h5, h6, header, hgroup, hr, i, img,
* ins, kbd, kbd, li, map, mark, nav, ol, p, pre,
* q, rp, rt, rtc, ruby, s, samp, small, span,
* strike, strong, sub, summary, sup, table, tbody,
* td, th, thead, time, tr, tt, u, ul, var, wbr
* ```
*/
// deno-fmt-ignore
tags: Set<string> = new Set([
"a", "abbr", "acronym", "area", "article", "aside", "b", "bdi",
"bdo", "blockquote", "br", "caption", "center", "cite", "code",
"col", "colgroup", "data", "dd", "del", "details", "dfn", "div",
"dl", "dt", "em", "figcaption", "figure", "footer", "h1", "h2",
"h3", "h4", "h5", "h6", "header", "hgroup", "hr", "i", "img",
"ins", "kbd", "kbd", "li", "map", "mark", "nav", "ol", "p", "pre",
"q", "rp", "rt", "rtc", "ruby", "s", "samp", "small", "span",
"strike", "strong", "sub", "summary", "sup", "table", "tbody",
"td", "th", "thead", "time", "tr", "tt", "u", "ul", "var", "wbr"
]);
/**
* The tags whose contents will be completely removed from the output.
*
* Adding tags which are whitelisted in tags or tagAttributes will cause a
* panic.
*
* # Defaults
*
* ```text
* script, style
* ```
*/
cleanContentTags: Set<string> = new Set(["script", "style"]);
/**
* The HTML attributes that are allowed on specific tags.
*
* The value is structured as a map from tag names to a set of attribute names.
*
* If a tag is not itself whitelisted, adding entries to this map will do
* nothing.
*
* # Defaults
*
* ```text
* a =>
* href, hreflang
* bdo =>
* dir
* blockquote =>
* cite
* col =>
* align, char, charoff, span
* colgroup =>
* align, char, charoff, span
* del =>
* cite, datetime
* hr =>
* align, size, width
* img =>
* align, alt, height, src, width
* ins =>
* cite, datetime
* ol =>
* start
* q =>
* cite
* table =>
* align, char, charoff, summary
* tbody =>
* align, char, charoff
* td =>
* align, char, charoff, colspan, headers, rowspan
* tfoot =>
* align, char, charoff
* th =>
* align, char, charoff, colspan, headers, rowspan, scope
* thead =>
* align, char, charoff
* tr =>
* align, char, charoff
* ```
*/
tagAttributes: Map<string, Set<string>> = new Map([
["a", new Set(["href", "hreflang"])],
["bdo", new Set(["dir"])],
["blockquote", new Set(["cite"])],
["col", new Set(["align", "char", "charoff", "span"])],
["colgroup", new Set(["align", "char", "charoff", "span"])],
["del", new Set(["cite", "datetime"])],
["hr", new Set(["align", "size", "width"])],
["img", new Set(["align", "alt", "height", "src", "width"])],
["ins", new Set(["cite", "datetime"])],
["ol", new Set(["start"])],
["q", new Set(["cite"])],
["table", new Set(["align", "char", "charoff", "summary"])],
["tbody", new Set(["align", "char", "charoff"])],
[
"td",
new Set(["align", "char", "charoff", "colspan", "headers", "rowspan"]),
],
["tfoot", new Set(["align", "char", "charoff"])],
[
"th",
new Set(["align", "char", "charoff", "colspan", "headers", "rowspan"]),
],
["thead", new Set(["align", "char", "charoff"])],
["tr", new Set(["align", "char", "charoff"])],
]);
/**
* Sets the values of HTML attributes that are allowed on specific tags.
*
* The value is structured as a map from tag names to a map from attribute
* names to a set of attribute values.
*
* If a tag is not itself whitelisted, adding entries to this map will do nothing.
*
* # Defaults
*
* None
*/
tagAttributeValues: Map<string, Map<string, Set<string>>> = new Map([]);
/**
* Sets the values of HTML attributes that are to be set on specific tags.
*
* The value is structured as a map from tag names to a map from attribute
* names to an attribute value.
*
* If a tag is not itself whitelisted, adding entries to this map will do
* nothing.
*
* # Defaults
*
* None
*/
setTagAttributeValues: Map<string, Map<string, Set<string>>> = new Map([]);
/**
* Sets the attributes that are allowed on any tag.
*
* # Defaults
*
* ```text
* lang, title
* ````
*/
genericAttributes: Set<string> = new Set(["lang", "title"]);
/**
* Sets the URL schemes permitted on `href` and `src` attributes.
*
* # Defaults
*
* ```text
* bitcoin, ftp, ftps, geo, http, https, im, irc,
* ircs, magnet, mailto, mms, mx, news, nntp,
* openpgp4fpr, sip, sms, smsto, ssh, tel, url,
* webcal, wtai, xmpp
* ```
*/
// deno-fmt-ignore
urlSchemes: Set<string> = new Set([
"bitcoin", "ftp", "ftps", "geo", "http", "https", "im", "irc", "ircs",
"magnet", "mailto", "mms", "mx", "news", "nntp", "openpgp4fpr", "sip",
"sms", "smsto", "ssh", "tel", "url", "webcal", "wtai", "xmpp",
]);
/**
* Configures a `rel` attribute that will be added on links.
*
* If `rel` is in the generic or tag attributes, this must be set to `null`.
* Common `rel` values to include:
*
* * `noopener`: This prevents [a particular type of XSS attack], and should
* usually be turned on for untrusted HTML.
* * `noreferrer`: This prevents the browser from [sending the source URL]
* to the website that is linked to.
* * `nofollow`: This prevents search engines from [using this link for
* ranking], which disincentivizes spammers.
*
* To turn on rel-insertion, call this function with a space-separated list.
* Ammonia does not parse rel-attributes; it just puts the given string into
* the attribute directly.
*
* [a particular type of XSS attack]: https://mathiasbynens.github.io/rel-noopener/
* [sending the source URL]: https://en.wikipedia.org/wiki/HTTP_referer
* [using this link for ranking]: https://en.wikipedia.org/wiki/Nofollow
*
* # Defaults
*
* `"noopener noreferrer"`
*/
linkRel: string | null = "noopener noreferrer";
/**
* Sets the CSS classes that are allowed on specific tags.
*
* The values is structured as a map from tag names to a set of class names.
*
* If the `class` attribute is itself whitelisted for a tag, then adding
* entries to this map will cause a panic.
*
* # Defaults
*
* The set of allowed classes is empty by default.
*/
allowedClasses: Map<string, Set<string>> = new Map([]);
/**
* Configures the handling of HTML comments.
*
* If this option is false, comments will be preserved.
*
* # Defaults
*
* `true`
*/
stripComments = true;
/**
* Prefixes all "id" attribute values with a given string. Note that the tag
* and attribute themselves must still be whitelisted.
*
* # Defaults
*
* `null`
*/
idPrefix: string | null = null;
/**
* Sets the prefix of attributes that are allowed on any tag.
*
* # Defaults
*
* `null`
*/
genericAttributePrefixes: Set<string> | null = null;
/**
* Build an `Ammonia` instance from this builder once all configuration is
* done.
*
* Once an `Ammonia` has been built, changes in the builder won't be reflected
* on the instance anymore.
*/
build(): Ammonia {
return new Ammonia(this);
}
}
/**
* A built `Ammonia` instance. To construct this, see the `AmmoniaBuilder` class.
*/
export class Ammonia {
#builder: AmmoniaBuilderWASM;
constructor(builder: AmmoniaBuilder) {
const raw = {
tags: Array.from(builder.tags.values()),
cleanContentTags: Array.from(builder.cleanContentTags.values()),
genericAttributes: Array.from(builder.genericAttributes.values()),
tagAttributes: Object.fromEntries(
Array.from(builder.tagAttributes.entries())
.map(([name, val]) => [name, Array.from(val.values())]),
),
tagAttributeValues: Object.fromEntries(
Array.from(builder.tagAttributeValues.entries())
.map((
[name, val],
) => [
name,
Array.from(val.entries()).map((
[name, val],
) => [name, Array.from(val.values())]),
]),
),
setTagAttributeValues: Object.fromEntries(
Array.from(builder.setTagAttributeValues.entries())
.map(([name, val]) => [name, Object.fromEntries(val.entries())]),
),
urlSchemes: Array.from(builder.urlSchemes.values()),
linkRel: builder.linkRel,
allowedClasses: Object.fromEntries(
Array.from(builder.allowedClasses.entries())
.map(([name, val]) => [name, Array.from(val.values())]),
),
stripComments: builder.stripComments,
idPrefix: builder.idPrefix,
genericAttributePrefixes: builder.genericAttributePrefixes === null
? null
: Array.from(builder.genericAttributePrefixes.values()),
};
this.#builder = new AmmoniaBuilderWASM(raw);
}
/**
* Clean HTML with the specified options.
*/
clean(src: string): string {
return this.#builder.clean(src);
}
}