Skip to content

Commit 216c1fb

Browse files
committed
Fix optional parser; Version bump for npm
1 parent 03b1c46 commit 216c1fb

File tree

3 files changed

+236
-239
lines changed

3 files changed

+236
-239
lines changed

lib/xmldoc.js

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@
2020
}
2121
}
2222

23-
/*
23+
/**
2424
* XmlElement is our basic building block. Everything is an XmlElement; even XmlDocument
2525
* behaves like an XmlElement by inheriting its attributes and functions.
2626
*/
27-
28-
function XmlElement(tag, parserIn) {
29-
// Capture the parser object off of the XmlDocument delegate
30-
var delegate = delegates[delegates.length - 1];
31-
var parser = parserIn;
32-
33-
if (delegate) {
34-
parser = delegate.parser;
27+
function XmlElement(tag, parser) {
28+
// If you didn't hand us a parser (common case) see if we can grab one
29+
// from the current execution stack.
30+
if (!parser) {
31+
var delegate = delegates[delegates.length - 1];
32+
33+
if (delegate.parser) {
34+
parser = delegate.parser;
35+
}
3536
}
3637

3738
this.name = tag.name;
@@ -42,10 +43,10 @@
4243
this.lastChild = null;
4344

4445
// Assign parse information
45-
this.line = parser?.line;
46-
this.column = parser?.column;
47-
this.position = parser?.position;
48-
this.startTagPosition = parser?.startTagPosition;
46+
this.line = parser ? parser.line : null;
47+
this.column = parser ? parser.column : null;
48+
this.position = parser ? parser.position : null;
49+
this.startTagPosition = parser ? parser.startTagPosition : null;
4950
}
5051

5152
// Private methods
@@ -136,20 +137,20 @@
136137
}
137138
return undefined;
138139
};
139-
140+
140141
XmlElement.prototype.descendantsNamed = function (name) {
141-
var matches = [];
142+
var matches = [];
142143

143-
for (var i = 0, l = this.children.length; i < l; i++) {
144-
var child = this.children[i];
145-
if (child.type === "element") {
146-
if (child.name === name) matches.push(child);
147-
matches = matches.concat(child.descendantsNamed(name));
148-
}
149-
}
144+
for (var i = 0, l = this.children.length; i < l; i++) {
145+
var child = this.children[i];
146+
if (child.type === "element") {
147+
if (child.name === name) matches.push(child);
148+
matches = matches.concat(child.descendantsNamed(name));
149+
}
150+
}
150151

151-
return matches;
152-
};
152+
return matches;
153+
};
153154

154155
XmlElement.prototype.descendantWithPath = function (path) {
155156
var descendant = this;
@@ -275,11 +276,10 @@
275276
XmlCDataNode.prototype.type = "cdata";
276277
XmlCommentNode.prototype.type = "comment";
277278

278-
/*
279+
/**
279280
* XmlDocument is the class we expose to the user; it uses the sax parser to create a hierarchy
280281
* of XmlElements.
281282
*/
282-
283283
function XmlDocument(xml) {
284284
xml && (xml = xml.toString().trim());
285285

@@ -297,10 +297,12 @@
297297
// It's safe to use a global because JS is single-threaded.
298298
delegates = [this];
299299

300-
this.parser.write(xml);
301-
302-
// Remove the parser as it is no longer needed and should not be exposed to clients
303-
delete this.parser;
300+
try {
301+
this.parser.write(xml);
302+
} finally {
303+
// Remove the parser as it is no longer needed and should not be exposed to clients
304+
delete this.parser;
305+
}
304306
}
305307

306308
// make XmlDocument inherit XmlElement's methods

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"email": "[email protected]",
77
"url": "http://nfarina.com"
88
},
9-
"version": "1.2.0",
9+
"version": "1.3.0",
1010
"main": "./index",
1111
"scripts": {
1212
"test": "tap test/*.js",

0 commit comments

Comments
 (0)