@viveeditor/sanitize 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +24 -2
- package/dist/index.d.mts +73 -0
- package/dist/index.mjs +996 -0
- package/dist/index.mjs.map +1 -0
- package/dist/profile.json +352 -0
- package/package.json +63 -3
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,996 @@
|
|
|
1
|
+
import { Parser, defaultTreeAdapter, html, serialize } from "parse5";
|
|
2
|
+
// src/profile.ts
|
|
3
|
+
const VIVE_PROFILE = {
|
|
4
|
+
version: 1,
|
|
5
|
+
limits: {
|
|
6
|
+
"maxBytes": 2e6,
|
|
7
|
+
"maxDepth": 64,
|
|
8
|
+
"maxAttributeLength": 2048,
|
|
9
|
+
"maxTextAttributeLength": 1e3,
|
|
10
|
+
"maxStyleValueLength": 64,
|
|
11
|
+
"maxSpan": 100,
|
|
12
|
+
"maxImageSize": 1e4,
|
|
13
|
+
"maxListStart": 99999,
|
|
14
|
+
"maxTableWidth": 99999,
|
|
15
|
+
"maxEmbedStart": 86400,
|
|
16
|
+
"maxEmbedTitle": 200,
|
|
17
|
+
"maxImageSrcLength": 15e5,
|
|
18
|
+
"maxUrlLength": 4096,
|
|
19
|
+
"maxMentionId": 128,
|
|
20
|
+
"maxMentionLabel": 100,
|
|
21
|
+
"maxMathLength": 2e3,
|
|
22
|
+
"maxMathDepth": 64,
|
|
23
|
+
"maxFootnoteLength": 5e3,
|
|
24
|
+
"maxAttributes": 64,
|
|
25
|
+
"maxNesting": 512,
|
|
26
|
+
"maxExtraElements": 2048
|
|
27
|
+
},
|
|
28
|
+
dropWithContent: [
|
|
29
|
+
"script",
|
|
30
|
+
"style",
|
|
31
|
+
"textarea",
|
|
32
|
+
"title",
|
|
33
|
+
"xmp",
|
|
34
|
+
"noscript",
|
|
35
|
+
"noembed",
|
|
36
|
+
"noframes",
|
|
37
|
+
"plaintext",
|
|
38
|
+
"template",
|
|
39
|
+
"iframe",
|
|
40
|
+
"svg",
|
|
41
|
+
"math",
|
|
42
|
+
"object",
|
|
43
|
+
"embed",
|
|
44
|
+
"select",
|
|
45
|
+
"option",
|
|
46
|
+
"button",
|
|
47
|
+
"head",
|
|
48
|
+
"frameset",
|
|
49
|
+
"frame"
|
|
50
|
+
],
|
|
51
|
+
elements: {
|
|
52
|
+
"p": { "styles": ["text-align"] },
|
|
53
|
+
"h1": { "styles": ["text-align"] },
|
|
54
|
+
"h2": { "styles": ["text-align"] },
|
|
55
|
+
"h3": { "styles": ["text-align"] },
|
|
56
|
+
"h4": { "styles": ["text-align"] },
|
|
57
|
+
"h5": { "styles": ["text-align"] },
|
|
58
|
+
"h6": { "styles": ["text-align"] },
|
|
59
|
+
"blockquote": {},
|
|
60
|
+
"hr": {},
|
|
61
|
+
"br": {},
|
|
62
|
+
"strong": {},
|
|
63
|
+
"em": {},
|
|
64
|
+
"u": {},
|
|
65
|
+
"s": {},
|
|
66
|
+
"code": { "attributes": ["class"] },
|
|
67
|
+
"a": { "attributes": [
|
|
68
|
+
"href",
|
|
69
|
+
"title",
|
|
70
|
+
"target"
|
|
71
|
+
] },
|
|
72
|
+
"ul": { "attributes": ["data-type"] },
|
|
73
|
+
"ol": { "attributes": ["start"] },
|
|
74
|
+
"li": { "attributes": ["data-type", "data-checked"] },
|
|
75
|
+
"input": { "attributes": [
|
|
76
|
+
"type",
|
|
77
|
+
"checked",
|
|
78
|
+
"disabled"
|
|
79
|
+
] },
|
|
80
|
+
"div": { "attributes": [
|
|
81
|
+
"class",
|
|
82
|
+
"data-embed",
|
|
83
|
+
"data-id",
|
|
84
|
+
"data-hash",
|
|
85
|
+
"data-start",
|
|
86
|
+
"data-title",
|
|
87
|
+
"data-math"
|
|
88
|
+
] },
|
|
89
|
+
"pre": { "attributes": ["data-language"] },
|
|
90
|
+
"table": { "styles": ["width", "min-width"] },
|
|
91
|
+
"colgroup": {},
|
|
92
|
+
"col": { "styles": ["width"] },
|
|
93
|
+
"tbody": {},
|
|
94
|
+
"tr": {},
|
|
95
|
+
"td": { "attributes": [
|
|
96
|
+
"colspan",
|
|
97
|
+
"rowspan",
|
|
98
|
+
"data-colwidth"
|
|
99
|
+
] },
|
|
100
|
+
"th": { "attributes": [
|
|
101
|
+
"colspan",
|
|
102
|
+
"rowspan",
|
|
103
|
+
"data-colwidth"
|
|
104
|
+
] },
|
|
105
|
+
"span": {
|
|
106
|
+
"styles": ["color"],
|
|
107
|
+
"attributes": [
|
|
108
|
+
"class",
|
|
109
|
+
"data-mention",
|
|
110
|
+
"data-math"
|
|
111
|
+
]
|
|
112
|
+
},
|
|
113
|
+
"mark": { "styles": ["background-color"] },
|
|
114
|
+
"img": { "attributes": [
|
|
115
|
+
"src",
|
|
116
|
+
"alt",
|
|
117
|
+
"title",
|
|
118
|
+
"width",
|
|
119
|
+
"height",
|
|
120
|
+
"loading",
|
|
121
|
+
"decoding",
|
|
122
|
+
"referrerpolicy"
|
|
123
|
+
] },
|
|
124
|
+
"sup": { "attributes": ["data-footnote"] }
|
|
125
|
+
},
|
|
126
|
+
mentionClass: "vive-mention",
|
|
127
|
+
footnoteElements: [
|
|
128
|
+
"strong",
|
|
129
|
+
"em",
|
|
130
|
+
"a"
|
|
131
|
+
],
|
|
132
|
+
textAlign: [
|
|
133
|
+
"left",
|
|
134
|
+
"center",
|
|
135
|
+
"right",
|
|
136
|
+
"justify"
|
|
137
|
+
],
|
|
138
|
+
linkProtocols: [
|
|
139
|
+
"http",
|
|
140
|
+
"https",
|
|
141
|
+
"mailto",
|
|
142
|
+
"tel"
|
|
143
|
+
],
|
|
144
|
+
linkRel: "noopener noreferrer nofollow",
|
|
145
|
+
image: {
|
|
146
|
+
"loading": "lazy",
|
|
147
|
+
"decoding": "async",
|
|
148
|
+
"referrerpolicy": "no-referrer",
|
|
149
|
+
"base64Types": [
|
|
150
|
+
"png",
|
|
151
|
+
"jpeg",
|
|
152
|
+
"gif",
|
|
153
|
+
"webp",
|
|
154
|
+
"avif"
|
|
155
|
+
]
|
|
156
|
+
},
|
|
157
|
+
embedProviders: {
|
|
158
|
+
"youtube": {
|
|
159
|
+
"label": "YouTube",
|
|
160
|
+
"host": "www.youtube-nocookie.com",
|
|
161
|
+
"width": 560,
|
|
162
|
+
"sandbox": "allow-scripts allow-same-origin allow-presentation allow-popups allow-popups-to-escape-sandbox",
|
|
163
|
+
"allow": "fullscreen; picture-in-picture; encrypted-media",
|
|
164
|
+
"height": 315,
|
|
165
|
+
"fluid": true
|
|
166
|
+
},
|
|
167
|
+
"vimeo": {
|
|
168
|
+
"label": "Vimeo",
|
|
169
|
+
"host": "player.vimeo.com",
|
|
170
|
+
"width": 640,
|
|
171
|
+
"sandbox": "allow-scripts allow-same-origin allow-presentation allow-popups allow-popups-to-escape-sandbox",
|
|
172
|
+
"allow": "fullscreen; picture-in-picture; encrypted-media",
|
|
173
|
+
"height": 360,
|
|
174
|
+
"fluid": true
|
|
175
|
+
},
|
|
176
|
+
"spotify": {
|
|
177
|
+
"label": "Spotify",
|
|
178
|
+
"host": "open.spotify.com",
|
|
179
|
+
"width": 560,
|
|
180
|
+
"sandbox": "allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox",
|
|
181
|
+
"allow": "fullscreen; encrypted-media",
|
|
182
|
+
"height": 352,
|
|
183
|
+
"compactHeight": 152,
|
|
184
|
+
"compactTypes": ["track", "episode"],
|
|
185
|
+
"fluid": false
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
colorKeywordsRejected: [
|
|
189
|
+
"inherit",
|
|
190
|
+
"initial",
|
|
191
|
+
"unset",
|
|
192
|
+
"revert",
|
|
193
|
+
"currentcolor",
|
|
194
|
+
"transparent",
|
|
195
|
+
"auto",
|
|
196
|
+
"none",
|
|
197
|
+
"windowtext"
|
|
198
|
+
],
|
|
199
|
+
namedColors: [
|
|
200
|
+
"aliceblue",
|
|
201
|
+
"antiquewhite",
|
|
202
|
+
"aqua",
|
|
203
|
+
"aquamarine",
|
|
204
|
+
"azure",
|
|
205
|
+
"beige",
|
|
206
|
+
"bisque",
|
|
207
|
+
"black",
|
|
208
|
+
"blanchedalmond",
|
|
209
|
+
"blue",
|
|
210
|
+
"blueviolet",
|
|
211
|
+
"brown",
|
|
212
|
+
"burlywood",
|
|
213
|
+
"cadetblue",
|
|
214
|
+
"chartreuse",
|
|
215
|
+
"chocolate",
|
|
216
|
+
"coral",
|
|
217
|
+
"cornflowerblue",
|
|
218
|
+
"cornsilk",
|
|
219
|
+
"crimson",
|
|
220
|
+
"cyan",
|
|
221
|
+
"darkblue",
|
|
222
|
+
"darkcyan",
|
|
223
|
+
"darkgoldenrod",
|
|
224
|
+
"darkgray",
|
|
225
|
+
"darkgreen",
|
|
226
|
+
"darkgrey",
|
|
227
|
+
"darkkhaki",
|
|
228
|
+
"darkmagenta",
|
|
229
|
+
"darkolivegreen",
|
|
230
|
+
"darkorange",
|
|
231
|
+
"darkorchid",
|
|
232
|
+
"darkred",
|
|
233
|
+
"darksalmon",
|
|
234
|
+
"darkseagreen",
|
|
235
|
+
"darkslateblue",
|
|
236
|
+
"darkslategray",
|
|
237
|
+
"darkslategrey",
|
|
238
|
+
"darkturquoise",
|
|
239
|
+
"darkviolet",
|
|
240
|
+
"deeppink",
|
|
241
|
+
"deepskyblue",
|
|
242
|
+
"dimgray",
|
|
243
|
+
"dimgrey",
|
|
244
|
+
"dodgerblue",
|
|
245
|
+
"firebrick",
|
|
246
|
+
"floralwhite",
|
|
247
|
+
"forestgreen",
|
|
248
|
+
"fuchsia",
|
|
249
|
+
"gainsboro",
|
|
250
|
+
"ghostwhite",
|
|
251
|
+
"gold",
|
|
252
|
+
"goldenrod",
|
|
253
|
+
"gray",
|
|
254
|
+
"green",
|
|
255
|
+
"greenyellow",
|
|
256
|
+
"grey",
|
|
257
|
+
"honeydew",
|
|
258
|
+
"hotpink",
|
|
259
|
+
"indianred",
|
|
260
|
+
"indigo",
|
|
261
|
+
"ivory",
|
|
262
|
+
"khaki",
|
|
263
|
+
"lavender",
|
|
264
|
+
"lavenderblush",
|
|
265
|
+
"lawngreen",
|
|
266
|
+
"lemonchiffon",
|
|
267
|
+
"lightblue",
|
|
268
|
+
"lightcoral",
|
|
269
|
+
"lightcyan",
|
|
270
|
+
"lightgoldenrodyellow",
|
|
271
|
+
"lightgray",
|
|
272
|
+
"lightgreen",
|
|
273
|
+
"lightgrey",
|
|
274
|
+
"lightpink",
|
|
275
|
+
"lightsalmon",
|
|
276
|
+
"lightseagreen",
|
|
277
|
+
"lightskyblue",
|
|
278
|
+
"lightslategray",
|
|
279
|
+
"lightslategrey",
|
|
280
|
+
"lightsteelblue",
|
|
281
|
+
"lightyellow",
|
|
282
|
+
"lime",
|
|
283
|
+
"limegreen",
|
|
284
|
+
"linen",
|
|
285
|
+
"magenta",
|
|
286
|
+
"maroon",
|
|
287
|
+
"mediumaquamarine",
|
|
288
|
+
"mediumblue",
|
|
289
|
+
"mediumorchid",
|
|
290
|
+
"mediumpurple",
|
|
291
|
+
"mediumseagreen",
|
|
292
|
+
"mediumslateblue",
|
|
293
|
+
"mediumspringgreen",
|
|
294
|
+
"mediumturquoise",
|
|
295
|
+
"mediumvioletred",
|
|
296
|
+
"midnightblue",
|
|
297
|
+
"mintcream",
|
|
298
|
+
"mistyrose",
|
|
299
|
+
"moccasin",
|
|
300
|
+
"navajowhite",
|
|
301
|
+
"navy",
|
|
302
|
+
"oldlace",
|
|
303
|
+
"olive",
|
|
304
|
+
"olivedrab",
|
|
305
|
+
"orange",
|
|
306
|
+
"orangered",
|
|
307
|
+
"orchid",
|
|
308
|
+
"palegoldenrod",
|
|
309
|
+
"palegreen",
|
|
310
|
+
"paleturquoise",
|
|
311
|
+
"palevioletred",
|
|
312
|
+
"papayawhip",
|
|
313
|
+
"peachpuff",
|
|
314
|
+
"peru",
|
|
315
|
+
"pink",
|
|
316
|
+
"plum",
|
|
317
|
+
"powderblue",
|
|
318
|
+
"purple",
|
|
319
|
+
"rebeccapurple",
|
|
320
|
+
"red",
|
|
321
|
+
"rosybrown",
|
|
322
|
+
"royalblue",
|
|
323
|
+
"saddlebrown",
|
|
324
|
+
"salmon",
|
|
325
|
+
"sandybrown",
|
|
326
|
+
"seagreen",
|
|
327
|
+
"seashell",
|
|
328
|
+
"sienna",
|
|
329
|
+
"silver",
|
|
330
|
+
"skyblue",
|
|
331
|
+
"slateblue",
|
|
332
|
+
"slategray",
|
|
333
|
+
"slategrey",
|
|
334
|
+
"snow",
|
|
335
|
+
"springgreen",
|
|
336
|
+
"steelblue",
|
|
337
|
+
"tan",
|
|
338
|
+
"teal",
|
|
339
|
+
"thistle",
|
|
340
|
+
"tomato",
|
|
341
|
+
"turquoise",
|
|
342
|
+
"violet",
|
|
343
|
+
"wheat",
|
|
344
|
+
"white",
|
|
345
|
+
"whitesmoke",
|
|
346
|
+
"yellow",
|
|
347
|
+
"yellowgreen"
|
|
348
|
+
],
|
|
349
|
+
spotifyTypes: [
|
|
350
|
+
"track",
|
|
351
|
+
"album",
|
|
352
|
+
"playlist",
|
|
353
|
+
"episode",
|
|
354
|
+
"show",
|
|
355
|
+
"artist"
|
|
356
|
+
]
|
|
357
|
+
};
|
|
358
|
+
// src/values.ts
|
|
359
|
+
const { limits: limits$1 } = VIVE_PROFILE;
|
|
360
|
+
const SCHEME = /^([a-z][a-z0-9+.-]*):/i;
|
|
361
|
+
const HEX = /^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
|
|
362
|
+
const RGB = /^rgba?\([\t\n\f\r ]*([0-9]{1,3})[\t\n\f\r ]*,[\t\n\f\r ]*([0-9]{1,3})[\t\n\f\r ]*,[\t\n\f\r ]*([0-9]{1,3})[\t\n\f\r ]*(?:,[\t\n\f\r ]*([0-9.]+%?)[\t\n\f\r ]*)?\)$/i;
|
|
363
|
+
const HSL = /^hsla?\([\t\n\f\r ]*[0-9]{1,3}(?:\.[0-9]+)?(?:deg)?[\t\n\f\r ]*,?[\t\n\f\r ]*[0-9]{1,3}(?:\.[0-9]+)?%[\t\n\f\r ]*,?[\t\n\f\r ]*[0-9]{1,3}(?:\.[0-9]+)?%[\t\n\f\r ]*(?:[,/][\t\n\f\r ]*[0-9.]+%?[\t\n\f\r ]*)?\)$/i;
|
|
364
|
+
const PIXELS = /^([0-9]{1,5})px$/;
|
|
365
|
+
const DIGITS = /^[0-9]{1,5}$/;
|
|
366
|
+
const LANGUAGE = /^[a-z0-9][a-z0-9+#.-]{0,31}$/i;
|
|
367
|
+
const HTTPS_URL = /^https:\/\/[a-z0-9](?:[a-z0-9.-]{0,251}[a-z0-9])?(?::[0-9]{1,5})?(?:[/?#][\x21-\x7e]*)?$/i;
|
|
368
|
+
const BASE64_IMAGE = /^data:image\/([a-z]+);base64,[A-Za-z0-9+/]+={0,2}$/;
|
|
369
|
+
const URL_FORBIDDEN = /["'<>\\`]/;
|
|
370
|
+
const BANNED_STYLE = [
|
|
371
|
+
"\\",
|
|
372
|
+
"/*",
|
|
373
|
+
"url(",
|
|
374
|
+
"expression",
|
|
375
|
+
"@",
|
|
376
|
+
"!important"
|
|
377
|
+
];
|
|
378
|
+
const NAMED_COLORS = new Set(VIVE_PROFILE.namedColors);
|
|
379
|
+
const REJECTED_COLORS = new Set(VIVE_PROFILE.colorKeywordsRejected);
|
|
380
|
+
const SPACE = /[\t\n\f\r ]+/g;
|
|
381
|
+
const MENTION_ID = new RegExp(`^(?!.*\\.\\.)[A-Za-z0-9_][A-Za-z0-9_.:@-]{0,${limits$1.maxMentionId - 1}}$`);
|
|
382
|
+
const byteLength = (value) => Buffer.byteLength(value, "utf8");
|
|
383
|
+
const lowerAscii = (value) => value.replace(/[A-Z]/g, (letter) => letter.toLowerCase());
|
|
384
|
+
const trimAscii = (value) => value.replace(/^[\t\n\f\r ]+|[\t\n\f\r ]+$/g, "");
|
|
385
|
+
function cleanText(value, max = limits$1.maxTextAttributeLength) {
|
|
386
|
+
const result = [];
|
|
387
|
+
for (const character of value) {
|
|
388
|
+
if (result.length >= max) break;
|
|
389
|
+
const code = character.codePointAt(0) ?? 0;
|
|
390
|
+
result.push(code < 32 || code >= 127 && code <= 159 ? " " : character);
|
|
391
|
+
}
|
|
392
|
+
return result.join("");
|
|
393
|
+
}
|
|
394
|
+
const isHidden = (code) => code <= 31 || code >= 127 && code <= 159 || code >= 8203 && code <= 8207 || code >= 8234 && code <= 8238 || code >= 8294 && code <= 8297 || code === 65279;
|
|
395
|
+
const isMentionId = (value) => MENTION_ID.test(value);
|
|
396
|
+
function cleanMentionLabel(value) {
|
|
397
|
+
let result = "";
|
|
398
|
+
for (const character of value) {
|
|
399
|
+
const code = character.codePointAt(0) ?? 0;
|
|
400
|
+
if (code === 9 || code === 10 || code === 13) result += " ";
|
|
401
|
+
else if (!isHidden(code)) result += character;
|
|
402
|
+
}
|
|
403
|
+
const label = trimAscii(trimAscii(result.replace(SPACE, " ")).replace(/^@+/, ""));
|
|
404
|
+
return trimAscii(Array.from(label).slice(0, limits$1.maxMentionLabel).join(""));
|
|
405
|
+
}
|
|
406
|
+
function cleanLatex(value) {
|
|
407
|
+
let result = "";
|
|
408
|
+
for (const character of value) {
|
|
409
|
+
const code = character.codePointAt(0) ?? 0;
|
|
410
|
+
if (code === 9 || code === 10 || !isHidden(code)) result += character;
|
|
411
|
+
}
|
|
412
|
+
const latex = trimAscii(result);
|
|
413
|
+
if (latex === "" || Array.from(latex).length > limits$1.maxMathLength) return null;
|
|
414
|
+
let depth = 0;
|
|
415
|
+
for (const character of latex) {
|
|
416
|
+
if (character === "{") depth++;
|
|
417
|
+
else if (character === "}") depth = Math.max(0, depth - 1);
|
|
418
|
+
if (depth > limits$1.maxMathDepth) return null;
|
|
419
|
+
}
|
|
420
|
+
return latex;
|
|
421
|
+
}
|
|
422
|
+
function takeCodePoints(value, budget) {
|
|
423
|
+
if (budget.left <= 0) return "";
|
|
424
|
+
const kept = Array.from(value).slice(0, budget.left);
|
|
425
|
+
budget.left -= kept.length;
|
|
426
|
+
return kept.join("");
|
|
427
|
+
}
|
|
428
|
+
function cleanInteger(value, min, max) {
|
|
429
|
+
if (value === void 0 || !DIGITS.test(value)) return null;
|
|
430
|
+
const number = Number(value);
|
|
431
|
+
return number >= min && number <= max ? number : null;
|
|
432
|
+
}
|
|
433
|
+
function cleanLanguage(value) {
|
|
434
|
+
return value !== void 0 && LANGUAGE.test(value) ? lowerAscii(value) : null;
|
|
435
|
+
}
|
|
436
|
+
function cleanColWidth(value) {
|
|
437
|
+
if (value === void 0) return null;
|
|
438
|
+
const parts = value.split(",");
|
|
439
|
+
if (parts.length > limits$1.maxSpan) return null;
|
|
440
|
+
return parts.every((part) => cleanInteger(part, 1, 9999) !== null) ? value : null;
|
|
441
|
+
}
|
|
442
|
+
const hexPair = (value) => Math.min(255, value).toString(16).padStart(2, "0");
|
|
443
|
+
function expandHex(hex) {
|
|
444
|
+
const digits = lowerAscii(hex.slice(1));
|
|
445
|
+
if (digits.length > 4) return `#${digits}`;
|
|
446
|
+
return `#${[...digits].map((digit) => digit + digit).join("")}`;
|
|
447
|
+
}
|
|
448
|
+
function alphaOf(raw) {
|
|
449
|
+
if (raw === void 0) return 1;
|
|
450
|
+
const value = Number.parseFloat(raw);
|
|
451
|
+
return raw.endsWith("%") ? value / 100 : value;
|
|
452
|
+
}
|
|
453
|
+
function cleanColor(input) {
|
|
454
|
+
const value = trimAscii(input);
|
|
455
|
+
const lower = lowerAscii(value);
|
|
456
|
+
if (value === "" || REJECTED_COLORS.has(lower)) return null;
|
|
457
|
+
if (HEX.test(value)) {
|
|
458
|
+
const hex = expandHex(value);
|
|
459
|
+
return hex.length === 9 && hex.endsWith("00") ? null : hex;
|
|
460
|
+
}
|
|
461
|
+
const rgb = RGB.exec(value);
|
|
462
|
+
if (rgb) {
|
|
463
|
+
const alpha = alphaOf(rgb[4]);
|
|
464
|
+
if (!(alpha > 0)) return null;
|
|
465
|
+
const [red, green, blue] = [
|
|
466
|
+
rgb[1],
|
|
467
|
+
rgb[2],
|
|
468
|
+
rgb[3]
|
|
469
|
+
].map((part) => Number(part));
|
|
470
|
+
const hex = `#${hexPair(red ?? 0)}${hexPair(green ?? 0)}${hexPair(blue ?? 0)}`;
|
|
471
|
+
return alpha >= 1 ? hex : `${hex}${hexPair(Math.round(alpha * 255))}`;
|
|
472
|
+
}
|
|
473
|
+
if (HSL.test(value)) return lower.replace(SPACE, " ");
|
|
474
|
+
return NAMED_COLORS.has(lower) ? lower : null;
|
|
475
|
+
}
|
|
476
|
+
function styleValue(property, value) {
|
|
477
|
+
if (property === "text-align") {
|
|
478
|
+
const align = lowerAscii(value);
|
|
479
|
+
return VIVE_PROFILE.textAlign.includes(align) ? align : null;
|
|
480
|
+
}
|
|
481
|
+
if (property === "color" || property === "background-color") return cleanColor(value);
|
|
482
|
+
const pixels = PIXELS.exec(value);
|
|
483
|
+
const number = pixels ? Number(pixels[1]) : 0;
|
|
484
|
+
return number > 0 && number <= limits$1.maxTableWidth ? `${number}px` : null;
|
|
485
|
+
}
|
|
486
|
+
function cleanStyle(value, allowed) {
|
|
487
|
+
if (byteLength(value) > allowed.length * (limits$1.maxStyleValueLength + 32)) return null;
|
|
488
|
+
const lower = lowerAscii(value);
|
|
489
|
+
if (BANNED_STYLE.some((banned) => lower.includes(banned))) return null;
|
|
490
|
+
const result = /* @__PURE__ */ new Map();
|
|
491
|
+
for (const declaration of value.split(";")) {
|
|
492
|
+
const colon = declaration.indexOf(":");
|
|
493
|
+
if (colon < 0) continue;
|
|
494
|
+
const property = lowerAscii(trimAscii(declaration.slice(0, colon)));
|
|
495
|
+
const raw = trimAscii(declaration.slice(colon + 1));
|
|
496
|
+
if (!allowed.includes(property) || result.has(property)) continue;
|
|
497
|
+
if (byteLength(raw) > limits$1.maxStyleValueLength) continue;
|
|
498
|
+
const clean = styleValue(property, raw);
|
|
499
|
+
if (clean !== null) result.set(property, clean);
|
|
500
|
+
}
|
|
501
|
+
return result.size > 0 ? [...result].map(([property, clean]) => `${property}: ${clean}`).join("; ") : null;
|
|
502
|
+
}
|
|
503
|
+
function trimEdges(value) {
|
|
504
|
+
let start = 0;
|
|
505
|
+
let end = value.length;
|
|
506
|
+
while (start < end && value.charCodeAt(start) <= 32) start++;
|
|
507
|
+
while (end > start && value.charCodeAt(end - 1) <= 32) end--;
|
|
508
|
+
return value.slice(start, end);
|
|
509
|
+
}
|
|
510
|
+
const hasControl = (value) => {
|
|
511
|
+
for (let index = 0; index < value.length; index++) {
|
|
512
|
+
const code = value.charCodeAt(index);
|
|
513
|
+
if (code < 32 || code >= 127 && code <= 159) return true;
|
|
514
|
+
}
|
|
515
|
+
return false;
|
|
516
|
+
};
|
|
517
|
+
function cleanHref(input) {
|
|
518
|
+
if (byteLength(input) > limits$1.maxAttributeLength) return null;
|
|
519
|
+
const value = trimEdges(input.replace(/[\t\n\r]/g, ""));
|
|
520
|
+
if (value === "" || hasControl(value) || value.includes("\\") || value.startsWith("//")) return null;
|
|
521
|
+
const found = SCHEME.exec(value)?.[1];
|
|
522
|
+
if (found === void 0) return value;
|
|
523
|
+
const scheme = lowerAscii(found);
|
|
524
|
+
if (!VIVE_PROFILE.linkProtocols.includes(scheme)) return null;
|
|
525
|
+
if ((scheme === "http" || scheme === "https") && !/^https?:\/\/[^/]/i.test(value)) return null;
|
|
526
|
+
return value;
|
|
527
|
+
}
|
|
528
|
+
function cleanImageSrc(input, policy) {
|
|
529
|
+
const value = trimEdges(input.replace(/[\t\n\r]/g, ""));
|
|
530
|
+
if (value === "" || hasControl(value)) return null;
|
|
531
|
+
const base64 = BASE64_IMAGE.exec(value);
|
|
532
|
+
if (base64) return policy.allowBase64 && byteLength(value) <= limits$1.maxImageSrcLength && VIVE_PROFILE.image.base64Types.includes(base64[1] ?? "") ? value : null;
|
|
533
|
+
if (byteLength(value) > limits$1.maxUrlLength || URL_FORBIDDEN.test(value)) return null;
|
|
534
|
+
const candidate = value.startsWith("//") ? `https:${value}` : /^http:\/\//i.test(value) ? `https://${value.slice(7)}` : value;
|
|
535
|
+
if (HTTPS_URL.test(candidate)) return candidate;
|
|
536
|
+
if (SCHEME.exec(value) || value.startsWith("//") || !policy.allowRelative) return null;
|
|
537
|
+
return /^[\x21-\x7e ]+$/.test(value) ? value.replaceAll(" ", "%20") : null;
|
|
538
|
+
}
|
|
539
|
+
// src/embed.ts
|
|
540
|
+
const IDS = {
|
|
541
|
+
youtube: /^[A-Za-z0-9_-]{11}$/,
|
|
542
|
+
vimeo: /^[1-9][0-9]{0,11}$/,
|
|
543
|
+
spotify: new RegExp(`^(?:${VIVE_PROFILE.spotifyTypes.join("|")}):[A-Za-z0-9]{22}$`)
|
|
544
|
+
};
|
|
545
|
+
const VIMEO_HASH = /^[0-9a-f]{8,20}$/;
|
|
546
|
+
function cleanEmbed(attributes) {
|
|
547
|
+
const provider = attributes.get("data-embed") ?? "";
|
|
548
|
+
const rule = Object.hasOwn(VIVE_PROFILE.embedProviders, provider) ? VIVE_PROFILE.embedProviders[provider] : void 0;
|
|
549
|
+
const pattern = IDS[provider];
|
|
550
|
+
const id = attributes.get("data-id") ?? "";
|
|
551
|
+
if (!rule || !pattern?.test(id)) return null;
|
|
552
|
+
const hash = attributes.get("data-hash");
|
|
553
|
+
const title = attributes.get("data-title");
|
|
554
|
+
const cleanTitle = title === void 0 ? null : cleanText(trimAscii(cleanText(title, Number.POSITIVE_INFINITY)), VIVE_PROFILE.limits.maxEmbedTitle) || null;
|
|
555
|
+
return {
|
|
556
|
+
provider,
|
|
557
|
+
rule,
|
|
558
|
+
id,
|
|
559
|
+
hash: provider === "vimeo" && hash !== void 0 && VIMEO_HASH.test(hash) ? hash : null,
|
|
560
|
+
start: provider === "spotify" ? null : cleanInteger(attributes.get("data-start"), 1, VIVE_PROFILE.limits.maxEmbedStart),
|
|
561
|
+
title: cleanTitle
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
function embedSrc(embed) {
|
|
565
|
+
const base = `https://${embed.rule.host}`;
|
|
566
|
+
if (embed.provider === "youtube") return `${base}/embed/${embed.id}${embed.start ? `?start=${embed.start}` : ""}`;
|
|
567
|
+
if (embed.provider === "vimeo") {
|
|
568
|
+
const hash = embed.hash ? `&h=${embed.hash}` : "";
|
|
569
|
+
const start = embed.start ? `#t=${embed.start}s` : "";
|
|
570
|
+
return `${base}/video/${embed.id}?dnt=1${hash}${start}`;
|
|
571
|
+
}
|
|
572
|
+
return `${base}/embed/${embed.id.replace(":", "/")}`;
|
|
573
|
+
}
|
|
574
|
+
function embedWatchUrl(embed) {
|
|
575
|
+
if (embed.provider === "youtube") return `https://www.youtube.com/watch?v=${embed.id}${embed.start ? `&t=${embed.start}s` : ""}`;
|
|
576
|
+
if (embed.provider === "vimeo") {
|
|
577
|
+
const hash = embed.hash ? `/${embed.hash}` : "";
|
|
578
|
+
return `https://vimeo.com/${embed.id}${hash}${embed.start ? `#t=${embed.start}s` : ""}`;
|
|
579
|
+
}
|
|
580
|
+
return `https://open.spotify.com/${embed.id.replace(":", "/")}`;
|
|
581
|
+
}
|
|
582
|
+
function wrapperAttributes(embed) {
|
|
583
|
+
const result = [
|
|
584
|
+
["class", "vive-embed"],
|
|
585
|
+
["data-embed", embed.provider],
|
|
586
|
+
["data-id", embed.id]
|
|
587
|
+
];
|
|
588
|
+
if (embed.hash) result.push(["data-hash", embed.hash]);
|
|
589
|
+
if (embed.start) result.push(["data-start", String(embed.start)]);
|
|
590
|
+
if (embed.title) result.push(["data-title", embed.title]);
|
|
591
|
+
return result;
|
|
592
|
+
}
|
|
593
|
+
function iframeAttributes(embed) {
|
|
594
|
+
const { rule } = embed;
|
|
595
|
+
const height = (rule.compactTypes?.some((type) => embed.id.startsWith(`${type}:`)) ?? false) && rule.compactHeight ? rule.compactHeight : rule.height;
|
|
596
|
+
return [
|
|
597
|
+
["src", embedSrc(embed)],
|
|
598
|
+
["title", embed.title ?? rule.label],
|
|
599
|
+
["width", String(rule.width)],
|
|
600
|
+
["height", String(height)],
|
|
601
|
+
["style", rule.fluid ? `width: 100%; height: auto; aspect-ratio: ${rule.width} / ${height}; border: 0` : `width: 100%; height: ${height}px; border: 0`],
|
|
602
|
+
["loading", "lazy"],
|
|
603
|
+
["referrerpolicy", "strict-origin-when-cross-origin"],
|
|
604
|
+
["sandbox", rule.sandbox],
|
|
605
|
+
["allow", rule.allow],
|
|
606
|
+
["allowfullscreen", ""]
|
|
607
|
+
];
|
|
608
|
+
}
|
|
609
|
+
// src/guard.ts
|
|
610
|
+
const STATES = 10;
|
|
611
|
+
const END = -1;
|
|
612
|
+
function isSpace(code) {
|
|
613
|
+
return code === 32 || code === 9 || code === 10 || code === 12 || code === 13;
|
|
614
|
+
}
|
|
615
|
+
function isAsciiLetter(code) {
|
|
616
|
+
return code >= 65 && code <= 90 || code >= 97 && code <= 122;
|
|
617
|
+
}
|
|
618
|
+
const move = (state, added = 0) => state * 2 + added;
|
|
619
|
+
function transition(state, code) {
|
|
620
|
+
switch (state) {
|
|
621
|
+
case 0:
|
|
622
|
+
if (isSpace(code)) return move(1);
|
|
623
|
+
if (code === 47) return move(9);
|
|
624
|
+
return code === 62 ? END : move(0);
|
|
625
|
+
case 1:
|
|
626
|
+
if (isSpace(code)) return move(1);
|
|
627
|
+
if (code === 47 || code === 62) return transition(3, code);
|
|
628
|
+
return move(2, 1);
|
|
629
|
+
case 2:
|
|
630
|
+
if (isSpace(code) || code === 47 || code === 62) return transition(3, code);
|
|
631
|
+
return code === 61 ? move(4) : move(2);
|
|
632
|
+
case 3:
|
|
633
|
+
if (isSpace(code)) return move(3);
|
|
634
|
+
if (code === 47) return move(9);
|
|
635
|
+
if (code === 61) return move(4);
|
|
636
|
+
return code === 62 ? END : move(2, 1);
|
|
637
|
+
case 4:
|
|
638
|
+
if (isSpace(code)) return move(4);
|
|
639
|
+
if (code === 34) return move(5);
|
|
640
|
+
if (code === 39) return move(6);
|
|
641
|
+
return code === 62 ? END : move(7);
|
|
642
|
+
case 5: return move(code === 34 ? 8 : 5);
|
|
643
|
+
case 6: return move(code === 39 ? 8 : 6);
|
|
644
|
+
case 7:
|
|
645
|
+
if (isSpace(code)) return move(1);
|
|
646
|
+
return code === 62 ? END : move(7);
|
|
647
|
+
case 8:
|
|
648
|
+
if (isSpace(code)) return move(1);
|
|
649
|
+
if (code === 47) return move(9);
|
|
650
|
+
return code === 62 ? END : transition(1, code);
|
|
651
|
+
case 9: return code === 62 ? END : transition(1, code);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
function opensTag(input, index) {
|
|
655
|
+
if (!isAsciiLetter(input.charCodeAt(index))) return false;
|
|
656
|
+
const previous = input.charCodeAt(index - 1);
|
|
657
|
+
return previous === 60 || previous === 47 && input.charCodeAt(index - 2) === 60;
|
|
658
|
+
}
|
|
659
|
+
function hasCrowdedTag(input, maxAttributes) {
|
|
660
|
+
let counts = new Int32Array(STATES).fill(END);
|
|
661
|
+
let next = new Int32Array(STATES);
|
|
662
|
+
let active = false;
|
|
663
|
+
for (let index = 0; index < input.length; index++) {
|
|
664
|
+
if (!active) {
|
|
665
|
+
const open = input.indexOf("<", index);
|
|
666
|
+
if (open === -1) return false;
|
|
667
|
+
index = open;
|
|
668
|
+
}
|
|
669
|
+
const code = input.charCodeAt(index);
|
|
670
|
+
if (opensTag(input, index)) counts[0] = Math.max(counts[0] ?? END, 0);
|
|
671
|
+
next.fill(END);
|
|
672
|
+
for (let state = 0; state < STATES; state++) {
|
|
673
|
+
const count = counts[state] ?? END;
|
|
674
|
+
if (count === END) continue;
|
|
675
|
+
const target = transition(state, code);
|
|
676
|
+
if (target === END) continue;
|
|
677
|
+
const total = count + (target & 1);
|
|
678
|
+
if (total > maxAttributes) return true;
|
|
679
|
+
const reached = target >> 1;
|
|
680
|
+
if (total > (next[reached] ?? END)) next[reached] = total;
|
|
681
|
+
}
|
|
682
|
+
[counts, next] = [next, counts];
|
|
683
|
+
active = counts.some((count) => count !== END) || code === 60 || code === 47 && input.charCodeAt(index - 1) === 60;
|
|
684
|
+
}
|
|
685
|
+
return false;
|
|
686
|
+
}
|
|
687
|
+
function guardedTreeAdapter(maxElements, maxNesting, onLimit) {
|
|
688
|
+
let created = 0;
|
|
689
|
+
const owners = /* @__PURE__ */ new WeakMap();
|
|
690
|
+
const checkDepth = (node) => {
|
|
691
|
+
if (!defaultTreeAdapter.isElementNode(node)) return;
|
|
692
|
+
let depth = 0;
|
|
693
|
+
let current = node;
|
|
694
|
+
while (current) {
|
|
695
|
+
depth++;
|
|
696
|
+
if (depth > maxNesting) onLimit();
|
|
697
|
+
current = ("parentNode" in current ? current.parentNode : null) ?? owners.get(current) ?? null;
|
|
698
|
+
}
|
|
699
|
+
};
|
|
700
|
+
return {
|
|
701
|
+
...defaultTreeAdapter,
|
|
702
|
+
createElement(tagName, namespaceURI, attrs) {
|
|
703
|
+
created++;
|
|
704
|
+
if (created > maxElements) onLimit();
|
|
705
|
+
return defaultTreeAdapter.createElement(tagName, namespaceURI, attrs);
|
|
706
|
+
},
|
|
707
|
+
appendChild(parentNode, newNode) {
|
|
708
|
+
defaultTreeAdapter.appendChild(parentNode, newNode);
|
|
709
|
+
checkDepth(newNode);
|
|
710
|
+
},
|
|
711
|
+
insertBefore(parentNode, newNode, referenceNode) {
|
|
712
|
+
defaultTreeAdapter.insertBefore(parentNode, newNode, referenceNode);
|
|
713
|
+
checkDepth(newNode);
|
|
714
|
+
},
|
|
715
|
+
setTemplateContent(templateElement, contentElement) {
|
|
716
|
+
defaultTreeAdapter.setTemplateContent(templateElement, contentElement);
|
|
717
|
+
owners.set(contentElement, templateElement);
|
|
718
|
+
}
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
// src/sanitize.ts
|
|
722
|
+
var SanitizeError = class extends Error {
|
|
723
|
+
code;
|
|
724
|
+
constructor(code, message) {
|
|
725
|
+
super(message);
|
|
726
|
+
this.name = "SanitizeError";
|
|
727
|
+
this.code = code;
|
|
728
|
+
}
|
|
729
|
+
};
|
|
730
|
+
const { limits } = VIVE_PROFILE;
|
|
731
|
+
const DROP = new Set(VIVE_PROFILE.dropWithContent);
|
|
732
|
+
const FOOTNOTE_ELEMENTS = new Set(VIVE_PROFILE.footnoteElements);
|
|
733
|
+
const ROOT = {
|
|
734
|
+
parent: "",
|
|
735
|
+
taskList: false,
|
|
736
|
+
taskItem: false,
|
|
737
|
+
inLink: false,
|
|
738
|
+
footnote: null
|
|
739
|
+
};
|
|
740
|
+
const BODY = defaultTreeAdapter.createElement("body", html.NS.HTML, []);
|
|
741
|
+
function attributesOf(element) {
|
|
742
|
+
const result = /* @__PURE__ */ new Map();
|
|
743
|
+
for (const attribute of element.attrs) {
|
|
744
|
+
const max = element.tagName === "img" && attribute.name === "src" ? limits.maxImageSrcLength : limits.maxAttributeLength;
|
|
745
|
+
if (attribute.namespace || byteLength(attribute.value) > max) continue;
|
|
746
|
+
if (!result.has(attribute.name)) result.set(attribute.name, attribute.value);
|
|
747
|
+
}
|
|
748
|
+
return result;
|
|
749
|
+
}
|
|
750
|
+
function append(target, tag, attributes) {
|
|
751
|
+
const element = defaultTreeAdapter.createElement(tag, html.NS.HTML, attributes.map(([name, value]) => ({
|
|
752
|
+
name,
|
|
753
|
+
value
|
|
754
|
+
})));
|
|
755
|
+
defaultTreeAdapter.appendChild(target, element);
|
|
756
|
+
return element;
|
|
757
|
+
}
|
|
758
|
+
function styleOf(tag, source) {
|
|
759
|
+
const allowed = VIVE_PROFILE.elements[tag]?.styles;
|
|
760
|
+
const style = source.get("style");
|
|
761
|
+
if (!allowed || style === void 0) return [];
|
|
762
|
+
const clean = cleanStyle(style, allowed);
|
|
763
|
+
return clean === null ? [] : [["style", clean]];
|
|
764
|
+
}
|
|
765
|
+
function textAttribute(source, name) {
|
|
766
|
+
const value = source.get(name);
|
|
767
|
+
return value === void 0 ? [] : [[name, cleanText(value)]];
|
|
768
|
+
}
|
|
769
|
+
function linkAttributes(source) {
|
|
770
|
+
const result = [["rel", VIVE_PROFILE.linkRel]];
|
|
771
|
+
const href = source.get("href");
|
|
772
|
+
const clean = href === void 0 ? null : cleanHref(href);
|
|
773
|
+
if (clean !== null) result.push(["href", clean]);
|
|
774
|
+
result.push(...textAttribute(source, "title"));
|
|
775
|
+
if (source.get("target") === "_blank") result.push(["target", "_blank"]);
|
|
776
|
+
return result;
|
|
777
|
+
}
|
|
778
|
+
function imageAttributes(source, policy) {
|
|
779
|
+
const raw = source.get("src");
|
|
780
|
+
const src = raw === void 0 ? null : cleanImageSrc(raw, policy);
|
|
781
|
+
if (src === null) return null;
|
|
782
|
+
const result = [["src", src]];
|
|
783
|
+
result.push(...textAttribute(source, "alt"), ...textAttribute(source, "title"));
|
|
784
|
+
for (const name of ["width", "height"]) {
|
|
785
|
+
const size = cleanInteger(source.get(name), 1, limits.maxImageSize);
|
|
786
|
+
if (size !== null) result.push([name, String(size)]);
|
|
787
|
+
}
|
|
788
|
+
const { loading, decoding, referrerpolicy } = VIVE_PROFILE.image;
|
|
789
|
+
result.push(["loading", loading], ["decoding", decoding], ["referrerpolicy", referrerpolicy]);
|
|
790
|
+
return result;
|
|
791
|
+
}
|
|
792
|
+
function cellAttributes(source) {
|
|
793
|
+
const result = [];
|
|
794
|
+
for (const name of ["colspan", "rowspan"]) {
|
|
795
|
+
const span = cleanInteger(source.get(name), 2, limits.maxSpan);
|
|
796
|
+
if (span !== null) result.push([name, String(span)]);
|
|
797
|
+
}
|
|
798
|
+
const widths = cleanColWidth(source.get("data-colwidth"));
|
|
799
|
+
if (widths !== null) result.push(["data-colwidth", widths]);
|
|
800
|
+
return result;
|
|
801
|
+
}
|
|
802
|
+
function embed(source, node, target) {
|
|
803
|
+
const data = cleanEmbed(source);
|
|
804
|
+
if (!data) return;
|
|
805
|
+
const wrapper = append(target, "div", wrapperAttributes(data));
|
|
806
|
+
if (node.childNodes.some((child) => defaultTreeAdapter.isElementNode(child) && child.tagName === "a")) {
|
|
807
|
+
const link = append(wrapper, "a", [["href", embedWatchUrl(data)], ["rel", "noopener noreferrer"]]);
|
|
808
|
+
defaultTreeAdapter.insertText(link, data.title ?? data.rule.label);
|
|
809
|
+
} else append(wrapper, "iframe", iframeAttributes(data));
|
|
810
|
+
}
|
|
811
|
+
function insertText(target, value, context) {
|
|
812
|
+
const text = context.footnote ? takeCodePoints(value, context.footnote) : value;
|
|
813
|
+
if (text) defaultTreeAdapter.insertText(target, text);
|
|
814
|
+
}
|
|
815
|
+
function textOf(node, depth) {
|
|
816
|
+
let text = "";
|
|
817
|
+
for (const child of node.childNodes) if (defaultTreeAdapter.isTextNode(child)) text += child.value;
|
|
818
|
+
else if (defaultTreeAdapter.isElementNode(child) && depth < limits.maxDepth && child.namespaceURI === html.NS.HTML && !DROP.has(child.tagName)) text += textOf(child, depth + 1);
|
|
819
|
+
return text;
|
|
820
|
+
}
|
|
821
|
+
function mention(id, node, target, depth, context) {
|
|
822
|
+
const text = textOf(node, depth);
|
|
823
|
+
if (context.inLink || context.footnote || !isMentionId(id)) {
|
|
824
|
+
insertText(target, text, context);
|
|
825
|
+
return;
|
|
826
|
+
}
|
|
827
|
+
const created = append(target, "span", [["class", VIVE_PROFILE.mentionClass], ["data-mention", id]]);
|
|
828
|
+
defaultTreeAdapter.insertText(created, `@${cleanMentionLabel(text) || id}`);
|
|
829
|
+
}
|
|
830
|
+
function math(tag, node, target, depth, context) {
|
|
831
|
+
const text = textOf(node, depth);
|
|
832
|
+
const latex = context.inLink || context.footnote ? null : cleanLatex(text);
|
|
833
|
+
if (latex === null) {
|
|
834
|
+
insertText(target, text, context);
|
|
835
|
+
return;
|
|
836
|
+
}
|
|
837
|
+
const created = append(target, tag, [["data-math", tag === "span" ? "inline" : "block"]]);
|
|
838
|
+
defaultTreeAdapter.insertText(created, latex);
|
|
839
|
+
}
|
|
840
|
+
function footnote(node, target, depth, context, settings) {
|
|
841
|
+
if (context.inLink || context.footnote) {
|
|
842
|
+
walk(node, target, depth, context, settings);
|
|
843
|
+
return;
|
|
844
|
+
}
|
|
845
|
+
walk(node, append(target, "sup", [["data-footnote", ""]]), depth, {
|
|
846
|
+
...ROOT,
|
|
847
|
+
parent: "sup",
|
|
848
|
+
footnote: { left: limits.maxFootnoteLength }
|
|
849
|
+
}, settings);
|
|
850
|
+
}
|
|
851
|
+
function walk(source, target, depth, context, settings) {
|
|
852
|
+
for (const child of source.childNodes) if (defaultTreeAdapter.isTextNode(child)) insertText(target, child.value, context);
|
|
853
|
+
else if (defaultTreeAdapter.isElementNode(child)) element(child, target, depth + 1, context, settings);
|
|
854
|
+
}
|
|
855
|
+
function element(node, target, depth, context, settings) {
|
|
856
|
+
if (depth > limits.maxDepth || node.namespaceURI !== html.NS.HTML) return;
|
|
857
|
+
const tag = node.tagName;
|
|
858
|
+
if (DROP.has(tag)) return;
|
|
859
|
+
const source = attributesOf(node);
|
|
860
|
+
if (context.footnote && !FOOTNOTE_ELEMENTS.has(tag)) {
|
|
861
|
+
walk(node, target, depth, context, settings);
|
|
862
|
+
return;
|
|
863
|
+
}
|
|
864
|
+
const mentionId = source.get("data-mention");
|
|
865
|
+
if (tag === "span" && mentionId !== void 0 && source.get("class") === VIVE_PROFILE.mentionClass) {
|
|
866
|
+
mention(mentionId, node, target, depth, context);
|
|
867
|
+
return;
|
|
868
|
+
}
|
|
869
|
+
const mathType = source.get("data-math");
|
|
870
|
+
if (tag === "span" && mathType === "inline" || tag === "div" && mathType === "block") {
|
|
871
|
+
math(tag, node, target, depth, context);
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
if (tag === "sup" && source.has("data-footnote")) {
|
|
875
|
+
footnote(node, target, depth, context, settings);
|
|
876
|
+
return;
|
|
877
|
+
}
|
|
878
|
+
const next = {
|
|
879
|
+
parent: tag,
|
|
880
|
+
taskList: false,
|
|
881
|
+
taskItem: false,
|
|
882
|
+
inLink: context.inLink || tag === "a",
|
|
883
|
+
footnote: context.footnote
|
|
884
|
+
};
|
|
885
|
+
let attributes = null;
|
|
886
|
+
switch (tag) {
|
|
887
|
+
case "div":
|
|
888
|
+
if (source.get("class") === "vive-embed" && source.has("data-embed")) {
|
|
889
|
+
if (settings.embeds) embed(source, node, target);
|
|
890
|
+
return;
|
|
891
|
+
}
|
|
892
|
+
if (source.has("data-embed-invalid")) return;
|
|
893
|
+
attributes = context.taskItem ? [] : null;
|
|
894
|
+
break;
|
|
895
|
+
case "input":
|
|
896
|
+
if (!context.taskItem || source.get("type") !== "checkbox") return;
|
|
897
|
+
append(target, "input", [
|
|
898
|
+
["type", "checkbox"],
|
|
899
|
+
["disabled", ""],
|
|
900
|
+
...source.has("checked") ? [["checked", ""]] : []
|
|
901
|
+
]);
|
|
902
|
+
return;
|
|
903
|
+
case "ul":
|
|
904
|
+
next.taskList = source.get("data-type") === "taskList";
|
|
905
|
+
attributes = next.taskList ? [["data-type", "taskList"]] : [];
|
|
906
|
+
break;
|
|
907
|
+
case "li":
|
|
908
|
+
next.taskItem = context.taskList && source.get("data-type") === "taskItem";
|
|
909
|
+
attributes = next.taskItem ? [["data-type", "taskItem"], ["data-checked", source.get("data-checked") === "true" ? "true" : "false"]] : [];
|
|
910
|
+
break;
|
|
911
|
+
case "ol": {
|
|
912
|
+
const start = cleanInteger(source.get("start"), 0, limits.maxListStart);
|
|
913
|
+
attributes = start !== null && start !== 1 ? [["start", String(start)]] : [];
|
|
914
|
+
break;
|
|
915
|
+
}
|
|
916
|
+
case "pre": {
|
|
917
|
+
const language = cleanLanguage(source.get("data-language"));
|
|
918
|
+
attributes = language ? [["data-language", language]] : [];
|
|
919
|
+
break;
|
|
920
|
+
}
|
|
921
|
+
case "code": {
|
|
922
|
+
const match = /^language-(.+)$/.exec(source.get("class") ?? "");
|
|
923
|
+
const language = context.parent === "pre" ? cleanLanguage(match?.[1]) : null;
|
|
924
|
+
attributes = language ? [["class", `language-${language}`]] : [];
|
|
925
|
+
break;
|
|
926
|
+
}
|
|
927
|
+
case "a":
|
|
928
|
+
attributes = context.inLink ? null : linkAttributes(source);
|
|
929
|
+
break;
|
|
930
|
+
case "sup":
|
|
931
|
+
attributes = null;
|
|
932
|
+
break;
|
|
933
|
+
case "img": {
|
|
934
|
+
const image = imageAttributes(source, settings.image);
|
|
935
|
+
if (image) append(target, "img", image);
|
|
936
|
+
return;
|
|
937
|
+
}
|
|
938
|
+
case "td":
|
|
939
|
+
case "th":
|
|
940
|
+
attributes = cellAttributes(source);
|
|
941
|
+
break;
|
|
942
|
+
default: attributes = Object.hasOwn(VIVE_PROFILE.elements, tag) ? styleOf(tag, source) : null;
|
|
943
|
+
}
|
|
944
|
+
if (attributes === null) {
|
|
945
|
+
walk(node, target, depth, context, settings);
|
|
946
|
+
return;
|
|
947
|
+
}
|
|
948
|
+
walk(node, append(target, tag, attributes), depth, next, settings);
|
|
949
|
+
}
|
|
950
|
+
const MAX_ROUNDS = 3;
|
|
951
|
+
function scrub(input) {
|
|
952
|
+
return input.toWellFormed().replaceAll("\0", "");
|
|
953
|
+
}
|
|
954
|
+
function tooComplex() {
|
|
955
|
+
throw new SanitizeError("TOO_COMPLEX", "HTML has too many attributes, elements or nesting levels.");
|
|
956
|
+
}
|
|
957
|
+
function tagCount(input) {
|
|
958
|
+
let count = 0;
|
|
959
|
+
for (let index = input.indexOf("<"); index !== -1; index = input.indexOf("<", index + 1)) count++;
|
|
960
|
+
return count;
|
|
961
|
+
}
|
|
962
|
+
function cleanOnce(input, settings) {
|
|
963
|
+
if (hasCrowdedTag(input, limits.maxAttributes)) tooComplex();
|
|
964
|
+
const treeAdapter = guardedTreeAdapter(tagCount(input) + limits.maxExtraElements, limits.maxNesting, tooComplex);
|
|
965
|
+
const parser = Parser.getFragmentParser(BODY, {
|
|
966
|
+
scriptingEnabled: false,
|
|
967
|
+
treeAdapter
|
|
968
|
+
});
|
|
969
|
+
parser.tokenizer.write(input, true);
|
|
970
|
+
const root = treeAdapter.getFirstChild(parser.document);
|
|
971
|
+
const output = defaultTreeAdapter.createDocumentFragment();
|
|
972
|
+
if (root && defaultTreeAdapter.isElementNode(root)) walk(root, output, 0, ROOT, settings);
|
|
973
|
+
return serialize(output);
|
|
974
|
+
}
|
|
975
|
+
function sanitizeHtml(input, options = {}) {
|
|
976
|
+
if (typeof input !== "string") return "";
|
|
977
|
+
const maxBytes = options.maxBytes ?? limits.maxBytes;
|
|
978
|
+
if (byteLength(input) > maxBytes) throw new SanitizeError("TOO_LARGE", `HTML is larger than ${maxBytes} bytes.`);
|
|
979
|
+
const settings = {
|
|
980
|
+
embeds: options.embeds === true,
|
|
981
|
+
image: {
|
|
982
|
+
allowBase64: options.allowBase64 === true,
|
|
983
|
+
allowRelative: options.allowRelativeImages === true
|
|
984
|
+
}
|
|
985
|
+
};
|
|
986
|
+
let clean = cleanOnce(scrub(input), settings);
|
|
987
|
+
for (let round = 1; round < MAX_ROUNDS; round++) {
|
|
988
|
+
const again = cleanOnce(clean, settings);
|
|
989
|
+
if (again === clean) break;
|
|
990
|
+
clean = again;
|
|
991
|
+
}
|
|
992
|
+
return clean;
|
|
993
|
+
}
|
|
994
|
+
export { SanitizeError, VIVE_PROFILE, sanitizeHtml };
|
|
995
|
+
|
|
996
|
+
//# sourceMappingURL=index.mjs.map
|