@viveeditor/sanitize 0.1.0 → 1.0.0-rc.1

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/dist/index.mjs CHANGED
@@ -8,22 +8,30 @@ const VIVE_PROFILE = {
8
8
  "maxAttributeLength": 2048,
9
9
  "maxTextAttributeLength": 1e3,
10
10
  "maxStyleValueLength": 64,
11
+ "minFontSizePt": 6,
12
+ "maxFontSizePt": 96,
11
13
  "maxSpan": 100,
12
14
  "maxImageSize": 1e4,
13
15
  "maxListStart": 99999,
14
16
  "maxTableWidth": 99999,
17
+ "maxRowHeight": 2e3,
15
18
  "maxEmbedStart": 86400,
16
19
  "maxEmbedTitle": 200,
17
20
  "maxImageSrcLength": 15e5,
18
21
  "maxUrlLength": 4096,
19
22
  "maxMentionId": 128,
20
23
  "maxMentionLabel": 100,
24
+ "maxCommentId": 64,
25
+ "maxChangeId": 64,
26
+ "maxAuthorId": 128,
27
+ "maxAuthorName": 100,
21
28
  "maxMathLength": 2e3,
22
29
  "maxMathDepth": 64,
23
30
  "maxFootnoteLength": 5e3,
24
31
  "maxAttributes": 64,
25
32
  "maxNesting": 512,
26
- "maxExtraElements": 2048
33
+ "maxExtraElements": 2048,
34
+ "maxTags": 1e5
27
35
  },
28
36
  dropWithContent: [
29
37
  "script",
@@ -49,13 +57,104 @@ const VIVE_PROFILE = {
49
57
  "frame"
50
58
  ],
51
59
  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"] },
60
+ "p": {
61
+ "styles": [
62
+ "text-align",
63
+ "margin-inline-start",
64
+ "line-height"
65
+ ],
66
+ "attributes": [
67
+ "data-vive-break",
68
+ "data-vive-change",
69
+ "data-author",
70
+ "data-name",
71
+ "data-time"
72
+ ]
73
+ },
74
+ "h1": {
75
+ "styles": [
76
+ "text-align",
77
+ "margin-inline-start",
78
+ "line-height"
79
+ ],
80
+ "attributes": [
81
+ "data-vive-break",
82
+ "data-vive-change",
83
+ "data-author",
84
+ "data-name",
85
+ "data-time"
86
+ ]
87
+ },
88
+ "h2": {
89
+ "styles": [
90
+ "text-align",
91
+ "margin-inline-start",
92
+ "line-height"
93
+ ],
94
+ "attributes": [
95
+ "data-vive-break",
96
+ "data-vive-change",
97
+ "data-author",
98
+ "data-name",
99
+ "data-time"
100
+ ]
101
+ },
102
+ "h3": {
103
+ "styles": [
104
+ "text-align",
105
+ "margin-inline-start",
106
+ "line-height"
107
+ ],
108
+ "attributes": [
109
+ "data-vive-break",
110
+ "data-vive-change",
111
+ "data-author",
112
+ "data-name",
113
+ "data-time"
114
+ ]
115
+ },
116
+ "h4": {
117
+ "styles": [
118
+ "text-align",
119
+ "margin-inline-start",
120
+ "line-height"
121
+ ],
122
+ "attributes": [
123
+ "data-vive-break",
124
+ "data-vive-change",
125
+ "data-author",
126
+ "data-name",
127
+ "data-time"
128
+ ]
129
+ },
130
+ "h5": {
131
+ "styles": [
132
+ "text-align",
133
+ "margin-inline-start",
134
+ "line-height"
135
+ ],
136
+ "attributes": [
137
+ "data-vive-break",
138
+ "data-vive-change",
139
+ "data-author",
140
+ "data-name",
141
+ "data-time"
142
+ ]
143
+ },
144
+ "h6": {
145
+ "styles": [
146
+ "text-align",
147
+ "margin-inline-start",
148
+ "line-height"
149
+ ],
150
+ "attributes": [
151
+ "data-vive-break",
152
+ "data-vive-change",
153
+ "data-author",
154
+ "data-name",
155
+ "data-time"
156
+ ]
157
+ },
59
158
  "blockquote": {},
60
159
  "hr": {},
61
160
  "br": {},
@@ -63,14 +162,29 @@ const VIVE_PROFILE = {
63
162
  "em": {},
64
163
  "u": {},
65
164
  "s": {},
165
+ "ins": { "attributes": [
166
+ "data-vive-change",
167
+ "data-author",
168
+ "data-name",
169
+ "data-time"
170
+ ] },
171
+ "del": { "attributes": [
172
+ "data-vive-change",
173
+ "data-author",
174
+ "data-name",
175
+ "data-time"
176
+ ] },
66
177
  "code": { "attributes": ["class"] },
67
178
  "a": { "attributes": [
68
179
  "href",
69
180
  "title",
70
181
  "target"
71
182
  ] },
72
- "ul": { "attributes": ["data-type"] },
73
- "ol": { "attributes": ["start"] },
183
+ "ul": {
184
+ "attributes": ["data-type"],
185
+ "styles": ["list-style-type"]
186
+ },
187
+ "ol": { "attributes": ["start", "type"] },
74
188
  "li": { "attributes": ["data-type", "data-checked"] },
75
189
  "input": { "attributes": [
76
190
  "type",
@@ -87,27 +201,58 @@ const VIVE_PROFILE = {
87
201
  "data-math"
88
202
  ] },
89
203
  "pre": { "attributes": ["data-language"] },
90
- "table": { "styles": ["width", "min-width"] },
204
+ "table": {
205
+ "styles": [
206
+ "width",
207
+ "min-width",
208
+ "--vive-table-border-color"
209
+ ],
210
+ "attributes": [
211
+ "data-width",
212
+ "data-align",
213
+ "data-border",
214
+ "data-striped"
215
+ ]
216
+ },
91
217
  "colgroup": {},
92
218
  "col": { "styles": ["width"] },
93
219
  "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
- ] },
220
+ "tr": { "styles": ["height"] },
221
+ "td": {
222
+ "attributes": [
223
+ "colspan",
224
+ "rowspan",
225
+ "data-colwidth"
226
+ ],
227
+ "styles": [
228
+ "background-color",
229
+ "vertical-align",
230
+ "text-align"
231
+ ]
232
+ },
233
+ "th": {
234
+ "attributes": [
235
+ "colspan",
236
+ "rowspan",
237
+ "data-colwidth"
238
+ ],
239
+ "styles": [
240
+ "background-color",
241
+ "vertical-align",
242
+ "text-align"
243
+ ]
244
+ },
105
245
  "span": {
106
- "styles": ["color"],
246
+ "styles": [
247
+ "color",
248
+ "font-size",
249
+ "font-family"
250
+ ],
107
251
  "attributes": [
108
252
  "class",
109
253
  "data-mention",
110
- "data-math"
254
+ "data-math",
255
+ "data-vive-comment"
111
256
  ]
112
257
  },
113
258
  "mark": { "styles": ["background-color"] },
@@ -121,6 +266,9 @@ const VIVE_PROFILE = {
121
266
  "decoding",
122
267
  "referrerpolicy"
123
268
  ] },
