From 4f3d618c8606a7eb39c35fbd1cf9939ca2bf5b7a Mon Sep 17 00:00:00 2001 From: Arlen22 Date: Thu, 18 May 2017 14:27:19 -0400 Subject: [PATCH 1/3] Update xml2js.js --- lib/xml2js.js | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/lib/xml2js.js b/lib/xml2js.js index e024924..e007611 100644 --- a/lib/xml2js.js +++ b/lib/xml2js.js @@ -1,12 +1,12 @@ var sax = require('sax'); -var expat /*= require('node-expat');*/ = {on: function () {}, parse: function () {}}; +var expat /*= require('node-expat');*/ = { on: function () { }, parse: function () { } }; var common = require('./common'); var options; var pureJsParser = 1; //true; var currentElement; -function validateOptions (userOptions) { +function validateOptions(userOptions) { options = common.copyOptions(userOptions); common.ensureFlagExists('ignoreDeclaration', options); common.ensureFlagExists('ignoreAttributes', options); @@ -22,6 +22,7 @@ function validateOptions (userOptions) { common.ensureFlagExists('nativeType', options); common.ensureFlagExists('sanitize', options); common.ensureKeyExists('declaration', options); + common.ensureKeyExists('processing', options); common.ensureKeyExists('attributes', options); common.ensureKeyExists('text', options); common.ensureKeyExists('comment', options); @@ -34,7 +35,7 @@ function validateOptions (userOptions) { return options; } -function nativeType (value) { +function nativeType(value) { var nValue = Number(value); if (!isNaN(nValue)) { return nValue; @@ -48,7 +49,7 @@ function nativeType (value) { return value; } -function addField (type, value, options) { +function addField(type, value, options) { if (options.compact) { if (!currentElement[options[type + 'Key']] && options.alwaysArray) { currentElement[options[type + 'Key']] = []; @@ -75,31 +76,40 @@ function addField (type, value, options) { } } -function onDeclaration (declaration) { +function onDeclaration(declaration) { if (options.ignoreDeclaration) { return; } - if (currentElement[options.declarationKey]) { - return; + var key = options.declarationKey; + var type = "decl"; + if (currentElement[key]) { + key = options.processingKey; + currentElement[key] = []; + type = "proc"; + } else { + currentElement[key] = {}; } - currentElement[options.declarationKey] = {}; + var item = {}; + item[options.nameKey] = declaration.name; while (declaration.body) { var attribute = declaration.body.match(/([\w:-]+)\s*=\s*"([^"]*)"|'([^']*)'|(\w+)\s*/); if (!attribute) { break; } - if (!currentElement[options.declarationKey][options.attributesKey]) { - currentElement[options.declarationKey][options.attributesKey] = {}; + if (!item[options.attributesKey]) { + item[options.attributesKey] = {}; } - currentElement[options.declarationKey][options.attributesKey][attribute[1]] = attribute[2]; + item[options.attributesKey][attribute[1]] = attribute[2]; declaration.body = declaration.body.slice(attribute[0].length); // advance the string } + if (type === "proc") currentElement[key].push(item); + else currentElement[key] = item; if (options.addParent) { currentElement[options.declarationKey][options.parentKey] = currentElement; } } -function onStartElement (name, attributes) { +function onStartElement(name, attributes) { var key, element; if (typeof name === 'object') { attributes = name.attributes; @@ -153,7 +163,7 @@ function onStartElement (name, attributes) { currentElement = element; } -function onText (text) { +function onText(text) { if (options.ignoreText) { return; } @@ -172,7 +182,7 @@ function onText (text) { addField('text', text, options); } -function onComment (comment) { +function onComment(comment) { if (options.ignoreComment) { return; } @@ -185,7 +195,7 @@ function onComment (comment) { addField('comment', comment, options); } -function onEndElement (name) { +function onEndElement(name) { var parentElement = currentElement[options.parentKey]; if (!options.addParent) { delete currentElement[options.parentKey]; @@ -193,7 +203,7 @@ function onEndElement (name) { currentElement = parentElement; } -function onCdata (cdata) { +function onCdata(cdata) { if (options.ignoreCdata) { return; } @@ -203,7 +213,7 @@ function onCdata (cdata) { addField('cdata', cdata, options); } -function onDoctype (doctype) { +function onDoctype(doctype) { if (options.ignoreDoctype) { return; } @@ -214,7 +224,7 @@ function onDoctype (doctype) { addField('doctype', doctype, options); } -function onError (error) { +function onError(error) { error.note = error; //console.error(error); } From 4b545e0449ca9079b65db9cce5b6639559c531f9 Mon Sep 17 00:00:00 2001 From: Arlen22 Date: Thu, 18 May 2017 14:29:01 -0400 Subject: [PATCH 2/3] Update js2xml.js --- lib/js2xml.js | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/lib/js2xml.js b/lib/js2xml.js index 39ea4cc..429af68 100644 --- a/lib/js2xml.js +++ b/lib/js2xml.js @@ -1,6 +1,6 @@ var common = require('./common'); -function validateOptions (userOptions) { +function validateOptions(userOptions) { var options = common.copyOptions(userOptions); common.ensureFlagExists('ignoreDeclaration', options); common.ensureFlagExists('ignoreAttributes', options); @@ -17,6 +17,7 @@ function validateOptions (userOptions) { options.spaces = Array(options.spaces + 1).join(' '); } common.ensureKeyExists('declaration', options); + common.ensureKeyExists('processing', options); common.ensureKeyExists('attributes', options); common.ensureKeyExists('text', options); common.ensureKeyExists('comment', options); @@ -28,11 +29,11 @@ function validateOptions (userOptions) { return options; } -function writeIndentation (options, depth, firstLine) { +function writeIndentation(options, depth, firstLine) { return (!firstLine && options.spaces ? '\n' : '') + Array(depth + 1).join(options.spaces); } -function writeAttributes (attributes) { +function writeAttributes(attributes) { var key, result = ''; for (key in attributes) { if (attributes.hasOwnProperty(key)) { @@ -41,28 +42,30 @@ function writeAttributes (attributes) { } return result; } - -function writeDeclaration (declaration, options) { - return ''; +function writeProcessingInstruction(name, declaration, options) { + return ''; } +// function writeDeclaration(declaration, options) { +// return ''; +// } -function writeComment (comment, options) { +function writeComment(comment, options) { return options.ignoreComment ? '' : ''; } -function writeCdata (cdata, options) { +function writeCdata(cdata, options) { return options.ignoreCdata ? '' : ''; } -function writeDoctype (doctype, options) { +function writeDoctype(doctype, options) { return options.ignoreDoctype ? '' : ''; } -function writeText (text, options) { +function writeText(text, options) { return options.ignoreText ? '' : text.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """).replace(/'/g, "'"); } -function hasContent (element, options) { +function hasContent(element, options) { var i; if (element.elements && element.elements.length) { for (i = 0; i < element.elements.length; ++i) { @@ -89,7 +92,7 @@ function hasContent (element, options) { return false; } -function writeElement (element, options, depth) { +function writeElement(element, options, depth) { var xml = ''; xml += '<' + element.name; if (element[options.attributesKey]) { @@ -108,7 +111,7 @@ function writeElement (element, options, depth) { return xml; } -function writeElements (elements, options, depth, firstLine) { +function writeElements(elements, options, depth, firstLine) { return elements.reduce(function (xml, element) { var indent = writeIndentation(options, depth, firstLine && !xml); switch (element.type) { @@ -121,7 +124,7 @@ function writeElements (elements, options, depth, firstLine) { }, ''); } -function hasContentCompact (element, options, anyContent) { +function hasContentCompact(element, options, anyContent) { var key; for (key in element) { if (element.hasOwnProperty(key)) { @@ -151,7 +154,7 @@ function hasContentCompact (element, options, anyContent) { return false; } -function writeElementCompact (element, name, options, depth, indent) { +function writeElementCompact(element, name, options, depth, indent) { var xml = ''; if (name) { xml += '<' + name; @@ -172,7 +175,7 @@ function writeElementCompact (element, name, options, depth, indent) { return xml; } -function writeElementsCompact (element, options, depth, firstLine) { +function writeElementsCompact(element, options, depth, firstLine) { var i, key, nodes, xml = ''; for (key in element) { if (element.hasOwnProperty(key)) { @@ -202,7 +205,12 @@ module.exports = function (js, options) { xml = writeElementsCompact(js, options, 0, true); } else { if (js[options.declarationKey]) { - xml += writeDeclaration(js[options.declarationKey], options); + xml += writeProcessingInstruction('xml', js[options.declarationKey], options); + } + if (js[options.processingKey]) { + js[options.processingKey].forEach(e => { + xml += writeProcessingInstruction(e.name, e, options); + }) } if (js[options.elementsKey] && js[options.elementsKey].length) { xml += writeElements(js[options.elementsKey], options, 0, !xml); From e25ffb641a37f99d13501572a10f4224b8745838 Mon Sep 17 00:00:00 2001 From: Arlen22 Date: Thu, 18 May 2017 14:34:35 -0400 Subject: [PATCH 3/3] Add modified writeDeclaration back in --- lib/js2xml.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/js2xml.js b/lib/js2xml.js index 429af68..02908bc 100644 --- a/lib/js2xml.js +++ b/lib/js2xml.js @@ -45,9 +45,9 @@ function writeAttributes(attributes) { function writeProcessingInstruction(name, declaration, options) { return ''; } -// function writeDeclaration(declaration, options) { -// return ''; -// } +function writeDeclaration(declaration, options) { + return writeProcessingInstruction('xml', declaration, options); +} function writeComment(comment, options) { return options.ignoreComment ? '' : '';