820 lines
30 KiB
JavaScript
820 lines
30 KiB
JavaScript
module.exports = (function() {
|
|
var __MODS__ = {};
|
|
var __DEFINE__ = function(modId, func, req) { var m = { exports: {}, _tempexports: {} }; __MODS__[modId] = { status: 0, func: func, req: req, m: m }; };
|
|
var __REQUIRE__ = function(modId, source) { if(!__MODS__[modId]) return require(source); if(!__MODS__[modId].status) { var m = __MODS__[modId].m; m._exports = m._tempexports; var desp = Object.getOwnPropertyDescriptor(m, "exports"); if (desp && desp.configurable) Object.defineProperty(m, "exports", { set: function (val) { if(typeof val === "object" && val !== m._exports) { m._exports.__proto__ = val.__proto__; Object.keys(val).forEach(function (k) { m._exports[k] = val[k]; }); } m._tempexports = val }, get: function () { return m._tempexports; } }); __MODS__[modId].status = 1; __MODS__[modId].func(__MODS__[modId].req, m, m.exports); } return __MODS__[modId].m.exports; };
|
|
var __REQUIRE_WILDCARD__ = function(obj) { if(obj && obj.__esModule) { return obj; } else { var newObj = {}; if(obj != null) { for(var k in obj) { if (Object.prototype.hasOwnProperty.call(obj, k)) newObj[k] = obj[k]; } } newObj.default = obj; return newObj; } };
|
|
var __REQUIRE_DEFAULT__ = function(obj) { return obj && obj.__esModule ? obj.default : obj; };
|
|
__DEFINE__(1738455898139, function(require, module, exports) {
|
|
/*jslint node:true */
|
|
|
|
var xml2js = require('./xml2js');
|
|
var xml2json = require('./xml2json');
|
|
var js2xml = require('./js2xml');
|
|
var json2xml = require('./json2xml');
|
|
|
|
module.exports = {
|
|
xml2js: xml2js,
|
|
xml2json: xml2json,
|
|
js2xml: js2xml,
|
|
json2xml: json2xml
|
|
};
|
|
|
|
}, function(modId) {var map = {"./xml2js":1738455898140,"./xml2json":1738455898143,"./js2xml":1738455898144,"./json2xml":1738455898145}; return __REQUIRE__(map[modId], modId); })
|
|
__DEFINE__(1738455898140, function(require, module, exports) {
|
|
var sax = require('sax');
|
|
var expat /*= require('node-expat');*/ = { on: function () { }, parse: function () { } };
|
|
var helper = require('./options-helper');
|
|
var isArray = require('./array-helper').isArray;
|
|
|
|
var options;
|
|
var pureJsParser = true;
|
|
var currentElement;
|
|
|
|
function validateOptions(userOptions) {
|
|
options = helper.copyOptions(userOptions);
|
|
helper.ensureFlagExists('ignoreDeclaration', options);
|
|
helper.ensureFlagExists('ignoreInstruction', options);
|
|
helper.ensureFlagExists('ignoreAttributes', options);
|
|
helper.ensureFlagExists('ignoreText', options);
|
|
helper.ensureFlagExists('ignoreComment', options);
|
|
helper.ensureFlagExists('ignoreCdata', options);
|
|
helper.ensureFlagExists('ignoreDoctype', options);
|
|
helper.ensureFlagExists('compact', options);
|
|
helper.ensureFlagExists('alwaysChildren', options);
|
|
helper.ensureFlagExists('addParent', options);
|
|
helper.ensureFlagExists('trim', options);
|
|
helper.ensureFlagExists('nativeType', options);
|
|
helper.ensureFlagExists('nativeTypeAttributes', options);
|
|
helper.ensureFlagExists('sanitize', options);
|
|
helper.ensureFlagExists('instructionHasAttributes', options);
|
|
helper.ensureFlagExists('captureSpacesBetweenElements', options);
|
|
helper.ensureAlwaysArrayExists(options);
|
|
helper.ensureKeyExists('declaration', options);
|
|
helper.ensureKeyExists('instruction', options);
|
|
helper.ensureKeyExists('attributes', options);
|
|
helper.ensureKeyExists('text', options);
|
|
helper.ensureKeyExists('comment', options);
|
|
helper.ensureKeyExists('cdata', options);
|
|
helper.ensureKeyExists('doctype', options);
|
|
helper.ensureKeyExists('type', options);
|
|
helper.ensureKeyExists('name', options);
|
|
helper.ensureKeyExists('elements', options);
|
|
helper.ensureKeyExists('parent', options);
|
|
helper.checkFnExists('doctype', options);
|
|
helper.checkFnExists('instruction', options);
|
|
helper.checkFnExists('cdata', options);
|
|
helper.checkFnExists('comment', options);
|
|
helper.checkFnExists('text', options);
|
|
helper.checkFnExists('instructionName', options);
|
|
helper.checkFnExists('elementName', options);
|
|
helper.checkFnExists('attributeName', options);
|
|
helper.checkFnExists('attributeValue', options);
|
|
helper.checkFnExists('attributes', options);
|
|
return options;
|
|
}
|
|
|
|
function nativeType(value) {
|
|
var nValue = Number(value);
|
|
if (!isNaN(nValue)) {
|
|
return nValue;
|
|
}
|
|
var bValue = value.toLowerCase();
|
|
if (bValue === 'true') {
|
|
return true;
|
|
} else if (bValue === 'false') {
|
|
return false;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
function addField(type, value) {
|
|
var key;
|
|
if (options.compact) {
|
|
if (
|
|
!currentElement[options[type + 'Key']] &&
|
|
(isArray(options.alwaysArray) ? options.alwaysArray.indexOf(options[type + 'Key']) !== -1 : options.alwaysArray)
|
|
) {
|
|
currentElement[options[type + 'Key']] = [];
|
|
}
|
|
if (currentElement[options[type + 'Key']] && !isArray(currentElement[options[type + 'Key']])) {
|
|
currentElement[options[type + 'Key']] = [currentElement[options[type + 'Key']]];
|
|
}
|
|
if (type + 'Fn' in options && typeof value === 'string') {
|
|
value = options[type + 'Fn'](value, currentElement);
|
|
}
|
|
if (type === 'instruction' && ('instructionFn' in options || 'instructionNameFn' in options)) {
|
|
for (key in value) {
|
|
if (value.hasOwnProperty(key)) {
|
|
if ('instructionFn' in options) {
|
|
value[key] = options.instructionFn(value[key], key, currentElement);
|
|
} else {
|
|
var temp = value[key];
|
|
delete value[key];
|
|
value[options.instructionNameFn(key, temp, currentElement)] = temp;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (isArray(currentElement[options[type + 'Key']])) {
|
|
currentElement[options[type + 'Key']].push(value);
|
|
} else {
|
|
currentElement[options[type + 'Key']] = value;
|
|
}
|
|
} else {
|
|
if (!currentElement[options.elementsKey]) {
|
|
currentElement[options.elementsKey] = [];
|
|
}
|
|
var element = {};
|
|
element[options.typeKey] = type;
|
|
if (type === 'instruction') {
|
|
for (key in value) {
|
|
if (value.hasOwnProperty(key)) {
|
|
break;
|
|
}
|
|
}
|
|
element[options.nameKey] = 'instructionNameFn' in options ? options.instructionNameFn(key, value, currentElement) : key;
|
|
if (options.instructionHasAttributes) {
|
|
element[options.attributesKey] = value[key][options.attributesKey];
|
|
if ('instructionFn' in options) {
|
|
element[options.attributesKey] = options.instructionFn(element[options.attributesKey], key, currentElement);
|
|
}
|
|
} else {
|
|
if ('instructionFn' in options) {
|
|
value[key] = options.instructionFn(value[key], key, currentElement);
|
|
}
|
|
element[options.instructionKey] = value[key];
|
|
}
|
|
} else {
|
|
if (type + 'Fn' in options) {
|
|
value = options[type + 'Fn'](value, currentElement);
|
|
}
|
|
element[options[type + 'Key']] = value;
|
|
}
|
|
if (options.addParent) {
|
|
element[options.parentKey] = currentElement;
|
|
}
|
|
currentElement[options.elementsKey].push(element);
|
|
}
|
|
}
|
|
|
|
function manipulateAttributes(attributes) {
|
|
if ('attributesFn' in options && attributes) {
|
|
attributes = options.attributesFn(attributes, currentElement);
|
|
}
|
|
if ((options.trim || 'attributeValueFn' in options || 'attributeNameFn' in options || options.nativeTypeAttributes) && attributes) {
|
|
var key;
|
|
for (key in attributes) {
|
|
if (attributes.hasOwnProperty(key)) {
|
|
if (options.trim) attributes[key] = attributes[key].trim();
|
|
if (options.nativeTypeAttributes) {
|
|
attributes[key] = nativeType(attributes[key]);
|
|
}
|
|
if ('attributeValueFn' in options) attributes[key] = options.attributeValueFn(attributes[key], key, currentElement);
|
|
if ('attributeNameFn' in options) {
|
|
var temp = attributes[key];
|
|
delete attributes[key];
|
|
attributes[options.attributeNameFn(key, attributes[key], currentElement)] = temp;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return attributes;
|
|
}
|
|
|
|
function onInstruction(instruction) {
|
|
var attributes = {};
|
|
if (instruction.body && (instruction.name.toLowerCase() === 'xml' || options.instructionHasAttributes)) {
|
|
var attrsRegExp = /([\w:-]+)\s*=\s*(?:"([^"]*)"|'([^']*)'|(\w+))\s*/g;
|
|
var match;
|
|
while ((match = attrsRegExp.exec(instruction.body)) !== null) {
|
|
attributes[match[1]] = match[2] || match[3] || match[4];
|
|
}
|
|
attributes = manipulateAttributes(attributes);
|
|
}
|
|
if (instruction.name.toLowerCase() === 'xml') {
|
|
if (options.ignoreDeclaration) {
|
|
return;
|
|
}
|
|
currentElement[options.declarationKey] = {};
|
|
if (Object.keys(attributes).length) {
|
|
currentElement[options.declarationKey][options.attributesKey] = attributes;
|
|
}
|
|
if (options.addParent) {
|
|
currentElement[options.declarationKey][options.parentKey] = currentElement;
|
|
}
|
|
} else {
|
|
if (options.ignoreInstruction) {
|
|
return;
|
|
}
|
|
if (options.trim) {
|
|
instruction.body = instruction.body.trim();
|
|
}
|
|
var value = {};
|
|
if (options.instructionHasAttributes && Object.keys(attributes).length) {
|
|
value[instruction.name] = {};
|
|
value[instruction.name][options.attributesKey] = attributes;
|
|
} else {
|
|
value[instruction.name] = instruction.body;
|
|
}
|
|
addField('instruction', value);
|
|
}
|
|
}
|
|
|
|
function onStartElement(name, attributes) {
|
|
var element;
|
|
if (typeof name === 'object') {
|
|
attributes = name.attributes;
|
|
name = name.name;
|
|
}
|
|
attributes = manipulateAttributes(attributes);
|
|
if ('elementNameFn' in options) {
|
|
name = options.elementNameFn(name, currentElement);
|
|
}
|
|
if (options.compact) {
|
|
element = {};
|
|
if (!options.ignoreAttributes && attributes && Object.keys(attributes).length) {
|
|
element[options.attributesKey] = {};
|
|
var key;
|
|
for (key in attributes) {
|
|
if (attributes.hasOwnProperty(key)) {
|
|
element[options.attributesKey][key] = attributes[key];
|
|
}
|
|
}
|
|
}
|
|
if (
|
|
!(name in currentElement) &&
|
|
(isArray(options.alwaysArray) ? options.alwaysArray.indexOf(name) !== -1 : options.alwaysArray)
|
|
) {
|
|
currentElement[name] = [];
|
|
}
|
|
if (currentElement[name] && !isArray(currentElement[name])) {
|
|
currentElement[name] = [currentElement[name]];
|
|
}
|
|
if (isArray(currentElement[name])) {
|
|
currentElement[name].push(element);
|
|
} else {
|
|
currentElement[name] = element;
|
|
}
|
|
} else {
|
|
if (!currentElement[options.elementsKey]) {
|
|
currentElement[options.elementsKey] = [];
|
|
}
|
|
element = {};
|
|
element[options.typeKey] = 'element';
|
|
element[options.nameKey] = name;
|
|
if (!options.ignoreAttributes && attributes && Object.keys(attributes).length) {
|
|
element[options.attributesKey] = attributes;
|
|
}
|
|
if (options.alwaysChildren) {
|
|
element[options.elementsKey] = [];
|
|
}
|
|
currentElement[options.elementsKey].push(element);
|
|
}
|
|
element[options.parentKey] = currentElement; // will be deleted in onEndElement() if !options.addParent
|
|
currentElement = element;
|
|
}
|
|
|
|
function onText(text) {
|
|
if (options.ignoreText) {
|
|
return;
|
|
}
|
|
if (!text.trim() && !options.captureSpacesBetweenElements) {
|
|
return;
|
|
}
|
|
if (options.trim) {
|
|
text = text.trim();
|
|
}
|
|
if (options.nativeType) {
|
|
text = nativeType(text);
|
|
}
|
|
if (options.sanitize) {
|
|
text = text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
}
|
|
addField('text', text);
|
|
}
|
|
|
|
function onComment(comment) {
|
|
if (options.ignoreComment) {
|
|
return;
|
|
}
|
|
if (options.trim) {
|
|
comment = comment.trim();
|
|
}
|
|
addField('comment', comment);
|
|
}
|
|
|
|
function onEndElement(name) {
|
|
var parentElement = currentElement[options.parentKey];
|
|
if (!options.addParent) {
|
|
delete currentElement[options.parentKey];
|
|
}
|
|
currentElement = parentElement;
|
|
}
|
|
|
|
function onCdata(cdata) {
|
|
if (options.ignoreCdata) {
|
|
return;
|
|
}
|
|
if (options.trim) {
|
|
cdata = cdata.trim();
|
|
}
|
|
addField('cdata', cdata);
|
|
}
|
|
|
|
function onDoctype(doctype) {
|
|
if (options.ignoreDoctype) {
|
|
return;
|
|
}
|
|
doctype = doctype.replace(/^ /, '');
|
|
if (options.trim) {
|
|
doctype = doctype.trim();
|
|
}
|
|
addField('doctype', doctype);
|
|
}
|
|
|
|
function onError(error) {
|
|
error.note = error; //console.error(error);
|
|
}
|
|
|
|
module.exports = function (xml, userOptions) {
|
|
|
|
var parser = pureJsParser ? sax.parser(true, {}) : parser = new expat.Parser('UTF-8');
|
|
var result = {};
|
|
currentElement = result;
|
|
|
|
options = validateOptions(userOptions);
|
|
|
|
if (pureJsParser) {
|
|
parser.opt = {strictEntities: true};
|
|
parser.onopentag = onStartElement;
|
|
parser.ontext = onText;
|
|
parser.oncomment = onComment;
|
|
parser.onclosetag = onEndElement;
|
|
parser.onerror = onError;
|
|
parser.oncdata = onCdata;
|
|
parser.ondoctype = onDoctype;
|
|
parser.onprocessinginstruction = onInstruction;
|
|
} else {
|
|
parser.on('startElement', onStartElement);
|
|
parser.on('text', onText);
|
|
parser.on('comment', onComment);
|
|
parser.on('endElement', onEndElement);
|
|
parser.on('error', onError);
|
|
//parser.on('startCdata', onStartCdata);
|
|
//parser.on('endCdata', onEndCdata);
|
|
//parser.on('entityDecl', onEntityDecl);
|
|
}
|
|
|
|
if (pureJsParser) {
|
|
parser.write(xml).close();
|
|
} else {
|
|
if (!parser.parse(xml)) {
|
|
throw new Error('XML parsing error: ' + parser.getError());
|
|
}
|
|
}
|
|
|
|
if (result[options.elementsKey]) {
|
|
var temp = result[options.elementsKey];
|
|
delete result[options.elementsKey];
|
|
result[options.elementsKey] = temp;
|
|
delete result.text;
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
}, function(modId) { var map = {"./options-helper":1738455898141,"./array-helper":1738455898142}; return __REQUIRE__(map[modId], modId); })
|
|
__DEFINE__(1738455898141, function(require, module, exports) {
|
|
var isArray = require('./array-helper').isArray;
|
|
|
|
module.exports = {
|
|
|
|
copyOptions: function (options) {
|
|
var key, copy = {};
|
|
for (key in options) {
|
|
if (options.hasOwnProperty(key)) {
|
|
copy[key] = options[key];
|
|
}
|
|
}
|
|
return copy;
|
|
},
|
|
|
|
ensureFlagExists: function (item, options) {
|
|
if (!(item in options) || typeof options[item] !== 'boolean') {
|
|
options[item] = false;
|
|
}
|
|
},
|
|
|
|
ensureSpacesExists: function (options) {
|
|
if (!('spaces' in options) || (typeof options.spaces !== 'number' && typeof options.spaces !== 'string')) {
|
|
options.spaces = 0;
|
|
}
|
|
},
|
|
|
|
ensureAlwaysArrayExists: function (options) {
|
|
if (!('alwaysArray' in options) || (typeof options.alwaysArray !== 'boolean' && !isArray(options.alwaysArray))) {
|
|
options.alwaysArray = false;
|
|
}
|
|
},
|
|
|
|
ensureKeyExists: function (key, options) {
|
|
if (!(key + 'Key' in options) || typeof options[key + 'Key'] !== 'string') {
|
|
options[key + 'Key'] = options.compact ? '_' + key : key;
|
|
}
|
|
},
|
|
|
|
checkFnExists: function (key, options) {
|
|
return key + 'Fn' in options;
|
|
}
|
|
|
|
};
|
|
|
|
}, function(modId) { var map = {"./array-helper":1738455898142}; return __REQUIRE__(map[modId], modId); })
|
|
__DEFINE__(1738455898142, function(require, module, exports) {
|
|
module.exports = {
|
|
|
|
isArray: function(value) {
|
|
if (Array.isArray) {
|
|
return Array.isArray(value);
|
|
}
|
|
// fallback for older browsers like IE 8
|
|
return Object.prototype.toString.call( value ) === '[object Array]';
|
|
}
|
|
|
|
};
|
|
|
|
}, function(modId) { var map = {}; return __REQUIRE__(map[modId], modId); })
|
|
__DEFINE__(1738455898143, function(require, module, exports) {
|
|
var helper = require('./options-helper');
|
|
var xml2js = require('./xml2js');
|
|
|
|
function validateOptions (userOptions) {
|
|
var options = helper.copyOptions(userOptions);
|
|
helper.ensureSpacesExists(options);
|
|
return options;
|
|
}
|
|
|
|
module.exports = function(xml, userOptions) {
|
|
var options, js, json, parentKey;
|
|
options = validateOptions(userOptions);
|
|
js = xml2js(xml, options);
|
|
parentKey = 'compact' in options && options.compact ? '_parent' : 'parent';
|
|
// parentKey = ptions.compact ? '_parent' : 'parent'; // consider this
|
|
if ('addParent' in options && options.addParent) {
|
|
json = JSON.stringify(js, function (k, v) { return k === parentKey? '_' : v; }, options.spaces);
|
|
} else {
|
|
json = JSON.stringify(js, null, options.spaces);
|
|
}
|
|
return json.replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');
|
|
};
|
|
|
|
}, function(modId) { var map = {"./options-helper":1738455898141,"./xml2js":1738455898140}; return __REQUIRE__(map[modId], modId); })
|
|
__DEFINE__(1738455898144, function(require, module, exports) {
|
|
var helper = require('./options-helper');
|
|
var isArray = require('./array-helper').isArray;
|
|
|
|
var currentElement, currentElementName;
|
|
|
|
function validateOptions(userOptions) {
|
|
var options = helper.copyOptions(userOptions);
|
|
helper.ensureFlagExists('ignoreDeclaration', options);
|
|
helper.ensureFlagExists('ignoreInstruction', options);
|
|
helper.ensureFlagExists('ignoreAttributes', options);
|
|
helper.ensureFlagExists('ignoreText', options);
|
|
helper.ensureFlagExists('ignoreComment', options);
|
|
helper.ensureFlagExists('ignoreCdata', options);
|
|
helper.ensureFlagExists('ignoreDoctype', options);
|
|
helper.ensureFlagExists('compact', options);
|
|
helper.ensureFlagExists('indentText', options);
|
|
helper.ensureFlagExists('indentCdata', options);
|
|
helper.ensureFlagExists('indentAttributes', options);
|
|
helper.ensureFlagExists('indentInstruction', options);
|
|
helper.ensureFlagExists('fullTagEmptyElement', options);
|
|
helper.ensureFlagExists('noQuotesForNativeAttributes', options);
|
|
helper.ensureSpacesExists(options);
|
|
if (typeof options.spaces === 'number') {
|
|
options.spaces = Array(options.spaces + 1).join(' ');
|
|
}
|
|
helper.ensureKeyExists('declaration', options);
|
|
helper.ensureKeyExists('instruction', options);
|
|
helper.ensureKeyExists('attributes', options);
|
|
helper.ensureKeyExists('text', options);
|
|
helper.ensureKeyExists('comment', options);
|
|
helper.ensureKeyExists('cdata', options);
|
|
helper.ensureKeyExists('doctype', options);
|
|
helper.ensureKeyExists('type', options);
|
|
helper.ensureKeyExists('name', options);
|
|
helper.ensureKeyExists('elements', options);
|
|
helper.checkFnExists('doctype', options);
|
|
helper.checkFnExists('instruction', options);
|
|
helper.checkFnExists('cdata', options);
|
|
helper.checkFnExists('comment', options);
|
|
helper.checkFnExists('text', options);
|
|
helper.checkFnExists('instructionName', options);
|
|
helper.checkFnExists('elementName', options);
|
|
helper.checkFnExists('attributeName', options);
|
|
helper.checkFnExists('attributeValue', options);
|
|
helper.checkFnExists('attributes', options);
|
|
helper.checkFnExists('fullTagEmptyElement', options);
|
|
return options;
|
|
}
|
|
|
|
function writeIndentation(options, depth, firstLine) {
|
|
return (!firstLine && options.spaces ? '\n' : '') + Array(depth + 1).join(options.spaces);
|
|
}
|
|
|
|
function writeAttributes(attributes, options, depth) {
|
|
if (options.ignoreAttributes) {
|
|
return '';
|
|
}
|
|
if ('attributesFn' in options) {
|
|
attributes = options.attributesFn(attributes, currentElementName, currentElement);
|
|
}
|
|
var key, attr, attrName, quote, result = [];
|
|
for (key in attributes) {
|
|
if (attributes.hasOwnProperty(key) && attributes[key] !== null && attributes[key] !== undefined) {
|
|
quote = options.noQuotesForNativeAttributes && typeof attributes[key] !== 'string' ? '' : '"';
|
|
attr = '' + attributes[key]; // ensure number and boolean are converted to String
|
|
attr = attr.replace(/"/g, '"');
|
|
attrName = 'attributeNameFn' in options ? options.attributeNameFn(key, attr, currentElementName, currentElement) : key;
|
|
result.push((options.spaces && options.indentAttributes? writeIndentation(options, depth+1, false) : ' '));
|
|
result.push(attrName + '=' + quote + ('attributeValueFn' in options ? options.attributeValueFn(attr, key, currentElementName, currentElement) : attr) + quote);
|
|
}
|
|
}
|
|
if (attributes && Object.keys(attributes).length && options.spaces && options.indentAttributes) {
|
|
result.push(writeIndentation(options, depth, false));
|
|
}
|
|
return result.join('');
|
|
}
|
|
|
|
function writeDeclaration(declaration, options, depth) {
|
|
currentElement = declaration;
|
|
currentElementName = 'xml';
|
|
return options.ignoreDeclaration ? '' : '<?' + 'xml' + writeAttributes(declaration[options.attributesKey], options, depth) + '?>';
|
|
}
|
|
|
|
function writeInstruction(instruction, options, depth) {
|
|
if (options.ignoreInstruction) {
|
|
return '';
|
|
}
|
|
var key;
|
|
for (key in instruction) {
|
|
if (instruction.hasOwnProperty(key)) {
|
|
break;
|
|
}
|
|
}
|
|
var instructionName = 'instructionNameFn' in options ? options.instructionNameFn(key, instruction[key], currentElementName, currentElement) : key;
|
|
if (typeof instruction[key] === 'object') {
|
|
currentElement = instruction;
|
|
currentElementName = instructionName;
|
|
return '<?' + instructionName + writeAttributes(instruction[key][options.attributesKey], options, depth) + '?>';
|
|
} else {
|
|
var instructionValue = instruction[key] ? instruction[key] : '';
|
|
if ('instructionFn' in options) instructionValue = options.instructionFn(instructionValue, key, currentElementName, currentElement);
|
|
return '<?' + instructionName + (instructionValue ? ' ' + instructionValue : '') + '?>';
|
|
}
|
|
}
|
|
|
|
function writeComment(comment, options) {
|
|
return options.ignoreComment ? '' : '<!--' + ('commentFn' in options ? options.commentFn(comment, currentElementName, currentElement) : comment) + '-->';
|
|
}
|
|
|
|
function writeCdata(cdata, options) {
|
|
return options.ignoreCdata ? '' : '<![CDATA[' + ('cdataFn' in options ? options.cdataFn(cdata, currentElementName, currentElement) : cdata.replace(']]>', ']]]]><![CDATA[>')) + ']]>';
|
|
}
|
|
|
|
function writeDoctype(doctype, options) {
|
|
return options.ignoreDoctype ? '' : '<!DOCTYPE ' + ('doctypeFn' in options ? options.doctypeFn(doctype, currentElementName, currentElement) : doctype) + '>';
|
|
}
|
|
|
|
function writeText(text, options) {
|
|
if (options.ignoreText) return '';
|
|
text = '' + text; // ensure Number and Boolean are converted to String
|
|
text = text.replace(/&/g, '&'); // desanitize to avoid double sanitization
|
|
text = text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
return 'textFn' in options ? options.textFn(text, currentElementName, currentElement) : text;
|
|
}
|
|
|
|
function hasContent(element, options) {
|
|
var i;
|
|
if (element.elements && element.elements.length) {
|
|
for (i = 0; i < element.elements.length; ++i) {
|
|
switch (element.elements[i][options.typeKey]) {
|
|
case 'text':
|
|
if (options.indentText) {
|
|
return true;
|
|
}
|
|
break; // skip to next key
|
|
case 'cdata':
|
|
if (options.indentCdata) {
|
|
return true;
|
|
}
|
|
break; // skip to next key
|
|
case 'instruction':
|
|
if (options.indentInstruction) {
|
|
return true;
|
|
}
|
|
break; // skip to next key
|
|
case 'doctype':
|
|
case 'comment':
|
|
case 'element':
|
|
return true;
|
|
default:
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function writeElement(element, options, depth) {
|
|
currentElement = element;
|
|
currentElementName = element.name;
|
|
var xml = [], elementName = 'elementNameFn' in options ? options.elementNameFn(element.name, element) : element.name;
|
|
xml.push('<' + elementName);
|
|
if (element[options.attributesKey]) {
|
|
xml.push(writeAttributes(element[options.attributesKey], options, depth));
|
|
}
|
|
var withClosingTag = element[options.elementsKey] && element[options.elementsKey].length || element[options.attributesKey] && element[options.attributesKey]['xml:space'] === 'preserve';
|
|
if (!withClosingTag) {
|
|
if ('fullTagEmptyElementFn' in options) {
|
|
withClosingTag = options.fullTagEmptyElementFn(element.name, element);
|
|
} else {
|
|
withClosingTag = options.fullTagEmptyElement;
|
|
}
|
|
}
|
|
if (withClosingTag) {
|
|
xml.push('>');
|
|
if (element[options.elementsKey] && element[options.elementsKey].length) {
|
|
xml.push(writeElements(element[options.elementsKey], options, depth + 1));
|
|
currentElement = element;
|
|
currentElementName = element.name;
|
|
}
|
|
xml.push(options.spaces && hasContent(element, options) ? '\n' + Array(depth + 1).join(options.spaces) : '');
|
|
xml.push('</' + elementName + '>');
|
|
} else {
|
|
xml.push('/>');
|
|
}
|
|
return xml.join('');
|
|
}
|
|
|
|
function writeElements(elements, options, depth, firstLine) {
|
|
return elements.reduce(function (xml, element) {
|
|
var indent = writeIndentation(options, depth, firstLine && !xml);
|
|
switch (element.type) {
|
|
case 'element': return xml + indent + writeElement(element, options, depth);
|
|
case 'comment': return xml + indent + writeComment(element[options.commentKey], options);
|
|
case 'doctype': return xml + indent + writeDoctype(element[options.doctypeKey], options);
|
|
case 'cdata': return xml + (options.indentCdata ? indent : '') + writeCdata(element[options.cdataKey], options);
|
|
case 'text': return xml + (options.indentText ? indent : '') + writeText(element[options.textKey], options);
|
|
case 'instruction':
|
|
var instruction = {};
|
|
instruction[element[options.nameKey]] = element[options.attributesKey] ? element : element[options.instructionKey];
|
|
return xml + (options.indentInstruction ? indent : '') + writeInstruction(instruction, options, depth);
|
|
}
|
|
}, '');
|
|
}
|
|
|
|
function hasContentCompact(element, options, anyContent) {
|
|
var key;
|
|
for (key in element) {
|
|
if (element.hasOwnProperty(key)) {
|
|
switch (key) {
|
|
case options.parentKey:
|
|
case options.attributesKey:
|
|
break; // skip to next key
|
|
case options.textKey:
|
|
if (options.indentText || anyContent) {
|
|
return true;
|
|
}
|
|
break; // skip to next key
|
|
case options.cdataKey:
|
|
if (options.indentCdata || anyContent) {
|
|
return true;
|
|
}
|
|
break; // skip to next key
|
|
case options.instructionKey:
|
|
if (options.indentInstruction || anyContent) {
|
|
return true;
|
|
}
|
|
break; // skip to next key
|
|
case options.doctypeKey:
|
|
case options.commentKey:
|
|
return true;
|
|
default:
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function writeElementCompact(element, name, options, depth, indent) {
|
|
currentElement = element;
|
|
currentElementName = name;
|
|
var elementName = 'elementNameFn' in options ? options.elementNameFn(name, element) : name;
|
|
if (typeof element === 'undefined' || element === null || element === '') {
|
|
return 'fullTagEmptyElementFn' in options && options.fullTagEmptyElementFn(name, element) || options.fullTagEmptyElement ? '<' + elementName + '></' + elementName + '>' : '<' + elementName + '/>';
|
|
}
|
|
var xml = [];
|
|
if (name) {
|
|
xml.push('<' + elementName);
|
|
if (typeof element !== 'object') {
|
|
xml.push('>' + writeText(element,options) + '</' + elementName + '>');
|
|
return xml.join('');
|
|
}
|
|
if (element[options.attributesKey]) {
|
|
xml.push(writeAttributes(element[options.attributesKey], options, depth));
|
|
}
|
|
var withClosingTag = hasContentCompact(element, options, true) || element[options.attributesKey] && element[options.attributesKey]['xml:space'] === 'preserve';
|
|
if (!withClosingTag) {
|
|
if ('fullTagEmptyElementFn' in options) {
|
|
withClosingTag = options.fullTagEmptyElementFn(name, element);
|
|
} else {
|
|
withClosingTag = options.fullTagEmptyElement;
|
|
}
|
|
}
|
|
if (withClosingTag) {
|
|
xml.push('>');
|
|
} else {
|
|
xml.push('/>');
|
|
return xml.join('');
|
|
}
|
|
}
|
|
xml.push(writeElementsCompact(element, options, depth + 1, false));
|
|
currentElement = element;
|
|
currentElementName = name;
|
|
if (name) {
|
|
xml.push((indent ? writeIndentation(options, depth, false) : '') + '</' + elementName + '>');
|
|
}
|
|
return xml.join('');
|
|
}
|
|
|
|
function writeElementsCompact(element, options, depth, firstLine) {
|
|
var i, key, nodes, xml = [];
|
|
for (key in element) {
|
|
if (element.hasOwnProperty(key)) {
|
|
nodes = isArray(element[key]) ? element[key] : [element[key]];
|
|
for (i = 0; i < nodes.length; ++i) {
|
|
switch (key) {
|
|
case options.declarationKey: xml.push(writeDeclaration(nodes[i], options, depth)); break;
|
|
case options.instructionKey: xml.push((options.indentInstruction ? writeIndentation(options, depth, firstLine) : '') + writeInstruction(nodes[i], options, depth)); break;
|
|
case options.attributesKey: case options.parentKey: break; // skip
|
|
case options.textKey: xml.push((options.indentText ? writeIndentation(options, depth, firstLine) : '') + writeText(nodes[i], options)); break;
|
|
case options.cdataKey: xml.push((options.indentCdata ? writeIndentation(options, depth, firstLine) : '') + writeCdata(nodes[i], options)); break;
|
|
case options.doctypeKey: xml.push(writeIndentation(options, depth, firstLine) + writeDoctype(nodes[i], options)); break;
|
|
case options.commentKey: xml.push(writeIndentation(options, depth, firstLine) + writeComment(nodes[i], options)); break;
|
|
default: xml.push(writeIndentation(options, depth, firstLine) + writeElementCompact(nodes[i], key, options, depth, hasContentCompact(nodes[i], options)));
|
|
}
|
|
firstLine = firstLine && !xml.length;
|
|
}
|
|
}
|
|
}
|
|
return xml.join('');
|
|
}
|
|
|
|
module.exports = function (js, options) {
|
|
options = validateOptions(options);
|
|
var xml = [];
|
|
currentElement = js;
|
|
currentElementName = '_root_';
|
|
if (options.compact) {
|
|
xml.push(writeElementsCompact(js, options, 0, true));
|
|
} else {
|
|
if (js[options.declarationKey]) {
|
|
xml.push(writeDeclaration(js[options.declarationKey], options, 0));
|
|
}
|
|
if (js[options.elementsKey] && js[options.elementsKey].length) {
|
|
xml.push(writeElements(js[options.elementsKey], options, 0, !xml.length));
|
|
}
|
|
}
|
|
return xml.join('');
|
|
};
|
|
|
|
}, function(modId) { var map = {"./options-helper":1738455898141,"./array-helper":1738455898142}; return __REQUIRE__(map[modId], modId); })
|
|
__DEFINE__(1738455898145, function(require, module, exports) {
|
|
var js2xml = require('./js2xml.js');
|
|
|
|
module.exports = function (json, options) {
|
|
if (json instanceof Buffer) {
|
|
json = json.toString();
|
|
}
|
|
var js = null;
|
|
if (typeof (json) === 'string') {
|
|
try {
|
|
js = JSON.parse(json);
|
|
} catch (e) {
|
|
throw new Error('The JSON structure is invalid');
|
|
}
|
|
} else {
|
|
js = json;
|
|
}
|
|
return js2xml(js, options);
|
|
};
|
|
|
|
}, function(modId) { var map = {"./js2xml.js":1738455898144}; return __REQUIRE__(map[modId], modId); })
|
|
return __REQUIRE__(1738455898139);
|
|
})()
|
|
//miniprogram-npm-outsideDeps=["sax"]
|
|
//# sourceMappingURL=index.js.map
|