-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
58 lines (53 loc) · 1.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* @typedef {import('./types.js').Node} Node
* @typedef {import('./types.js').Options} Options
* @typedef {import('./types.js').Context} Context
* @typedef {import('./types.js').Quote} Quote
*/
import {html, svg} from 'property-information'
import {htmlVoidElements} from 'html-void-elements'
import {omission} from './omission/index.js'
import {one} from './tree.js'
/**
* @param {Node|Array.<Node>} node
* @param {Options} [options]
* @returns {string}
*/
export function toHtml(node, options = {}) {
var quote = options.quote || '"'
/** @type {Quote} */
var alternative = quote === '"' ? "'" : '"'
if (quote !== '"' && quote !== "'") {
throw new Error('Invalid quote `' + quote + '`, expected `\'` or `"`')
}
/** @type {Context} */
var context = {
valid: options.allowParseErrors ? 0 : 1,
safe: options.allowDangerousCharacters ? 0 : 1,
schema: options.space === 'svg' ? svg : html,
omit: options.omitOptionalTags ? omission : undefined,
quote,
alternative,
smart: options.quoteSmart,
unquoted: options.preferUnquoted,
tight: options.tightAttributes,
upperDoctype: options.upperDoctype,
tightDoctype: options.tightDoctype,
bogusComments: options.bogusComments,
tightLists: options.tightCommaSeparatedLists,
tightClose: options.tightSelfClosing,
collapseEmpty: options.collapseEmptyAttributes,
dangerous: options.allowDangerousHtml,
voids: options.voids || htmlVoidElements.concat(),
entities: options.entities || {},
close: options.closeSelfClosing,
closeEmpty: options.closeEmptyElements
}
return one(
context,
// @ts-ignore Assume `node` does not contain a root.
Array.isArray(node) ? {type: 'root', children: node} : node,
null,
null
)
}