Skip to content

Commit b1e81c6

Browse files
committed
Add docs to types
1 parent ab4dc20 commit b1e81c6

File tree

1 file changed

+48
-42
lines changed

1 file changed

+48
-42
lines changed

Diff for: lib/index.js

+48-42
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,49 @@
11
/**
2-
* @typedef {import('hast').Parent} Parent
32
* @typedef {import('hast').Root} Root
3+
* @typedef {import('hast').Content} Content
44
* @typedef {import('hast').Element} Element
55
* @typedef {import('hast').Properties} Properties
6-
* @typedef {Parent['children'][number]|Root} Node
6+
* @typedef {Content | Root} Node
77
*
88
* @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.
1412
*
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.
3642
*
3743
* @typedef {(schema: Schema, value: any, node: any, stack: Array<string>) => unknown} Handler
3844
* @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
4147
*/
4248

4349
import {defaultSchema} from './schema.js'
@@ -100,18 +106,18 @@ export function sanitize(node, schema) {
100106
* @param {Schema} schema
101107
* @param {Node} node
102108
* @param {Array<string>} stack
103-
* @returns {Node|Array<Node>|undefined}
109+
* @returns {Node | Array<Node> | undefined}
104110
*/
105111
function one(schema, node, stack) {
106112
const type = node && node.type
107113
/** @type {Node} */
108114
// @ts-expect-error rest of props added later.
109115
const replacement = {type: node.type}
110-
/** @type {boolean|undefined} */
116+
/** @type {boolean | undefined} */
111117
let replace
112118

113119
if (own.call(nodeSchema, type)) {
114-
/** @type {NodeDefinition|NodeDefinitionGetter|undefined} */
120+
/** @type {NodeDefinition | NodeDefinitionGetter | undefined} */
115121
let definition = nodeSchema[type]
116122

117123
if (typeof definition === 'function') {
@@ -236,7 +242,7 @@ function handleProperties(schema, properties, node, stack) {
236242
for (key in props) {
237243
if (own.call(props, key)) {
238244
let value = props[key]
239-
/** @type {Array<PrimitivePropertyValue|RegExp>} */
245+
/** @type {AttributeClean[string]} */
240246
let definition
241247

242248
if (own.call(allowed, key)) {
@@ -285,7 +291,7 @@ function handleDoctypeName() {
285291
* @param {string} tagName
286292
* @param {Node} _
287293
* @param {Array<string>} stack
288-
* @returns {string|false}
294+
* @returns {string | false}
289295
*/
290296
function handleTagName(schema, tagName, _, stack) {
291297
const name = typeof tagName === 'string' ? tagName : ''
@@ -355,12 +361,12 @@ function allow(_, value) {
355361
* @param {Schema} schema
356362
* @param {Array<unknown>} values
357363
* @param {string} prop
358-
* @param {Array<PrimitivePropertyValue|RegExp>} definition
359-
* @returns {Array<string|number>}
364+
* @param {AttributeClean[string]} definition
365+
* @returns {Array<string | number>}
360366
*/
361367
function handlePropertyValues(schema, values, prop, definition) {
362368
let index = -1
363-
/** @type {Array<string|number>} */
369+
/** @type {Array<string | number>} */
364370
const result = []
365371

366372
while (++index < values.length) {
@@ -381,7 +387,7 @@ function handlePropertyValues(schema, values, prop, definition) {
381387
* @param {Schema} schema
382388
* @param {unknown} value
383389
* @param {string} prop
384-
* @param {Array<PropertyValue|RegExp>} definition
390+
* @param {AttributeClean[string]} definition
385391
* @returns {PropertyValue}
386392
*/
387393
function handlePropertyValue(schema, value, prop, definition) {
@@ -451,11 +457,11 @@ function safeProtocol(schema, value, prop) {
451457
/**
452458
* Create a map from a list of props or a list of properties and values.
453459
*
454-
* @param {Array<AttributeValue>} values
455-
* @returns {AttributeMap}
460+
* @param {Attributes[string]} values
461+
* @returns {AttributeClean}
456462
*/
457463
function toPropertyValueMap(values) {
458-
/** @type {AttributeMap} */
464+
/** @type {AttributeClean} */
459465
const result = {}
460466
let index = -1
461467

0 commit comments

Comments
 (0)