UNPKG

5.39 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/// <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 ALLOW_SELF_CLOSE_IN_ATTR?: boolean | undefined;
65 ALLOWED_ATTR?: string[] | undefined;
66 ALLOWED_TAGS?: string[] | undefined;
67 ALLOWED_NAMESPACES?: string[] | undefined;
68 ALLOWED_URI_REGEXP?: RegExp | undefined;
69 FORBID_ATTR?: string[] | undefined;
70 FORBID_CONTENTS?: string[] | undefined;
71 FORBID_TAGS?: string[] | undefined;
72 FORCE_BODY?: boolean | undefined;
73 IN_PLACE?: boolean | undefined;
74 KEEP_CONTENT?: boolean | undefined;
75 /**
76 * change the default namespace from HTML to something different
77 */
78 NAMESPACE?: string | undefined;
79 PARSER_MEDIA_TYPE?: string | undefined;
80 RETURN_DOM_FRAGMENT?: boolean | undefined;
81 /**
82 * This defaults to `true` starting DOMPurify 2.2.0. Note that setting it to `false`
83 * might cause XSS from attacks hidden in closed shadowroots in case the browser
84 * supports Declarative Shadow: DOM https://web.dev/declarative-shadow-dom/
85 */
86 RETURN_DOM_IMPORT?: boolean | undefined;
87 RETURN_DOM?: boolean | undefined;
88 RETURN_TRUSTED_TYPE?: boolean | undefined;
89 SAFE_FOR_TEMPLATES?: boolean | undefined;
90 SANITIZE_DOM?: boolean | undefined;
91 /** @default false */
92 SANITIZE_NAMED_PROPS?: boolean | undefined;
93 USE_PROFILES?:
94 | false
95 | {
96 mathMl?: boolean | undefined;
97 svg?: boolean | undefined;
98 svgFilters?: boolean | undefined;
99 html?: boolean | undefined;
100 }
101 | undefined;
102 WHOLE_DOCUMENT?: boolean | undefined;
103 CUSTOM_ELEMENT_HANDLING?: {
104 tagNameCheck?: RegExp | ((tagName: string) => boolean) | null | undefined;
105 attributeNameCheck?: RegExp | ((lcName: string) => boolean) | null | undefined;
106 allowCustomizedBuiltInElements?: boolean | undefined;
107 };
108 }
109
110 type HookName =
111 | 'beforeSanitizeElements'
112 | 'uponSanitizeElement'
113 | 'afterSanitizeElements'
114 | 'beforeSanitizeAttributes'
115 | 'uponSanitizeAttribute'
116 | 'afterSanitizeAttributes'
117 | 'beforeSanitizeShadowDOM'
118 | 'uponSanitizeShadowNode'
119 | 'afterSanitizeShadowDOM';
120
121 type HookEvent = SanitizeElementHookEvent | SanitizeAttributeHookEvent | null;
122
123 interface SanitizeElementHookEvent {
124 tagName: string;
125 allowedTags: { [key: string]: boolean };
126 }
127
128 interface SanitizeAttributeHookEvent {
129 attrName: string;
130 attrValue: string;
131 keepAttr: boolean;
132 allowedAttributes: { [key: string]: boolean };
133 forceKeepAttr?: boolean | undefined;
134 }
135}
136
\No newline at end of file