Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/browser/dompurify/dompurify.d.ts
5263 views
1
/*! @license DOMPurify 3.2.7 | (c) Cure53 and other contributors | Released under the Apache license 2.0 and Mozilla Public License 2.0 | github.com/cure53/DOMPurify/blob/3.2.7/LICENSE */
2
3
import type { TrustedTypePolicy, TrustedHTML, TrustedTypesWindow } from 'trusted-types/lib/index.d.ts';
4
5
/**
6
* Configuration to control DOMPurify behavior.
7
*/
8
interface Config {
9
/**
10
* Extend the existing array of allowed attributes.
11
*/
12
ADD_ATTR?: string[] | undefined;
13
/**
14
* Extend the existing array of elements that can use Data URIs.
15
*/
16
ADD_DATA_URI_TAGS?: string[] | undefined;
17
/**
18
* Extend the existing array of allowed tags.
19
*/
20
ADD_TAGS?: string[] | undefined;
21
/**
22
* Extend the existing array of elements that are safe for URI-like values (be careful, XSS risk).
23
*/
24
ADD_URI_SAFE_ATTR?: string[] | undefined;
25
/**
26
* Allow ARIA attributes, leave other safe HTML as is (default is true).
27
*/
28
ALLOW_ARIA_ATTR?: boolean | undefined;
29
/**
30
* Allow HTML5 data attributes, leave other safe HTML as is (default is true).
31
*/
32
ALLOW_DATA_ATTR?: boolean | undefined;
33
/**
34
* Allow external protocol handlers in URL attributes (default is false, be careful, XSS risk).
35
* By default only `http`, `https`, `ftp`, `ftps`, `tel`, `mailto`, `callto`, `sms`, `cid` and `xmpp` are allowed.
36
*/
37
ALLOW_UNKNOWN_PROTOCOLS?: boolean | undefined;
38
/**
39
* Decide if self-closing tags in attributes are allowed.
40
* Usually removed due to a mXSS issue in jQuery 3.0.
41
*/
42
ALLOW_SELF_CLOSE_IN_ATTR?: boolean | undefined;
43
/**
44
* Allow only specific attributes.
45
*/
46
ALLOWED_ATTR?: string[] | undefined;
47
/**
48
* Allow only specific elements.
49
*/
50
ALLOWED_TAGS?: string[] | undefined;
51
/**
52
* Allow only specific namespaces. Defaults to:
53
* - `http://www.w3.org/1999/xhtml`
54
* - `http://www.w3.org/2000/svg`
55
* - `http://www.w3.org/1998/Math/MathML`
56
*/
57
ALLOWED_NAMESPACES?: string[] | undefined;
58
/**
59
* Allow specific protocols handlers in URL attributes via regex (be careful, XSS risk).
60
* Default RegExp:
61
* ```
62
* /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i;
63
* ```
64
*/
65
ALLOWED_URI_REGEXP?: RegExp | undefined;
66
/**
67
* Define how custom elements are handled.
68
*/
69
CUSTOM_ELEMENT_HANDLING?: {
70
/**
71
* Regular expression or function to match to allowed elements.
72
* Default is null (disallow any custom elements).
73
*/
74
tagNameCheck?: RegExp | ((tagName: string) => boolean) | null | undefined;
75
/**
76
* Regular expression or function to match to allowed attributes.
77
* Default is null (disallow any attributes not on the allow list).
78
*/
79
attributeNameCheck?: RegExp | ((attributeName: string, tagName?: string) => boolean) | null | undefined;
80
/**
81
* Allow custom elements derived from built-ins if they pass `tagNameCheck`. Default is false.
82
*/
83
allowCustomizedBuiltInElements?: boolean | undefined;
84
};
85
/**
86
* Add attributes to block-list.
87
*/
88
FORBID_ATTR?: string[] | undefined;
89
/**
90
* Add child elements to be removed when their parent is removed.
91
*/
92
FORBID_CONTENTS?: string[] | undefined;
93
/**
94
* Add elements to block-list.
95
*/
96
FORBID_TAGS?: string[] | undefined;
97
/**
98
* Glue elements like style, script or others to `document.body` and prevent unintuitive browser behavior in several edge-cases (default is false).
99
*/
100
FORCE_BODY?: boolean | undefined;
101
/**
102
* Map of non-standard HTML element names to support. Map to true to enable support. For example:
103
*
104
* ```
105
* HTML_INTEGRATION_POINTS: { foreignobject: true }
106
* ```
107
*/
108
HTML_INTEGRATION_POINTS?: Record<string, boolean> | undefined;
109
/**
110
* Sanitize a node "in place", which is much faster depending on how you use DOMPurify.
111
*/
112
IN_PLACE?: boolean | undefined;
113
/**
114
* Keep an element's content when the element is removed (default is true).
115
*/
116
KEEP_CONTENT?: boolean | undefined;
117
/**
118
* Map of MathML element names to support. Map to true to enable support. For example:
119
*
120
* ```
121
* MATHML_TEXT_INTEGRATION_POINTS: { mtext: true }
122
* ```
123
*/
124
MATHML_TEXT_INTEGRATION_POINTS?: Record<string, boolean> | undefined;
125
/**
126
* Change the default namespace from HTML to something different.
127
*/
128
NAMESPACE?: string | undefined;
129
/**
130
* Change the parser type so sanitized data is treated as XML and not as HTML, which is the default.
131
*/
132
PARSER_MEDIA_TYPE?: DOMParserSupportedType | undefined;
133
/**
134
* Return a DOM `DocumentFragment` instead of an HTML string (default is false).
135
*/
136
RETURN_DOM_FRAGMENT?: boolean | undefined;
137
/**
138
* Return a DOM `HTMLBodyElement` instead of an HTML string (default is false).
139
*/
140
RETURN_DOM?: boolean | undefined;
141
/**
142
* Return a TrustedHTML object instead of a string if possible.
143
*/
144
RETURN_TRUSTED_TYPE?: boolean | undefined;
145
/**
146
* Strip `{{ ... }}`, `${ ... }` and `<% ... %>` to make output safe for template systems.
147
* Be careful please, this mode is not recommended for production usage.
148
* Allowing template parsing in user-controlled HTML is not advised at all.
149
* Only use this mode if there is really no alternative.
150
*/
151
SAFE_FOR_TEMPLATES?: boolean | undefined;
152
/**
153
* Change how e.g. comments containing risky HTML characters are treated.
154
* Be very careful, this setting should only be set to `false` if you really only handle
155
* HTML and nothing else, no SVG, MathML or the like.
156
* Otherwise, changing from `true` to `false` will lead to XSS in this or some other way.
157
*/
158
SAFE_FOR_XML?: boolean | undefined;
159
/**
160
* Use DOM Clobbering protection on output (default is true, handle with care, minor XSS risks here).
161
*/
162
SANITIZE_DOM?: boolean | undefined;
163
/**
164
* Enforce strict DOM Clobbering protection via namespace isolation (default is false).
165
* When enabled, isolates the namespace of named properties (i.e., `id` and `name` attributes)
166
* from JS variables by prefixing them with the string `user-content-`
167
*/
168
SANITIZE_NAMED_PROPS?: boolean | undefined;
169
/**
170
* Supplied policy must define `createHTML` and `createScriptURL`.
171
*/
172
TRUSTED_TYPES_POLICY?: TrustedTypePolicy | undefined;
173
/**
174
* Controls categories of allowed elements.
175
*
176
* Note that the `USE_PROFILES` setting will override the `ALLOWED_TAGS` setting
177
* so don't use them together.
178
*/
179
USE_PROFILES?: false | UseProfilesConfig | undefined;
180
/**
181
* Return entire document including <html> tags (default is false).
182
*/
183
WHOLE_DOCUMENT?: boolean | undefined;
184
}
185
/**
186
* Defines categories of allowed elements.
187
*/
188
interface UseProfilesConfig {
189
/**
190
* Allow all safe MathML elements.
191
*/
192
mathMl?: boolean | undefined;
193
/**
194
* Allow all safe SVG elements.
195
*/
196
svg?: boolean | undefined;
197
/**
198
* Allow all save SVG Filters.
199
*/
200
svgFilters?: boolean | undefined;
201
/**
202
* Allow all safe HTML elements.
203
*/
204
html?: boolean | undefined;
205
}
206
207
declare const _default: DOMPurify;
208
209
interface DOMPurify {
210
/**
211
* Creates a DOMPurify instance using the given window-like object. Defaults to `window`.
212
*/
213
(root?: WindowLike): DOMPurify;
214
/**
215
* Version label, exposed for easier checks
216
* if DOMPurify is up to date or not
217
*/
218
version: string;
219
/**
220
* Array of elements that DOMPurify removed during sanitation.
221
* Empty if nothing was removed.
222
*/
223
removed: Array<RemovedElement | RemovedAttribute>;
224
/**
225
* Expose whether this browser supports running the full DOMPurify.
226
*/
227
isSupported: boolean;
228
/**
229
* Set the configuration once.
230
*
231
* @param cfg configuration object
232
*/
233
setConfig(cfg?: Config): void;
234
/**
235
* Removes the configuration.
236
*/
237
clearConfig(): void;
238
/**
239
* Provides core sanitation functionality.
240
*
241
* @param dirty string or DOM node
242
* @param cfg object
243
* @returns Sanitized TrustedHTML.
244
*/
245
sanitize(dirty: string | Node, cfg: Config & {
246
RETURN_TRUSTED_TYPE: true;
247
}): TrustedHTML;
248
/**
249
* Provides core sanitation functionality.
250
*
251
* @param dirty DOM node
252
* @param cfg object
253
* @returns Sanitized DOM node.
254
*/
255
sanitize(dirty: Node, cfg: Config & {
256
IN_PLACE: true;
257
}): Node;
258
/**
259
* Provides core sanitation functionality.
260
*
261
* @param dirty string or DOM node
262
* @param cfg object
263
* @returns Sanitized DOM node.
264
*/
265
sanitize(dirty: string | Node, cfg: Config & {
266
RETURN_DOM: true;
267
}): Node;
268
/**
269
* Provides core sanitation functionality.
270
*
271
* @param dirty string or DOM node
272
* @param cfg object
273
* @returns Sanitized document fragment.
274
*/
275
sanitize(dirty: string | Node, cfg: Config & {
276
RETURN_DOM_FRAGMENT: true;
277
}): DocumentFragment;
278
/**
279
* Provides core sanitation functionality.
280
*
281
* @param dirty string or DOM node
282
* @param cfg object
283
* @returns Sanitized string.
284
*/
285
sanitize(dirty: string | Node, cfg?: Config): string;
286
/**
287
* Checks if an attribute value is valid.
288
* Uses last set config, if any. Otherwise, uses config defaults.
289
*
290
* @param tag Tag name of containing element.
291
* @param attr Attribute name.
292
* @param value Attribute value.
293
* @returns Returns true if `value` is valid. Otherwise, returns false.
294
*/
295
isValidAttribute(tag: string, attr: string, value: string): boolean;
296
/**
297
* Adds a DOMPurify hook.
298
*
299
* @param entryPoint entry point for the hook to add
300
* @param hookFunction function to execute
301
*/
302
addHook(entryPoint: BasicHookName, hookFunction: NodeHook): void;
303
/**
304
* Adds a DOMPurify hook.
305
*
306
* @param entryPoint entry point for the hook to add
307
* @param hookFunction function to execute
308
*/
309
addHook(entryPoint: ElementHookName, hookFunction: ElementHook): void;
310
/**
311
* Adds a DOMPurify hook.
312
*
313
* @param entryPoint entry point for the hook to add
314
* @param hookFunction function to execute
315
*/
316
addHook(entryPoint: DocumentFragmentHookName, hookFunction: DocumentFragmentHook): void;
317
/**
318
* Adds a DOMPurify hook.
319
*
320
* @param entryPoint entry point for the hook to add
321
* @param hookFunction function to execute
322
*/
323
addHook(entryPoint: 'uponSanitizeElement', hookFunction: UponSanitizeElementHook): void;
324
/**
325
* Adds a DOMPurify hook.
326
*
327
* @param entryPoint entry point for the hook to add
328
* @param hookFunction function to execute
329
*/
330
addHook(entryPoint: 'uponSanitizeAttribute', hookFunction: UponSanitizeAttributeHook): void;
331
/**
332
* Remove a DOMPurify hook at a given entryPoint
333
* (pops it from the stack of hooks if hook not specified)
334
*
335
* @param entryPoint entry point for the hook to remove
336
* @param hookFunction optional specific hook to remove
337
* @returns removed hook
338
*/
339
removeHook(entryPoint: BasicHookName, hookFunction?: NodeHook): NodeHook | undefined;
340
/**
341
* Remove a DOMPurify hook at a given entryPoint
342
* (pops it from the stack of hooks if hook not specified)
343
*
344
* @param entryPoint entry point for the hook to remove
345
* @param hookFunction optional specific hook to remove
346
* @returns removed hook
347
*/
348
removeHook(entryPoint: ElementHookName, hookFunction?: ElementHook): ElementHook | undefined;
349
/**
350
* Remove a DOMPurify hook at a given entryPoint
351
* (pops it from the stack of hooks if hook not specified)
352
*
353
* @param entryPoint entry point for the hook to remove
354
* @param hookFunction optional specific hook to remove
355
* @returns removed hook
356
*/
357
removeHook(entryPoint: DocumentFragmentHookName, hookFunction?: DocumentFragmentHook): DocumentFragmentHook | undefined;
358
/**
359
* Remove a DOMPurify hook at a given entryPoint
360
* (pops it from the stack of hooks if hook not specified)
361
*
362
* @param entryPoint entry point for the hook to remove
363
* @param hookFunction optional specific hook to remove
364
* @returns removed hook
365
*/
366
removeHook(entryPoint: 'uponSanitizeElement', hookFunction?: UponSanitizeElementHook): UponSanitizeElementHook | undefined;
367
/**
368
* Remove a DOMPurify hook at a given entryPoint
369
* (pops it from the stack of hooks if hook not specified)
370
*
371
* @param entryPoint entry point for the hook to remove
372
* @param hookFunction optional specific hook to remove
373
* @returns removed hook
374
*/
375
removeHook(entryPoint: 'uponSanitizeAttribute', hookFunction?: UponSanitizeAttributeHook): UponSanitizeAttributeHook | undefined;
376
/**
377
* Removes all DOMPurify hooks at a given entryPoint
378
*
379
* @param entryPoint entry point for the hooks to remove
380
*/
381
removeHooks(entryPoint: HookName): void;
382
/**
383
* Removes all DOMPurify hooks.
384
*/
385
removeAllHooks(): void;
386
}
387
/**
388
* An element removed by DOMPurify.
389
*/
390
interface RemovedElement {
391
/**
392
* The element that was removed.
393
*/
394
element: Node;
395
}
396
/**
397
* An element removed by DOMPurify.
398
*/
399
interface RemovedAttribute {
400
/**
401
* The attribute that was removed.
402
*/
403
attribute: Attr | null;
404
/**
405
* The element that the attribute was removed.
406
*/
407
from: Node;
408
}
409
type BasicHookName = 'beforeSanitizeElements' | 'afterSanitizeElements' | 'uponSanitizeShadowNode';
410
type ElementHookName = 'beforeSanitizeAttributes' | 'afterSanitizeAttributes';
411
type DocumentFragmentHookName = 'beforeSanitizeShadowDOM' | 'afterSanitizeShadowDOM';
412
type UponSanitizeElementHookName = 'uponSanitizeElement';
413
type UponSanitizeAttributeHookName = 'uponSanitizeAttribute';
414
type HookName = BasicHookName | ElementHookName | DocumentFragmentHookName | UponSanitizeElementHookName | UponSanitizeAttributeHookName;
415
type NodeHook = (this: DOMPurify, currentNode: Node, hookEvent: null, config: Config) => void;
416
type ElementHook = (this: DOMPurify, currentNode: Element, hookEvent: null, config: Config) => void;
417
type DocumentFragmentHook = (this: DOMPurify, currentNode: DocumentFragment, hookEvent: null, config: Config) => void;
418
type UponSanitizeElementHook = (this: DOMPurify, currentNode: Node, hookEvent: UponSanitizeElementHookEvent, config: Config) => void;
419
type UponSanitizeAttributeHook = (this: DOMPurify, currentNode: Element, hookEvent: UponSanitizeAttributeHookEvent, config: Config) => void;
420
interface UponSanitizeElementHookEvent {
421
tagName: string;
422
allowedTags: Record<string, boolean>;
423
}
424
interface UponSanitizeAttributeHookEvent {
425
attrName: string;
426
attrValue: string;
427
keepAttr: boolean;
428
allowedAttributes: Record<string, boolean>;
429
forceKeepAttr: boolean | undefined;
430
}
431
/**
432
* A `Window`-like object containing the properties and types that DOMPurify requires.
433
*/
434
type WindowLike = Pick<typeof globalThis, 'DocumentFragment' | 'HTMLTemplateElement' | 'Node' | 'Element' | 'NodeFilter' | 'NamedNodeMap' | 'HTMLFormElement' | 'DOMParser'> & {
435
document?: Document;
436
MozNamedAttrMap?: typeof window.NamedNodeMap;
437
} & Pick<TrustedTypesWindow, 'trustedTypes'>;
438
439
export { type Config, type DOMPurify, type DocumentFragmentHook, type ElementHook, type HookName, type NodeHook, type RemovedAttribute, type RemovedElement, type UponSanitizeAttributeHook, type UponSanitizeAttributeHookEvent, type UponSanitizeElementHook, type UponSanitizeElementHookEvent, type WindowLike, _default as default };
440
441