(data, userOptions = {})
| 64 | * @returns {string} |
| 65 | */ |
| 66 | export const stringifySvg = (data, userOptions = {}) => { |
| 67 | /** @type {Options} */ |
| 68 | const config = { ...defaults, ...userOptions }; |
| 69 | const indent = config.indent; |
| 70 | let newIndent = ' '; |
| 71 | if (typeof indent === 'number' && Number.isNaN(indent) === false) { |
| 72 | newIndent = indent < 0 ? '\t' : ' '.repeat(indent); |
| 73 | } else if (typeof indent === 'string') { |
| 74 | newIndent = indent; |
| 75 | } |
| 76 | /** @type {State} */ |
| 77 | const state = { |
| 78 | indent: newIndent, |
| 79 | textContext: null, |
| 80 | indentLevel: 0, |
| 81 | }; |
| 82 | const eol = config.eol === 'crlf' ? '\r\n' : '\n'; |
| 83 | if (config.pretty) { |
| 84 | config.doctypeEnd += eol; |
| 85 | config.procInstEnd += eol; |
| 86 | config.commentEnd += eol; |
| 87 | config.cdataEnd += eol; |
| 88 | config.tagShortEnd += eol; |
| 89 | config.tagOpenEnd += eol; |
| 90 | config.tagCloseEnd += eol; |
| 91 | config.textEnd += eol; |
| 92 | } |
| 93 | let svg = stringifyNode(data, config, state); |
| 94 | if (config.finalNewline && svg.length > 0 && !svg.endsWith('\n')) { |
| 95 | svg += eol; |
| 96 | } |
| 97 | return svg; |
| 98 | }; |
| 99 | |
| 100 | /** |
| 101 | * @param {import('./types.js').XastParent} data |
no test coverage detected