dompurify 3.4.9 → 3.4.11

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