|
| 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 | + |
1 | 8 | import {html, svg} from 'property-information'
|
2 | 9 | import {htmlVoidElements} from 'html-void-elements'
|
3 | 10 | import {omission} from './omission/index.js'
|
4 | 11 | import {one} from './one.js'
|
5 | 12 |
|
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} */ |
9 | 21 | var alternative = quote === '"' ? "'" : '"'
|
10 | 22 |
|
11 | 23 | if (quote !== '"' && quote !== "'") {
|
12 | 24 | throw new Error('Invalid quote `' + quote + '`, expected `\'` or `"`')
|
13 | 25 | }
|
14 | 26 |
|
| 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 | + |
15 | 52 | 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 |
41 | 58 | )
|
42 | 59 | }
|
0 commit comments