-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig-utils.js
More file actions
194 lines (176 loc) · 5.53 KB
/
Copy pathconfig-utils.js
File metadata and controls
194 lines (176 loc) · 5.53 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { HTML as HTMLNS } from '@aegisjsproject/sanitizer/namespaces.js';
/**
* @typedef {Readonly<{name: string, namespace?: string|null}>} SanitizerElementNamespace
*/
/**
* @typedef {Readonly<{name: string, namespace?: string|null}>} SanitizerAttributeNamespace
*/
/**
* @typedef {string|SanitizerAttributeNamespace} SanitizerAttribute
*/
/**
* @typedef {SanitizerElementNamespace & Readonly<{
* attributes?: SanitizerAttribute[],
* removeAttributes?: SanitizerAttribute[]
* }>} SanitizerElementNamespaceWithAttributes
*/
/**
* @typedef {string|SanitizerElementNamespace} SanitizerElement
*/
/**
* @typedef {string|SanitizerElementNamespaceWithAttributes} SanitizerElementWithAttributes
*/
/**
* @typedef {Readonly<{
* elements?: SanitizerElementWithAttributes[],
* removeElements?: SanitizerElement[],
* replaceWithChildrenElements?: SanitizerElement[],
* attributes?: SanitizerAttribute[],
* removeAttributes?: SanitizerAttribute[],
* comments?: boolean,
* dataAttributes?: boolean
* }>} SanitizerConfig
*/
export const EVENT_ATTRS = new Set(('HTMLElement' in globalThis
? Object.keys(HTMLElement.prototype) : [])
.filter(name => name.startsWith('on')));
/**
*
* @param {string|object} opt
* @param {string?} defaultNS
* @returns {SanitizerElement}
*/
export function normalizeElement(opt, defaultNS = HTMLNS) {
if (typeof opt === 'string') {
return Object.freeze({ name: opt, namespace: defaultNS });
} else if (typeof opt === 'object' && typeof opt.name === 'string') {
const { name, namespace = defaultNS, attributes } = opt;
return Object.freeze({
name,
namespace: typeof namespace === 'string' && namespace.length !== 0 ? namespace : defaultNS,
attributes,
});
} else {
throw new TypeError('Invalid config entry for `elements`.');
}
}
/**
*
* @param {object} els
* @param {Array} [els.elements]
* @param {Array} [els.allowElements]
* @param {string} defaultNS
* @returns {SanitizerElementNamespace[]}
*/
function normalizeElementsConfig({ elements, allowElements }, defaultNS = HTMLNS) {
if (Array.isArray(allowElements)) {
console.warn('Use of `allowElements` is deprecated. Please use `elements` instead.');
return normalizeElementsConfig({ elements: allowElements }, defaultNS);
} else if (! Array.isArray(elements)) {
throw new TypeError('`elements` expected to be an array.');
} else {
return elements.map(opt => normalizeElement(opt, defaultNS));
}
}
/**
*
* @param {string} opt
* @param {string?} defaultNS
* @returns {SanitizerAttributeNamespace}
*/
export function normalizeAttr(opt, defaultNS) {
if (typeof opt === 'string') {
return Object.freeze({ name: opt, namespace: defaultNS });
} else if (typeof opt === 'object' && typeof opt.name === 'string') {
const { name, namespace = defaultNS, elements, ...rest } = opt;
return Object.freeze({
name,
namespace: typeof namespace === 'string' ? namespace : defaultNS,
elements,
...rest
});
} else {
throw new TypeError('Invalid entry in `attributes` config.');
}
}
function normalizeAttrsConfig({ attributes, allowAttributes }, defaultNS) {
if (typeof allowAttributes !== 'undefined') {
console.warn('Use of `allowAttributes` is deprecated. Please use `attributes` instead.');
return normalizeAttrsConfig({ attributes: allowAttributes }, defaultNS);
} else if (Array.isArray(attributes)) {
return attributes.map(opt => normalizeAttr(opt, defaultNS));
} else if (typeof attributes === 'object' && attributes !== null) {
console.warn('`attributes` should be an array, not an oobject.');
return normalizeAttrsConfig({
attributes: Object.entries(attributes).map(([name, elements]) => ({ name, elements })),
}, defaultNS);
} else {
throw new TypeError('`attributes` expected to be an array.');
}
}
function normalizeCommentsConfig({ comments, allowComments }) {
if (typeof allowComments === 'boolean') {
console.warn('Use of `allowComments` is deprecated. Please use `comments` instead.');
return allowComments;
} else if (typeof comments === 'boolean') {
return comments;
} else {
return true;
}
}
export function normalizeConfig(config, {
elementNS = HTMLNS,
attributeNS,
} = {}) {
const {
elements,
attributes,
allowElements,
allowAttributes,
comments = false,
allowComments,
dataAttributes = true,
...rest
} = config;
const cfg = {
elements: normalizeElementsConfig({ elements, allowElements }, elementNS),
attributes: normalizeAttrsConfig({ attributes, allowAttributes }, attributeNS),
comments: comments || allowComments,
dataAttributes,
...rest,
};
return cfg;
}
function convertAttrConfig({ attributes, allowAttributes }, defaultNS) {
return Object.freeze(
Object.groupBy(
normalizeAttrsConfig({ attributes, allowAttributes }, defaultNS),
({ namespace }) => namespace ?? ''
)
);
}
function convertElementConfig({ elements, allowElements }, defaultNS = HTMLNS) {
return Object.freeze(
Object.groupBy(
normalizeElementsConfig({ elements, allowElements }, defaultNS),
({ namespace }) => namespace
)
);
}
export function convertConfig(config, {
elementNS = HTMLNS,
attributeNS,
} = {}) {
if (typeof config !== 'object' || config === null) {
throw new TypeError('Sanitizer config must be an object.');
} else if (config.get instanceof Function) {
return convertConfig(config.get());
} else {
return Object.freeze({
elements: convertElementConfig(config, elementNS),
attributes: convertAttrConfig(config, attributeNS),
comments: normalizeCommentsConfig(config),
dataAttributes: typeof config.dataAttributes === 'undefined' ? true : config.dataAttributes,
});
}
}