@salmonui/rich-text 0.0.0-stage → 0.3.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 +79 -2
- package/dist/blocks/find.d.ts +4 -0
- package/dist/blocks/find.d.ts.map +1 -0
- package/dist/blocks/find.js +14 -0
- package/dist/blocks/find.js.map +1 -0
- package/dist/blocks/from-html.d.ts +10 -0
- package/dist/blocks/from-html.d.ts.map +1 -0
- package/dist/blocks/from-html.js +459 -0
- package/dist/blocks/from-html.js.map +1 -0
- package/dist/blocks/index.d.ts +5 -0
- package/dist/blocks/index.d.ts.map +1 -0
- package/dist/blocks/index.js +4 -0
- package/dist/blocks/index.js.map +1 -0
- package/dist/blocks/ops.d.ts +27 -0
- package/dist/blocks/ops.d.ts.map +1 -0
- package/dist/blocks/ops.js +172 -0
- package/dist/blocks/ops.js.map +1 -0
- package/dist/blocks/to-html.d.ts +18 -0
- package/dist/blocks/to-html.d.ts.map +1 -0
- package/dist/blocks/to-html.js +207 -0
- package/dist/blocks/to-html.js.map +1 -0
- package/dist/blocks/types.d.ts +76 -0
- package/dist/blocks/types.d.ts.map +1 -0
- package/dist/blocks/types.js +4 -0
- package/dist/blocks/types.js.map +1 -0
- package/dist/collab.d.ts +43 -0
- package/dist/collab.d.ts.map +1 -0
- package/dist/collab.js +45 -0
- package/dist/collab.js.map +1 -0
- package/dist/content/RichTextContent.d.ts +19 -0
- package/dist/content/RichTextContent.d.ts.map +1 -0
- package/dist/content/RichTextContent.js +20 -0
- package/dist/content/RichTextContent.js.map +1 -0
- package/dist/content/index.d.ts +2 -0
- package/dist/content/index.d.ts.map +1 -0
- package/dist/content/index.js +3 -0
- package/dist/content/index.js.map +1 -0
- package/dist/engine/commands.d.ts +19 -0
- package/dist/engine/commands.d.ts.map +1 -0
- package/dist/engine/commands.js +457 -0
- package/dist/engine/commands.js.map +1 -0
- package/dist/engine/core.d.ts +116 -0
- package/dist/engine/core.d.ts.map +1 -0
- package/dist/engine/core.js +460 -0
- package/dist/engine/core.js.map +1 -0
- package/dist/engine/dom-shim.d.ts +47 -0
- package/dist/engine/dom-shim.d.ts.map +1 -0
- package/dist/engine/dom-shim.js +128 -0
- package/dist/engine/dom-shim.js.map +1 -0
- package/dist/engine/html-out.d.ts +12 -0
- package/dist/engine/html-out.d.ts.map +1 -0
- package/dist/engine/html-out.js +24 -0
- package/dist/engine/html-out.js.map +1 -0
- package/dist/engine/index.d.ts +6 -0
- package/dist/engine/index.d.ts.map +1 -0
- package/dist/engine/index.js +5 -0
- package/dist/engine/index.js.map +1 -0
- package/dist/engine/keymap.d.ts +18 -0
- package/dist/engine/keymap.d.ts.map +1 -0
- package/dist/engine/keymap.js +85 -0
- package/dist/engine/keymap.js.map +1 -0
- package/dist/engine/load.d.ts +10 -0
- package/dist/engine/load.d.ts.map +1 -0
- package/dist/engine/load.js +11 -0
- package/dist/engine/load.js.map +1 -0
- package/dist/engine/nodeviews.d.ts +19 -0
- package/dist/engine/nodeviews.d.ts.map +1 -0
- package/dist/engine/nodeviews.js +241 -0
- package/dist/engine/nodeviews.js.map +1 -0
- package/dist/engine/plugins.d.ts +77 -0
- package/dist/engine/plugins.d.ts.map +1 -0
- package/dist/engine/plugins.js +312 -0
- package/dist/engine/plugins.js.map +1 -0
- package/dist/engine/schema.d.ts +11 -0
- package/dist/engine/schema.d.ts.map +1 -0
- package/dist/engine/schema.js +350 -0
- package/dist/engine/schema.js.map +1 -0
- package/dist/engine/serialize.d.ts +13 -0
- package/dist/engine/serialize.d.ts.map +1 -0
- package/dist/engine/serialize.js +119 -0
- package/dist/engine/serialize.js.map +1 -0
- package/dist/engine/snapshot.d.ts +4 -0
- package/dist/engine/snapshot.d.ts.map +1 -0
- package/dist/engine/snapshot.js +62 -0
- package/dist/engine/snapshot.js.map +1 -0
- package/dist/engine/types.d.ts +215 -0
- package/dist/engine/types.d.ts.map +1 -0
- package/dist/engine/types.js +2 -0
- package/dist/engine/types.js.map +1 -0
- package/dist/html/entities.d.ts +7 -0
- package/dist/html/entities.d.ts.map +1 -0
- package/dist/html/entities.js +69 -0
- package/dist/html/entities.js.map +1 -0
- package/dist/html/index.d.ts +7 -0
- package/dist/html/index.d.ts.map +1 -0
- package/dist/html/index.js +9 -0
- package/dist/html/index.js.map +1 -0
- package/dist/html/parse.d.ts +30 -0
- package/dist/html/parse.d.ts.map +1 -0
- package/dist/html/parse.js +394 -0
- package/dist/html/parse.js.map +1 -0
- package/dist/html/paste.d.ts +47 -0
- package/dist/html/paste.d.ts.map +1 -0
- package/dist/html/paste.js +373 -0
- package/dist/html/paste.js.map +1 -0
- package/dist/html/policy.d.ts +56 -0
- package/dist/html/policy.d.ts.map +1 -0
- package/dist/html/policy.js +245 -0
- package/dist/html/policy.js.map +1 -0
- package/dist/html/sanitize.d.ts +60 -0
- package/dist/html/sanitize.d.ts.map +1 -0
- package/dist/html/sanitize.js +699 -0
- package/dist/html/sanitize.js.map +1 -0
- package/dist/html/serialize.d.ts +16 -0
- package/dist/html/serialize.d.ts.map +1 -0
- package/dist/html/serialize.js +68 -0
- package/dist/html/serialize.js.map +1 -0
- package/dist/html/text.d.ts +18 -0
- package/dist/html/text.d.ts.map +1 -0
- package/dist/html/text.js +96 -0
- package/dist/html/text.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/dist/mention/editable.d.ts +15 -0
- package/dist/mention/editable.d.ts.map +1 -0
- package/dist/mention/editable.js +167 -0
- package/dist/mention/editable.js.map +1 -0
- package/dist/mention/index.d.ts +2 -0
- package/dist/mention/index.d.ts.map +1 -0
- package/dist/mention/index.js +3 -0
- package/dist/mention/index.js.map +1 -0
- package/dist/react/RichTextEditor.d.ts +15 -0
- package/dist/react/RichTextEditor.d.ts.map +1 -0
- package/dist/react/RichTextEditor.js +497 -0
- package/dist/react/RichTextEditor.js.map +1 -0
- package/dist/react/block-editor/BlockEditor.d.ts +5 -0
- package/dist/react/block-editor/BlockEditor.d.ts.map +1 -0
- package/dist/react/block-editor/BlockEditor.js +106 -0
- package/dist/react/block-editor/BlockEditor.js.map +1 -0
- package/dist/react/block-editor/controller.d.ts +94 -0
- package/dist/react/block-editor/controller.d.ts.map +1 -0
- package/dist/react/block-editor/controller.js +758 -0
- package/dist/react/block-editor/controller.js.map +1 -0
- package/dist/react/block-editor/index.d.ts +4 -0
- package/dist/react/block-editor/index.d.ts.map +1 -0
- package/dist/react/block-editor/index.js +5 -0
- package/dist/react/block-editor/index.js.map +1 -0
- package/dist/react/block-editor/locale.d.ts +73 -0
- package/dist/react/block-editor/locale.d.ts.map +1 -0
- package/dist/react/block-editor/locale.js +65 -0
- package/dist/react/block-editor/locale.js.map +1 -0
- package/dist/react/block-editor/transforms.d.ts +26 -0
- package/dist/react/block-editor/transforms.d.ts.map +1 -0
- package/dist/react/block-editor/transforms.js +233 -0
- package/dist/react/block-editor/transforms.js.map +1 -0
- package/dist/react/block-editor/types.d.ts +180 -0
- package/dist/react/block-editor/types.d.ts.map +1 -0
- package/dist/react/block-editor/types.js +2 -0
- package/dist/react/block-editor/types.js.map +1 -0
- package/dist/react/block-editor/ui.d.ts +21 -0
- package/dist/react/block-editor/ui.d.ts.map +1 -0
- package/dist/react/block-editor/ui.js +601 -0
- package/dist/react/block-editor/ui.js.map +1 -0
- package/dist/react/client.d.ts +28 -0
- package/dist/react/client.d.ts.map +1 -0
- package/dist/react/client.js +111 -0
- package/dist/react/client.js.map +1 -0
- package/dist/react/dialogs.d.ts +46 -0
- package/dist/react/dialogs.d.ts.map +1 -0
- package/dist/react/dialogs.js +312 -0
- package/dist/react/dialogs.js.map +1 -0
- package/dist/react/emoji-data.d.ts +3 -0
- package/dist/react/emoji-data.d.ts.map +1 -0
- package/dist/react/emoji-data.js +6 -0
- package/dist/react/emoji-data.js.map +1 -0
- package/dist/react/icons.d.ts +5 -0
- package/dist/react/icons.d.ts.map +1 -0
- package/dist/react/icons.js +69 -0
- package/dist/react/icons.js.map +1 -0
- package/dist/react/index.d.ts +5 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +5 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/items.d.ts +60 -0
- package/dist/react/items.d.ts.map +1 -0
- package/dist/react/items.js +255 -0
- package/dist/react/items.js.map +1 -0
- package/dist/react/locale.d.ts +151 -0
- package/dist/react/locale.d.ts.map +1 -0
- package/dist/react/locale.js +139 -0
- package/dist/react/locale.js.map +1 -0
- package/dist/react/overlay.d.ts +41 -0
- package/dist/react/overlay.d.ts.map +1 -0
- package/dist/react/overlay.js +162 -0
- package/dist/react/overlay.js.map +1 -0
- package/dist/react/store.d.ts +23 -0
- package/dist/react/store.d.ts.map +1 -0
- package/dist/react/store.js +84 -0
- package/dist/react/store.js.map +1 -0
- package/dist/react/types.d.ts +205 -0
- package/dist/react/types.d.ts.map +1 -0
- package/dist/react/types.js +2 -0
- package/dist/react/types.js.map +1 -0
- package/dist/rich-text.css +1555 -0
- package/package.json +61 -4
|
@@ -0,0 +1,699 @@
|
|
|
1
|
+
// Server-safe allow-list sanitizer. Always on: RichTextEditor runs it on `value` in and HTML
|
|
2
|
+
// out, RichTextContent on what it renders, paste cleanup on clipboard HTML.
|
|
3
|
+
//
|
|
4
|
+
// The allow-list is exactly the shared HTML vocabulary of the schema (the
|
|
5
|
+
// engine, the block serializers and this module agree on it): every other
|
|
6
|
+
// element is unwrapped keeping its text, except the ones in DROP_WITH_CONTENT,
|
|
7
|
+
// which go with their content. Attributes are kept only per element and per
|
|
8
|
+
// value rule (URLs by scheme, styles per property, integers by range), text is
|
|
9
|
+
// always escaped, comments are removed. Structure is normalized the way the
|
|
10
|
+
// schema expects it: table sections become one <tbody>, a list directly in a
|
|
11
|
+
// list moves into the previous item, <pre> holds one <code>, spans carry one
|
|
12
|
+
// style property each and font-weight / font-style / text-decoration /
|
|
13
|
+
// vertical-align styles become marks.
|
|
14
|
+
import { parseHtml } from './parse.js';
|
|
15
|
+
import { serializeHtml } from './serialize.js';
|
|
16
|
+
import { cleanColor, cleanFontFamily, cleanFontSize, cleanKeyword, cleanLineHeight, cleanUrl, embedAllowed, parseStyle, cleanAllowedAttr, compileAllow, } from './policy.js';
|
|
17
|
+
/** Elements removed together with their content. */
|
|
18
|
+
export const DROP_WITH_CONTENT = new Set([
|
|
19
|
+
'script', 'style', 'template', 'noscript', 'iframe', 'object', 'embed', 'applet', 'head', 'title', 'svg', 'math',
|
|
20
|
+
'select', 'option', 'optgroup', 'datalist', 'frame', 'frameset', 'noframes', 'noembed', 'xml', 'link', 'meta', 'base',
|
|
21
|
+
'input', 'param', 'source', 'track', 'col', 'colgroup', 'canvas', 'portal', 'area', 'map', 'basefont', 'bgsound', 'keygen',
|
|
22
|
+
]);
|
|
23
|
+
const BLOCK_TAGS = new Set([
|
|
24
|
+
'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'hr', 'pre', 'ul', 'ol', 'li', 'figure', 'figcaption', 'audio', 'video',
|
|
25
|
+
'iframe', 'table', 'div', 'details', 'summary', 'address', 'article', 'aside', 'footer', 'header', 'main', 'nav', 'section',
|
|
26
|
+
'dl', 'dt', 'dd', 'center', 'form', 'fieldset', 'hgroup', 'menu', 'dir', 'listing', 'tr', 'td', 'th', 'tbody', 'thead', 'tfoot',
|
|
27
|
+
]);
|
|
28
|
+
const HEADINGS = new Set(['h1', 'h2', 'h3', 'h4', 'h5', 'h6']);
|
|
29
|
+
const MARK_TAGS = {
|
|
30
|
+
strong: 'strong', b: 'strong', em: 'em', i: 'em', u: 'u', ins: 'u', s: 's', del: 's', strike: 's', sub: 'sub', sup: 'sup',
|
|
31
|
+
code: 'code', kbd: 'code', samp: 'code', tt: 'code', mark: 'mark',
|
|
32
|
+
};
|
|
33
|
+
const TEXT_ALIGN = new Set(['left', 'center', 'right', 'justify']);
|
|
34
|
+
const UL_STYLES = new Set(['disc', 'circle', 'square']);
|
|
35
|
+
const OL_STYLES = new Set(['decimal', 'lower-alpha', 'upper-alpha', 'lower-roman', 'upper-roman']);
|
|
36
|
+
const TONES = new Set(['info', 'success', 'warning', 'danger', 'neutral']);
|
|
37
|
+
const TABLE_VARIANTS = new Set(['salmon-rich-text-table--bordered', 'salmon-rich-text-table--striped']);
|
|
38
|
+
const BLOCK_ID = /^[A-Za-z0-9_\-:.]{1,128}$/;
|
|
39
|
+
const LANGUAGE = /^language-[A-Za-z0-9_+#.-]{1,40}$/;
|
|
40
|
+
const SPAN_STYLES = ['font-family', 'font-size', 'color', 'background-color'];
|
|
41
|
+
/** Output tags of the built-in vocabulary; an extension cannot redefine them. */
|
|
42
|
+
const VOCABULARY = new Set([
|
|
43
|
+
'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'br', 'hr', 'blockquote', 'pre', 'code', 'ul', 'ol', 'li', 'figure', 'figcaption', 'img',
|
|
44
|
+
'audio', 'video', 'iframe', 'table', 'thead', 'tbody', 'tfoot', 'caption', 'tr', 'td', 'th', 'div', 'details', 'summary', 'a', 'span',
|
|
45
|
+
'strong', 'em', 'u', 's', 'sub', 'sup', 'mark', 'font',
|
|
46
|
+
]);
|
|
47
|
+
function int(value, min, max) {
|
|
48
|
+
if (value === undefined || !/^\s*\d{1,6}\s*$/.test(value))
|
|
49
|
+
return null;
|
|
50
|
+
const n = Number.parseInt(value, 10);
|
|
51
|
+
return n >= min && n <= max ? String(n) : null;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Validates one attribute of a vocabulary element. Shared by the main pass and
|
|
55
|
+
* the hook pass, so a hook-provided value goes through the same rules.
|
|
56
|
+
*/
|
|
57
|
+
function cleanAttr(tag, name, value, ctx) {
|
|
58
|
+
const v = builtInAttr(tag, name, value, ctx);
|
|
59
|
+
return v !== null || !ctx.allow ? v : cleanAllowedAttr(ctx.allow, tag, name, value);
|
|
60
|
+
}
|
|
61
|
+
function builtInAttr(tag, name, value, ctx) {
|
|
62
|
+
if (name === 'data-block-id') {
|
|
63
|
+
return ctx.blockIds && BLOCK_ATTR_TAGS.has(tag) && BLOCK_ID.test(value) ? value : null;
|
|
64
|
+
}
|
|
65
|
+
switch (tag) {
|
|
66
|
+
case 'p':
|
|
67
|
+
case 'h1':
|
|
68
|
+
case 'h2':
|
|
69
|
+
case 'h3':
|
|
70
|
+
case 'h4':
|
|
71
|
+
case 'h5':
|
|
72
|
+
case 'h6':
|
|
73
|
+
if (name === 'data-indent')
|
|
74
|
+
return int(value, 1, 8);
|
|
75
|
+
if (name === 'style')
|
|
76
|
+
return cleanBlockStyle(value);
|
|
77
|
+
return null;
|
|
78
|
+
case 'ul':
|
|
79
|
+
if (name === 'data-type')
|
|
80
|
+
return value === 'checklist' ? value : null;
|
|
81
|
+
if (name === 'style')
|
|
82
|
+
return listStyle(value, UL_STYLES);
|
|
83
|
+
return null;
|
|
84
|
+
case 'ol':
|
|
85
|
+
if (name === 'start')
|
|
86
|
+
return int(value, 0, 999999);
|
|
87
|
+
if (name === 'style')
|
|
88
|
+
return listStyle(value, OL_STYLES);
|
|
89
|
+
return null;
|
|
90
|
+
case 'li':
|
|
91
|
+
if (name === 'data-checked')
|
|
92
|
+
return ctx.list === 'checklist' ? (value === 'true' ? 'true' : 'false') : null;
|
|
93
|
+
return null;
|
|
94
|
+
case 'code':
|
|
95
|
+
return name === 'class' && LANGUAGE.test(value.trim()) ? value.trim() : null;
|
|
96
|
+
case 'a':
|
|
97
|
+
if (name === 'href')
|
|
98
|
+
return cleanUrl(value, 'link');
|
|
99
|
+
if (name === 'title')
|
|
100
|
+
return value;
|
|
101
|
+
if (name === 'target')
|
|
102
|
+
return value === '_blank' ? value : null;
|
|
103
|
+
if (name === 'rel')
|
|
104
|
+
return value === 'noopener noreferrer' ? value : null;
|
|
105
|
+
return null;
|
|
106
|
+
case 'img':
|
|
107
|
+
if (name === 'src')
|
|
108
|
+
return cleanUrl(value, 'image');
|
|
109
|
+
if (name === 'alt' || name === 'title')
|
|
110
|
+
return value;
|
|
111
|
+
if (name === 'width' || name === 'height')
|
|
112
|
+
return int(value, 1, 100000);
|
|
113
|
+
return null;
|
|
114
|
+
case 'audio':
|
|
115
|
+
case 'video':
|
|
116
|
+
if (name === 'src')
|
|
117
|
+
return cleanUrl(value, 'media');
|
|
118
|
+
if (name === 'controls')
|
|
119
|
+
return '';
|
|
120
|
+
if (tag === 'video' && (name === 'width' || name === 'height'))
|
|
121
|
+
return int(value, 1, 100000);
|
|
122
|
+
return null;
|
|
123
|
+
case 'iframe':
|
|
124
|
+
if (name === 'src') {
|
|
125
|
+
const src = cleanUrl(value, 'embed');
|
|
126
|
+
return src !== null && embedAllowed(src, ctx.embedHosts) ? src : null;
|
|
127
|
+
}
|
|
128
|
+
if (name === 'width' || name === 'height')
|
|
129
|
+
return int(value, 1, 100000);
|
|
130
|
+
if (name === 'allowfullscreen')
|
|
131
|
+
return '';
|
|
132
|
+
return null;
|
|
133
|
+
case 'table':
|
|
134
|
+
return name === 'class' && TABLE_VARIANTS.has(value.trim()) ? value.trim() : null;
|
|
135
|
+
case 'td':
|
|
136
|
+
case 'th':
|
|
137
|
+
if (name === 'colspan' || name === 'rowspan')
|
|
138
|
+
return int(value, 1, 1000);
|
|
139
|
+
if (name === 'data-colwidth')
|
|
140
|
+
return /^\d{1,5}(?:,\d{1,5})*$/.test(value) ? value : null;
|
|
141
|
+
return null;
|
|
142
|
+
case 'div':
|
|
143
|
+
if (name === 'data-type')
|
|
144
|
+
return value === 'callout' ? value : null;
|
|
145
|
+
if (name === 'data-tone')
|
|
146
|
+
return TONES.has(value) ? value : null;
|
|
147
|
+
return null;
|
|
148
|
+
case 'details':
|
|
149
|
+
return name === 'open' ? '' : null;
|
|
150
|
+
case 'span':
|
|
151
|
+
if (name === 'style')
|
|
152
|
+
return value;
|
|
153
|
+
if (name === 'data-type')
|
|
154
|
+
return value === 'mention' || value === 'label' ? value : null;
|
|
155
|
+
if (name === 'data-id' || name === 'data-label')
|
|
156
|
+
return value.length <= 500 ? value : null;
|
|
157
|
+
if (name === 'data-trigger')
|
|
158
|
+
return value.length >= 1 && value.length <= 3 && !/[\s\w<>"'&]/.test(value) ? value : null;
|
|
159
|
+
if (name === 'data-tone')
|
|
160
|
+
return TONES.has(value) ? value : null;
|
|
161
|
+
return null;
|
|
162
|
+
default:
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
const BLOCK_ATTR_TAGS = new Set([
|
|
167
|
+
'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'hr', 'pre', 'ul', 'ol', 'li', 'figure', 'audio', 'video', 'iframe', 'table', 'div', 'details',
|
|
168
|
+
]);
|
|
169
|
+
function cleanBlockStyle(style) {
|
|
170
|
+
const decls = parseStyle(style);
|
|
171
|
+
const out = [];
|
|
172
|
+
const align = decls.get('text-align');
|
|
173
|
+
const a = align === undefined ? null : cleanKeyword(align === 'start' ? 'left' : align === 'end' ? 'right' : align, TEXT_ALIGN);
|
|
174
|
+
if (a)
|
|
175
|
+
out.push(`text-align:${a}`);
|
|
176
|
+
const lh = decls.get('line-height');
|
|
177
|
+
const l = lh === undefined ? null : cleanLineHeight(lh);
|
|
178
|
+
if (l)
|
|
179
|
+
out.push(`line-height:${l}`);
|
|
180
|
+
return out.length ? out.join(';') : null;
|
|
181
|
+
}
|
|
182
|
+
function listStyle(style, allowed) {
|
|
183
|
+
const v = parseStyle(style).get('list-style-type');
|
|
184
|
+
const k = v === undefined ? null : cleanKeyword(v, allowed);
|
|
185
|
+
return k ? `list-style-type:${k}` : null;
|
|
186
|
+
}
|
|
187
|
+
function cleanAttrs(tag, attrs, ctx) {
|
|
188
|
+
const out = {};
|
|
189
|
+
for (const name of Object.keys(attrs)) {
|
|
190
|
+
const v = cleanAttr(tag, name, attrs[name], ctx);
|
|
191
|
+
if (v !== null)
|
|
192
|
+
out[name] = v;
|
|
193
|
+
}
|
|
194
|
+
return out;
|
|
195
|
+
}
|
|
196
|
+
function el(tag, attrs, children) {
|
|
197
|
+
return { type: 'element', tag, attrs, children };
|
|
198
|
+
}
|
|
199
|
+
function pushText(out, value) {
|
|
200
|
+
if (value === '')
|
|
201
|
+
return;
|
|
202
|
+
const last = out[out.length - 1];
|
|
203
|
+
if (last && last.type === 'text')
|
|
204
|
+
last.value += value;
|
|
205
|
+
else
|
|
206
|
+
out.push({ type: 'text', value });
|
|
207
|
+
}
|
|
208
|
+
function pushAll(out, nodes) {
|
|
209
|
+
for (const n of nodes) {
|
|
210
|
+
if (n.type === 'text')
|
|
211
|
+
pushText(out, n.value);
|
|
212
|
+
else
|
|
213
|
+
out.push(n);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
function isWhitespace(node) {
|
|
217
|
+
return node.type === 'text' && /^[\t\n\f\r ]*$/.test(node.value);
|
|
218
|
+
}
|
|
219
|
+
function textOf(nodes, out) {
|
|
220
|
+
for (const n of nodes) {
|
|
221
|
+
if (n.type === 'text')
|
|
222
|
+
out.push(n.value);
|
|
223
|
+
else if (n.type === 'element') {
|
|
224
|
+
if (DROP_WITH_CONTENT.has(n.tag))
|
|
225
|
+
continue;
|
|
226
|
+
if (n.tag === 'br')
|
|
227
|
+
out.push('\n');
|
|
228
|
+
else
|
|
229
|
+
textOf(n.children, out);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
function hasBlockChild(nodes) {
|
|
234
|
+
return nodes.some((n) => n.type === 'element' && BLOCK_TAGS.has(n.tag));
|
|
235
|
+
}
|
|
236
|
+
function sanitizeChildren(nodes, ctx) {
|
|
237
|
+
const out = [];
|
|
238
|
+
for (const node of nodes) {
|
|
239
|
+
if (node.type === 'text')
|
|
240
|
+
pushText(out, node.value);
|
|
241
|
+
else if (node.type === 'element')
|
|
242
|
+
pushAll(out, sanitizeElement(node, ctx));
|
|
243
|
+
}
|
|
244
|
+
return out;
|
|
245
|
+
}
|
|
246
|
+
function child(ctx, parent, list = ctx.list) {
|
|
247
|
+
return { parent, list, embedHosts: ctx.embedHosts, blockIds: ctx.blockIds, allow: ctx.allow };
|
|
248
|
+
}
|
|
249
|
+
/** Sanitizes one element; returns the nodes that replace it. */
|
|
250
|
+
function sanitizeElement(node, ctx) {
|
|
251
|
+
const tag = node.tag;
|
|
252
|
+
if (DROP_WITH_CONTENT.has(tag) && tag !== 'iframe')
|
|
253
|
+
return [];
|
|
254
|
+
if (tag.indexOf(':') >= 0)
|
|
255
|
+
return sanitizeChildren(node.children, ctx);
|
|
256
|
+
// An extension element: kept with its allowed attributes; its content is sanitized as usual.
|
|
257
|
+
if (ctx.allow?.tags.has(tag) && !VOCABULARY.has(tag))
|
|
258
|
+
return [el(tag, cleanAttrs(tag, node.attrs, ctx), sanitizeChildren(node.children, child(ctx, tag)))];
|
|
259
|
+
const mark = MARK_TAGS[tag];
|
|
260
|
+
if (mark !== undefined)
|
|
261
|
+
return sanitizeMark(node, mark, ctx);
|
|
262
|
+
switch (tag) {
|
|
263
|
+
case 'p':
|
|
264
|
+
case 'h1':
|
|
265
|
+
case 'h2':
|
|
266
|
+
case 'h3':
|
|
267
|
+
case 'h4':
|
|
268
|
+
case 'h5':
|
|
269
|
+
case 'h6':
|
|
270
|
+
return [el(tag, cleanAttrs(tag, node.attrs, ctx), sanitizeChildren(node.children, child(ctx, tag)))];
|
|
271
|
+
case 'br':
|
|
272
|
+
return [el('br', {}, [])];
|
|
273
|
+
case 'hr':
|
|
274
|
+
return [el('hr', cleanAttrs(tag, node.attrs, ctx), [])];
|
|
275
|
+
case 'blockquote':
|
|
276
|
+
return [el(tag, cleanAttrs(tag, node.attrs, ctx), sanitizeChildren(node.children, child(ctx, tag)))];
|
|
277
|
+
case 'pre':
|
|
278
|
+
case 'listing':
|
|
279
|
+
case 'xmp':
|
|
280
|
+
case 'plaintext':
|
|
281
|
+
return [sanitizePre(node, ctx)];
|
|
282
|
+
case 'ul':
|
|
283
|
+
case 'ol':
|
|
284
|
+
case 'dir':
|
|
285
|
+
case 'menu':
|
|
286
|
+
return [sanitizeList(node, ctx)];
|
|
287
|
+
case 'li':
|
|
288
|
+
if (ctx.parent === 'ul' || ctx.parent === 'ol') {
|
|
289
|
+
return [el('li', cleanAttrs('li', node.attrs, ctx), sanitizeChildren(node.children, child(ctx, 'li')))];
|
|
290
|
+
}
|
|
291
|
+
return asParagraph(node, ctx);
|
|
292
|
+
case 'figure':
|
|
293
|
+
return sanitizeFigure(node, ctx);
|
|
294
|
+
case 'figcaption':
|
|
295
|
+
return ctx.parent === 'figure' ? [el(tag, {}, sanitizeChildren(node.children, child(ctx, tag)))] : asParagraph(node, ctx);
|
|
296
|
+
case 'img': {
|
|
297
|
+
const attrs = cleanAttrs(tag, node.attrs, ctx);
|
|
298
|
+
return attrs.src ? [el('img', attrs, [])] : [];
|
|
299
|
+
}
|
|
300
|
+
case 'audio':
|
|
301
|
+
case 'video': {
|
|
302
|
+
const raw = { ...node.attrs };
|
|
303
|
+
if (raw.src === undefined) {
|
|
304
|
+
const source = node.children.find((c) => c.type === 'element' && c.tag === 'source' && typeof c.attrs.src === 'string');
|
|
305
|
+
if (source)
|
|
306
|
+
raw.src = source.attrs.src;
|
|
307
|
+
}
|
|
308
|
+
raw.controls = '';
|
|
309
|
+
const attrs = cleanAttrs(tag, raw, ctx);
|
|
310
|
+
return attrs.src ? [el(tag, orderMedia(attrs), [])] : [];
|
|
311
|
+
}
|
|
312
|
+
case 'iframe': {
|
|
313
|
+
const attrs = cleanAttrs(tag, node.attrs, ctx);
|
|
314
|
+
return attrs.src ? [el('iframe', attrs, [])] : [];
|
|
315
|
+
}
|
|
316
|
+
case 'table':
|
|
317
|
+
return sanitizeTable(node, ctx);
|
|
318
|
+
case 'div':
|
|
319
|
+
return sanitizeDiv(node, ctx);
|
|
320
|
+
case 'details': {
|
|
321
|
+
const body = sanitizeChildren(node.children, child(ctx, 'details'));
|
|
322
|
+
const first = body.findIndex((n) => !isWhitespace(n));
|
|
323
|
+
const summary = first >= 0 ? body[first] : undefined;
|
|
324
|
+
const rest = body.filter((n, i) => i !== first || !(summary && summary.type === 'element' && summary.tag === 'summary'));
|
|
325
|
+
const head = summary && summary.type === 'element' && summary.tag === 'summary' ? summary : el('summary', {}, []);
|
|
326
|
+
return [el('details', cleanAttrs(tag, node.attrs, ctx), [head, ...rest.filter((n) => !(n.type === 'element' && n.tag === 'summary'))])];
|
|
327
|
+
}
|
|
328
|
+
case 'summary':
|
|
329
|
+
return ctx.parent === 'details' ? [el('summary', {}, sanitizeChildren(node.children, child(ctx, 'summary')))] : asParagraph(node, ctx);
|
|
330
|
+
case 'a': {
|
|
331
|
+
const attrs = cleanAttrs(tag, node.attrs, ctx);
|
|
332
|
+
const kids = sanitizeChildren(node.children, child(ctx, 'a'));
|
|
333
|
+
if (attrs.href === undefined)
|
|
334
|
+
return kids;
|
|
335
|
+
const out = { href: attrs.href };
|
|
336
|
+
if (attrs.title !== undefined)
|
|
337
|
+
out.title = attrs.title;
|
|
338
|
+
if (attrs.target === '_blank') {
|
|
339
|
+
out.target = '_blank';
|
|
340
|
+
out.rel = 'noopener noreferrer';
|
|
341
|
+
}
|
|
342
|
+
return [el('a', out, kids)];
|
|
343
|
+
}
|
|
344
|
+
case 'span':
|
|
345
|
+
return sanitizeSpan(node, ctx);
|
|
346
|
+
case 'font': {
|
|
347
|
+
const style = [];
|
|
348
|
+
if (node.attrs.face)
|
|
349
|
+
style.push(`font-family:${node.attrs.face}`);
|
|
350
|
+
if (node.attrs.color)
|
|
351
|
+
style.push(`color:${node.attrs.color}`);
|
|
352
|
+
if (node.attrs.style)
|
|
353
|
+
style.push(node.attrs.style);
|
|
354
|
+
return sanitizeSpan(el('span', { style: style.join(';') }, node.children), ctx);
|
|
355
|
+
}
|
|
356
|
+
case 'tr':
|
|
357
|
+
case 'td':
|
|
358
|
+
case 'th':
|
|
359
|
+
case 'tbody':
|
|
360
|
+
case 'thead':
|
|
361
|
+
case 'tfoot':
|
|
362
|
+
case 'caption':
|
|
363
|
+
// Table parts outside a table: keep the content.
|
|
364
|
+
return sanitizeChildren(node.children, ctx);
|
|
365
|
+
default:
|
|
366
|
+
// Unknown element: unwrapped, its text kept.
|
|
367
|
+
return sanitizeChildren(node.children, ctx);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
function orderMedia(attrs) {
|
|
371
|
+
const out = { controls: '' };
|
|
372
|
+
for (const k of ['src', 'width', 'height', 'data-block-id'])
|
|
373
|
+
if (attrs[k] !== undefined)
|
|
374
|
+
out[k] = attrs[k];
|
|
375
|
+
return out;
|
|
376
|
+
}
|
|
377
|
+
function asParagraph(node, ctx) {
|
|
378
|
+
const kids = sanitizeChildren(node.children, child(ctx, 'p'));
|
|
379
|
+
if (hasBlockChild(kids))
|
|
380
|
+
return kids;
|
|
381
|
+
return [el('p', {}, kids)];
|
|
382
|
+
}
|
|
383
|
+
function sanitizeMark(node, tag, ctx) {
|
|
384
|
+
// `<b style="font-weight:normal">` (Google Docs wrapper) is not bold.
|
|
385
|
+
const style = node.attrs.style ? parseStyle(node.attrs.style) : null;
|
|
386
|
+
if (style && tag === 'strong') {
|
|
387
|
+
const w = style.get('font-weight');
|
|
388
|
+
if (w !== undefined && !isBold(w))
|
|
389
|
+
return sanitizeChildren(node.children, ctx);
|
|
390
|
+
}
|
|
391
|
+
if (style && tag === 'em') {
|
|
392
|
+
const s = style.get('font-style');
|
|
393
|
+
if (s !== undefined && s.trim().toLowerCase() === 'normal')
|
|
394
|
+
return sanitizeChildren(node.children, ctx);
|
|
395
|
+
}
|
|
396
|
+
const attrs = tag === 'code' ? cleanAttrs('code', node.attrs, ctx) : {};
|
|
397
|
+
const kids = sanitizeChildren(node.children, child(ctx, tag));
|
|
398
|
+
if (kids.length === 0)
|
|
399
|
+
return [];
|
|
400
|
+
return [el(tag, attrs, kids)];
|
|
401
|
+
}
|
|
402
|
+
function isBold(weight) {
|
|
403
|
+
const w = weight.trim().toLowerCase();
|
|
404
|
+
if (w === 'bold' || w === 'bolder')
|
|
405
|
+
return true;
|
|
406
|
+
const n = Number.parseInt(w, 10);
|
|
407
|
+
return Number.isFinite(n) && n >= 600;
|
|
408
|
+
}
|
|
409
|
+
function sanitizeSpan(node, ctx) {
|
|
410
|
+
const type = node.attrs['data-type'];
|
|
411
|
+
if (type === 'mention' || type === 'label') {
|
|
412
|
+
const attrs = cleanAttrs('span', node.attrs, ctx);
|
|
413
|
+
delete attrs.style;
|
|
414
|
+
if (attrs['data-id'] === undefined)
|
|
415
|
+
return sanitizeChildren(node.children, ctx);
|
|
416
|
+
if (type === 'mention')
|
|
417
|
+
delete attrs['data-tone'];
|
|
418
|
+
const text = [];
|
|
419
|
+
textOf(node.children, text);
|
|
420
|
+
const ordered = { 'data-type': type, 'data-id': attrs['data-id'] };
|
|
421
|
+
for (const k of ['data-label', 'data-trigger', 'data-tone'])
|
|
422
|
+
if (attrs[k] !== undefined)
|
|
423
|
+
ordered[k] = attrs[k];
|
|
424
|
+
return [el('span', ordered, text.length ? [{ type: 'text', value: text.join('') }] : [])];
|
|
425
|
+
}
|
|
426
|
+
let kids = sanitizeChildren(node.children, child(ctx, 'span'));
|
|
427
|
+
// Extension attributes on a span keep it (outermost), with the style marks inside.
|
|
428
|
+
const extra = {};
|
|
429
|
+
if (ctx.allow)
|
|
430
|
+
for (const name of Object.keys(node.attrs)) {
|
|
431
|
+
const v = name === 'style' ? null : cleanAllowedAttr(ctx.allow, 'span', name, node.attrs[name]);
|
|
432
|
+
if (v !== null)
|
|
433
|
+
extra[name] = v;
|
|
434
|
+
}
|
|
435
|
+
const wrap = (inner) => (Object.keys(extra).length && inner.length ? [el('span', extra, inner)] : inner);
|
|
436
|
+
const style = node.attrs.style;
|
|
437
|
+
if (!style || kids.length === 0)
|
|
438
|
+
return wrap(kids);
|
|
439
|
+
const decls = parseStyle(style);
|
|
440
|
+
// Marks implied by styles (Google Docs, Word, other editors).
|
|
441
|
+
const marks = [];
|
|
442
|
+
const weight = decls.get('font-weight');
|
|
443
|
+
if (weight !== undefined && isBold(weight))
|
|
444
|
+
marks.push('strong');
|
|
445
|
+
const fontStyle = decls.get('font-style');
|
|
446
|
+
if (fontStyle !== undefined && /^(?:italic|oblique)/i.test(fontStyle.trim()))
|
|
447
|
+
marks.push('em');
|
|
448
|
+
const deco = `${decls.get('text-decoration') ?? ''} ${decls.get('text-decoration-line') ?? ''}`.toLowerCase();
|
|
449
|
+
if (/\bunderline\b/.test(deco))
|
|
450
|
+
marks.push('u');
|
|
451
|
+
if (/\bline-through\b/.test(deco))
|
|
452
|
+
marks.push('s');
|
|
453
|
+
const va = (decls.get('vertical-align') ?? '').trim().toLowerCase();
|
|
454
|
+
if (va === 'super')
|
|
455
|
+
marks.push('sup');
|
|
456
|
+
else if (va === 'sub')
|
|
457
|
+
marks.push('sub');
|
|
458
|
+
for (let i = marks.length - 1; i >= 0; i--)
|
|
459
|
+
kids = [el(marks[i], {}, kids)];
|
|
460
|
+
// One style property per span, outermost first in vocabulary order.
|
|
461
|
+
for (let i = SPAN_STYLES.length - 1; i >= 0; i--) {
|
|
462
|
+
const prop = SPAN_STYLES[i];
|
|
463
|
+
const raw = decls.get(prop);
|
|
464
|
+
if (raw === undefined)
|
|
465
|
+
continue;
|
|
466
|
+
const value = prop === 'font-family' ? cleanFontFamily(raw) : prop === 'font-size' ? cleanFontSize(raw) : cleanColor(raw);
|
|
467
|
+
if (value !== null)
|
|
468
|
+
kids = [el('span', { style: `${prop}:${value}` }, kids)];
|
|
469
|
+
}
|
|
470
|
+
return wrap(kids);
|
|
471
|
+
}
|
|
472
|
+
function sanitizePre(node, ctx) {
|
|
473
|
+
const text = [];
|
|
474
|
+
textOf(node.children, text);
|
|
475
|
+
const code = node.children.find((c) => c.type === 'element' && c.tag === 'code');
|
|
476
|
+
const codeAttrs = code ? cleanAttrs('code', code.attrs, ctx) : {};
|
|
477
|
+
const value = text.join('');
|
|
478
|
+
return el('pre', cleanAttrs('pre', node.attrs, ctx), [el('code', codeAttrs, value ? [{ type: 'text', value }] : [])]);
|
|
479
|
+
}
|
|
480
|
+
function sanitizeList(node, ctx) {
|
|
481
|
+
const tag = node.tag === 'ol' ? 'ol' : 'ul';
|
|
482
|
+
const attrs = cleanAttrs(tag, node.attrs, ctx);
|
|
483
|
+
const kind = tag === 'ol' ? 'ol' : attrs['data-type'] === 'checklist' ? 'checklist' : 'ul';
|
|
484
|
+
if (kind === 'checklist')
|
|
485
|
+
delete attrs.style;
|
|
486
|
+
const kids = sanitizeChildren(node.children, child(ctx, tag, kind));
|
|
487
|
+
const items = [];
|
|
488
|
+
let loose = null;
|
|
489
|
+
for (const k of kids) {
|
|
490
|
+
if (k.type === 'element' && k.tag === 'li') {
|
|
491
|
+
items.push(k);
|
|
492
|
+
loose = null;
|
|
493
|
+
continue;
|
|
494
|
+
}
|
|
495
|
+
if (isWhitespace(k))
|
|
496
|
+
continue;
|
|
497
|
+
const last = items[items.length - 1];
|
|
498
|
+
if (k.type === 'element' && (k.tag === 'ul' || k.tag === 'ol') && last && last.type === 'element') {
|
|
499
|
+
// A list directly inside a list (Google Docs nesting) belongs to the previous item.
|
|
500
|
+
last.children.push(k);
|
|
501
|
+
continue;
|
|
502
|
+
}
|
|
503
|
+
if (!loose) {
|
|
504
|
+
loose = el('li', kind === 'checklist' ? { 'data-checked': 'false' } : {}, []);
|
|
505
|
+
items.push(loose);
|
|
506
|
+
}
|
|
507
|
+
if (k.type === 'text')
|
|
508
|
+
pushText(loose.children, k.value);
|
|
509
|
+
else
|
|
510
|
+
loose.children.push(k);
|
|
511
|
+
}
|
|
512
|
+
if (kind === 'checklist') {
|
|
513
|
+
for (const it of items)
|
|
514
|
+
if (it.type === 'element' && it.attrs['data-checked'] === undefined)
|
|
515
|
+
it.attrs = { 'data-checked': 'false', ...it.attrs };
|
|
516
|
+
}
|
|
517
|
+
return el(tag, attrs, items);
|
|
518
|
+
}
|
|
519
|
+
function sanitizeFigure(node, ctx) {
|
|
520
|
+
const kids = sanitizeChildren(node.children, child(ctx, 'figure'));
|
|
521
|
+
let img = null;
|
|
522
|
+
let caption = null;
|
|
523
|
+
const rest = [];
|
|
524
|
+
const find = (nodes) => {
|
|
525
|
+
for (const k of nodes) {
|
|
526
|
+
if (k.type !== 'element') {
|
|
527
|
+
if (!isWhitespace(k))
|
|
528
|
+
rest.push(k);
|
|
529
|
+
continue;
|
|
530
|
+
}
|
|
531
|
+
if (k.tag === 'img' && !img)
|
|
532
|
+
img = k;
|
|
533
|
+
else if (k.tag === 'figcaption' && !caption)
|
|
534
|
+
caption = k;
|
|
535
|
+
else if (k.tag === 'p' && !img && k.children.some((c) => c.type === 'element' && c.tag === 'img'))
|
|
536
|
+
find(k.children);
|
|
537
|
+
else
|
|
538
|
+
rest.push(k);
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
find(kids);
|
|
542
|
+
if (!img)
|
|
543
|
+
return kids;
|
|
544
|
+
const out = [img];
|
|
545
|
+
if (caption)
|
|
546
|
+
out.push(caption);
|
|
547
|
+
return [el('figure', cleanAttrs('figure', node.attrs, ctx), out), ...rest];
|
|
548
|
+
}
|
|
549
|
+
function sanitizeDiv(node, ctx) {
|
|
550
|
+
if (node.attrs['data-type'] === 'callout') {
|
|
551
|
+
const attrs = cleanAttrs('div', node.attrs, ctx);
|
|
552
|
+
const ordered = { 'data-type': 'callout', 'data-tone': attrs['data-tone'] ?? 'info' };
|
|
553
|
+
if (attrs['data-block-id'] !== undefined)
|
|
554
|
+
ordered['data-block-id'] = attrs['data-block-id'];
|
|
555
|
+
return [el('div', ordered, sanitizeChildren(node.children, child(ctx, 'div')))];
|
|
556
|
+
}
|
|
557
|
+
// A plain div is a paragraph when it holds only inline content, else unwrapped.
|
|
558
|
+
if (ctx.parent === 'p' || HEADINGS.has(ctx.parent ?? '') || ctx.parent === 'summary' || ctx.parent === 'figcaption')
|
|
559
|
+
return sanitizeChildren(node.children, ctx);
|
|
560
|
+
const kids = sanitizeChildren(node.children, child(ctx, 'p'));
|
|
561
|
+
if (hasBlockChild(kids) || kids.every(isWhitespace))
|
|
562
|
+
return kids;
|
|
563
|
+
// A paragraph written as <div> (RichTextEditor `enterKey: 'div'`) keeps its paragraph attributes.
|
|
564
|
+
return [el('p', cleanAttrs('p', node.attrs, ctx), kids)];
|
|
565
|
+
}
|
|
566
|
+
function sanitizeTable(node, ctx) {
|
|
567
|
+
const fostered = [];
|
|
568
|
+
const rows = [];
|
|
569
|
+
const rowCtx = child(ctx, 'tr', null);
|
|
570
|
+
const addRow = (tr) => {
|
|
571
|
+
const cells = [];
|
|
572
|
+
for (const c of tr.children) {
|
|
573
|
+
if (c.type === 'element' && (c.tag === 'td' || c.tag === 'th')) {
|
|
574
|
+
cells.push(el(c.tag, cleanAttrs(c.tag, c.attrs, rowCtx), sanitizeChildren(c.children, child(ctx, c.tag, null))));
|
|
575
|
+
}
|
|
576
|
+
else if (c.type === 'element' && c.tag !== 'script') {
|
|
577
|
+
pushAll(fostered, sanitizeElement(c, ctx));
|
|
578
|
+
}
|
|
579
|
+
else if (c.type === 'text' && !isWhitespace(c))
|
|
580
|
+
pushText(fostered, c.value);
|
|
581
|
+
}
|
|
582
|
+
if (cells.length)
|
|
583
|
+
rows.push(el('tr', {}, cells));
|
|
584
|
+
};
|
|
585
|
+
const walk = (nodes) => {
|
|
586
|
+
for (const c of nodes) {
|
|
587
|
+
if (c.type === 'element') {
|
|
588
|
+
if (c.tag === 'tr')
|
|
589
|
+
addRow(c);
|
|
590
|
+
else if (c.tag === 'tbody' || c.tag === 'thead' || c.tag === 'tfoot')
|
|
591
|
+
walk(c.children);
|
|
592
|
+
else if (c.tag === 'td' || c.tag === 'th')
|
|
593
|
+
addRow(el('tr', {}, [c]));
|
|
594
|
+
else if (c.tag === 'caption') {
|
|
595
|
+
const kids = sanitizeChildren(c.children, child(ctx, 'p'));
|
|
596
|
+
if (kids.length)
|
|
597
|
+
fostered.push(hasBlockChild(kids) ? el('div', {}, kids) : el('p', {}, kids));
|
|
598
|
+
}
|
|
599
|
+
else if (c.tag === 'colgroup' || c.tag === 'col')
|
|
600
|
+
continue;
|
|
601
|
+
else
|
|
602
|
+
pushAll(fostered, sanitizeElement(c, ctx));
|
|
603
|
+
}
|
|
604
|
+
else if (c.type === 'text' && !isWhitespace(c))
|
|
605
|
+
pushText(fostered, c.value);
|
|
606
|
+
}
|
|
607
|
+
};
|
|
608
|
+
walk(node.children);
|
|
609
|
+
const flat = fostered.flatMap((n) => (n.type === 'element' && n.tag === 'div' && Object.keys(n.attrs).length === 0 ? n.children : [n]));
|
|
610
|
+
if (rows.length === 0)
|
|
611
|
+
return flat;
|
|
612
|
+
return [...flat, el('table', cleanAttrs('table', node.attrs, ctx), [el('tbody', {}, rows)])];
|
|
613
|
+
}
|
|
614
|
+
// The hook pass: parents before children, restriction only.
|
|
615
|
+
function applyHook(nodes, hook, ctx, changed) {
|
|
616
|
+
const out = [];
|
|
617
|
+
for (const node of nodes) {
|
|
618
|
+
if (node.type !== 'element') {
|
|
619
|
+
if (node.type === 'text')
|
|
620
|
+
pushText(out, node.value);
|
|
621
|
+
continue;
|
|
622
|
+
}
|
|
623
|
+
const view = Object.freeze({ tag: node.tag, attrs: Object.freeze({ ...node.attrs }), parent: ctx.parent });
|
|
624
|
+
const result = hook(view);
|
|
625
|
+
const list = node.tag === 'ol' ? 'ol' : node.tag === 'ul' ? (node.attrs['data-type'] === 'checklist' ? 'checklist' : 'ul') : ctx.list;
|
|
626
|
+
const inner = child(ctx, node.tag, list);
|
|
627
|
+
if (result === null || result === 'remove') {
|
|
628
|
+
changed.structure = true;
|
|
629
|
+
continue;
|
|
630
|
+
}
|
|
631
|
+
if (result === 'unwrap') {
|
|
632
|
+
changed.structure = true;
|
|
633
|
+
pushAll(out, applyHook(node.children, hook, ctx, changed));
|
|
634
|
+
continue;
|
|
635
|
+
}
|
|
636
|
+
let attrs = node.attrs;
|
|
637
|
+
if (result !== undefined && result !== 'keep' && typeof result === 'object' && result.attrs) {
|
|
638
|
+
const next = {};
|
|
639
|
+
for (const name of Object.keys(result.attrs)) {
|
|
640
|
+
if (!Object.prototype.hasOwnProperty.call(node.attrs, name))
|
|
641
|
+
continue;
|
|
642
|
+
const value = result.attrs[name];
|
|
643
|
+
if (typeof value !== 'string')
|
|
644
|
+
continue;
|
|
645
|
+
// Values the built-in rules produced unchanged are kept; others are re-validated.
|
|
646
|
+
const v = value === node.attrs[name] ? value : name === 'style' && node.tag === 'span' ? reStyleSpan(value, node.attrs[name]) : cleanAttr(node.tag, name, value, ctx);
|
|
647
|
+
if (v !== null)
|
|
648
|
+
next[name] = v;
|
|
649
|
+
}
|
|
650
|
+
if (node.tag === 'a' && next.target === undefined)
|
|
651
|
+
delete next.rel;
|
|
652
|
+
if ((node.tag === 'a' && next.href === undefined) || (node.tag === 'img' && next.src === undefined) || ((node.tag === 'audio' || node.tag === 'video' || node.tag === 'iframe') && next.src === undefined)) {
|
|
653
|
+
changed.structure = true;
|
|
654
|
+
if (node.tag === 'a')
|
|
655
|
+
pushAll(out, applyHook(node.children, hook, ctx, changed));
|
|
656
|
+
continue;
|
|
657
|
+
}
|
|
658
|
+
if (node.tag === 'span' && Object.keys(next).length === 0) {
|
|
659
|
+
pushAll(out, applyHook(node.children, hook, ctx, changed));
|
|
660
|
+
continue;
|
|
661
|
+
}
|
|
662
|
+
attrs = next;
|
|
663
|
+
}
|
|
664
|
+
out.push(el(node.tag, attrs, applyHook(node.children, hook, inner, changed)));
|
|
665
|
+
}
|
|
666
|
+
return out;
|
|
667
|
+
}
|
|
668
|
+
// A hook may change a span's single style declaration only to another valid value of the same property.
|
|
669
|
+
function reStyleSpan(value, original) {
|
|
670
|
+
const prop = original.slice(0, original.indexOf(':'));
|
|
671
|
+
const decl = parseStyle(value).get(prop);
|
|
672
|
+
if (decl === undefined)
|
|
673
|
+
return null;
|
|
674
|
+
const v = prop === 'font-family' ? cleanFontFamily(decl) : prop === 'font-size' ? cleanFontSize(decl) : cleanColor(decl);
|
|
675
|
+
return v === null ? null : `${prop}:${v}`;
|
|
676
|
+
}
|
|
677
|
+
/** Sanitizes a parsed tree (see {@link sanitizeHtml}); returns a new tree. */
|
|
678
|
+
export function sanitizeNodes(nodes, options = {}) {
|
|
679
|
+
const ctx = { parent: null, list: null, embedHosts: options.embedHosts ?? [], blockIds: options.blockIds !== false, allow: compileAllow(options.allow) };
|
|
680
|
+
let out = sanitizeChildren(nodes, ctx);
|
|
681
|
+
const hook = options.sanitize;
|
|
682
|
+
if (hook) {
|
|
683
|
+
const changed = { structure: false };
|
|
684
|
+
out = applyHook(out, hook, ctx, changed);
|
|
685
|
+
// Re-normalize after unwrapping or removing (the built-in pass is idempotent).
|
|
686
|
+
if (changed.structure)
|
|
687
|
+
out = sanitizeChildren(out, ctx);
|
|
688
|
+
}
|
|
689
|
+
return out;
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* Sanitizes HTML against the shared vocabulary. Pure and
|
|
693
|
+
* server-safe: no DOM, no globals, deterministic, linear in the input size.
|
|
694
|
+
* `sanitizeHtml(sanitizeHtml(x)) === sanitizeHtml(x)`.
|
|
695
|
+
*/
|
|
696
|
+
export function sanitizeHtml(html, options = {}) {
|
|
697
|
+
return serializeHtml(sanitizeNodes(parseHtml(html), options));
|
|
698
|
+
}
|
|
699
|
+
//# sourceMappingURL=sanitize.js.map
|