UNPKG

5.1 kBTypeScriptView Raw
1// Type definitions for DOM Purify 2.3
2// Project: https://github.com/cure53/DOMPurify
3// Definitions by: Dave Taylor https://github.com/davetayls
4// Samira Bazuzi <https://github.com/bazuzi>
5// FlowCrypt <https://github.com/FlowCrypt>
6// Exigerr <https://github.com/Exigerr>
7// Piotr Błażejewicz <https://github.com/peterblazejewicz>
8// Nicholas Ellul <https://github.com/NicholasEllul>
9// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
10/// <reference types="trusted-types"/>
11
12export as namespace DOMPurify;
13export = DOMPurify;
14
15declare const DOMPurify: createDOMPurifyI;
16
17interface createDOMPurifyI extends DOMPurify.DOMPurifyI {
18 (window?: Window): DOMPurify.DOMPurifyI;
19}
20
21declare namespace DOMPurify {
22 interface DOMPurifyI {
23 sanitize(source: string | Node): string;
24 sanitize(source: string | Node, config: Config & { RETURN_TRUSTED_TYPE: true }): TrustedHTML;
25 sanitize(source: string | Node, config: Config & { RETURN_DOM_FRAGMENT?: false | undefined; RETURN_DOM?: false | undefined }): string;
26 sanitize(source: string | Node, config: Config & { RETURN_DOM_FRAGMENT: true }): DocumentFragment;
27 sanitize(source: string | Node, config: Config & { RETURN_DOM: true }): HTMLElement;
28 sanitize(source: string | Node, config: Config): string | HTMLElement | DocumentFragment;
29
30 addHook(hook: 'uponSanitizeElement', cb: (currentNode: Element, data: SanitizeElementHookEvent, config: Config) => void): void;
31 addHook(hook: 'uponSanitizeAttribute', cb: (currentNode: Element, data: SanitizeAttributeHookEvent, config: Config) => void): void;
32 addHook(hook: HookName, cb: (currentNode: Element, data: HookEvent, config: Config) => void): void;
33
34 setConfig(cfg: Config): void;
35 clearConfig(): void;
36 isValidAttribute(tag: string, attr: string, value: string): boolean;
37
38 removeHook(entryPoint: HookName): void;
39 removeHooks(entryPoint: HookName): void;
40 removeAllHooks(): void;
41
42 version: string;
43 removed: any[];
44 isSupported: boolean;
45 }
46
47 interface Config {
48 ADD_ATTR?: string[] | undefined;
49 ADD_DATA_URI_TAGS?: string[] | undefined;
50 ADD_TAGS?: string[] | undefined;
51 ADD_URI_SAFE_ATTR?: string[] | undefined;
52 ALLOW_ARIA_ATTR?: boolean | undefined;
53 ALLOW_DATA_ATTR?: boolean | undefined;
54 ALLOW_UNKNOWN_PROTOCOLS?: boolean | undefined;
55 ALLOWED_ATTR?: string[] | undefined;
56 ALLOWED_TAGS?: string[] | undefined;
57 ALLOWED_URI_REGEXP?: RegExp | undefined;
58 FORBID_ATTR?: string[] | undefined;
59 FORBID_CONTENTS?: string[] | undefined;
60 FORBID_TAGS?: string[] | undefined;
61 FORCE_BODY?: boolean | undefined;
62 IN_PLACE?: boolean | undefined;
63 KEEP_CONTENT?: boolean | undefined;
64 /**
65 * change the default namespace from HTML to something different
66 */
67 NAMESPACE?: string | undefined;
68 PARSER_MEDIA_TYPE?: string | undefined;
69 RETURN_DOM_FRAGMENT?: boolean | undefined;
70 /**
71 * This defaults to `true` starting DOMPurify 2.2.0. Note that setting it to `false`
72 * might cause XSS from attacks hidden in closed shadowroots in case the browser
73 * supports Declarative Shadow: DOM https://web.dev/declarative-shadow-dom/
74 */
75 RETURN_DOM_IMPORT?: boolean | undefined;
76 RETURN_DOM?: boolean | undefined;
77 RETURN_TRUSTED_TYPE?: boolean | undefined;
78 SAFE_FOR_TEMPLATES?: boolean | undefined;
79 SANITIZE_DOM?: boolean | undefined;
80 USE_PROFILES?:
81 | false
82 | {
83 mathMl?: boolean | undefined;
84 svg?: boolean | undefined;
85 svgFilters?: boolean | undefined;
86 html?: boolean | undefined;
87 }
88 | undefined;
89 WHOLE_DOCUMENT?: boolean | undefined;
90 CUSTOM_ELEMENT_HANDLING?: {
91 tagNameCheck?: RegExp | ((tagName: string) => boolean) | null | undefined;
92 attributeNameCheck?: RegExp | ((lcName: string) => boolean) | null | undefined;
93 allowCustomizedBuiltInElements?: boolean | undefined;
94 };
95 }
96
97 type HookName =
98 | 'beforeSanitizeElements'
99 | 'uponSanitizeElement'
100 | 'afterSanitizeElements'
101 | 'beforeSanitizeAttributes'
102 | 'uponSanitizeAttribute'
103 | 'afterSanitizeAttributes'
104 | 'beforeSanitizeShadowDOM'
105 | 'uponSanitizeShadowNode'
106 | 'afterSanitizeShadowDOM';
107
108 type HookEvent = SanitizeElementHookEvent | SanitizeAttributeHookEvent | null;
109
110 interface SanitizeElementHookEvent {
111 tagName: string;
112 allowedTags: { [key: string]: boolean };
113 }
114
115 interface SanitizeAttributeHookEvent {
116 attrName: string;
117 attrValue: string;
118 keepAttr: boolean;
119 allowedAttributes: { [key: string]: boolean };
120 forceKeepAttr?: boolean | undefined;
121 }
122}
123
\No newline at end of file