This repository was 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
Expand file tree
/
Copy pathhtmlpurify.js
More file actions
51 lines (46 loc) · 1.35 KB
/
htmlpurify.js
File metadata and controls
51 lines (46 loc) · 1.35 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
/**
* @copyright 2022-2023 Chris Zuber <admin@kernvalley.us>
*/
import { createPolicy } from './trust.js';
import { events } from './attributes.js';
import { sanitize } from './sanitizerUtils.js';
/**
* TrustedTypePolicy for internal use
* @type {TrustedTypePolicy}
*/
const nullPolicy = createPolicy('purify-raw#html', { createHTML: input => input });
const config = {
allowElements: null,
allowAttributes: null,
allowComments: false,
allowCustomElements: true,
allowUnknownMarkup: true,
blockElements: null,
dropElements: [
'script', 'object', 'embed', 'param', 'head', 'body', 'frame', 'noscript',
'base', 'iframe',
],
dropAttributes: Object.fromEntries([...events, 'ping', 'style'].map(attr => [attr, ['*']])),
};
/**
* [trustPolicy description]
* @type {TrustedTypePolicy}
*/
export const purify = createPolicy('purify#html', {
createHTML: input => {
const div = document.createElement('div');
const tmp = document.createElement('template');
tmp.innerHTML = nullPolicy.createHTML(input);
div.append(sanitize(tmp.content, { config }));
return div.innerHTML;
},
});
/**
* Alias of `purify.createHTML()`
* @param {string} input [description]
* @return {TrustedHTML} [description]
*/
export function createHTML(input) {
return purify.createHTML(input);
}
export const trustPolicies = [nullPolicy.name, purify.name];