Skip to content

Commit 9beb8ed

Browse files
committed
Refactor code-style
1 parent 76bf5bc commit 9beb8ed

File tree

3 files changed

+54
-40
lines changed

3 files changed

+54
-40
lines changed

lib/index.js

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,39 @@
55
* @typedef {import('hast').Properties} Properties
66
* @typedef {Parent['children'][number]|Root} Node
77
*
8-
* @typedef {Properties[string]} PropertyValue Possible property values
9-
* @typedef {string|number|boolean} PrimitivePropertyValue Possible primitive HTML attribute values
10-
* @typedef {string|[string, ...PrimitivePropertyValue[]]} AttributeValue
11-
* @typedef {Object.<string, Array.<PrimitivePropertyValue>>} AttributeMap
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>]} AttributeValue
13+
* @typedef {Record<string, Array<PrimitivePropertyValue>>} AttributeMap
1214
*
1315
* @typedef Schema Sanitization configuration
14-
* @property {Object<string, Array<AttributeValue>>} [attributes] Map of tag names to allowed property names. The special '*' key defines property names allowed on all elements
15-
* @property {Object<string, Object<string, PropertyValue>>} [required] Map of tag names to required property names and their default property value
16-
* @property {Array.<string>} [tagNames] List of allowed tag names
17-
* @property {Object<string, Array.<string>>} [protocols] Map of protocols to allow in property values
18-
* @property {Object<string, Array.<string>>} [ancestors] Map of tag names to their required ancestor elements
19-
* @property {Array.<string>} [clobber] List of allowed property names which can clobber
20-
* @property {string} [clobberPrefix] Prefix to use before potentially clobbering property names
21-
* @property {Array.<string>} [strip] Names of elements to strip from the tree
22-
* @property {boolean} [allowComments] Whether to allow comments
23-
* @property {boolean} [allowDoctypes] Whether to allow doctypes
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
2436
*
25-
* @typedef {(schema: Schema, value: any, node: any, stack: Array.<string>) => unknown} Handler
26-
* @typedef {Object.<string, Handler>} NodeDefinition
37+
* @typedef {(schema: Schema, value: any, node: any, stack: Array<string>) => unknown} Handler
38+
* @typedef {Record<string, Handler>} NodeDefinition
2739
* @typedef {((schema: Schema, node: Node) => NodeDefinition|undefined)} NodeDefinitionGetter
28-
* @typedef {Object.<string, NodeDefinition|NodeDefinitionGetter>} NodeSchema
40+
* @typedef {Record<string, NodeDefinition|NodeDefinitionGetter>} NodeSchema
2941
*/
3042

