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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | 1x 1x 1x 1x 36x 1x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 1x 87x 87x 87x 87x 208x 207x 1x 1x 1x 87x 87x 1x 258x 258x 258x 258x 1x 1x 1x 1x 207x 156x 156x 51x 51x 51x 51x 51x 51x 51x 51x 1x 207x 207x 244x 244x 207x 1x | import { Attr, JsApi } from './jsapi'; import { EOL } from 'os'; const entities: { [key: string]: string } = { '&': '&', "'": ''', '"': '"', '>': '>', '<': '<', }; const defaults = { doctypeStart: '<!DOCTYPE', doctypeEnd: '>', procInstStart: '<?', procInstEnd: '?>', tagOpenStart: '<', tagOpenEnd: '>', tagCloseStart: '</', tagCloseEnd: '>', tagShortStart: '<', tagShortEnd: '/>', attrStart: '="', attrEnd: '"', commentStart: '<!--', commentEnd: '-->', cdataStart: '<![CDATA[', cdataEnd: ']]>', textStart: '', textEnd: '', indentSize: 4, regEntities: /[&'"<>]/g, regValEntities: /[&"<>]/g, encodeEntity: (char: string) => entities[char], pretty: false, useShortTags: true, }; // TODO: make the indentation size configurable? export interface Options { pretty?: boolean; } /** * Convert XML-as-JS object to XML string. * @param {Object} data input data * @param {Object} options options * @return {Object} output data */ export function js2xml(data: JsApi, options?: Options) { return new Js2Xml(options).convert(data); } class Js2Xml { private readonly options: Options & typeof defaults; private indentLevel = 0; constructor(options?: Options) { Eif (options) { this.options = { ...defaults, ...options }; } else { this.options = defaults; } Eif (this.options.pretty) { this.options.doctypeEnd += EOL; this.options.procInstEnd += EOL; this.options.commentEnd += EOL; this.options.cdataEnd += EOL; this.options.tagShortEnd += EOL; this.options.tagOpenEnd += EOL; this.options.tagCloseEnd += EOL; this.options.textEnd += EOL; } } /** * Start conversion. * @param {Object} data input data * @return {String} */ convert(data: JsApi) { let xml = ''; Eif (data.content) { this.indentLevel++; data.content.forEach(function(this: Js2Xml, item) { if (item.elem) { xml += this.createElem(item); } else Iif (item.processingInstruction) { xml += this.createProcInst(item.processingInstruction); } else Eif (item.comment) { xml += this.createComment(item.comment.text); } }, this); } this.indentLevel--; return xml; } /** * Create indent string in accordance with the current node level. * * @return {String} */ private createIndent() { let indent = ''; Eif (this.options.pretty) { // TODO: make the indentation size configurable? indent = ' '.repeat(this.indentLevel - 1); } return indent; } /** * Create comment tag. * @param {String} comment comment body * @return {String} */ private createComment(comment: string) { return this.options.commentStart + comment + this.options.commentEnd; } /** * Create XML Processing Instruction tag. * @param {Object} instruction instruction object * @return {String} */ private createProcInst(instruction: { name: string; body: string }) { return ( this.options.procInstStart + instruction.name + ' ' + instruction.body + this.options.procInstEnd ); } /** * Create element tag. * @param {Object} data element object * @return {String} */ private createElem(data: JsApi) { // Empty element and short tag. if (data.isEmpty()) { Eif (this.options.useShortTags) { return ( this.createIndent() + this.options.tagShortStart + data.elem + this.createAttrs(data) + this.options.tagShortEnd ); } else { return ( this.createIndent() + this.options.tagShortStart + data.elem + this.createAttrs(data) + this.options.tagOpenEnd + this.options.tagCloseStart + data.elem + this.options.tagCloseEnd ); } } else { // Non-empty element. const tagOpenStart = this.options.tagOpenStart; const tagOpenEnd = this.options.tagOpenEnd; const tagCloseStart = this.options.tagCloseStart; const tagCloseEnd = this.options.tagCloseEnd; const openIndent = this.createIndent(); const dataEnd = ''; const processedData = '' + this.convert(data); return ( openIndent + tagOpenStart + data.elem + this.createAttrs(data) + tagOpenEnd + processedData + dataEnd + this.createIndent() + tagCloseStart + data.elem + tagCloseEnd ); } } /** * Create element attributes. * @param {Object} elem attributes object * @return {String} */ private createAttrs(elem: JsApi) { let attrs = ''; elem.eachAttr(function(this: Js2Xml, attr: Attr) { Eif (attr.value !== undefined) { attrs += ' ' + attr.name + this.options.attrStart + String(attr.value).replace( this.options.regValEntities, this.options.encodeEntity, ) + this.options.attrEnd; } else { attrs += ' ' + attr.name; } }, this); return attrs; } } |