1
1
/**
2
- * @typedef {import('hast').Parent } Parent
3
2
* @typedef {import('hast').Root } Root
3
+ * @typedef {import('hast').Content } Content
4
4
* @typedef {import('hast').Element } Element
5
5
* @typedef {import('hast').Properties } Properties
6
- * @typedef {Parent['children'][number]| Root } Node
6
+ * @typedef {Content | Root } Node
7
7
*
8
8
* @typedef {Properties[string] } PropertyValue
9
- * Possible property values
10
- * @typedef {string|number|boolean } PrimitivePropertyValue
11
- * Possible primitive HTML attribute values
12
- * @typedef {string|[string, ...Array<PrimitivePropertyValue|RegExp>] } AttributeValue
13
- * @typedef {Record<string, Array<PrimitivePropertyValue|RegExp>> } AttributeMap
9
+ * Possible property values.
10
+ * @typedef {string | number | boolean } PrimitivePropertyValue
11
+ * Possible primitive HTML attribute values.
14
12
*
15
- * @typedef Schema Sanitization configuration
16
- * @property {Record<string, Array<AttributeValue>> } [attributes]
17
- * Map of tag names to allowed property names. The special '*' key defines property names allowed on all elements
18
- * @property {Record<string, Record<string, PropertyValue>> } [required]
19
- * Map of tag names to required property names and their default property value
20
- * @property {Array<string> } [tagNames]
21
- * List of allowed tag names
22
- * @property {Record<string, Array<string>> } [protocols]
23
- * Map of protocols to allow in property values
24
- * @property {Record<string, Array<string>> } [ancestors]
25
- * Map of tag names to their required ancestor elements
26
- * @property {Array<string> } [clobber]
27
- * List of allowed property names which can clobber
28
- * @property {string } [clobberPrefix]
29
- * Prefix to use before potentially clobbering property names
30
- * @property {Array<string> } [strip]
31
- * Names of elements to strip from the tree
32
- * @property {boolean } [allowComments]
33
- * Whether to allow comments
34
- * @property {boolean } [allowDoctypes]
35
- * Whether to allow doctypes
13
+ * @typedef {Record<string, Array<string | [string, ...Array<PrimitivePropertyValue | RegExp>]>> } Attributes
14
+ * Map of tag names to allow lists for each property.
15
+ * @typedef {Record<string, Array<PrimitivePropertyValue | RegExp>> } AttributeClean
16
+ * Normalized input.
17
+ *
18
+ * @typedef Schema
19
+ * Sanitization configuration.
20
+ * @property {Attributes | undefined } [attributes]
21
+ * Map of tag names to allowed properties.
22
+ *
23
+ * The special `'*'` key defines property names allowed on all elements.
24
+ * @property {Record<string, Record<string, PropertyValue>> | undefined } [required]
25
+ * Map of tag names to required property names and their default property value.
26
+ * @property {Array<string> | undefined } [tagNames]
27
+ * List of allowed tag names.
28
+ * @property {Record<string, Array<string>> | undefined } [protocols]
29
+ * Map of protocols to allow in property values.
30
+ * @property {Record<string, Array<string>> | undefined } [ancestors]
31
+ * Map of tag names to their required ancestor elements.
32
+ * @property {Array<string> | undefined } [clobber]
33
+ * List of allowed property names which can clobber.
34
+ * @property {string | undefined } [clobberPrefix]
35
+ * Prefix to use before potentially clobbering property names.
36
+ * @property {Array<string> | undefined } [strip]
37
+ * Names of elements to strip from the tree.
38
+ * @property {boolean | undefined } [allowComments]
39
+ * Whether to allow comments.
40
+ * @property {boolean | undefined } [allowDoctypes]
41
+ * Whether to allow doctypes.
36
42
*
37
43
* @typedef {(schema: Schema, value: any, node: any, stack: Array<string>) => unknown } Handler
38
44
* @typedef {Record<string, Handler> } NodeDefinition
39
- * @typedef {((schema: Schema, node: Node) => NodeDefinition| undefined) } NodeDefinitionGetter
40
- * @typedef {Record<string, NodeDefinition| NodeDefinitionGetter> } NodeSchema
45
+ * @typedef {((schema: Schema, node: Node) => NodeDefinition | undefined) } NodeDefinitionGetter
46
+ * @typedef {Record<string, NodeDefinition | NodeDefinitionGetter> } NodeSchema
41
47
*/
42
48
43
49
import { defaultSchema } from './schema.js'
@@ -100,18 +106,18 @@ export function sanitize(node, schema) {
100
106
* @param {Schema } schema
101
107
* @param {Node } node
102
108
* @param {Array<string> } stack
103
- * @returns {Node| Array<Node>| undefined }
109
+ * @returns {Node | Array<Node> | undefined }
104
110
*/
105
111
function one ( schema , node , stack ) {
106
112
const type = node && node . type
107
113
/** @type {Node } */
108
114
// @ts -expect-error rest of props added later.
109
115
const replacement = { type : node . type }
110
- /** @type {boolean| undefined } */
116
+ /** @type {boolean | undefined } */
111
117
let replace
112
118
113
119
if ( own . call ( nodeSchema , type ) ) {
114
- /** @type {NodeDefinition| NodeDefinitionGetter| undefined } */
120
+ /** @type {NodeDefinition | NodeDefinitionGetter | undefined } */
115
121
let definition = nodeSchema [ type ]
116
122
117
123
if ( typeof definition === 'function' ) {
@@ -236,7 +242,7 @@ function handleProperties(schema, properties, node, stack) {
236
242
for ( key in props ) {
237
243
if ( own . call ( props , key ) ) {
238
244
let value = props [ key ]
239
- /** @type {Array<PrimitivePropertyValue|RegExp> } */
245
+ /** @type {AttributeClean[string] } */
240
246
let definition
241
247
242
248
if ( own . call ( allowed , key ) ) {
@@ -285,7 +291,7 @@ function handleDoctypeName() {
285
291
* @param {string } tagName
286
292
* @param {Node } _
287
293
* @param {Array<string> } stack
288
- * @returns {string| false }
294
+ * @returns {string | false }
289
295
*/
290
296
function handleTagName ( schema , tagName , _ , stack ) {
291
297
const name = typeof tagName === 'string' ? tagName : ''
@@ -355,12 +361,12 @@ function allow(_, value) {
355
361
* @param {Schema } schema
356
362
* @param {Array<unknown> } values
357
363
* @param {string } prop
358
- * @param {Array<PrimitivePropertyValue|RegExp> } definition
359
- * @returns {Array<string| number> }
364
+ * @param {AttributeClean[string] } definition
365
+ * @returns {Array<string | number> }
360
366
*/
361
367
function handlePropertyValues ( schema , values , prop , definition ) {
362
368
let index = - 1
363
- /** @type {Array<string| number> } */
369
+ /** @type {Array<string | number> } */
364
370
const result = [ ]
365
371
366
372
while ( ++ index < values . length ) {
@@ -381,7 +387,7 @@ function handlePropertyValues(schema, values, prop, definition) {
381
387
* @param {Schema } schema
382
388
* @param {unknown } value
383
389
* @param {string } prop
384
- * @param {Array<PropertyValue|RegExp> } definition
390
+ * @param {AttributeClean[string] } definition
385
391
* @returns {PropertyValue }
386
392
*/
387
393
function handlePropertyValue ( schema , value , prop , definition ) {
@@ -451,11 +457,11 @@ function safeProtocol(schema, value, prop) {
451
457
/**
452
458
* Create a map from a list of props or a list of properties and values.
453
459
*
454
- * @param {Array<AttributeValue> } values
455
- * @returns {AttributeMap }
460
+ * @param {Attributes[string] } values
461
+ * @returns {AttributeClean }
456
462
*/
457
463
function toPropertyValueMap ( values ) {
458
- /** @type {AttributeMap } */
464
+ /** @type {AttributeClean } */
459
465
const result = { }
460
466
let index = - 1
461
467
0 commit comments