-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.ts
99 lines (91 loc) · 2.4 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { SelectorsToElement } from './override';
function isObject(item: any) {
return item && typeof item === 'object' && !Array.isArray(item);
}
function deepMerge(target: any, source: any) {
const output = Object.assign({}, target);
if (isObject(target) && isObject(source)) {
Object.keys(source).forEach((key) => {
if (isObject(source[key])) {
if (!(key in target)) {
Object.assign(output, { [key]: source[key] });
} else {
output[key] = deepMerge(target[key], source[key]);
}
} else {
Object.assign(output, { [key]: source[key] });
}
});
}
return output;
}
export interface UserProfiles {
html?: boolean;
mathMl?: boolean;
svg?: boolean;
svgFilters?: boolean;
}
/**
* DOMPurify configuration
* The values RETURN_DOM, RETURN_DOM_FRAGMENT and RETURN_DOM_IMPORT cannot be overriden
* because they are used internaly by the library
*/
export interface DOMConfig {
ADD_ATTR?: string[];
ADD_TAGS?: string[];
ALLOW_DATA_ATTR?: boolean;
ALLOW_UNKNOWN_PROTOCOLS?: boolean;
ALLOWED_ATTR?: string[];
ALLOWED_TAGS?: string[];
ALLOWED_URI_REGEXP?: RegExp;
FORBID_ATTR?: string[];
FORBID_TAGS?: string[];
FORCE_BODY?: boolean;
IN_PLACE?: boolean;
KEEP_CONTENT?: boolean;
RETURN_DOM?: boolean;
RETURN_DOM_FRAGMENT?: boolean;
RETURN_DOM_IMPORT?: boolean;
SAFE_FOR_JQUERY?: boolean;
SAFE_FOR_TEMPLATES?: boolean;
SANITIZE_DOM?: boolean;
USE_PROFILES?: UserProfiles;
WHOLE_DOCUMENT?: boolean;
}
/**
* Configuration of the library
* The key dom correspond to a DOMPurify configuration
* the key eventHandlers allow to attach event handlers to the newly created React element
*/
export interface Config {
dom?: DOMConfig;
overrides?: SelectorsToElement;
useAsKey?: string[];
}
/**
* Options that cannot be overidden by the user because they are used internaly by the library
*/
const mandatoryConfig: Config = {
dom: {
RETURN_DOM: false,
RETURN_DOM_FRAGMENT: true,
RETURN_DOM_IMPORT: false,
},
useAsKey: ['key'],
};
/**
* Default configuration that can be overriden by the user
*/
const defaultConfig: Config = {
dom: {
ADD_ATTR: ['key'],
},
useAsKey: ['key'],
};
export function getConfig(config?: Partial<Config>): Config {
if (config) {
return deepMerge(defaultConfig, deepMerge(config, mandatoryConfig));
} else {
return deepMerge(defaultConfig, mandatoryConfig);
}
}