UNPKG

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