Skip to content

Commit be3657a

Browse files
committed
Refactor code-style
1 parent 609e26a commit be3657a

File tree

4 files changed

+93
-97
lines changed

4 files changed

+93
-97
lines changed

lib/index.js

Lines changed: 52 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @typedef {import('hast').Parent} Parent
33
* @typedef {import('hast').Root} Root
4+
* @typedef {import('hast').Element} Element
45
* @typedef {import('hast').Properties} Properties
56
* @typedef {Parent['children'][number]|Root} Node
67
*
@@ -29,11 +30,10 @@
2930

3031
import {defaultSchema} from './schema.js'
3132

32-
var own = {}.hasOwnProperty
33-
var push = [].push
33+
const own = {}.hasOwnProperty
3434

3535
/** @type {NodeSchema} */
36-
var nodeSchema = {
36+
const nodeSchema = {
3737
root: {children: all},
3838
doctype: handleDoctype,
3939
comment: handleComment,
@@ -54,12 +54,14 @@ var nodeSchema = {
5454
*/
5555
export function sanitize(node, schema) {
5656
/** @type {Node} */
57-
var ctx = {type: 'root', children: []}
58-
/** @type {Node|Array.<Node>} */
59-
var replace
57+
let ctx = {type: 'root', children: []}
6058

6159
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+
)
6365

6466
if (replace) {
6567
if (Array.isArray(replace)) {
@@ -87,35 +89,30 @@ export function sanitize(node, schema) {
8789
* @returns {Node|Array.<Node>|null}
8890
*/
8991
function one(schema, node, stack) {
90-
var type = node && node.type
92+
const type = node && node.type
9193
/** @type {Node} */
9294
// @ts-ignore rest of props added later.
93-
var replacement = {type: node.type}
95+
const replacement = {type: node.type}
9496
/** @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
10498

10599
if (own.call(nodeSchema, type)) {
106-
definition = nodeSchema[type]
100+
let definition = nodeSchema[type]
107101

108102
if (typeof definition === 'function') {
109103
definition = definition(schema, node)
110104
}
111105

112106
if (definition) {
107+
const allowed = Object.assign({}, definition, nodeSchema['*'])
108+
/** @type {string} */
109+
let key
110+
113111
replace = true
114-
allowed = Object.assign({}, definition, nodeSchema['*'])
115112

116113
for (key in allowed) {
117114
if (own.call(allowed, key)) {
118-
result = allowed[key](schema, node[key], node, stack)
115+
const result = allowed[key](schema, node[key], node, stack)
119116

120117
// eslint-disable-next-line max-depth
121118
if (result === false) {
@@ -149,22 +146,21 @@ function one(schema, node, stack) {
149146
*/
150147
function all(schema, children, node, stack) {
151148
/** @type {Array.<Node>} */
152-
var results = []
153-
var index = -1
154-
/** @type {Node|Array.<Node>} */
155-
var value
149+
const results = []
156150

157151
if (Array.isArray(children)) {
152+
let index = -1
153+
158154
if (node.type === 'element') {
159155
stack.push(node.tagName)
160156
}
161157

162158
while (++index < children.length) {
163-
value = one(schema, children[index], stack)
159+
const value = one(schema, children[index], stack)
164160

165161
if (value) {
166-
if ('length' in value) {
167-
push.apply(results, value)
162+
if (Array.isArray(value)) {
163+
results.push(...value)
168164
} else {
169165
results.push(value)
170166
}
@@ -194,34 +190,32 @@ function handleComment(schema) {
194190
*
195191
* @type {Handler}
196192
* @param {Properties} properties
193+
* @param {Element} node
197194
* @returns {Properties}
198195
*/
199196
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)
204198
/* 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(
208202
{},
209203
toPropertyValueMap(schema.attributes['*']),
210204
toPropertyValueMap(
211205
name && own.call(schema.attributes, name) ? schema.attributes[name] : []
212206
)
213207
)
214208
/** @type {Properties} */
215-
var result = {}
216-
/** @type {Array.<PrimitivePropertyValue>} */
217-
var definition
218-
/** @type {PropertyValue} */
219-
var value
209+
const result = {}
220210
/** @type {string} */
221-
var key
211+
let key
222212

223213
for (key in props) {
224214
if (own.call(props, key)) {
215+
let value = props[key]
216+
/** @type {Array.<PrimitivePropertyValue>} */
217+
let definition
218+
225219
if (own.call(allowed, key)) {
226220
definition = allowed[key]
227221
} else if (data(key) && own.call(allowed, 'data*')) {
@@ -230,7 +224,6 @@ function handleProperties(schema, properties, node, stack) {
230224
continue
231225
}
232226

233-
value = props[key]
234227
value = Array.isArray(value)
235228
? handlePropertyValues(schema, value, key, definition)
236229
: handlePropertyValue(schema, value, key, definition)
@@ -269,8 +262,8 @@ function handleDoctypeName() {
269262
* @returns {string|false}
270263
*/
271264
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
274267

275268
if (!name || name === '*' || !schema.tagNames.includes(name)) {
276269
return false
@@ -299,8 +292,8 @@ function handleTagName(schema, tagName, _, stack) {
299292
*/
300293
function handleCommentValue(_, value) {
301294
/** @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('-->')
304297
return index < 0 ? result : result.slice(0, index)
305298
}
306299

@@ -333,14 +326,12 @@ function allow(_, value) {
333326
* @returns {Array.<string|number>}
334327
*/
335328
function handlePropertyValues(schema, values, prop, definition) {
336-
var index = -1
329+
let index = -1
337330
/** @type {Array.<string|number>} */
338-
var result = []
339-
/** @type {PropertyValue} */
340-
var value
331+
const result = []
341332

342333
while (++index < values.length) {
343-
value = handlePropertyValue(schema, values[index], prop, definition)
334+
const value = handlePropertyValue(schema, values[index], prop, definition)
344335

345336
if (value !== undefined && value !== null) {
346337
// @ts-ignore Assume no booleans were in arrays.
@@ -381,15 +372,15 @@ function handlePropertyValue(schema, value, prop, definition) {
381372
* @returns {boolean}
382373
*/
383374
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)
390381
? schema.protocols[prop].concat()
391382
: []
392-
var index = -1
383+
let index = -1
393384

394385
if (
395386
protocols.length === 0 ||
@@ -422,13 +413,11 @@ function safeProtocol(schema, value, prop) {
422413
*/
423414
function toPropertyValueMap(values) {
424415
/** @type {AttributeMap} */
425-
var result = {}
426-
var index = -1
427-
/** @type {AttributeValue} */
428-
var value
416+
const result = {}
417+
let index = -1
429418

430419
while (++index < values.length) {
431-
value = values[index]
420+
const value = values[index]
432421

433422
if (Array.isArray(value)) {
434423
result[value[0]] = value.slice(1)

package.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,7 @@
7171
"trailingComma": "none"
7272
},
7373
"xo": {
74-
"prettier": true,
75-
"rules": {
76-
"unicorn/no-array-for-each": "off",
77-
"no-var": "off",
78-
"prefer-arrow-callback": "off"
79-
}
74+
"prettier": true
8075
},
8176
"remarkConfig": {
8277
"plugins": [

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {h} from 'hastscript'
2929
import {sanitize} from 'hast-util-sanitize'
3030
import {toHtml} from 'hast-util-to-html'
3131

32-
var tree = h('div', {onmouseover: 'alert("alpha")'}, [
32+
const tree = h('div', {onmouseover: 'alert("alpha")'}, [
3333
h(
3434
'a',
3535
{href: 'jAva script:alert("bravo")', onclick: 'alert("charlie")'},
@@ -45,8 +45,8 @@ var tree = h('div', {onmouseover: 'alert("alpha")'}, [
4545
h('math', h('mi', {'xlink:href': 'data:x,<script>alert("foxtrot")</script>'}))
4646
])
4747

48-
var unsanitized = toHtml(tree)
49-
var sanitized = toHtml(sanitize(tree))
48+
const unsanitized = toHtml(tree)
49+
const sanitized = toHtml(sanitize(tree))
5050

5151
console.log(unsanitized)
5252
console.log(sanitized)

0 commit comments

Comments
 (0)