dompurify 3.4.9 → 3.4.10

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/src/regexp.ts CHANGED
@@ -14,3 +14,11 @@ export const ATTR_WHITESPACE = seal(
14
14
  );
15
15
  export const DOCTYPE_NAME = seal(/^html$/i);
16
16
  export const CUSTOM_ELEMENT = seal(/^[a-z][.\w]*(-[.\w]+)+$/i);
17
+
18
+ // Markup-significant character probes used by _sanitizeElements.
19
+ // Shared module-level instances are safe despite the sticky /g flags:
20
+ // unapply() resets lastIndex for RegExp receivers before every call.
21
+ export const ELEMENT_MARKUP_PROBE = seal(/<[/\w!]/g);
22
+ export const COMMENT_MARKUP_PROBE = seal(/<[/\w]/g);
23
+ export const FALLBACK_TAG_CLOSE = seal(/<\/no(script|embed|frames)/i);
24
+ export const SELF_CLOSING_TAG = seal(/\/>/i);
package/src/types.ts ADDED
@@ -0,0 +1,356 @@
1
+ /* eslint-disable @typescript-eslint/indent */
2
+
3
+ import type {
4
+ TrustedHTML,
5
+ TrustedTypesWindow,
6
+ } from 'trusted-types/lib/index.js';
7
+ import type { Config } from './config';
8
+
9
+ export interface DOMPurify {
10
+ /**
11
+ * Creates a DOMPurify instance using the given window-like object. Defaults to `window`.
12
+ */
13
+ (root?: WindowLike): DOMPurify;
14
+
15
+ /**
16
+ * Version label, exposed for easier checks
17
+ * if DOMPurify is up to date or not
18
+ */
19
+ version: string;
20
+
21
+ /**
22
+ * Array of elements that DOMPurify removed during sanitation.
23
+ * Empty if nothing was removed.
24
+ */
25
+ removed: Array<RemovedElement | RemovedAttribute>;
26
+
27
+ /**
28
+ * Expose whether this browser supports running the full DOMPurify.
29
+ */
30
+ isSupported: boolean;
31
+
32
+ /**
33
+ * Set the configuration once.
34
+ *
35
+ * @param cfg configuration object
36
+ */
37
+ setConfig(cfg?: Config): void;
38
+
39
+ /**
40
+ * Removes the configuration.
41
+ */
42
+ clearConfig(): void;
43
+
44
+ /**
45
+ * Provides core sanitation functionality.
46
+ *
47
+ * @param dirty string or DOM node
48
+ * @param cfg object
49
+ * @returns Sanitized TrustedHTML.
50
+ */
51
+ sanitize(
52
+ dirty: string | Node,
53
+ cfg: Config & { RETURN_TRUSTED_TYPE: true }
54
+ ): TrustedHTML;
55
+
56
+ /**
57
+ * Provides core sanitation functionality.
58
+ *
59
+ * @param dirty DOM node
60
+ * @param cfg object
61
+ * @returns Sanitized DOM node.
62
+ */
63
+ sanitize(dirty: Node, cfg: Config & { IN_PLACE: true }): Node;
64
+
65
+ /**
66
+ * Provides core sanitation functionality.
67
+ *
68
+ * @param dirty string or DOM node
69
+ * @param cfg object
70
+ * @returns Sanitized DOM node.
71
+ */
72
+ sanitize(dirty: string | Node, cfg: Config & { RETURN_DOM: true }): Node;
73
+
74
+ /**
75
+ * Provides core sanitation functionality.
76
+ *
77
+ * @param dirty string or DOM node
78
+ * @param cfg object
79
+ * @returns Sanitized document fragment.
80
+ */
81
+ sanitize(
82
+ dirty: string | Node,
83
+ cfg: Config & { RETURN_DOM_FRAGMENT: true }
84
+ ): DocumentFragment;
85
+
86
+ /**
87
+ * Provides core sanitation functionality.
88
+ *
89
+ * @param dirty string or DOM node
90
+ * @param cfg object
91
+ * @returns Sanitized string.
92
+ */
93
+ sanitize(dirty: string | Node, cfg?: Config): string;
94
+
95
+ /**
96
+ * Checks if an attribute value is valid.
97
+ * Uses last set config, if any. Otherwise, uses config defaults.
98
+ *
99
+ * @param tag Tag name of containing element.
100
+ * @param attr Attribute name.
101
+ * @param value Attribute value.
102
+ * @returns Returns true if `value` is valid. Otherwise, returns false.
103
+ */
104
+ isValidAttribute(tag: string, attr: string, value: string): boolean;
105
+
106
+ /**
107
+ * Adds a DOMPurify hook.
108
+ *
109
+ * @param entryPoint entry point for the hook to add
110
+ * @param hookFunction function to execute
111
+ */
112
+ addHook(entryPoint: BasicHookName, hookFunction: NodeHook): void;
113
+
114
+ /**
115
+ * Adds a DOMPurify hook.
116
+ *
117
+ * @param entryPoint entry point for the hook to add
118
+ * @param hookFunction function to execute
119
+ */
120
+ addHook(entryPoint: ElementHookName, hookFunction: ElementHook): void;
121
+
122
+ /**
123
+ * Adds a DOMPurify hook.
124
+ *
125
+ * @param entryPoint entry point for the hook to add
126
+ * @param hookFunction function to execute
127
+ */
128
+ addHook(
129
+ entryPoint: DocumentFragmentHookName,
130
+ hookFunction: DocumentFragmentHook
131
+ ): void;
132
+
133
+ /**
134
+ * Adds a DOMPurify hook.
135
+ *
136
+ * @param entryPoint entry point for the hook to add
137
+ * @param hookFunction function to execute
138
+ */
139
+ addHook(
140
+ entryPoint: 'uponSanitizeElement',
141
+ hookFunction: UponSanitizeElementHook
142
+ ): void;
143
+
144
+ /**
145
+ * Adds a DOMPurify hook.
146
+ *
147
+ * @param entryPoint entry point for the hook to add
148
+ * @param hookFunction function to execute
149
+ */
150
+ addHook(
151
+ entryPoint: 'uponSanitizeAttribute',
152
+ hookFunction: UponSanitizeAttributeHook
153
+ ): void;
154
+
155
+ /**
156
+ * Remove a DOMPurify hook at a given entryPoint
157
+ * (pops it from the stack of hooks if hook not specified)
158
+ *
159
+ * @param entryPoint entry point for the hook to remove
160
+ * @param hookFunction optional specific hook to remove
161
+ * @returns removed hook
162
+ */
163
+ removeHook(
164
+ entryPoint: BasicHookName,
165
+ hookFunction?: NodeHook
166
+ ): NodeHook | undefined;
167
+
168
+ /**
169
+ * Remove a DOMPurify hook at a given entryPoint
170
+ * (pops it from the stack of hooks if hook not specified)
171
+ *
172
+ * @param entryPoint entry point for the hook to remove
173
+ * @param hookFunction optional specific hook to remove
174
+ * @returns removed hook
175
+ */
176
+ removeHook(
177
+ entryPoint: ElementHookName,
178
+ hookFunction?: ElementHook
179
+ ): ElementHook | undefined;
180
+
181
+ /**
182
+ * Remove a DOMPurify hook at a given entryPoint
183
+ * (pops it from the stack of hooks if hook not specified)
184
+ *
185
+ * @param entryPoint entry point for the hook to remove
186
+ * @param hookFunction optional specific hook to remove
187
+ * @returns removed hook
188
+ */
189
+ removeHook(
190
+ entryPoint: DocumentFragmentHookName,
191
+ hookFunction?: DocumentFragmentHook
192
+ ): DocumentFragmentHook | undefined;
193
+
194
+ /**
195
+ * Remove a DOMPurify hook at a given entryPoint
196
+ * (pops it from the stack of hooks if hook not specified)
197
+ *
198
+ * @param entryPoint entry point for the hook to remove
199
+ * @param hookFunction optional specific hook to remove
200
+ * @returns removed hook
201
+ */
202
+ removeHook(
203
+ entryPoint: 'uponSanitizeElement',
204
+ hookFunction?: UponSanitizeElementHook
205
+ ): UponSanitizeElementHook | undefined;
206
+
207
+ /**
208
+ * Remove a DOMPurify hook at a given entryPoint
209
+ * (pops it from the stack of hooks if hook not specified)
210
+ *
211
+ * @param entryPoint entry point for the hook to remove
212
+ * @param hookFunction optional specific hook to remove
213
+ * @returns removed hook
214
+ */
215
+ removeHook(
216
+ entryPoint: 'uponSanitizeAttribute',
217
+ hookFunction?: UponSanitizeAttributeHook
218
+ ): UponSanitizeAttributeHook | undefined;
219
+
220
+ /**
221
+ * Removes all DOMPurify hooks at a given entryPoint
222
+ *
223
+ * @param entryPoint entry point for the hooks to remove
224
+ */
225
+ removeHooks(entryPoint: HookName): void;
226
+
227
+ /**
228
+ * Removes all DOMPurify hooks.
229
+ */
230
+ removeAllHooks(): void;
231
+ }
232
+
233
+ /**
234
+ * An element removed by DOMPurify.
235
+ */
236
+ export interface RemovedElement {
237
+ /**
238
+ * The element that was removed.
239
+ */
240
+ element: Node;
241
+ }
242
+
243
+ /**
244
+ * An element removed by DOMPurify.
245
+ */
246
+ export interface RemovedAttribute {
247
+ /**
248
+ * The attribute that was removed.
249
+ */
250
+ attribute: Attr | null;
251
+
252
+ /**
253
+ * The element that the attribute was removed.
254
+ */
255
+ from: Node;
256
+ }
257
+
258
+ type BasicHookName =
259
+ | 'beforeSanitizeElements'
260
+ | 'afterSanitizeElements'
261
+ | 'uponSanitizeShadowNode';
262
+ type ElementHookName = 'beforeSanitizeAttributes' | 'afterSanitizeAttributes';
263
+ type DocumentFragmentHookName =
264
+ | 'beforeSanitizeShadowDOM'
265
+ | 'afterSanitizeShadowDOM';
266
+ type UponSanitizeElementHookName = 'uponSanitizeElement';
267
+ type UponSanitizeAttributeHookName = 'uponSanitizeAttribute';
268
+
269
+ export interface HooksMap {
270
+ beforeSanitizeElements: NodeHook[];
271
+ afterSanitizeElements: NodeHook[];
272
+ beforeSanitizeShadowDOM: DocumentFragmentHook[];
273
+ uponSanitizeShadowNode: NodeHook[];
274
+ afterSanitizeShadowDOM: DocumentFragmentHook[];
275
+ beforeSanitizeAttributes: ElementHook[];
276
+ afterSanitizeAttributes: ElementHook[];
277
+ uponSanitizeElement: UponSanitizeElementHook[];
278
+ uponSanitizeAttribute: UponSanitizeAttributeHook[];
279
+ }
280
+
281
+ type ArrayElement<T> = T extends Array<infer U> ? U : never;
282
+
283
+ export type HookFunction = ArrayElement<HooksMap[keyof HooksMap]>;
284
+
285
+ export type HookName =
286
+ | BasicHookName
287
+ | ElementHookName
288
+ | DocumentFragmentHookName
289
+ | UponSanitizeElementHookName
290
+ | UponSanitizeAttributeHookName;
291
+
292
+ export type NodeHook = (
293
+ this: DOMPurify,
294
+ currentNode: Node,
295
+ hookEvent: null,
296
+ config: Config
297
+ ) => void;
298
+
299
+ export type ElementHook = (
300
+ this: DOMPurify,
301
+ currentNode: Element,
302
+ hookEvent: null,
303
+ config: Config
304
+ ) => void;
305
+
306
+ export type DocumentFragmentHook = (
307
+ this: DOMPurify,
308
+ currentNode: DocumentFragment,
309
+ hookEvent: null,
310
+ config: Config
311
+ ) => void;
312
+
313
+ export type UponSanitizeElementHook = (
314
+ this: DOMPurify,
315
+ currentNode: Node,
316
+ hookEvent: UponSanitizeElementHookEvent,
317
+ config: Config
318
+ ) => void;
319
+
320
+ export type UponSanitizeAttributeHook = (
321
+ this: DOMPurify,
322
+ currentNode: Element,
323
+ hookEvent: UponSanitizeAttributeHookEvent,
324
+ config: Config
325
+ ) => void;
326
+
327
+ export interface UponSanitizeElementHookEvent {
328
+ tagName: string;
329
+ allowedTags: Record<string, boolean>;
330
+ }
331
+
332
+ export interface UponSanitizeAttributeHookEvent {
333
+ attrName: string;
334
+ attrValue: string;
335
+ keepAttr: boolean;
336
+ allowedAttributes: Record<string, boolean>;
337
+ forceKeepAttr: boolean | undefined;
338
+ }
339
+
340
+ /**
341
+ * A `Window`-like object containing the properties and types that DOMPurify requires.
342
+ */
343
+ export type WindowLike = Pick<
344
+ typeof globalThis,
345
+ | 'DocumentFragment'
346
+ | 'HTMLTemplateElement'
347
+ | 'Node'
348
+ | 'Element'
349
+ | 'NodeFilter'
350
+ | 'NamedNodeMap'
351
+ | 'HTMLFormElement'
352
+ | 'DOMParser'
353
+ > & {
354
+ document?: Document;
355
+ MozNamedAttrMap?: typeof window.NamedNodeMap;
356
+ } & Pick<TrustedTypesWindow, 'trustedTypes'>;