Skip to content

Commit 3cc3cb6

Browse files
committed
Add JSDoc based types
1 parent e294d41 commit 3cc3cb6

30 files changed

+489
-368
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.DS_Store
2+
*.d.ts
23
*.log
34
coverage/
45
node_modules/

index.js

+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
/**
2+
* @typedef {import('./lib/types.js').Options} Options
3+
*/
4+
15
export {toHtml} from './lib/index.js'

lib/all.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
/**
2+
* @typedef {import('./types.js').Handle} Handle
3+
* @typedef {import('./types.js').Parent} Parent
4+
*/
15
import {one} from './one.js'
26

3-
// Serialize all children of `parent`.
7+
/**
8+
* Serialize all children of `parent`.
9+
*
10+
* @type {Handle}
11+
* @param {Parent} parent
12+
*/
413
export function all(ctx, parent) {
14+
/** @type {Array.<string>} */
515
var results = []
616
var children = (parent && parent.children) || []
717
var index = -1

lib/comment.js

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
/**
2+
* @typedef {import('./types.js').Handle} Handle
3+
* @typedef {import('./types.js').Comment} Comment
4+
*/
5+
16
import {stringifyEntities} from 'stringify-entities'
27

8+
/**
9+
* @type {Handle}
10+
* @param {Comment} node
11+
*/
312
export function comment(ctx, node) {
413
// See: <https://html.spec.whatwg.org/multipage/syntax.html#comments>
514
return ctx.bogusComments
@@ -11,6 +20,9 @@ export function comment(ctx, node) {
1120
'>'
1221
: '<!--' + node.value.replace(/^>|^->|<!--|-->|--!>|<!-$/g, encode) + '-->'
1322

23+
/**
24+
* @param {string} $0
25+
*/
1426
function encode($0) {
1527
return stringifyEntities(
1628
$0,

lib/doctype.js

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* @typedef {import('./types.js').Handle} Handle
3+
*/
4+
5+
/**
6+
* @type {Handle}
7+
*/
18
export function doctype(ctx) {
29
return (
310
'<!' +

lib/element.js

+35-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/**
2+
* @typedef {import('./types.js').Handle} Handle
3+
* @typedef {import('./types.js').Element} Element
4+
* @typedef {import('./types.js').Context} Context
5+
* @typedef {import('./types.js').Properties} Properties
6+
* @typedef {import('./types.js').PropertyValue} PropertyValue
7+
*/
8+
19
import {svg, find} from 'property-information'
210
import {stringify as spaces} from 'space-separated-tokens'
311
import {stringify as commas} from 'comma-separated-tokens'
@@ -6,17 +14,25 @@ import {ccount} from 'ccount'
614
import {all} from './all.js'
715
import {constants} from './constants.js'
816

17+
/**
18+
* @type {Handle}
19+
* @param {Element} node
20+
*/
921
// eslint-disable-next-line complexity
1022
export function element(ctx, node, index, parent) {
1123
var schema = ctx.schema
12-
var omit = schema.space === 'svg' ? false : ctx.omit
13-
var parts = []
24+
var omit = schema.space === 'svg' ? undefined : ctx.omit
1425
var selfClosing =
1526
schema.space === 'svg'
1627
? ctx.closeEmpty
1728
: ctx.voids.includes(node.tagName.toLowerCase())
29+
/** @type {Array.<string>} */
30+
var parts = []
31+
/** @type {string} */
1832
var attrs
33+
/** @type {string} */
1934
var content
35+
/** @type {string} */
2036
var last
2137

2238
if (schema.space === 'html' && node.tagName === 'svg') {
@@ -66,11 +82,20 @@ export function element(ctx, node, index, parent) {
6682
return parts.join('')
6783
}
6884

85+
/**
86+
* @param {Context} ctx
87+
* @param {Properties} props
88+
* @returns {string}
89+
*/
6990
function serializeAttributes(ctx, props) {
91+
/** @type {Array.<string>} */
7092
var values = []
7193
var index = -1
94+
/** @type {string} */
7295
var key
96+
/** @type {string} */
7397
var value
98+
/** @type {string} */
7499
var last
75100

76101
for (key in props) {
@@ -92,11 +117,19 @@ function serializeAttributes(ctx, props) {
92117
return values.join('')
93118
}
94119

120+
/**
121+
* @param {Context} ctx
122+
* @param {string} key
123+
* @param {PropertyValue} value
124+
* @returns {string}
125+
*/
95126
// eslint-disable-next-line complexity
96127
function serializeAttribute(ctx, key, value) {
97128
var info = find(ctx.schema, key)
98129
var quote = ctx.quote
130+
/** @type {string} */
99131
var result
132+
/** @type {string} */
100133
var name
101134

102135
if (info.overloadedBoolean && (value === info.attribute || value === '')) {

lib/index.js

+45-28
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,59 @@
1+
/**
2+
* @typedef {import('./types.js').Node} Node
3+
* @typedef {import('./types.js').Options} Options
4+
* @typedef {import('./types.js').Context} Context
5+
* @typedef {import('./types.js').Quote} Quote
6+
*/
7+
18
import {html, svg} from 'property-information'
29
import {htmlVoidElements} from 'html-void-elements'
310
import {omission} from './omission/index.js'
411
import {one} from './one.js'
512

6-
export function toHtml(node, options) {
7-
var settings = options || {}
8-
var quote = settings.quote || '"'
13+
/**
14+
* @param {Node|Array.<Node>} node
15+
* @param {Options} [options]
16+
* @returns {string}
17+
*/
18+
export function toHtml(node, options = {}) {
19+
var quote = options.quote || '"'
20+
/** @type {Quote} */
921
var alternative = quote === '"' ? "'" : '"'
1022

1123
if (quote !== '"' && quote !== "'") {
1224
throw new Error('Invalid quote `' + quote + '`, expected `\'` or `"`')
1325
}
1426

27+
/** @type {Context} */
28+
var context = {
29+
valid: options.allowParseErrors ? 0 : 1,
30+
safe: options.allowDangerousCharacters ? 0 : 1,
31+
schema: options.space === 'svg' ? svg : html,
32+
omit: options.omitOptionalTags ? omission : undefined,
33+
quote,
34+
alternative,
35+
smart: options.quoteSmart,
36+
unquoted: options.preferUnquoted,
37+
tight: options.tightAttributes,
38+
upperDoctype: options.upperDoctype,
39+
tightDoctype: options.tightDoctype,
40+
bogusComments: options.bogusComments,
41+
tightLists: options.tightCommaSeparatedLists,
42+
tightClose: options.tightSelfClosing,
43+
collapseEmpty: options.collapseEmptyAttributes,
44+
// @ts-ignore `allowDangerousHTML` is deprecated.
45+
dangerous: options.allowDangerousHtml || options.allowDangerousHTML,
46+
voids: options.voids || htmlVoidElements.concat(),
47+
entities: options.entities || {},
48+
close: options.closeSelfClosing,
49+
closeEmpty: options.closeEmptyElements
50+
}
51+
1552
return one(
16-
{
17-
valid: settings.allowParseErrors ? 0 : 1,
18-
safe: settings.allowDangerousCharacters ? 0 : 1,
19-
schema: settings.space === 'svg' ? svg : html,
20-
omit: settings.omitOptionalTags && omission,
21-
quote,
22-
alternative,
23-
smart: settings.quoteSmart,
24-
unquoted: settings.preferUnquoted,
25-
tight: settings.tightAttributes,
26-
upperDoctype: settings.upperDoctype,
27-
tightDoctype: settings.tightDoctype,
28-
bogusComments: settings.bogusComments,
29-
tightLists: settings.tightCommaSeparatedLists,
30-
tightClose: settings.tightSelfClosing,
31-
collapseEmpty: settings.collapseEmptyAttributes,
32-
dangerous: settings.allowDangerousHtml || settings.allowDangerousHTML,
33-
voids: settings.voids || htmlVoidElements.concat(),
34-
entities: settings.entities || {},
35-
close: settings.closeSelfClosing,
36-
closeEmpty: settings.closeEmptyElements
37-
},
38-
node && typeof node === 'object' && 'length' in node
39-
? {type: 'root', children: node}
40-
: node
53+
context,
54+
// @ts-ignore Assume `node` does not contain a root.
55+
Array.isArray(node) ? {type: 'root', children: node} : node,
56+
null,
57+
null
4158
)
4259
}

0 commit comments

Comments
 (0)