|
20 | 20 | }
|
21 | 21 | }
|
22 | 22 |
|
23 |
| - /* |
| 23 | + /** |
24 | 24 | * XmlElement is our basic building block. Everything is an XmlElement; even XmlDocument
|
25 | 25 | * behaves like an XmlElement by inheriting its attributes and functions.
|
26 | 26 | */
|
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 | + } |
35 | 36 | }
|
36 | 37 |
|
37 | 38 | this.name = tag.name;
|
|
42 | 43 | this.lastChild = null;
|
43 | 44 |
|
44 | 45 | // 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; |
49 | 50 | }
|
50 | 51 |
|
51 | 52 | // Private methods
|
|
136 | 137 | }
|
137 | 138 | return undefined;
|
138 | 139 | };
|
139 |
| - |
| 140 | + |
140 | 141 | XmlElement.prototype.descendantsNamed = function (name) {
|
141 |
| - var matches = []; |
| 142 | + var matches = []; |
142 | 143 |
|
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 | + } |
150 | 151 |
|
151 |
| - return matches; |
152 |
| - }; |
| 152 | + return matches; |
| 153 | + }; |
153 | 154 |
|
154 | 155 | XmlElement.prototype.descendantWithPath = function (path) {
|
155 | 156 | var descendant = this;
|
|
275 | 276 | XmlCDataNode.prototype.type = "cdata";
|
276 | 277 | XmlCommentNode.prototype.type = "comment";
|
277 | 278 |
|
278 |
| - /* |
| 279 | + /** |
279 | 280 | * XmlDocument is the class we expose to the user; it uses the sax parser to create a hierarchy
|
280 | 281 | * of XmlElements.
|
281 | 282 | */
|
282 |
| - |
283 | 283 | function XmlDocument(xml) {
|
284 | 284 | xml && (xml = xml.toString().trim());
|
285 | 285 |
|
|
297 | 297 | // It's safe to use a global because JS is single-threaded.
|
298 | 298 | delegates = [this];
|
299 | 299 |
|
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 | + } |
304 | 306 | }
|
305 | 307 |
|
306 | 308 | // make XmlDocument inherit XmlElement's methods
|
|
0 commit comments