UNPKG

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