1
1
/**
2
2
* @typedef {import('hast').Parent } Parent
3
3
* @typedef {import('hast').Root } Root
4
+ * @typedef {import('hast').Element } Element
4
5
* @typedef {import('hast').Properties } Properties
5
6
* @typedef {Parent['children'][number]|Root } Node
6
7
*
29
30
30
31
import { defaultSchema } from './schema.js'
31
32
32
- var own = { } . hasOwnProperty
33
- var push = [ ] . push
33
+ const own = { } . hasOwnProperty
34
34
35
35
/** @type {NodeSchema } */
36
- var nodeSchema = {
36
+ const nodeSchema = {
37
37
root : { children : all } ,
38
38
doctype : handleDoctype ,
39
39
comment : handleComment ,
@@ -54,12 +54,14 @@ var nodeSchema = {
54
54
*/
55
55
export function sanitize ( node , schema ) {
56
56
/** @type {Node } */
57
- var ctx = { type : 'root' , children : [ ] }
58
- /** @type {Node|Array.<Node> } */
59
- var replace
57
+ let ctx = { type : 'root' , children : [ ] }
60
58
61
59
if ( node && typeof node === 'object' && node . type ) {
62
- replace = one ( Object . assign ( { } , defaultSchema , schema || { } ) , node , [ ] )
60
+ const replace = one (
61
+ Object . assign ( { } , defaultSchema , schema || { } ) ,
62
+ node ,
63
+ [ ]
64
+ )
63
65
64
66
if ( replace ) {
65
67
if ( Array . isArray ( replace ) ) {
@@ -87,35 +89,30 @@ export function sanitize(node, schema) {
87
89
* @returns {Node|Array.<Node>|null }
88
90
*/
89
91
function one ( schema , node , stack ) {
90
- var type = node && node . type
92
+ const type = node && node . type
91
93
/** @type {Node } */
92
94
// @ts -ignore rest of props added later.
93
- var replacement = { type : node . type }
95
+ const replacement = { type : node . type }
94
96
/** @type {boolean } */
95
- var replace
96
- /** @type {NodeDefinition|NodeDefinitionGetter } */
97
- var definition
98
- /** @type {NodeDefinition } */
99
- var allowed
100
- /** @type {unknown } */
101
- var result
102
- /** @type {string } */
103
- var key
97
+ let replace
104
98
105
99
if ( own . call ( nodeSchema , type ) ) {
106
- definition = nodeSchema [ type ]
100
+ let definition = nodeSchema [ type ]
107
101
108
102
if ( typeof definition === 'function' ) {
109
103
definition = definition ( schema , node )
110
104
}
111
105
112
106
if ( definition ) {
107
+ const allowed = Object . assign ( { } , definition , nodeSchema [ '*' ] )
108
+ /** @type {string } */
109
+ let key
110
+
113
111
replace = true
114
- allowed = Object . assign ( { } , definition , nodeSchema [ '*' ] )
115
112
116
113
for ( key in allowed ) {
117
114
if ( own . call ( allowed , key ) ) {
118
- result = allowed [ key ] ( schema , node [ key ] , node , stack )
115
+ const result = allowed [ key ] ( schema , node [ key ] , node , stack )
119
116
120
117
// eslint-disable-next-line max-depth
121
118
if ( result === false ) {
@@ -149,22 +146,21 @@ function one(schema, node, stack) {
149
146
*/
150
147
function all ( schema , children , node , stack ) {
151
148
/** @type {Array.<Node> } */
152
- var results = [ ]
153
- var index = - 1
154
- /** @type {Node|Array.<Node> } */
155
- var value
149
+ const results = [ ]
156
150
157
151
if ( Array . isArray ( children ) ) {
152
+ let index = - 1
153
+
158
154
if ( node . type === 'element' ) {
159
155
stack . push ( node . tagName )
160
156
}
161
157
162
158
while ( ++ index < children . length ) {
163
- value = one ( schema , children [ index ] , stack )
159
+ const value = one ( schema , children [ index ] , stack )
164
160
165
161
if ( value ) {
166
- if ( 'length' in value ) {
167
- push . apply ( results , value )
162
+ if ( Array . isArray ( value ) ) {
163
+ results . push ( ... value )
168
164
} else {
169
165
results . push ( value )
170
166
}
@@ -194,34 +190,32 @@ function handleComment(schema) {
194
190
*
195
191
* @type {Handler }
196
192
* @param {Properties } properties
193
+ * @param {Element } node
197
194
* @returns {Properties }
198
195
*/
199
196
function handleProperties ( schema , properties , node , stack ) {
200
- var name =
201
- node . type === 'element'
202
- ? handleTagName ( schema , node . tagName , node , stack )
203
- : false
197
+ const name = handleTagName ( schema , node . tagName , node , stack )
204
198
/* c8 ignore next */
205
- var reqs = schema . required || { }
206
- var props = properties || { }
207
- var allowed = Object . assign (
199
+ const reqs = schema . required || { }
200
+ const props = properties || { }
201
+ const allowed = Object . assign (
208
202
{ } ,
209
203
toPropertyValueMap ( schema . attributes [ '*' ] ) ,
210
204
toPropertyValueMap (
211
205
name && own . call ( schema . attributes , name ) ? schema . attributes [ name ] : [ ]
212
206
)
213
207
)
214
208
/** @type {Properties } */
215
- var result = { }
216
- /** @type {Array.<PrimitivePropertyValue> } */
217
- var definition
218
- /** @type {PropertyValue } */
219
- var value
209
+ const result = { }
220
210
/** @type {string } */
221
- var key
211
+ let key
222
212
223
213
for ( key in props ) {
224
214
if ( own . call ( props , key ) ) {
215
+ let value = props [ key ]
216
+ /** @type {Array.<PrimitivePropertyValue> } */
217
+ let definition
218
+
225
219
if ( own . call ( allowed , key ) ) {
226
220
definition = allowed [ key ]
227
221
} else if ( data ( key ) && own . call ( allowed , 'data*' ) ) {
@@ -230,7 +224,6 @@ function handleProperties(schema, properties, node, stack) {
230
224
continue
231
225
}
232
226
233
- value = props [ key ]
234
227
value = Array . isArray ( value )
235
228
? handlePropertyValues ( schema , value , key , definition )
236
229
: handlePropertyValue ( schema , value , key , definition )
@@ -269,8 +262,8 @@ function handleDoctypeName() {
269
262
* @returns {string|false }
270
263
*/
271
264
function handleTagName ( schema , tagName , _ , stack ) {
272
- var name = typeof tagName === 'string' ? tagName : ''
273
- var index = - 1
265
+ const name = typeof tagName === 'string' ? tagName : ''
266
+ let index = - 1
274
267
275
268
if ( ! name || name === '*' || ! schema . tagNames . includes ( name ) ) {
276
269
return false
@@ -299,8 +292,8 @@ function handleTagName(schema, tagName, _, stack) {
299
292
*/
300
293
function handleCommentValue ( _ , value ) {
301
294
/** @type {string } */
302
- var result = typeof value === 'string' ? value : ''
303
- var index = result . indexOf ( '-->' )
295
+ const result = typeof value === 'string' ? value : ''
296
+ const index = result . indexOf ( '-->' )
304
297
return index < 0 ? result : result . slice ( 0 , index )
305
298
}
306
299
@@ -333,14 +326,12 @@ function allow(_, value) {
333
326
* @returns {Array.<string|number> }
334
327
*/
335
328
function handlePropertyValues ( schema , values , prop , definition ) {
336
- var index = - 1
329
+ let index = - 1
337
330
/** @type {Array.<string|number> } */
338
- var result = [ ]
339
- /** @type {PropertyValue } */
340
- var value
331
+ const result = [ ]
341
332
342
333
while ( ++ index < values . length ) {
343
- value = handlePropertyValue ( schema , values [ index ] , prop , definition )
334
+ const value = handlePropertyValue ( schema , values [ index ] , prop , definition )
344
335
345
336
if ( value !== undefined && value !== null ) {
346
337
// @ts -ignore Assume no booleans were in arrays.
@@ -381,15 +372,15 @@ function handlePropertyValue(schema, value, prop, definition) {
381
372
* @returns {boolean }
382
373
*/
383
374
function safeProtocol ( schema , value , prop ) {
384
- var url = String ( value )
385
- var colon = url . indexOf ( ':' )
386
- var questionMark = url . indexOf ( '?' )
387
- var numberSign = url . indexOf ( '#' )
388
- var slash = url . indexOf ( '/' )
389
- var protocols = own . call ( schema . protocols , prop )
375
+ const url = String ( value )
376
+ const colon = url . indexOf ( ':' )
377
+ const questionMark = url . indexOf ( '?' )
378
+ const numberSign = url . indexOf ( '#' )
379
+ const slash = url . indexOf ( '/' )
380
+ const protocols = own . call ( schema . protocols , prop )
390
381
? schema . protocols [ prop ] . concat ( )
391
382
: [ ]
392
- var index = - 1
383
+ let index = - 1
393
384
394
385
if (
395
386
protocols . length === 0 ||
@@ -422,13 +413,11 @@ function safeProtocol(schema, value, prop) {
422
413
*/
423
414
function toPropertyValueMap ( values ) {
424
415
/** @type {AttributeMap } */
425
- var result = { }
426
- var index = - 1
427
- /** @type {AttributeValue } */
428
- var value
416
+ const result = { }
417
+ let index = - 1
429
418
430
419
while ( ++ index < values . length ) {
431
- value = values [ index ]
420
+ const value = values [ index ]
432
421
433
422
if ( Array . isArray ( value ) ) {
434
423
result [ value [ 0 ] ] = value . slice ( 1 )
0 commit comments