Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes allowing processing instructions to be read and written #18

Merged
merged 3 commits into from
May 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions lib/js2xml.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
Expand All @@ -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)) {
Expand All @@ -41,28 +42,30 @@ function writeAttributes (attributes) {
}
return result;
}

function writeDeclaration (declaration, options) {
return '<?xml' + writeAttributes(declaration[options.attributesKey]) + '?>';
function writeProcessingInstruction(name, declaration, options) {
return '<?' + (name || declaration[options.nameKey]) + writeAttributes(declaration[options.attributesKey]) + '?>';
}
function writeDeclaration(declaration, options) {
return writeProcessingInstruction('xml', declaration, options);
}

function writeComment (comment, options) {
function writeComment(comment, options) {
return options.ignoreComment ? '' : '<!--' + comment + '-->';
}

function writeCdata (cdata, options) {
function writeCdata(cdata, options) {
return options.ignoreCdata ? '' : '<![CDATA[' + cdata + ']]>';
}

function writeDoctype (doctype, options) {
function writeDoctype(doctype, options) {
return options.ignoreDoctype ? '' : '<!DOCTYPE ' + doctype + '>';
}

function writeText (text, options) {
function writeText(text, options) {
return options.ignoreText ? '' : text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
}

function hasContent (element, options) {
function hasContent(element, options) {
var i;
if (element.elements && element.elements.length) {
for (i = 0; i < element.elements.length; ++i) {
Expand All @@ -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]) {
Expand All @@ -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) {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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;
Expand All @@ -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)) {
Expand Down Expand Up @@ -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);
Expand Down
46 changes: 28 additions & 18 deletions lib/xml2js.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
Expand All @@ -34,7 +35,7 @@ function validateOptions (userOptions) {
return options;
}

function nativeType (value) {
function nativeType(value) {
var nValue = Number(value);
if (!isNaN(nValue)) {
return nValue;
Expand All @@ -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']] = [];
Expand All @@ -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;
Expand Down Expand Up @@ -153,7 +163,7 @@ function onStartElement (name, attributes) {
currentElement = element;
}

function onText (text) {
function onText(text) {
if (options.ignoreText) {
return;
}
Expand All @@ -172,7 +182,7 @@ function onText (text) {
addField('text', text, options);
}

function onComment (comment) {
function onComment(comment) {
if (options.ignoreComment) {
return;
}
Expand All @@ -185,15 +195,15 @@ 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];
}
currentElement = parentElement;
}

function onCdata (cdata) {
function onCdata(cdata) {
if (options.ignoreCdata) {
return;
}
Expand All @@ -203,7 +213,7 @@ function onCdata (cdata) {
addField('cdata', cdata, options);
}

function onDoctype (doctype) {
function onDoctype(doctype) {
if (options.ignoreDoctype) {
return;
}
Expand All @@ -214,7 +224,7 @@ function onDoctype (doctype) {
addField('doctype', doctype, options);
}

function onError (error) {
function onError(error) {
error.note = error; //console.error(error);
}

Expand Down