-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrust.js
More file actions
71 lines (65 loc) · 1.95 KB
/
trust.js
File metadata and controls
71 lines (65 loc) · 1.95 KB
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
export function createPolicy(name, { createHTML: html, createScript: script, createScriptURL: scriptURL }) {
if ('trustedTypes' in globalThis) {
return trustedTypes.createPolicy(name, { createHTML: html, createScript: script, createScriptURL: scriptURL });
} else {
return Object.freeze({
[Symbol.for('policy-name')]: name.toString(),
createHTML(input, ...args) {
if (html instanceof Function) {
const result = html(input.toString(), ...args).toString();
return Object.freeze({
toString() {
return result;
}
});
} else {
throw new TypeError(`Policy "${name}" does not provide a createHTML method.`);
}
},
createScript(input, ...args) {
if (script instanceof Function) {
const result = script(input.toString(), ...args).toString();
return Object.freeze({
toString() {
return result;
}
});
} else {
throw new TypeError(`Policy "${name}" does not provide a createScript method.`);
}
},
createScriptURL(input, ...args) {
if (scriptURL instanceof Function) {
const result = scriptURL(input.toString(), ...args).toString();
return Object.freeze({
toString() {
return result;
}
});
} else {
throw new TypeError(`Policy "${name}" does not provide a createScriptURL method.`);
}
},
});
}
}
export function createSanitizerPolicy(name = 'aegis#html', { defaultConfig = {} }= {}) {
return createPolicy(name, {
createHTML(input, {
elements = defaultConfig.elements,
attributes = defaultConfig.attributes,
comments = defaultConfig.comments,
dataAttributes = defaultConfig.dataAttributes,
sanitizer,
...rest
} = defaultConfig) {
const el = document.createElement('div');
if (typeof sanitizer === 'object') {
el.setHTML(input, { sanitizer });
} else {
el.setHTML(input, { sanitizer: { elements, attributes, comments, dataAttributes, ...rest }});
}
return el.innerHTML;
}
});
}