This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Sanitizer.js
59 lines (50 loc) · 1.96 KB
/
Sanitizer.js
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
/**
* @copyright 2022-2023 Chris Zuber <admin@kernvalley.us>
*/
import { nativeSupport, getSantizerUtils, sanitize, sanitizeFor, trustPolicies } from './sanitizerUtils.js';
import { SanitizerConfig as defaultConfig } from './SanitizerConfigW3C.js';
const protectedData = new WeakMap();
/**
* Need to create a policy for the Sanitizer API since
* `trustedTypes.defaultPolicy.createHTML` will most likely use `new Sanitizer().sanitize()`
* which would create infinite recursion.
* @type {TrustedTypePolicy}
*/
/**
* @SEE https://wicg.github.io/sanitizer-api/
* @SEE https://developer.mozilla.org/en-US/docs/Web/API/Sanitizer/Sanitizer
* @TODO: Figure out how to handle `allowElements`, `allowAttributes`, and how each
* works with their `block*` and/or `drop*` counterparts.
* @TODO: Handle `svg:*` and `mathml:*`
*
* @NOTE: The spec is still under development and is likely to change.
* @NOTE: This is a very imperfect implementation and may not perform very well,
* as it may involve a lot of querying & modifying.
*/
export class Sanitizer {
constructor({
allowElements, allowAttributes, blockElements, dropAttributes,
dropElements, allowComments = defaultConfig.allowComments,
allowCustomElements = defaultConfig.allowCustomElements,
allowUnknownMarkup = defaultConfig.allowUnknownMarkup,
} = Sanitizer.getDefaultConfiguration()) {
protectedData.set(this, {
allowElements, allowComments, allowAttributes, allowCustomElements,
blockElements, dropAttributes, dropElements, allowUnknownMarkup,
});
}
getConfiguration() {
return protectedData.get(this);
}
sanitize(input) {
return sanitize(input, { config: this.getConfiguration() });
}
sanitizeFor(tag, content) {
return sanitizeFor(tag, content, { config: this.getConfiguration() });
}
static getDefaultConfiguration() {
return defaultConfig;
}
}
const { setHTML, polyfill } = getSantizerUtils(Sanitizer, defaultConfig);
export { nativeSupport, setHTML, polyfill, trustPolicies };