|
1 |
| -import {one} from './one.js' |
| 1 | +/** |
| 2 | + * @typedef {import('xast').Root} Root |
| 3 | + * @typedef {import('xast').Element} Element |
| 4 | + * @typedef {import('xast').Cdata} Cdata |
| 5 | + * @typedef {import('xast').Comment} Comment |
| 6 | + * @typedef {import('xast').Doctype} Doctype |
| 7 | + * @typedef {import('xast').Instruction} Instruction |
| 8 | + * @typedef {import('xast').Text} Text |
| 9 | + * @typedef {import('xast').Literal & {type: 'raw'}} Raw |
| 10 | + * @typedef {Root|Element} Parent |
| 11 | + * @typedef {import('xast').Attributes} Attributes |
| 12 | + * @typedef {Root['children'][number]} Child |
| 13 | + * @typedef {Child|Root} Node |
| 14 | + * |
| 15 | + * @typedef {'"'|"'"} Quote |
| 16 | + * |
| 17 | + * @typedef Options |
| 18 | + * @property {Quote} [quote='"'] Preferred quote to use |
| 19 | + * @property {boolean} [quoteSmart=false] Use the other quote if that results in |
| 20 | + * less bytes |
| 21 | + * @property {boolean} [closeEmptyElements=false] Close elements without any |
| 22 | + * content with slash (/) on the opening tag instead of an end tag: |
| 23 | + * `<circle />` instead of `<circle></circle>`. |
| 24 | + * See `tightClose` to control whether a space is used before the slash. |
| 25 | + * @property {boolean} [tightClose=false] Do not use an extra space when closing |
| 26 | + * self-closing elements: `<circle/>` instead of `<circle />`. |
| 27 | + * @property {boolean} [allowDangerousXml=false] Allow `raw` nodes and insert |
| 28 | + * them as raw XML. When falsey, encodes `raw` nodes. |
| 29 | + * Only set this if you completely trust the content! |
| 30 | + * |
| 31 | + * @typedef Context |
| 32 | + * @property {Quote} quote |
| 33 | + * @property {Quote} alternative |
| 34 | + * @property {boolean} close |
| 35 | + * @property {boolean} tight |
| 36 | + * @property {boolean} dangerous |
| 37 | + * |
| 38 | + * @callback Handle |
| 39 | + * @param {Node} node |
| 40 | + * @param {Context} context |
| 41 | + * @returns {string} |
| 42 | + */ |
2 | 43 |
|
3 |
| -var quotationMark = '"' |
4 |
| -var apostrophe = "'" |
| 44 | +import {one} from './one.js' |
5 | 45 |
|
6 |
| -// Serialize the given node. |
7 |
| -export function toXml(node, options) { |
8 |
| - var settings = options || {} |
9 |
| - var quote = settings.quote || quotationMark |
10 |
| - var alternative = quote === quotationMark ? apostrophe : quotationMark |
11 |
| - var smart = settings.quoteSmart |
12 |
| - var value = |
13 |
| - node && typeof node === 'object' && 'length' in node |
14 |
| - ? {type: 'root', children: node} |
15 |
| - : node |
| 46 | +/** |
| 47 | + * Serialize the given xast tree (or list of nodes). |
| 48 | + * |
| 49 | + * @param {Node|Array.<Node>} node |
| 50 | + * @param {Options} [options] |
| 51 | + * @returns {string} |
| 52 | + */ |
| 53 | +export function toXml(node, options = {}) { |
| 54 | + var quote = options.quote || '"' |
| 55 | + /** @type {Quote} */ |
| 56 | + var alternative = quote === '"' ? "'" : '"' |
| 57 | + var smart = options.quoteSmart |
| 58 | + /** @type {Node} */ |
| 59 | + // @ts-ignore Assume no `root` in `node`. |
| 60 | + var value = Array.isArray(node) ? {type: 'root', children: node} : node |
16 | 61 |
|
17 |
| - if (quote !== quotationMark && quote !== apostrophe) { |
18 |
| - throw new Error( |
19 |
| - 'Invalid quote `' + |
20 |
| - quote + |
21 |
| - '`, expected `' + |
22 |
| - apostrophe + |
23 |
| - '` or `' + |
24 |
| - quotationMark + |
25 |
| - '`' |
26 |
| - ) |
| 62 | + if (quote !== '"' && quote !== "'") { |
| 63 | + throw new Error('Invalid quote `' + quote + '`, expected `\'` or `"`') |
27 | 64 | }
|
28 | 65 |
|
29 | 66 | return one(value, {
|
30 |
| - dangerous: settings.allowDangerousXml, |
31 |
| - close: settings.closeEmptyElements, |
32 |
| - tight: settings.tightClose, |
| 67 | + dangerous: options.allowDangerousXml, |
| 68 | + close: options.closeEmptyElements, |
| 69 | + tight: options.tightClose, |
33 | 70 | quote,
|
34 | 71 | alternative: smart ? alternative : null
|
35 | 72 | })
|
|
0 commit comments