3143
import {defaultSchema} from './schema.js'
@@ -49,8 +61,10 @@ const nodeSchema = {
4961
/**
5062
* Utility to sanitize a tree
5163
*
52-
* @param {Node} node Hast tree to sanitize
53-
* @param {Schema} [schema] Schema defining how to sanitize - defaults to Github style sanitation
64+
* @param {Node} node
65+
* Hast tree to sanitize
66+
* @param {Schema} [schema]
67+
* Schema defining how to sanitize - defaults to Github style sanitation
5468
*/
5569
export function sanitize(node, schema) {
5670
/** @type {Node} */
@@ -85,8 +99,8 @@ export function sanitize(node, schema) {
8599
*
86100
* @param {Schema} schema
87101
* @param {Node} node
88-
* @param {Array.<string>} stack
89-
* @returns {Node|Array.<Node>|undefined}
102+
* @param {Array<string>} stack
103+
* @returns {Node|Array<Node>|undefined}
90104
*/
91105
function one(schema, node, stack) {
92106
const type = node && node.type
@@ -149,12 +163,12 @@ function one(schema, node, stack) {
149163
* Sanitize `children`.
150164
*
151165
* @type {Handler}
152-
* @param {Array.<Node>} children
166+
* @param {Array<Node>} children
153167
* @param {Node} node
154-
* @returns {Array.<Node>}
168+
* @returns {Array<Node>}
155169
*/
156170
function all(schema, children, node, stack) {
157-
/** @type {Array.<Node>} */
171+
/** @type {Array<Node>} */
158172
const results = []
159173

160174
if (Array.isArray(children)) {
@@ -222,7 +236,7 @@ function handleProperties(schema, properties, node, stack) {
222236
for (key in props) {
223237
if (own.call(props, key)) {
224238
let value = props[key]
225-
/** @type {Array.<PrimitivePropertyValue>} */
239+
/** @type {Array<PrimitivePropertyValue>} */
226240
let definition
227241

228242
if (own.call(allowed, key)) {
@@ -338,14 +352,14 @@ function allow(_, value) {
338352
* Sanitize a property value which is a list.
339353
*
340354
* @param {Schema} schema
341-
* @param {Array.<unknown>} values
355+
* @param {Array<unknown>} values
342356
* @param {string} prop
343-
* @param {Array.<PrimitivePropertyValue>} definition
344-
* @returns {Array.<string|number>}
357+
* @param {Array<PrimitivePropertyValue>} definition
358+
* @returns {Array<string|number>}
345359
*/
346360
function handlePropertyValues(schema, values, prop, definition) {
347361
let index = -1
348-
/** @type {Array.<string|number>} */
362+
/** @type {Array<string|number>} */
349363
const result = []
350364

351365
while (++index < values.length) {
@@ -366,7 +380,7 @@ function handlePropertyValues(schema, values, prop, definition) {
366380
* @param {Schema} schema
367381
* @param {unknown} value
368382
* @param {string} prop
369-
* @param {Array.<PropertyValue>} definition
383+
* @param {Array<PropertyValue>} definition
370384
* @returns {PropertyValue}
371385
*/
372386
function handlePropertyValue(schema, value, prop, definition) {
@@ -431,7 +445,7 @@ function safeProtocol(schema, value, prop) {
431445
/**
432446
* Create a map from a list of props or a list of properties and values.
433447
*
434-
* @param {Array.<AttributeValue>} values
448+
* @param {Array<AttributeValue>} values
435449
* @returns {AttributeMap}
436450
*/
437451
function toPropertyValueMap(values) {

readme.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ console.log(tree)
122122
###### `attributes`
123123

124124
Map of tag names to allowed [*property names*][name]
125-
(`Object.<Array.<string>>`).
125+
(`Record<string, Array<string>>`).
126126

127127
The special `'*'` key defines [*property names*][name] allowed on all
128128
[*elements*][element].
@@ -180,7 +180,7 @@ span: [
180180
###### `required`
181181

182182
Map of tag names to required [*property names*][name] and their default
183-
[*property value*][value] (`Object.<Object.<*>>`).
183+
[*property value*][value] (`Record<string, Record<string, *>>`).
184184
If the defined keys do not exist in an [*element*][element]’s
185185
[*properties*][properties], they are added and set to the specified value.
186186

@@ -196,7 +196,7 @@ required: {
196196

197197
###### `tagNames`
198198

199-
List of allowed tag names (`Array.<string>`).
199+
List of allowed tag names (`Array<string>`).
200200

201201
```js
202202
tagNames: [
@@ -213,7 +213,7 @@ tagNames: [
213213
###### `protocols`
214214

215215
Map of protocols to allow in [*property values*][value]
216-
(`Object.<Array.<string>>`).
216+
(`Record<string, Array<string>>`).
217217

218218
```js
219219
protocols: {
@@ -226,7 +226,7 @@ protocols: {
226226
###### `ancestors`
227227

228228
Map of tag names to their required [*ancestor*][ancestor] [*elements*][element]
229-
(`Object.<Array.<string>>`).
229+
(`Record<string, Array<string>>`).
230230

231231
```js
232232
ancestors: {
@@ -238,7 +238,7 @@ ancestors: {
238238

239239
###### `clobber`
240240

241-
List of allowed [*property names*][name] which can clobber (`Array.<string>`).
241+
List of allowed [*property names*][name] which can clobber (`Array<string>`).
242242

243243
```js
244244
clobber: ['name', 'id']
@@ -255,7 +255,7 @@ clobberPrefix: 'user-content-'
255255
###### `strip`
256256

257257
Names of [*elements*][element] to strip from the [*tree*][tree]
258-
(`Array.<string>`).
258+
(`Array<string>`).
259259

260260
By default, unsafe *elements* are replaced by their [*children*][child].
261261
Some *elements*, should however be entirely stripped from the *tree*.

test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ function toString() {
702702
* @param {import('tape').Test} t
703703
* @param {string} tagName
704704
* @param {string} prop
705-
* @param {{valid: Object.<string, string>, invalid: Object.<string, string>}} all
705+
* @param {{valid: Record<string, string>, invalid: Record<string, string>}} all
706706
*/
707707
function testAllUrls(t, tagName, prop, all) {
708708
testUrls(t, tagName, prop, all.valid, true)
@@ -715,7 +715,7 @@ function testAllUrls(t, tagName, prop, all) {
715715
* @param {import('tape').Test} t
716716
* @param {string} tagName
717717
* @param {string} prop
718-
* @param {Object.<string, string>} urls
718+
* @param {Record<string, string>} urls
719719
* @param {boolean} valid
720720
*/
721721
function testUrls(t, tagName, prop, urls, valid) {

0 commit comments

Comments
 (0)