From 7b205fe6e9fc28b664ea2402931a15f0f8b2a9b1 Mon Sep 17 00:00:00 2001 From: Pieter Sheth-Voss Date: Wed, 13 Jun 2018 23:48:21 -0400 Subject: [PATCH] Add script to compile, add roundtrip test --- lib/bom.js | 2 +- lib/processors.js | 2 +- lib/xml2js.js | 29 ++++++++++++++++++++++++----- package.json | 1 + test/roundtrip.coffee | 38 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 test/roundtrip.coffee diff --git a/lib/bom.js b/lib/bom.js index 0f6be316..210b7128 100644 --- a/lib/bom.js +++ b/lib/bom.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.10.0 +// Generated by CoffeeScript 1.12.7 (function() { "use strict"; var xml2js; diff --git a/lib/processors.js b/lib/processors.js index 31ccde28..89aa85f2 100644 --- a/lib/processors.js +++ b/lib/processors.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.10.0 +// Generated by CoffeeScript 1.12.7 (function() { "use strict"; var prefixMatch; diff --git a/lib/xml2js.js b/lib/xml2js.js index f9b403ba..df7b4bc1 100644 --- a/lib/xml2js.js +++ b/lib/xml2js.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.10.0 +// Generated by CoffeeScript 1.12.7 (function() { "use strict"; var bom, builder, escapeCDATA, events, isEmpty, processName, processors, requiresCDATA, sax, setImmediate, wrapCDATA, @@ -152,17 +152,36 @@ } render = (function(_this) { return function(element, obj) { - var attr, child, entry, index, key, value; + var attr, child, entry, index, key, ref, value; if (typeof obj !== 'object') { if (_this.options.cdata && requiresCDATA(obj)) { element.raw(wrapCDATA(obj)); } else { element.txt(obj); } + } else if ((_this.options.preserveChildrenOrder != null) && ((obj != null ? obj[_this.options.childkey] : void 0) != null)) { + ref = obj[_this.options.childkey]; + for (index in ref) { + if (!hasProp.call(ref, index)) continue; + entry = ref[index]; + key = entry["#name"]; + if (typeof entry === 'string') { + if (_this.options.cdata && requiresCDATA(entry)) { + element = element.ele(key).raw(wrapCDATA(entry)).up(); + } else { + element = element.ele(key, entry).up(); + } + } else { + element = render(element.ele(key), entry).up(); + } + } } else { for (key in obj) { if (!hasProp.call(obj, key)) continue; child = obj[key]; + if ((_this.options.preserveChildrenOrder != null) && key === "#name") { + continue; + } if (key === attrkey) { if (typeof child === "object") { for (attr in child) { @@ -254,7 +273,7 @@ } Parser.prototype.processAsync = function() { - var chunk, err, error1; + var chunk, err; try { if (this.remaining.length <= this.options.chunkSize) { chunk = this.remaining; @@ -356,7 +375,7 @@ })(this); this.saxParser.onclosetag = (function(_this) { return function() { - var cdata, emptyStr, err, error1, key, node, nodeName, obj, objClone, old, s, xpath; + var cdata, emptyStr, err, key, node, nodeName, obj, objClone, old, s, xpath; obj = stack.pop(); nodeName = obj["#name"]; if (!_this.options.explicitChildren || !_this.options.preserveChildrenOrder) { @@ -476,7 +495,7 @@ }; Parser.prototype.parseString = function(str, cb) { - var err, error1; + var err; if ((cb != null) && typeof cb === "function") { this.on("end", function(result) { this.reset(); diff --git a/package.json b/package.json index 0adc52cd..bdaa3f3d 100644 --- a/package.json +++ b/package.json @@ -59,6 +59,7 @@ "lib": "./lib" }, "scripts": { + "compile": "coffee --compile --output lib/ src", "test": "zap", "coverage": "nyc npm test && nyc report", "coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls" diff --git a/test/roundtrip.coffee b/test/roundtrip.coffee new file mode 100644 index 00000000..95a53e1b --- /dev/null +++ b/test/roundtrip.coffee @@ -0,0 +1,38 @@ +xml2js = require '../lib/xml2js' +assert = require 'assert' +equ = assert.equal + +module.exports = + 'xml to js to xml': (test) -> + + xml = """ + + milk + celery + eggs + apples + butter + eggplant + + """ + console.log xml + + options = + explicitChildren: true + preserveChildrenOrder:true + headless: true + renderOpts: + pretty: true + + xml2js.parseString xml, options, (err, js) -> + if err + throw err + else + console.log js + builder = new xml2js.Builder options + xml2 = builder.buildObject js + console.log xml2 + + # compare ignore whitespace + equ (xml.replace /\s+/g, " "), (xml2.replace /\s+/g, " ") + test.done()