269
+ "figure": { "attributes": ["data-align"] },
270
+ "figcaption": {},
271
+ "sub": {},
124
272
  "sup": { "attributes": ["data-footnote"] }
125
273
  },
126
274
  mentionClass: "vive-mention",
@@ -135,6 +283,58 @@ const VIVE_PROFILE = {
135
283
  "right",
136
284
  "justify"
137
285
  ],
286
+ fontSizes: [
287
+ "0.75em",
288
+ "0.875em",
289
+ "1.25em",
290
+ "1.5em"
291
+ ],
292
+ indents: [
293
+ "2em",
294
+ "4em",
295
+ "6em",
296
+ "8em",
297
+ "10em",
298
+ "12em",
299
+ "14em",
300
+ "16em"
301
+ ],
302
+ imageAlign: [
303
+ "left",
304
+ "center",
305
+ "right"
306
+ ],
307
+ verticalAlign: ["middle", "bottom"],
308
+ lineHeights: [
309
+ "1",
310
+ "1.15",
311
+ "1.5",
312
+ "2"
313
+ ],
314
+ orderedListTypes: [
315
+ "a",
316
+ "A",
317
+ "i",
318
+ "I"
319
+ ],
320
+ bulletStyles: [
321
+ "disc",
322
+ "circle",
323
+ "square"
324
+ ],
325
+ tableWidths: ["full"],
326
+ tableAlign: ["center"],
327
+ tableBorders: ["rows", "none"],
328
+ fontFamilies: [
329
+ "serif",
330
+ "sans-serif",
331
+ "monospace",
332
+ "Arial, Helvetica, sans-serif",
333
+ "Georgia, serif",
334
+ "'Times New Roman', Times, serif",
335
+ "'Courier New', Courier, monospace",
336
+ "Verdana, Geneva, sans-serif"
337
+ ],
138
338
  linkProtocols: [
139
339
  "http",
140
340
  "https",
@@ -360,8 +560,11 @@ const { limits: limits$1 } = VIVE_PROFILE;
360
560
  const SCHEME = /^([a-z][a-z0-9+.-]*):/i;
361
561
  const HEX = /^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
362
562
  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;
563
+ 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
564
  const PIXELS = /^([0-9]{1,5})px$/;
565
+ const POINTS = /^([0-9]{1,2})(?:\.([05]))?pt$/;
566
+ const FAMILY_PART = /^(?:'[A-Za-z][A-Za-z0-9 -]*'|[A-Za-z][A-Za-z0-9 -]*)$/;
567
+ const MAX_FAMILY_BYTES = 64;
365
568
  const DIGITS = /^[0-9]{1,5}$/;
366
569
  const LANGUAGE = /^[a-z0-9][a-z0-9+#.-]{0,31}$/i;
367
570
  const HTTPS_URL = /^https:\/\/[a-z0-9](?:[a-z0-9.-]{0,251}[a-z0-9])?(?::[0-9]{1,5})?(?:[/?#][\x21-\x7e]*)?$/i;
@@ -379,6 +582,10 @@ const NAMED_COLORS = new Set(VIVE_PROFILE.namedColors);
379
582
  const REJECTED_COLORS = new Set(VIVE_PROFILE.colorKeywordsRejected);
380
583
  const SPACE = /[\t\n\f\r ]+/g;
381
584
  const MENTION_ID = new RegExp(`^(?!.*\\.\\.)[A-Za-z0-9_][A-Za-z0-9_.:@-]{0,${limits$1.maxMentionId - 1}}$`);
585
+ const COMMENT_ID = new RegExp(`^[A-Za-z0-9_-]{1,${limits$1.maxCommentId}}$`);
586
+ const CHANGE_ID = new RegExp(`^[A-Za-z0-9_-]{1,${limits$1.maxChangeId}}$`);
587
+ const CHANGE_TIME = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d{3})?Z$/;
588
+ const BLANK = /^[\t\n\r \u00A0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]*$/;
382
589
  const byteLength = (value) => Buffer.byteLength(value, "utf8");
383
590
  const lowerAscii = (value) => value.replace(/[A-Z]/g, (letter) => letter.toLowerCase());
384
591
  const trimAscii = (value) => value.replace(/^[\t\n\f\r ]+|[\t\n\f\r ]+$/g, "");
@@ -393,6 +600,27 @@ function cleanText(value, max = limits$1.maxTextAttributeLength) {
393
600
  }
394
601
  const isHidden = (code) => code <= 31 || code >= 127 && code <= 159 || code >= 8203 && code <= 8207 || code >= 8234 && code <= 8238 || code >= 8294 && code <= 8297 || code === 65279;
395
602
  const isMentionId = (value) => MENTION_ID.test(value);
603
+ const isCommentId = (value) => COMMENT_ID.test(value);
604
+ const isChangeId = (value) => CHANGE_ID.test(value);
605
+ const isLineBreaker = (code) => code < 32 || code === 127 || code >= 8234 && code <= 8238 || code >= 8294 && code <= 8297 || code >= 55296 && code <= 57343;
606
+ function isChangeLine(value, max) {
607
+ let count = 0;
608
+ for (const character of value) {
609
+ if (isLineBreaker(character.codePointAt(0) ?? 0)) return false;
610
+ count++;
611
+ }
612
+ return count <= max && !BLANK.test(value);
613
+ }
614
+ const isAuthorId = (value) => isChangeLine(value, limits$1.maxAuthorId);
615
+ const isAuthorName = (value) => isChangeLine(value, limits$1.maxAuthorName);
616
+ function isChangeTime(value) {
617
+ const match = CHANGE_TIME.exec(value);
618
+ if (!match) return false;
619
+ const [year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0] = match.slice(1).map(Number);
620
+ const date = new Date(Date.UTC(year, month - 1, day));
621
+ date.setUTCFullYear(year);
622
+ return year >= 1 && date.getUTCMonth() === month - 1 && date.getUTCDate() === day && hour <= 23 && minute <= 59 && second <= 59;
623
+ }
396
624
  function cleanMentionLabel(value) {
397
625
  let result = "";
398
626
  for (const character of value) {
@@ -470,20 +698,66 @@ function cleanColor(input) {
470
698
  const hex = `#${hexPair(red ?? 0)}${hexPair(green ?? 0)}${hexPair(blue ?? 0)}`;
471
699
  return alpha >= 1 ? hex : `${hex}${hexPair(Math.round(alpha * 255))}`;
472
700
  }
473
- if (HSL.test(value)) return lower.replace(SPACE, " ");
701
+ const hsl = HSL.exec(value);
702
+ if (hsl) return alphaOf(hsl[1]) > 0 ? lower.replace(SPACE, " ") : null;
474
703
  return NAMED_COLORS.has(lower) ? lower : null;
475
704
  }
476
- function styleValue(property, value) {
705
+ const familyKey = (value) => lowerAscii(value).replace(/["']/g, "").split(",").map((part) => trimAscii(part).replace(SPACE, " ")).join(",");
706
+ const leadKey = (key) => `\n${key.split(",")[0] ?? ""}`;
707
+ const findFamily = (fonts, value) => {
708
+ const key = familyKey(value);
709
+ return fonts.get(key) ?? fonts.get(leadKey(key)) ?? null;
710
+ };
711
+ function fontFamilyMap(list) {
712
+ const map = /* @__PURE__ */ new Map();
713
+ for (const entry of list) {
714
+ const parts = typeof entry === "string" ? entry.split(",").map(trimAscii) : [];
715
+ if (parts.length === 0 || byteLength(entry) > MAX_FAMILY_BYTES || !parts.every((part) => FAMILY_PART.test(part))) throw new TypeError("Each font family must be a comma-separated list of plain family names.");
716
+ const canonical = parts.join(", ");
717
+ const key = familyKey(canonical);
718
+ map.set(key, canonical);
719
+ const first = leadKey(key);
720
+ if (!map.has(first)) map.set(first, canonical);
721
+ }
722
+ return map;
723
+ }
724
+ const DEFAULT_FONT_FAMILIES = fontFamilyMap(VIVE_PROFILE.fontFamilies);
725
+ function cleanFontSize(value) {
726
+ const step = lowerAscii(value);
727
+ if (VIVE_PROFILE.fontSizes.includes(step)) return step;
728
+ const points = POINTS.exec(step);
729
+ if (!points) return null;
730
+ const halves = Number(points[1]) * 2 + (points[2] === "5" ? 1 : 0);
731
+ if (halves < limits$1.minFontSizePt * 2 || halves > limits$1.maxFontSizePt * 2) return null;
732
+ return `${Math.floor(halves / 2)}${halves % 2 === 1 ? ".5" : ""}pt`;
733
+ }
734
+ function styleValue(property, value, fonts) {
477
735
  if (property === "text-align") {
478
736
  const align = lowerAscii(value);
479
737
  return VIVE_PROFILE.textAlign.includes(align) ? align : null;
480
738
  }
481
- if (property === "color" || property === "background-color") return cleanColor(value);
739
+ if (property === "vertical-align") {
740
+ const align = lowerAscii(value);
741
+ return VIVE_PROFILE.verticalAlign.includes(align) ? align : null;
742
+ }
743
+ if (property === "color" || property === "background-color" || property === "--vive-table-border-color") return cleanColor(value);
744
+ if (property === "font-size") return cleanFontSize(value);
745
+ if (property === "margin-inline-start") {
746
+ const step = lowerAscii(value);
747
+ return VIVE_PROFILE.indents.includes(step) ? step : null;
748
+ }
749
+ if (property === "line-height") return VIVE_PROFILE.lineHeights.includes(value) ? value : null;
750
+ if (property === "list-style-type") {
751
+ const style = lowerAscii(value);
752
+ return VIVE_PROFILE.bulletStyles.includes(style) ? style : null;
753
+ }
754
+ if (property === "font-family") return findFamily(fonts, value);
482
755
  const pixels = PIXELS.exec(value);
483
756
  const number = pixels ? Number(pixels[1]) : 0;
484
- return number > 0 && number <= limits$1.maxTableWidth ? `${number}px` : null;
757
+ const max = property === "height" ? limits$1.maxRowHeight : limits$1.maxTableWidth;
758
+ return number > 0 && number <= max ? `${number}px` : null;
485
759
  }
486
- function cleanStyle(value, allowed) {
760
+ function cleanStyle(value, allowed, fonts = DEFAULT_FONT_FAMILIES) {
487
761
  if (byteLength(value) > allowed.length * (limits$1.maxStyleValueLength + 32)) return null;
488
762
  const lower = lowerAscii(value);
489
763
  if (BANNED_STYLE.some((banned) => lower.includes(banned))) return null;
@@ -495,7 +769,7 @@ function cleanStyle(value, allowed) {
495
769
  const raw = trimAscii(declaration.slice(colon + 1));
496
770
  if (!allowed.includes(property) || result.has(property)) continue;
497
771
  if (byteLength(raw) > limits$1.maxStyleValueLength) continue;
498
- const clean = styleValue(property, raw);
772
+ const clean = styleValue(property, raw, fonts);
499
773
  if (clean !== null) result.set(property, clean);
500
774
  }
501
775
  return result.size > 0 ? [...result].map(([property, clean]) => `${property}: ${clean}`).join("; ") : null;
@@ -659,6 +933,7 @@ function opensTag(input, index) {
659
933
  function hasCrowdedTag(input, maxAttributes) {
660
934
  let counts = new Int32Array(STATES).fill(END);
661
935
  let next = new Int32Array(STATES);
936
+ const ahead = (/* @__PURE__ */ new Int32Array(3)).fill(-1);
662
937
  let active = false;
663
938
  for (let index = 0; index < input.length; index++) {
664
939
  if (!active) {
@@ -681,9 +956,33 @@ function hasCrowdedTag(input, maxAttributes) {
681
956
  }
682
957
  [counts, next] = [next, counts];
683
958
  active = counts.some((count) => count !== END) || code === 60 || code === 47 && input.charCodeAt(index - 1) === 60;
959
+ const skip = quotedSkip(input, index, code, counts, ahead);
960
+ if (skip === null) return false;
961
+ index = skip;
684
962
  }
685
963
  return false;
686
964
  }
965
+ function nextAt(input, char, from, ahead, slot) {
966
+ if ((ahead[slot] ?? -1) < from) {
967
+ const found = input.indexOf(char, from);
968
+ ahead[slot] = found === -1 ? input.length : found;
969
+ }
970
+ return ahead[slot] ?? input.length;
971
+ }
972
+ function quotedSkip(input, index, code, counts, ahead) {
973
+ if (code === 60 || code === 47) return index;
974
+ let quote = null;
975
+ for (let state = 0; state < STATES; state++) {
976
+ if ((counts[state] ?? END) === END) continue;
977
+ if (quote !== null || state !== 5 && state !== 6) return index;
978
+ quote = state === 5 ? "\"" : "'";
979
+ }
980
+ if (quote === null) return index;
981
+ const close = nextAt(input, quote, index + 1, ahead, quote === "\"" ? 0 : 1);
982
+ const open = nextAt(input, "<", index + 1, ahead, 2);
983
+ if (close === input.length && open === input.length) return null;
984
+ return Math.min(close, open) - 1;
985
+ }
687
986
  function guardedTreeAdapter(maxElements, maxNesting, onLimit) {
688
987
  let created = 0;
689
988
  const owners = /* @__PURE__ */ new WeakMap();
@@ -697,6 +996,12 @@ function guardedTreeAdapter(maxElements, maxNesting, onLimit) {
697
996
  current = ("parentNode" in current ? current.parentNode : null) ?? owners.get(current) ?? null;
698
997
  }
699
998
  };
999
+ const insertBefore = (parentNode, newNode, referenceNode) => {
1000
+ const children = parentNode.childNodes;
1001
+ children.splice(children.lastIndexOf(referenceNode), 0, newNode);
1002
+ newNode.parentNode = parentNode;
1003
+ checkDepth(newNode);
1004
+ };
700
1005
  return {
701
1006
  ...defaultTreeAdapter,
702
1007
  createElement(tagName, namespaceURI, attrs) {
@@ -708,9 +1013,16 @@ function guardedTreeAdapter(maxElements, maxNesting, onLimit) {
708
1013
  defaultTreeAdapter.appendChild(parentNode, newNode);
709
1014
  checkDepth(newNode);
710
1015
  },
711
- insertBefore(parentNode, newNode, referenceNode) {
712
- defaultTreeAdapter.insertBefore(parentNode, newNode, referenceNode);
713
- checkDepth(newNode);
1016
+ insertBefore,
1017
+ insertTextBefore(parentNode, text, referenceNode) {
1018
+ const children = parentNode.childNodes;
1019
+ const previous = children[children.lastIndexOf(referenceNode) - 1];
1020
+ if (previous && defaultTreeAdapter.isTextNode(previous)) previous.value += text;
1021
+ else insertBefore(parentNode, {
1022
+ nodeName: "#text",
1023
+ value: text,
1024
+ parentNode: null
1025
+ }, referenceNode);
714
1026
  },
715
1027
  setTemplateContent(templateElement, contentElement) {
716
1028
  defaultTreeAdapter.setTemplateContent(templateElement, contentElement);
@@ -730,11 +1042,22 @@ var SanitizeError = class extends Error {
730
1042
  const { limits } = VIVE_PROFILE;
731
1043
  const DROP = new Set(VIVE_PROFILE.dropWithContent);
732
1044
  const FOOTNOTE_ELEMENTS = new Set(VIVE_PROFILE.footnoteElements);
1045
+ const BREAK_BLOCKS = /* @__PURE__ */ new Set([
1046
+ "p",
1047
+ "h1",
1048
+ "h2",
1049
+ "h3",
1050
+ "h4",
1051
+ "h5",
1052
+ "h6"
1053
+ ]);
733
1054
  const ROOT = {
734
1055
  parent: "",
735
1056
  taskList: false,
736
1057
  taskItem: false,
737
1058
  inLink: false,
1059
+ inScript: false,
1060
+ inFontSize: false,
738
1061
  footnote: null
739
1062
  };
740
1063
  const BODY = defaultTreeAdapter.createElement("body", html.NS.HTML, []);
@@ -755,13 +1078,16 @@ function append(target, tag, attributes) {
755
1078
  defaultTreeAdapter.appendChild(target, element);
756
1079
  return element;
757
1080
  }
758
- function styleOf(tag, source) {
759
- const allowed = VIVE_PROFILE.elements[tag]?.styles;
1081
+ function styleOf(tag, source, fonts, without = null) {
1082
+ const allowed = VIVE_PROFILE.elements[tag]?.styles?.filter((style) => style !== without);
760
1083
  const style = source.get("style");
761
1084
  if (!allowed || style === void 0) return [];
762
- const clean = cleanStyle(style, allowed);
1085
+ const clean = cleanStyle(style, allowed, fonts);
763
1086
  return clean === null ? [] : [["style", clean]];
764
1087
  }
1088
+ function hasStyle(attributes, property) {
1089
+ return attributes?.some(([name, value]) => name === "style" && value.includes(`${property}:`)) ?? false;
1090
+ }
765
1091
  function textAttribute(source, name) {
766
1092
  const value = source.get(name);
767
1093
  return value === void 0 ? [] : [[name, cleanText(value)]];
@@ -789,7 +1115,7 @@ function imageAttributes(source, policy) {
789
1115
  result.push(["loading", loading], ["decoding", decoding], ["referrerpolicy", referrerpolicy]);
790
1116
  return result;
791
1117
  }
792
- function cellAttributes(source) {
1118
+ function cellAttributes(tag, source, fonts) {
793
1119
  const result = [];
794
1120
  for (const name of ["colspan", "rowspan"]) {
795
1121
  const span = cleanInteger(source.get(name), 2, limits.maxSpan);
@@ -797,8 +1123,24 @@ function cellAttributes(source) {
797
1123
  }
798
1124
  const widths = cleanColWidth(source.get("data-colwidth"));
799
1125
  if (widths !== null) result.push(["data-colwidth", widths]);
1126
+ result.push(...styleOf(tag, source, fonts));
800
1127
  return result;
801
1128
  }
1129
+ function enumAttribute(source, name, allowed) {
1130
+ const value = lowerAscii(trimAscii(source.get(name) ?? ""));
1131
+ return allowed.includes(value) ? [[name, value]] : [];
1132
+ }
1133
+ function tableAttributes(source, fonts) {
1134
+ const width = enumAttribute(source, "data-width", VIVE_PROFILE.tableWidths);
1135
+ const striped = lowerAscii(trimAscii(source.get("data-striped") ?? "")) === "true";
1136
+ return [
1137
+ ...width,
1138
+ ...enumAttribute(source, "data-align", VIVE_PROFILE.tableAlign),
1139
+ ...enumAttribute(source, "data-border", VIVE_PROFILE.tableBorders),
1140
+ ...striped ? [["data-striped", "true"]] : [],
1141
+ ...styleOf("table", source, fonts, width.length > 0 ? "width" : null)
1142
+ ];
1143
+ }
802
1144
  function embed(source, node, target) {
803
1145
  const data = cleanEmbed(source);
804
1146
  if (!data) return;
@@ -848,6 +1190,36 @@ function footnote(node, target, depth, context, settings) {
848
1190
  footnote: { left: limits.maxFootnoteLength }
849
1191
  }, settings);
850
1192
  }
1193
+ function changeAttributes(source) {
1194
+ const id = source.get("data-vive-change") ?? "";
1195
+ const author = source.get("data-author") ?? "";
1196
+ const name = source.get("data-name") ?? "";
1197
+ const time = source.get("data-time") ?? "";
1198
+ return isChangeId(id) && isAuthorId(author) && isAuthorName(name) && isChangeTime(time) ? [
1199
+ ["data-vive-change", id],
1200
+ ["data-author", author],
1201
+ ["data-name", name],
1202
+ ["data-time", time]
1203
+ ] : null;
1204
+ }
1205
+ function breakAttributes(source) {
1206
+ const kind = source.get("data-vive-break");
1207
+ const change = kind === "insert" || kind === "delete" ? changeAttributes(source) : null;
1208
+ return change ? [["data-vive-break", kind], ...change] : [];
1209
+ }
1210
+ function suggestion(tag, source, node, target, depth, context, settings) {
1211
+ const change = settings.suggestions === "keep" && !context.footnote ? changeAttributes(source) : null;
1212
+ if (change) {
1213
+ walk(node, append(target, tag, change), depth, {
1214
+ ...context,
1215
+ parent: tag,
1216
+ taskList: false,
1217
+ taskItem: false
1218
+ }, settings);
1219
+ return;
1220
+ }
1221
+ if (tag === "ins" === (settings.suggestions === "accept" && !context.footnote)) walk(node, target, depth, context, settings);
1222
+ }
851
1223
  function walk(source, target, depth, context, settings) {
852
1224
  for (const child of source.childNodes) if (defaultTreeAdapter.isTextNode(child)) insertText(target, child.value, context);
853
1225
  else if (defaultTreeAdapter.isElementNode(child)) element(child, target, depth + 1, context, settings);
@@ -857,6 +1229,10 @@ function element(node, target, depth, context, settings) {
857
1229
  const tag = node.tagName;
858
1230
  if (DROP.has(tag)) return;
859
1231
  const source = attributesOf(node);
1232
+ if ((tag === "ins" || tag === "del") && source.has("data-vive-change")) {
1233
+ suggestion(tag, source, node, target, depth, context, settings);
1234
+ return;
1235
+ }
860
1236
  if (context.footnote && !FOOTNOTE_ELEMENTS.has(tag)) {
861
1237
  walk(node, target, depth, context, settings);
862
1238
  return;
@@ -880,6 +1256,8 @@ function element(node, target, depth, context, settings) {
880
1256
  taskList: false,
881
1257
  taskItem: false,
882
1258
  inLink: context.inLink || tag === "a",
1259
+ inScript: context.inScript,
1260
+ inFontSize: context.inFontSize,
883
1261
  footnote: context.footnote
884
1262
  };
885
1263
  let attributes = null;
@@ -902,7 +1280,7 @@ function element(node, target, depth, context, settings) {
902
1280
  return;
903
1281
  case "ul":
904
1282
  next.taskList = source.get("data-type") === "taskList";
905
- attributes = next.taskList ? [["data-type", "taskList"]] : [];
1283
+ attributes = next.taskList ? [["data-type", "taskList"]] : styleOf("ul", source, settings.fonts);
906
1284
  break;
907
1285
  case "li":
908
1286
  next.taskItem = context.taskList && source.get("data-type") === "taskItem";
@@ -910,7 +1288,8 @@ function element(node, target, depth, context, settings) {
910
1288
  break;
911
1289
  case "ol": {
912
1290
  const start = cleanInteger(source.get("start"), 0, limits.maxListStart);
913
- attributes = start !== null && start !== 1 ? [["start", String(start)]] : [];
1291
+ const type = trimAscii(source.get("type") ?? "");
1292
+ attributes = [...start !== null && start !== 1 ? [["start", String(start)]] : [], ...VIVE_PROFILE.orderedListTypes.includes(type) ? [["type", type]] : []];
914
1293
  break;
915
1294
  }
916
1295
  case "pre": {
@@ -928,7 +1307,9 @@ function element(node, target, depth, context, settings) {
928
1307
  attributes = context.inLink ? null : linkAttributes(source);
929
1308
  break;
930
1309
  case "sup":
931
- attributes = null;
1310
+ case "sub":
1311
+ attributes = context.inScript ? null : [];
1312
+ next.inScript = true;
932
1313
  break;
933
1314
  case "img": {
934
1315
  const image = imageAttributes(source, settings.image);
@@ -937,9 +1318,26 @@ function element(node, target, depth, context, settings) {
937
1318
  }
938
1319
  case "td":
939
1320
  case "th":
940
- attributes = cellAttributes(source);
1321
+ attributes = cellAttributes(tag, source, settings.fonts);
1322
+ break;
1323
+ case "table":
1324
+ attributes = tableAttributes(source, settings.fonts);
941
1325
  break;
942
- default: attributes = Object.hasOwn(VIVE_PROFILE.elements, tag) ? styleOf(tag, source) : null;
1326
+ case "ins":
1327
+ case "del": break;
1328
+ case "figure": {
1329
+ const align = lowerAscii(source.get("data-align") ?? "");
1330
+ attributes = VIVE_PROFILE.imageAlign.includes(align) ? [["data-align", align]] : [];
1331
+ break;
1332
+ }
1333
+ default:
1334
+ attributes = Object.hasOwn(VIVE_PROFILE.elements, tag) ? styleOf(tag, source, settings.fonts, context.inFontSize ? "font-size" : null) : null;
1335
+ next.inFontSize ||= tag === "span" && hasStyle(attributes, "font-size");
1336
+ if (BREAK_BLOCKS.has(tag) && attributes !== null && settings.suggestions === "keep") attributes = [...breakAttributes(source), ...attributes];
1337
+ if (tag === "span" && attributes !== null && settings.comments) {
1338
+ const comment = source.get("data-vive-comment");
1339
+ if (comment !== void 0 && isCommentId(comment)) attributes = [["data-vive-comment", comment], ...attributes];
1340
+ }
943
1341
  }
944
1342
  if (attributes === null) {
945
1343
  walk(node, target, depth, context, settings);
@@ -959,13 +1357,37 @@ function tagCount(input) {
959
1357
  for (let index = input.indexOf("<"); index !== -1; index = input.indexOf("<", index + 1)) count++;
960
1358
  return count;
961
1359
  }
962
- function cleanOnce(input, settings) {
1360
+ const TEMPLATE = /<template(?:[\t\n\f\r />]|$)/i;
1361
+ const START_TAG = /<[A-Za-z]/g;
1362
+ function startTagCount(input) {
1363
+ return input.match(START_TAG)?.length ?? 0;
1364
+ }
1365
+ const PARSE_WORK_PER_TAG = 50;
1366
+ function countParseWork(parser, budget) {
1367
+ let work = 0;
1368
+ const charge = () => {
1369
+ work += parser.openElements.stackTop + 1;
1370
+ if (work > budget) tooComplex();
1371
+ };
1372
+ const onStartTag = parser.onStartTag.bind(parser);
1373
+ const onEndTag = parser.onEndTag.bind(parser);
1374
+ parser.onStartTag = (token) => {
1375
+ charge();
1376
+ onStartTag(token);
1377
+ };
1378
+ parser.onEndTag = (token) => {
1379
+ charge();
1380
+ onEndTag(token);
1381
+ };
1382
+ }
1383
+ function cleanOnce(input, settings, maxTags) {
963
1384
  if (hasCrowdedTag(input, limits.maxAttributes)) tooComplex();
964
- const treeAdapter = guardedTreeAdapter(tagCount(input) + limits.maxExtraElements, limits.maxNesting, tooComplex);
1385
+ const treeAdapter = guardedTreeAdapter(Math.min(tagCount(input), maxTags) + limits.maxExtraElements, limits.maxNesting, tooComplex);
965
1386
  const parser = Parser.getFragmentParser(BODY, {
966
1387
  scriptingEnabled: false,
967
1388
  treeAdapter
968
1389
  });
1390
+ countParseWork(parser, Math.max(maxTags, limits.maxTags) * PARSE_WORK_PER_TAG);
969
1391
  parser.tokenizer.write(input, true);
970
1392
  const root = treeAdapter.getFirstChild(parser.document);
971
1393
  const output = defaultTreeAdapter.createDocumentFragment();
@@ -978,14 +1400,21 @@ function sanitizeHtml(input, options = {}) {
978
1400
  if (byteLength(input) > maxBytes) throw new SanitizeError("TOO_LARGE", `HTML is larger than ${maxBytes} bytes.`);
979
1401
  const settings = {
980
1402
  embeds: options.embeds === true,
1403
+ comments: options.comments === "keep",
1404
+ suggestions: options.suggestions === "accept" || options.suggestions === "keep" ? options.suggestions : "reject",
981
1405
  image: {
982
1406
  allowBase64: options.allowBase64 === true,
983
1407
  allowRelative: options.allowRelativeImages === true
984
- }
1408
+ },
1409
+ fonts: options.fontFamilies === void 0 ? DEFAULT_FONT_FAMILIES : fontFamilyMap(options.fontFamilies)
985
1410
  };
986
- let clean = cleanOnce(scrub(input), settings);
1411
+ const html = scrub(input);
1412
+ const maxTags = options.maxTags ?? limits.maxTags;
1413
+ if (TEMPLATE.test(html)) tooComplex();
1414
+ if (startTagCount(html) > maxTags) throw new SanitizeError("TOO_COMPLEX", `HTML has more than ${maxTags} tags.`);
1415
+ let clean = cleanOnce(html, settings, maxTags);
987
1416
  for (let round = 1; round < MAX_ROUNDS; round++) {
988
- const again = cleanOnce(clean, settings);
1417
+ const again = cleanOnce(clean, settings, maxTags);
989
1418
  if (again === clean) break;
990
1419
  clean = again;
991
1420
  }