2016-11-16 01:59:39 +01:00
|
|
|
// Generated by CoffeeScript 1.10.0
|
2016-11-03 01:33:53 +01:00
|
|
|
(function() {
|
2016-11-16 01:59:39 +01:00
|
|
|
"use strict";
|
|
|
|
var bom, builder, escapeCDATA, events, isEmpty, processName, processors, requiresCDATA, sax, setImmediate, wrapCDATA,
|
|
|
|
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
|
|
|
hasProp = {}.hasOwnProperty,
|
|
|
|
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
2016-11-03 01:33:53 +01:00
|
|
|
|
|
|
|
sax = require('sax');
|
|
|
|
|
|
|
|
events = require('events');
|
|
|
|
|
|
|
|
builder = require('xmlbuilder');
|
|
|
|
|
|
|
|
bom = require('./bom');
|
|
|
|
|
|
|
|
processors = require('./processors');
|
|
|
|
|
2016-11-16 01:59:39 +01:00
|
|
|
setImmediate = require('timers').setImmediate;
|
|
|
|
|
2016-11-03 01:33:53 +01:00
|
|
|
isEmpty = function(thing) {
|
|
|
|
return typeof thing === "object" && (thing != null) && Object.keys(thing).length === 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
processName = function(processors, processedName) {
|
2016-11-16 01:59:39 +01:00
|
|
|
var i, len, process;
|
|
|
|
for (i = 0, len = processors.length; i < len; i++) {
|
|
|
|
process = processors[i];
|
2016-11-03 01:33:53 +01:00
|
|
|
processedName = process(processedName);
|
|
|
|
}
|
|
|
|
return processedName;
|
|
|
|
};
|
|
|
|
|
2016-11-16 01:59:39 +01:00
|
|
|
requiresCDATA = function(entry) {
|
|
|
|
return entry.indexOf('&') >= 0 || entry.indexOf('>') >= 0 || entry.indexOf('<') >= 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
wrapCDATA = function(entry) {
|
|
|
|
return "<![CDATA[" + (escapeCDATA(entry)) + "]]>";
|
|
|
|
};
|
|
|
|
|
|
|
|
escapeCDATA = function(entry) {
|
|
|
|
return entry.replace(']]>', ']]]]><![CDATA[>');
|
|
|
|
};
|
|
|
|
|
2016-11-03 01:33:53 +01:00
|
|
|
exports.processors = processors;
|
|
|
|
|
|
|
|
exports.defaults = {
|
|
|
|
"0.1": {
|
|
|
|
explicitCharkey: false,
|
|
|
|
trim: true,
|
|
|
|
normalize: true,
|
|
|
|
normalizeTags: false,
|
|
|
|
attrkey: "@",
|
|
|
|
charkey: "#",
|
|
|
|
explicitArray: false,
|
|
|
|
ignoreAttrs: false,
|
|
|
|
mergeAttrs: false,
|
|
|
|
explicitRoot: false,
|
|
|
|
validator: null,
|
|
|
|
xmlns: false,
|
|
|
|
explicitChildren: false,
|
|
|
|
childkey: '@@',
|
|
|
|
charsAsChildren: false,
|
2016-11-16 01:59:39 +01:00
|
|
|
includeWhiteChars: false,
|
2016-11-03 01:33:53 +01:00
|
|
|
async: false,
|
|
|
|
strict: true,
|
|
|
|
attrNameProcessors: null,
|
2016-11-16 01:59:39 +01:00
|
|
|
attrValueProcessors: null,
|
|
|
|
tagNameProcessors: null,
|
|
|
|
valueProcessors: null,
|
|
|
|
emptyTag: ''
|
2016-11-03 01:33:53 +01:00
|
|
|
},
|
|
|
|
"0.2": {
|
|
|
|
explicitCharkey: false,
|
|
|
|
trim: false,
|
|
|
|
normalize: false,
|
|
|
|
normalizeTags: false,
|
|
|
|
attrkey: "$",
|
|
|
|
charkey: "_",
|
|
|
|
explicitArray: true,
|
|
|
|
ignoreAttrs: false,
|
|
|
|
mergeAttrs: false,
|
|
|
|
explicitRoot: true,
|
|
|
|
validator: null,
|
|
|
|
xmlns: false,
|
|
|
|
explicitChildren: false,
|
2016-11-16 01:59:39 +01:00
|
|
|
preserveChildrenOrder: false,
|
2016-11-03 01:33:53 +01:00
|
|
|
childkey: '$$',
|
|
|
|
charsAsChildren: false,
|
2016-11-16 01:59:39 +01:00
|
|
|
includeWhiteChars: false,
|
2016-11-03 01:33:53 +01:00
|
|
|
async: false,
|
|
|
|
strict: true,
|
|
|
|
attrNameProcessors: null,
|
2016-11-16 01:59:39 +01:00
|
|
|
attrValueProcessors: null,
|
2016-11-03 01:33:53 +01:00
|
|
|
tagNameProcessors: null,
|
2016-11-16 01:59:39 +01:00
|
|
|
valueProcessors: null,
|
2016-11-03 01:33:53 +01:00
|
|
|
rootName: 'root',
|
|
|
|
xmldec: {
|
|
|
|
'version': '1.0',
|
|
|
|
'encoding': 'UTF-8',
|
|
|
|
'standalone': true
|
|
|
|
},
|
|
|
|
doctype: null,
|
|
|
|
renderOpts: {
|
|
|
|
'pretty': true,
|
|
|
|
'indent': ' ',
|
|
|
|
'newline': '\n'
|
|
|
|
},
|
2016-11-16 01:59:39 +01:00
|
|
|
headless: false,
|
|
|
|
chunkSize: 10000,
|
|
|
|
emptyTag: '',
|
|
|
|
cdata: false
|
2016-11-03 01:33:53 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-16 01:59:39 +01:00
|
|
|
exports.ValidationError = (function(superClass) {
|
|
|
|
extend(ValidationError, superClass);
|
2016-11-03 01:33:53 +01:00
|
|
|
|
|
|
|
function ValidationError(message) {
|
|
|
|
this.message = message;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ValidationError;
|
|
|
|
|
|
|
|
})(Error);
|
|
|
|
|
|
|
|
exports.Builder = (function() {
|
|
|
|
function Builder(opts) {
|
2016-11-16 01:59:39 +01:00
|
|
|
var key, ref, value;
|
2016-11-03 01:33:53 +01:00
|
|
|
this.options = {};
|
2016-11-16 01:59:39 +01:00
|
|
|
ref = exports.defaults["0.2"];
|
|
|
|
for (key in ref) {
|
|
|
|
if (!hasProp.call(ref, key)) continue;
|
|
|
|
value = ref[key];
|
2016-11-03 01:33:53 +01:00
|
|
|
this.options[key] = value;
|
|
|
|
}
|
|
|
|
for (key in opts) {
|
2016-11-16 01:59:39 +01:00
|
|
|
if (!hasProp.call(opts, key)) continue;
|
2016-11-03 01:33:53 +01:00
|
|
|
value = opts[key];
|
|
|
|
this.options[key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Builder.prototype.buildObject = function(rootObj) {
|
|
|
|
var attrkey, charkey, render, rootElement, rootName;
|
|
|
|
attrkey = this.options.attrkey;
|
|
|
|
charkey = this.options.charkey;
|
|
|
|
if ((Object.keys(rootObj).length === 1) && (this.options.rootName === exports.defaults['0.2'].rootName)) {
|
|
|
|
rootName = Object.keys(rootObj)[0];
|
|
|
|
rootObj = rootObj[rootName];
|
|
|
|
} else {
|
|
|
|
rootName = this.options.rootName;
|
|
|
|
}
|
2016-11-16 01:59:39 +01:00
|
|
|
render = (function(_this) {
|
|
|
|
return function(element, obj) {
|
|
|
|
var attr, child, entry, index, key, value;
|
|
|
|
if (typeof obj !== 'object') {
|
|
|
|
if (_this.options.cdata && requiresCDATA(obj)) {
|
|
|
|
element.raw(wrapCDATA(obj));
|
|
|
|
} else {
|
|
|
|
element.txt(obj);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (key in obj) {
|
|
|
|
if (!hasProp.call(obj, key)) continue;
|
|
|
|
child = obj[key];
|
|
|
|
if (key === attrkey) {
|
|
|
|
if (typeof child === "object") {
|
|
|
|
for (attr in child) {
|
|
|
|
value = child[attr];
|
|
|
|
element = element.att(attr, value);
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
}
|
2016-11-16 01:59:39 +01:00
|
|
|
} else if (key === charkey) {
|
|
|
|
if (_this.options.cdata && requiresCDATA(child)) {
|
|
|
|
element = element.raw(wrapCDATA(child));
|
2016-11-03 01:33:53 +01:00
|
|
|
} else {
|
2016-11-16 01:59:39 +01:00
|
|
|
element = element.txt(child);
|
|
|
|
}
|
|
|
|
} else if (Array.isArray(child)) {
|
|
|
|
for (index in child) {
|
|
|
|
if (!hasProp.call(child, index)) continue;
|
|
|
|
entry = child[index];
|
|
|
|
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 if (typeof child === "object") {
|
|
|
|
element = render(element.ele(key), child).up();
|
|
|
|
} else {
|
|
|
|
if (typeof child === 'string' && _this.options.cdata && requiresCDATA(child)) {
|
|
|
|
element = element.ele(key).raw(wrapCDATA(child)).up();
|
|
|
|
} else {
|
|
|
|
if (child == null) {
|
|
|
|
child = '';
|
|
|
|
}
|
|
|
|
element = element.ele(key, child.toString()).up();
|
2016-11-03 01:33:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-16 01:59:39 +01:00
|
|
|
return element;
|
|
|
|
};
|
|
|
|
})(this);
|
2016-11-03 01:33:53 +01:00
|
|
|
rootElement = builder.create(rootName, this.options.xmldec, this.options.doctype, {
|
2016-11-16 01:59:39 +01:00
|
|
|
headless: this.options.headless,
|
|
|
|
allowSurrogateChars: this.options.allowSurrogateChars
|
2016-11-03 01:33:53 +01:00
|
|
|
});
|
|
|
|
return render(rootElement, rootObj).end(this.options.renderOpts);
|
|
|
|
};
|
|
|
|
|
|
|
|
return Builder;
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
2016-11-16 01:59:39 +01:00
|
|
|
exports.Parser = (function(superClass) {
|
|
|
|
extend(Parser, superClass);
|
2016-11-03 01:33:53 +01:00
|
|
|
|
|
|
|
function Parser(opts) {
|
2016-11-16 01:59:39 +01:00
|
|
|
this.parseString = bind(this.parseString, this);
|
|
|
|
this.reset = bind(this.reset, this);
|
|
|
|
this.assignOrPush = bind(this.assignOrPush, this);
|
|
|
|
this.processAsync = bind(this.processAsync, this);
|
|
|
|
var key, ref, value;
|
2016-11-03 01:33:53 +01:00
|
|
|
if (!(this instanceof exports.Parser)) {
|
|
|
|
return new exports.Parser(opts);
|
|
|
|
}
|
|
|
|
this.options = {};
|
2016-11-16 01:59:39 +01:00
|
|
|
ref = exports.defaults["0.2"];
|
|
|
|
for (key in ref) {
|
|
|
|
if (!hasProp.call(ref, key)) continue;
|
|
|
|
value = ref[key];
|
2016-11-03 01:33:53 +01:00
|
|
|
this.options[key] = value;
|
|
|
|
}
|
|
|
|
for (key in opts) {
|
2016-11-16 01:59:39 +01:00
|
|
|
if (!hasProp.call(opts, key)) continue;
|
2016-11-03 01:33:53 +01:00
|
|
|
value = opts[key];
|
|
|
|
this.options[key] = value;
|
|
|
|
}
|
|
|
|
if (this.options.xmlns) {
|
|
|
|
this.options.xmlnskey = this.options.attrkey + "ns";
|
|
|
|
}
|
|
|
|
if (this.options.normalizeTags) {
|
|
|
|
if (!this.options.tagNameProcessors) {
|
|
|
|
this.options.tagNameProcessors = [];
|
|
|
|
}
|
|
|
|
this.options.tagNameProcessors.unshift(processors.normalize);
|
|
|
|
}
|
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
|
2016-11-16 01:59:39 +01:00
|
|
|
Parser.prototype.processAsync = function() {
|
|
|
|
var chunk, err, error1;
|
|
|
|
try {
|
|
|
|
if (this.remaining.length <= this.options.chunkSize) {
|
|
|
|
chunk = this.remaining;
|
|
|
|
this.remaining = '';
|
|
|
|
this.saxParser = this.saxParser.write(chunk);
|
|
|
|
return this.saxParser.close();
|
|
|
|
} else {
|
|
|
|
chunk = this.remaining.substr(0, this.options.chunkSize);
|
|
|
|
this.remaining = this.remaining.substr(this.options.chunkSize, this.remaining.length);
|
|
|
|
this.saxParser = this.saxParser.write(chunk);
|
|
|
|
return setImmediate(this.processAsync);
|
|
|
|
}
|
|
|
|
} catch (error1) {
|
|
|
|
err = error1;
|
|
|
|
if (!this.saxParser.errThrown) {
|
|
|
|
this.saxParser.errThrown = true;
|
|
|
|
return this.emit(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-03 01:33:53 +01:00
|
|
|
Parser.prototype.assignOrPush = function(obj, key, newValue) {
|
|
|
|
if (!(key in obj)) {
|
|
|
|
if (!this.options.explicitArray) {
|
|
|
|
return obj[key] = newValue;
|
|
|
|
} else {
|
|
|
|
return obj[key] = [newValue];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!(obj[key] instanceof Array)) {
|
|
|
|
obj[key] = [obj[key]];
|
|
|
|
}
|
|
|
|
return obj[key].push(newValue);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Parser.prototype.reset = function() {
|
|
|
|
var attrkey, charkey, ontext, stack;
|
|
|
|
this.removeAllListeners();
|
|
|
|
this.saxParser = sax.parser(this.options.strict, {
|
|
|
|
trim: false,
|
|
|
|
normalize: false,
|
|
|
|
xmlns: this.options.xmlns
|
|
|
|
});
|
|
|
|
this.saxParser.errThrown = false;
|
|
|
|
this.saxParser.onerror = (function(_this) {
|
|
|
|
return function(error) {
|
|
|
|
_this.saxParser.resume();
|
|
|
|
if (!_this.saxParser.errThrown) {
|
|
|
|
_this.saxParser.errThrown = true;
|
|
|
|
return _this.emit("error", error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})(this);
|
2016-11-16 01:59:39 +01:00
|
|
|
this.saxParser.onend = (function(_this) {
|
|
|
|
return function() {
|
|
|
|
if (!_this.saxParser.ended) {
|
|
|
|
_this.saxParser.ended = true;
|
|
|
|
return _this.emit("end", _this.resultObject);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})(this);
|
2016-11-03 01:33:53 +01:00
|
|
|
this.saxParser.ended = false;
|
|
|
|
this.EXPLICIT_CHARKEY = this.options.explicitCharkey;
|
|
|
|
this.resultObject = null;
|
|
|
|
stack = [];
|
|
|
|
attrkey = this.options.attrkey;
|
|
|
|
charkey = this.options.charkey;
|
|
|
|
this.saxParser.onopentag = (function(_this) {
|
|
|
|
return function(node) {
|
2016-11-16 01:59:39 +01:00
|
|
|
var key, newValue, obj, processedKey, ref;
|
2016-11-03 01:33:53 +01:00
|
|
|
obj = {};
|
|
|
|
obj[charkey] = "";
|
|
|
|
if (!_this.options.ignoreAttrs) {
|
2016-11-16 01:59:39 +01:00
|
|
|
ref = node.attributes;
|
|
|
|
for (key in ref) {
|
|
|
|
if (!hasProp.call(ref, key)) continue;
|
2016-11-03 01:33:53 +01:00
|
|
|
if (!(attrkey in obj) && !_this.options.mergeAttrs) {
|
|
|
|
obj[attrkey] = {};
|
|
|
|
}
|
2016-11-16 01:59:39 +01:00
|
|
|
newValue = _this.options.attrValueProcessors ? processName(_this.options.attrValueProcessors, node.attributes[key]) : node.attributes[key];
|
2016-11-03 01:33:53 +01:00
|
|
|
processedKey = _this.options.attrNameProcessors ? processName(_this.options.attrNameProcessors, key) : key;
|
|
|
|
if (_this.options.mergeAttrs) {
|
|
|
|
_this.assignOrPush(obj, processedKey, newValue);
|
|
|
|
} else {
|
|
|
|
obj[attrkey][processedKey] = newValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
obj["#name"] = _this.options.tagNameProcessors ? processName(_this.options.tagNameProcessors, node.name) : node.name;
|
|
|
|
if (_this.options.xmlns) {
|
|
|
|
obj[_this.options.xmlnskey] = {
|
|
|
|
uri: node.uri,
|
|
|
|
local: node.local
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return stack.push(obj);
|
|
|
|
};
|
|
|
|
})(this);
|
|
|
|
this.saxParser.onclosetag = (function(_this) {
|
|
|
|
return function() {
|
2016-11-16 01:59:39 +01:00
|
|
|
var cdata, emptyStr, err, error1, key, node, nodeName, obj, objClone, old, s, xpath;
|
2016-11-03 01:33:53 +01:00
|
|
|
obj = stack.pop();
|
|
|
|
nodeName = obj["#name"];
|
2016-11-16 01:59:39 +01:00
|
|
|
if (!_this.options.explicitChildren || !_this.options.preserveChildrenOrder) {
|
|
|
|
delete obj["#name"];
|
|
|
|
}
|
|
|
|
if (obj.cdata === true) {
|
|
|
|
cdata = obj.cdata;
|
|
|
|
delete obj.cdata;
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
s = stack[stack.length - 1];
|
|
|
|
if (obj[charkey].match(/^\s*$/) && !cdata) {
|
|
|
|
emptyStr = obj[charkey];
|
|
|
|
delete obj[charkey];
|
|
|
|
} else {
|
|
|
|
if (_this.options.trim) {
|
|
|
|
obj[charkey] = obj[charkey].trim();
|
|
|
|
}
|
|
|
|
if (_this.options.normalize) {
|
|
|
|
obj[charkey] = obj[charkey].replace(/\s{2,}/g, " ").trim();
|
|
|
|
}
|
2016-11-16 01:59:39 +01:00
|
|
|
obj[charkey] = _this.options.valueProcessors ? processName(_this.options.valueProcessors, obj[charkey]) : obj[charkey];
|
2016-11-03 01:33:53 +01:00
|
|
|
if (Object.keys(obj).length === 1 && charkey in obj && !_this.EXPLICIT_CHARKEY) {
|
|
|
|
obj = obj[charkey];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isEmpty(obj)) {
|
2016-11-16 01:59:39 +01:00
|
|
|
obj = _this.options.emptyTag !== '' ? _this.options.emptyTag : emptyStr;
|
2016-11-03 01:33:53 +01:00
|
|
|
}
|
|
|
|
if (_this.options.validator != null) {
|
|
|
|
xpath = "/" + ((function() {
|
2016-11-16 01:59:39 +01:00
|
|
|
var i, len, results;
|
|
|
|
results = [];
|
|
|
|
for (i = 0, len = stack.length; i < len; i++) {
|
|
|
|
node = stack[i];
|
|
|
|
results.push(node["#name"]);
|
2016-11-03 01:33:53 +01:00
|
|
|
}
|
2016-11-16 01:59:39 +01:00
|
|
|
return results;
|
2016-11-03 01:33:53 +01:00
|
|
|
})()).concat(nodeName).join("/");
|
|
|
|
try {
|
|
|
|
obj = _this.options.validator(xpath, s && s[nodeName], obj);
|
2016-11-16 01:59:39 +01:00
|
|
|
} catch (error1) {
|
|
|
|
err = error1;
|
2016-11-03 01:33:53 +01:00
|
|
|
_this.emit("error", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_this.options.explicitChildren && !_this.options.mergeAttrs && typeof obj === 'object') {
|
2016-11-16 01:59:39 +01:00
|
|
|
if (!_this.options.preserveChildrenOrder) {
|
|
|
|
node = {};
|
|
|
|
if (_this.options.attrkey in obj) {
|
|
|
|
node[_this.options.attrkey] = obj[_this.options.attrkey];
|
|
|
|
delete obj[_this.options.attrkey];
|
|
|
|
}
|
|
|
|
if (!_this.options.charsAsChildren && _this.options.charkey in obj) {
|
|
|
|
node[_this.options.charkey] = obj[_this.options.charkey];
|
|
|
|
delete obj[_this.options.charkey];
|
|
|
|
}
|
|
|
|
if (Object.getOwnPropertyNames(obj).length > 0) {
|
|
|
|
node[_this.options.childkey] = obj;
|
|
|
|
}
|
|
|
|
obj = node;
|
|
|
|
} else if (s) {
|
|
|
|
s[_this.options.childkey] = s[_this.options.childkey] || [];
|
|
|
|
objClone = {};
|
|
|
|
for (key in obj) {
|
|
|
|
if (!hasProp.call(obj, key)) continue;
|
|
|
|
objClone[key] = obj[key];
|
|
|
|
}
|
|
|
|
s[_this.options.childkey].push(objClone);
|
|
|
|
delete obj["#name"];
|
|
|
|
if (Object.keys(obj).length === 1 && charkey in obj && !_this.EXPLICIT_CHARKEY) {
|
|
|
|
obj = obj[charkey];
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (stack.length > 0) {
|
|
|
|
return _this.assignOrPush(s, nodeName, obj);
|
|
|
|
} else {
|
|
|
|
if (_this.options.explicitRoot) {
|
|
|
|
old = obj;
|
|
|
|
obj = {};
|
|
|
|
obj[nodeName] = old;
|
|
|
|
}
|
|
|
|
_this.resultObject = obj;
|
|
|
|
_this.saxParser.ended = true;
|
|
|
|
return _this.emit("end", _this.resultObject);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})(this);
|
|
|
|
ontext = (function(_this) {
|
|
|
|
return function(text) {
|
2016-11-16 01:59:39 +01:00
|
|
|
var charChild, s;
|
2016-11-03 01:33:53 +01:00
|
|
|
s = stack[stack.length - 1];
|
|
|
|
if (s) {
|
|
|
|
s[charkey] += text;
|
2016-11-16 01:59:39 +01:00
|
|
|
if (_this.options.explicitChildren && _this.options.preserveChildrenOrder && _this.options.charsAsChildren && (_this.options.includeWhiteChars || text.replace(/\\n/g, '').trim() !== '')) {
|
|
|
|
s[_this.options.childkey] = s[_this.options.childkey] || [];
|
|
|
|
charChild = {
|
|
|
|
'#name': '__text__'
|
|
|
|
};
|
|
|
|
charChild[charkey] = text;
|
|
|
|
if (_this.options.normalize) {
|
|
|
|
charChild[charkey] = charChild[charkey].replace(/\s{2,}/g, " ").trim();
|
|
|
|
}
|
|
|
|
s[_this.options.childkey].push(charChild);
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})(this);
|
|
|
|
this.saxParser.ontext = ontext;
|
|
|
|
return this.saxParser.oncdata = (function(_this) {
|
|
|
|
return function(text) {
|
|
|
|
var s;
|
|
|
|
s = ontext(text);
|
|
|
|
if (s) {
|
|
|
|
return s.cdata = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
Parser.prototype.parseString = function(str, cb) {
|
2016-11-16 01:59:39 +01:00
|
|
|
var err, error1;
|
2016-11-03 01:33:53 +01:00
|
|
|
if ((cb != null) && typeof cb === "function") {
|
|
|
|
this.on("end", function(result) {
|
|
|
|
this.reset();
|
2016-11-16 01:59:39 +01:00
|
|
|
return cb(null, result);
|
2016-11-03 01:33:53 +01:00
|
|
|
});
|
|
|
|
this.on("error", function(err) {
|
|
|
|
this.reset();
|
2016-11-16 01:59:39 +01:00
|
|
|
return cb(err);
|
2016-11-03 01:33:53 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
try {
|
2016-11-16 01:59:39 +01:00
|
|
|
str = str.toString();
|
|
|
|
if (str.trim() === '') {
|
|
|
|
this.emit("end", null);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
str = bom.stripBOM(str);
|
|
|
|
if (this.options.async) {
|
|
|
|
this.remaining = str;
|
|
|
|
setImmediate(this.processAsync);
|
|
|
|
return this.saxParser;
|
|
|
|
}
|
|
|
|
return this.saxParser.write(str).close();
|
|
|
|
} catch (error1) {
|
|
|
|
err = error1;
|
2016-11-03 01:33:53 +01:00
|
|
|
if (!(this.saxParser.errThrown || this.saxParser.ended)) {
|
|
|
|
this.emit('error', err);
|
|
|
|
return this.saxParser.errThrown = true;
|
2016-11-16 01:59:39 +01:00
|
|
|
} else if (this.saxParser.ended) {
|
|
|
|
throw err;
|
2016-11-03 01:33:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return Parser;
|
|
|
|
|
|
|
|
})(events.EventEmitter);
|
|
|
|
|
|
|
|
exports.parseString = function(str, a, b) {
|
|
|
|
var cb, options, parser;
|
|
|
|
if (b != null) {
|
|
|
|
if (typeof b === 'function') {
|
|
|
|
cb = b;
|
|
|
|
}
|
|
|
|
if (typeof a === 'object') {
|
|
|
|
options = a;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (typeof a === 'function') {
|
|
|
|
cb = a;
|
|
|
|
}
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
parser = new exports.Parser(options);
|
|
|
|
return parser.parseString(str, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
}).call(this);
|