|
18 | 18 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
19 | 19 | */ |
20 | 20 |
|
21 | | -(function() { |
| 21 | +(function () { |
22 | 22 | "use strict"; |
23 | 23 |
|
24 | 24 | var xmlDeclaration = true; |
25 | 25 | var xmlVersion = "1.0"; |
26 | 26 | var xmlEncoding = "UTF-8"; |
27 | 27 | var attributeString = "@"; |
28 | | - var nodeAliasString = "="; |
| 28 | + var aliasString = "="; |
29 | 29 | var valueString = "#"; |
30 | 30 | var prettyPrinting = true; |
31 | 31 | var indentString = "\t"; |
|
37 | 37 | }; |
38 | 38 |
|
39 | 39 | // Initialization |
40 | | - var init = function(root, data, options) { |
| 40 | + var init = function (root, data, options) { |
41 | 41 | // Set option defaults |
42 | 42 | setOptionDefaults(); |
43 | 43 |
|
|
86 | 86 | throw new Error("valueString option must be a string"); |
87 | 87 | } |
88 | 88 | } |
89 | | - if ("nodeAliasString" in options) { |
90 | | - if (typeof options.nodeAliasString === "string") { |
91 | | - nodeAliasString = options.nodeAliasString; |
| 89 | + if ("aliasString" in options) { |
| 90 | + if (typeof options.aliasString === "string") { |
| 91 | + aliasString = options.aliasString; |
92 | 92 | } |
93 | 93 | else { |
94 | | - throw new Error("nodeAliasString option must be a string"); |
| 94 | + throw new Error("aliasString option must be a string"); |
95 | 95 | } |
96 | 96 | } |
97 | 97 | if ("prettyPrinting" in options) { |
|
156 | 156 | }; |
157 | 157 |
|
158 | 158 | // Convert object to XML |
159 | | - var toXML = function(object) { |
| 159 | + var toXML = function (object) { |
160 | 160 | // Initialize arguments, if necessary |
161 | 161 | var xml = arguments[1] || ""; |
162 | 162 | var level = arguments[2] || 0; |
|
172 | 172 | elementName = "_" + property; |
173 | 173 | } |
174 | 174 |
|
175 | | - // Allow to use an alias for element names |
176 | | - if (Object.prototype.toString.call(object[property]) === "[object Object]" && nodeAliasString in object[property]) { |
177 | | - elementName = object[property][nodeAliasString]; |
| 175 | + // Skip alias string property |
| 176 | + if (elementName === aliasString) { |
| 177 | + continue; |
178 | 178 | } |
179 | 179 |
|
180 | | - // Skip alias key |
181 | | - if (elementName === nodeAliasString) { |
182 | | - continue; |
| 180 | + // When alias string property is present, use as alias for element name |
| 181 | + if (Object.prototype.toString.call(object[property]) === "[object Object]" && |
| 182 | + aliasString in object[property]) { |
| 183 | + elementName = object[property][aliasString]; |
183 | 184 | } |
184 | 185 |
|
185 | 186 | // Arrays |
|
192 | 193 | xml = toXML(tempObject, xml, level); |
193 | 194 | } |
194 | 195 | } |
195 | | - |
196 | 196 | // JSON-type objects with properties |
197 | 197 | else if (Object.prototype.toString.call(object[property]) === "[object Object]") { |
198 | 198 | xml += addIndent("<" + elementName, level); |
|
204 | 204 | for (var attribute in object[property][attributeString]) { |
205 | 205 | if (object[property][attributeString].hasOwnProperty(attribute)) { |
206 | 206 | xml += " " + attribute + "=\"" + |
207 | | - toString(object[property][attributeString][attribute], true) + "\""; |
| 207 | + toString(object[property][attributeString][attribute], true) + "\""; |
208 | 208 | } |
209 | 209 | } |
210 | 210 | } |
|
219 | 219 | if (lengthExcludingAttributes === 0) { // Empty object |
220 | 220 | xml += addBreak("/>"); |
221 | 221 | } |
222 | | - else if (lengthExcludingAttributes === 1 && valueString in object[property]) { // Value string only |
223 | | - xml += addBreak(">" + toString(object[property][valueString], false) + "</" + elementName + ">"); |
| 222 | + else if ((lengthExcludingAttributes === 1 || |
| 223 | + (lengthExcludingAttributes === 2 && aliasString in object[property])) && |
| 224 | + valueString in object[property]) { // Value string only |
| 225 | + xml += addBreak(">" + toString(object[property][valueString], false) + "</" + elementName + |
| 226 | + ">"); |
224 | 227 | } |
225 | 228 | else { // Object with properties |
226 | 229 | xml += addBreak(">"); |
227 | 230 |
|
228 | 231 | // Create separate object for each property and pass to this function |
229 | 232 | for (var subProperty in object[property]) { |
230 | | - if (object[property].hasOwnProperty(subProperty) && subProperty !== attributeString && subProperty !== valueString) { |
| 233 | + if (object[property].hasOwnProperty(subProperty) && subProperty !== attributeString && |
| 234 | + subProperty !== valueString) { |
231 | 235 | tempObject = {}; |
232 | 236 | tempObject[subProperty] = object[property][subProperty]; |
233 | 237 |
|
|
266 | 270 | }; |
267 | 271 |
|
268 | 272 | // Add indenting to data for pretty printing |
269 | | - var addIndent = function(data, level) { |
| 273 | + var addIndent = function (data, level) { |
270 | 274 | if (prettyPrinting) { |
271 | 275 |
|
272 | 276 | var indent = ""; |
|
280 | 284 | }; |
281 | 285 |
|
282 | 286 | // Add line break to data for pretty printing |
283 | | - var addBreak = function(data) { |
| 287 | + var addBreak = function (data) { |
284 | 288 | return prettyPrinting ? data + "\n" : data; |
285 | 289 | }; |
286 | 290 |
|
287 | 291 | // Convert anything into a valid XML string representation |
288 | | - var toString = function(data, isAttribute) { |
| 292 | + var toString = function (data, isAttribute) { |
289 | 293 | // Recursive function used to handle nested functions |
290 | | - var functionHelper = function(data) { |
| 294 | + var functionHelper = function (data) { |
291 | 295 | if (Object.prototype.toString.call(data) === "[object Function]") { |
292 | 296 | return functionHelper(data()); |
293 | 297 | } |
|
334 | 338 | }; |
335 | 339 |
|
336 | 340 | // Revert options back to their default settings |
337 | | - var setOptionDefaults = function() { |
| 341 | + var setOptionDefaults = function () { |
338 | 342 | useCDATA = false; |
339 | 343 | convertMap = {}; |
340 | 344 | xmlDeclaration = true; |
341 | 345 | xmlVersion = "1.0"; |
342 | 346 | xmlEncoding = "UTF-8"; |
343 | 347 | attributeString = "@"; |
344 | | - nodeAliasString = "="; |
| 348 | + aliasString = "="; |
345 | 349 | valueString = "#"; |
346 | 350 | prettyPrinting = true; |
347 | 351 | indentString = "\t"; |
|
0 commit comments