@@ -192,11 +192,28 @@ module.exports = (function fakeBrowser() {
192
192
function DOMParser ( ) {
193
193
// parser
194
194
function parseXml ( s , sink ) {
195
- var i = 0 , scopes = [ { space :"default" , xmlns :"" , namespaces : { "xmlns" :"http://www.w3.org/2000/xmlns/" , "xml" :"http://www.w3.org/XML/1998/namespace" } } ] ;
195
+ var i = 0 , scopes = [ {
196
+ space : "default" ,
197
+ xmlns : "" ,
198
+ namespaces : {
199
+ "xmlns" :"http://www.w3.org/2000/xmlns/" ,
200
+ "xml" :"http://www.w3.org/XML/1998/namespace"
201
+ }
202
+ } ] ;
203
+
204
+ function unSelfClose ( s ) {
205
+ // This code is actually not good at parsing self-closed elements,
206
+ // so as a preprocess step we "open" self closed elements.
207
+ return s . replace ( / < ( [ a - z A - Z - _ ] + ) [ ^ \/ > ] * \/ > / g, function ( a , b ) {
208
+ if ( a === "undefined" && b === "undefined" ) return '' ;
209
+ return a . replace ( / \s * \/ > / , "></" + b + ">" ) ;
210
+ } ) ;
211
+ }
196
212
197
213
function trim ( s ) {
198
214
return s . replace ( / ^ \s + / , "" ) . replace ( / \s + $ / , "" ) ;
199
215
}
216
+
200
217
function resolveEntities ( s ) {
201
218
return s . replace ( / & ( [ ^ ; ] + ) ; / g, function ( all , entity ) {
202
219
if ( entity . substring ( 0 , 2 ) === "#x" ) {
@@ -212,6 +229,7 @@ module.exports = (function fakeBrowser() {
212
229
throw "Unknown entity: " + entity ;
213
230
} ) ;
214
231
}
232
+
215
233
function isWhitespacePreserved ( ) {
216
234
for ( var j = scopes . length - 1 ; j >= 0 ; -- j ) {
217
235
if ( scopes [ j ] . space === "preserve" ) {
@@ -220,13 +238,15 @@ module.exports = (function fakeBrowser() {
220
238
}
221
239
return false ;
222
240
}
241
+
223
242
function lookupDefaultNs ( ) {
224
243
for ( var j = scopes . length - 1 ; j >= 0 ; -- j ) {
225
244
if ( scopes [ j ] . hasOwnProperty ( "xmlns" ) ) {
226
245
return scopes [ j ] . xmlns ;
227
246
}
228
247
}
229
248
}
249
+
230
250
function lookupNs ( prefix ) {
231
251
for ( var j = scopes . length - 1 ; j >= 0 ; -- j ) {
232
252
if ( scopes [ j ] . namespaces . hasOwnProperty ( prefix ) ) {
@@ -235,6 +255,7 @@ module.exports = (function fakeBrowser() {
235
255
}
236
256
throw "Unknow namespace: " + prefix ;
237
257
}
258
+
238
259
function getName ( name , resolveDefaultNs ) {
239
260
var j = name . indexOf ( ":" ) ;
240
261
if ( j >= 0 ) {
@@ -245,10 +266,12 @@ module.exports = (function fakeBrowser() {
245
266
return { name :name , prefix : "" , namespace : "" } ;
246
267
}
247
268
}
269
+
248
270
function isWhitespace ( s , index ) {
249
271
var ch = s . charCodeAt ( index ) ;
250
272
return ch == 10 || ch == 13 || ch == 9 || ch == 32 ;
251
273
}
274
+
252
275
function parseContent ( s , start ) {
253
276
var pos = start , name , attributes = [ ] ;
254
277
function skipWs ( ) {
@@ -284,6 +307,12 @@ module.exports = (function fakeBrowser() {
284
307
}
285
308
return { name : name , attributes : attributes , parsed : pos - start } ;
286
309
}
310
+
311
+ // ==================================
312
+ // The actual XML parsing starts here
313
+ // ==================================
314
+
315
+ s = unSelfClose ( s ) ;
287
316
while ( i < s . length ) {
288
317
var ch = s . charAt ( i ) ;
289
318
var j = i ;
0 commit comments