UNPKG

5.66 kBTypeScriptView Raw
1// Type definitions for DOM Purify 3.0
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// Minimum TypeScript Version: 4.5
11/// <reference types="trusted-types"/>
12
13export as namespace DOMPurify;
14export = DOMPurify;
15
16declare const DOMPurify: createDOMPurifyI;
17
18type WindowLike = Pick<
19 typeof globalThis,
20 | 'NodeFilter'
21 | 'Node'
22 | 'Element'
23 | 'HTMLTemplateElement'
24 | 'DocumentFragment'
25 | 'HTMLFormElement'
26 | 'DOMParser'
27 | 'NamedNodeMap'
28>;
29
30interface createDOMPurifyI extends DOMPurify.DOMPurifyI {
31 (window?: Window | WindowLike): DOMPurify.DOMPurifyI;
32}
33
34declare namespace DOMPurify {
35 interface DOMPurifyI {
36 sanitize(source: string | Node): string;
37 sanitize(source: string | Node, config: Config & { RETURN_TRUSTED_TYPE: true }): TrustedHTML;
38 sanitize(
39 source: string | Node,
40 config: Config & { RETURN_DOM_FRAGMENT?: false | undefined; RETURN_DOM?: false | undefined },
41 ): string;
42 sanitize(source: string | Node, config: Config & { RETURN_DOM_FRAGMENT: true }): DocumentFragment;
43 sanitize(source: string | Node, config: Config & { RETURN_DOM: true }): HTMLElement;
44 sanitize(source: string | Node, config: Config): string | HTMLElement | DocumentFragment;
45
46 addHook(
47 hook: 'uponSanitizeElement',
48 cb: (currentNode: Element, data: SanitizeElementHookEvent, config: Config) => void,
49 ): void;
50 addHook(
51 hook: 'uponSanitizeAttribute',
52 cb: (currentNode: Element, data: SanitizeAttributeHookEvent, config: Config) => void,
53 ): void;
54 addHook(hook: HookName, cb: (currentNode: Element, data: HookEvent, config: Config) => void): void;
55
56 setConfig(cfg: Config): void;
57 clearConfig(): void;
58 isValidAttribute(tag: string, attr: string, value: string): boolean;
59
60 removeHook(entryPoint: HookName): void;
61 removeHooks(entryPoint: HookName): void;
62 removeAllHooks(): void;
63
64 version: string;
65 removed: any[];
66 isSupported: boolean;
67 }
68
69 interface Config {
70 ADD_ATTR?: string[] | undefined;
71 ADD_DATA_URI_TAGS?: string[] | undefined;
72 ADD_TAGS?: string[] | undefined;
73 ADD_URI_SAFE_ATTR?: string[] | undefined;
74 ALLOW_ARIA_ATTR?: boolean | undefined;
75 ALLOW_DATA_ATTR?: boolean | undefined;
76 ALLOW_UNKNOWN_PROTOCOLS?: boolean | undefined;
77 ALLOW_SELF_CLOSE_IN_ATTR?: boolean | undefined;
78 ALLOWED_ATTR?: string[] | undefined;
79 ALLOWED_TAGS?: string[] | undefined;
80 ALLOWED_NAMESPACES?: string[] | undefined;
81 ALLOWED_URI_REGEXP?: RegExp | undefined;
82 FORBID_ATTR?: string[] | undefined;
83 FORBID_CONTENTS?: string[] | undefined;
84 FORBID_TAGS?: string[] | undefined;
85 FORCE_BODY?: boolean | undefined;
86 IN_PLACE?: boolean | undefined;
87 KEEP_CONTENT?: boolean | undefined;
88 /**
89 * change the default namespace from HTML to something different
90 */
91 NAMESPACE?: string | undefined;
92 PARSER_MEDIA_TYPE?: string | undefined;
93 RETURN_DOM_FRAGMENT?: boolean | undefined;
94 /**
95 * This defaults to `true` starting DOMPurify 2.2.0. Note that setting it to `false`
96 * might cause XSS from attacks hidden in closed shadowroots in case the browser
97 * supports Declarative Shadow: DOM https://web.dev/declarative-shadow-dom/
98 */
99 RETURN_DOM_IMPORT?: boolean | undefined;
100 RETURN_DOM?: boolean | undefined;
101 RETURN_TRUSTED_TYPE?: boolean | undefined;
102 SAFE_FOR_TEMPLATES?: boolean | undefined;
103 SANITIZE_DOM?: boolean | undefined;
104 /** @default false */
105 SANITIZE_NAMED_PROPS?: boolean | undefined;
106 USE_PROFILES?:
107 | false
108 | {
109 mathMl?: boolean | undefined;
110 svg?: boolean | undefined;
111 svgFilters?: boolean | undefined;
112 html?: boolean | undefined;
113 }
114 | undefined;
115 WHOLE_DOCUMENT?: boolean | undefined;
116 CUSTOM_ELEMENT_HANDLING?: {
117 tagNameCheck?: RegExp | ((tagName: string) => boolean) | null | undefined;
118 attributeNameCheck?: RegExp | ((lcName: string) => boolean) | null | undefined;
119 allowCustomizedBuiltInElements?: boolean | undefined;
120 };
121 }
122
123 type HookName =
124 | 'beforeSanitizeElements'
125 | 'uponSanitizeElement'
126 | 'afterSanitizeElements'
127 | 'beforeSanitizeAttributes'
128 | 'uponSanitizeAttribute'
129 | 'afterSanitizeAttributes'
130 | 'beforeSanitizeShadowDOM'
131 | 'uponSanitizeShadowNode'
132 | 'afterSanitizeShadowDOM';
133
134 type HookEvent = SanitizeElementHookEvent | SanitizeAttributeHookEvent | null;
135
136 interface SanitizeElementHookEvent {
137 tagName: string;
138 allowedTags: { [key: string]: boolean };
139 }
140
141 interface SanitizeAttributeHookEvent {
142 attrName: string;
143 attrValue: string;
144 keepAttr: boolean;
145 allowedAttributes: { [key: string]: boolean };
146 forceKeepAttr?: boolean | undefined;
147 }
148}
149
\No newline at end of file