xml2js
Advanced tools
Comparing version 0.1.12 to 0.1.13
(function() { | ||
var events, isEmpty, sax; | ||
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = Object.prototype.hasOwnProperty, __extends = 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; | ||
}; | ||
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = Object.prototype.hasOwnProperty, __extends = 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; }; | ||
sax = require('sax'); | ||
events = require('events'); | ||
isEmpty = function(thing) { | ||
return typeof thing === "object" && (thing != null) && Object.keys(thing).length === 0; | ||
}; | ||
exports.Parser = (function() { | ||
__extends(Parser, events.EventEmitter); | ||
function Parser(opts) { | ||
@@ -29,3 +28,4 @@ this.parseString = __bind(this.parseString, this); | ||
explicitArray: false, | ||
ignoreAttrs: false | ||
ignoreAttrs: false, | ||
mergeAttrs: false | ||
}; | ||
@@ -39,4 +39,6 @@ for (key in opts) { | ||
} | ||
Parser.prototype.reset = function() { | ||
var attrkey, charkey, err, stack; | ||
var _this = this; | ||
this.removeAllListeners(); | ||
@@ -48,8 +50,8 @@ this.saxParser = sax.parser(true, { | ||
err = false; | ||
this.saxParser.onerror = __bind(function(error) { | ||
this.saxParser.onerror = function(error) { | ||
if (!err) { | ||
err = true; | ||
return this.emit("error", error); | ||
return _this.emit("error", error); | ||
} | ||
}, this); | ||
}; | ||
this.EXPLICIT_CHARKEY = this.options.explicitCharkey; | ||
@@ -60,14 +62,16 @@ this.resultObject = null; | ||
charkey = this.options.charkey; | ||
this.saxParser.onopentag = __bind(function(node) { | ||
this.saxParser.onopentag = function(node) { | ||
var key, obj, _ref; | ||
obj = {}; | ||
obj[charkey] = ""; | ||
if (!this.options.ignoreAttrs) { | ||
if (!_this.options.ignoreAttrs) { | ||
_ref = node.attributes; | ||
for (key in _ref) { | ||
if (!__hasProp.call(_ref, key)) continue; | ||
if (!(attrkey in obj)) { | ||
obj[attrkey] = {}; | ||
if (!(attrkey in obj) && !_this.options.mergeAttrs) obj[attrkey] = {}; | ||
if (_this.options.mergeAttrs) { | ||
obj[key] = node.attributes[key]; | ||
} else { | ||
obj[attrkey][key] = node.attributes[key]; | ||
} | ||
obj[attrkey][key] = node.attributes[key]; | ||
} | ||
@@ -77,4 +81,4 @@ } | ||
return stack.push(obj); | ||
}, this); | ||
this.saxParser.onclosetag = __bind(function() { | ||
}; | ||
this.saxParser.onclosetag = function() { | ||
var nodeName, obj, old, s; | ||
@@ -88,17 +92,15 @@ obj = stack.pop(); | ||
} else { | ||
if (this.options.trim) { | ||
obj[charkey] = obj[charkey].trim(); | ||
} | ||
if (this.options.normalize) { | ||
if (_this.options.trim) obj[charkey] = obj[charkey].trim(); | ||
if (_this.options.normalize) { | ||
obj[charkey] = obj[charkey].replace(/\s{2,}/g, " ").trim(); | ||
} | ||
if (Object.keys(obj).length === 1 && charkey in obj && !this.EXPLICIT_CHARKEY) { | ||
if (Object.keys(obj).length === 1 && charkey in obj && !_this.EXPLICIT_CHARKEY) { | ||
obj = obj[charkey]; | ||
} | ||
} | ||
if (this.options.emptyTag !== void 0 && isEmpty(obj)) { | ||
obj = this.options.emptyTag; | ||
if (_this.options.emptyTag !== void 0 && isEmpty(obj)) { | ||
obj = _this.options.emptyTag; | ||
} | ||
if (stack.length > 0) { | ||
if (!this.options.explicitArray) { | ||
if (!_this.options.explicitArray) { | ||
if (!(nodeName in s)) { | ||
@@ -114,9 +116,7 @@ return s[nodeName] = obj; | ||
} else { | ||
if (!(s[nodeName] instanceof Array)) { | ||
s[nodeName] = []; | ||
} | ||
if (!(s[nodeName] instanceof Array)) s[nodeName] = []; | ||
return s[nodeName].push(obj); | ||
} | ||
} else { | ||
if (this.options.explicitRoot) { | ||
if (_this.options.explicitRoot) { | ||
old = obj; | ||
@@ -126,14 +126,13 @@ obj = {}; | ||
} | ||
this.resultObject = obj; | ||
return this.emit("end", this.resultObject); | ||
_this.resultObject = obj; | ||
return _this.emit("end", _this.resultObject); | ||
} | ||
}, this); | ||
return this.saxParser.ontext = this.saxParser.oncdata = __bind(function(text) { | ||
}; | ||
return this.saxParser.ontext = this.saxParser.oncdata = function(text) { | ||
var s; | ||
s = stack[stack.length - 1]; | ||
if (s) { | ||
return s[charkey] += text; | ||
} | ||
}, this); | ||
if (s) return s[charkey] += text; | ||
}; | ||
}; | ||
Parser.prototype.parseString = function(str, cb) { | ||
@@ -156,4 +155,7 @@ if ((cb != null) && typeof cb === "function") { | ||
}; | ||
return Parser; | ||
})(); | ||
}).call(this); |
@@ -6,3 +6,3 @@ { | ||
"homepage" : "https://github.com/Leonidas-from-XIV/node-xml2js", | ||
"version" : "0.1.12", | ||
"version" : "0.1.13", | ||
"author" : "Marek Kubica <marek@xivilization.net> (http://xivilization.net)", | ||
@@ -19,3 +19,5 @@ "contributors" : [ | ||
"Marcelo Diniz <marudiniz@gmail.com> (https://github.com/mdiniz)", | ||
"Michael Hart (https://github.com/mhart)" | ||
"Michael Hart (https://github.com/mhart)", | ||
"Zachary Scott <zachary@zacharyscott.net> (http://zacharyscott.net/)", | ||
"Raoul Millais (https://github.com/raoulmillais)" | ||
], | ||
@@ -22,0 +24,0 @@ "main" : "./lib/xml2js", |
@@ -126,2 +126,5 @@ node-xml2js | ||
text nodes. | ||
* `mergeAttrs` (default: `false`): Merge attributes and child elements as | ||
properties of the parent, instead of keying attributes off a child | ||
attribute object. This option is ignored if `ignoreAttrs` is `false`. | ||
@@ -128,0 +131,0 @@ These default settings are for backward-compatibility (and might change in the |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
24850
140
0
142