Skip to content

Commit 01467b5

Browse files
committed
fix to the unit test XML parser, to deal with self-closed elements
1 parent c22e849 commit 01467b5

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/Browser.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,28 @@ module.exports = (function fakeBrowser() {
192192
function DOMParser() {
193193
// parser
194194
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-zA-Z-_]+)[^\/>]*\/>/g, function(a,b) {
208+
if(a==="undefined" && b==="undefined") return '';
209+
return a.replace(/\s*\/>/, "></"+b+">");
210+
});
211+
}
196212

197213
function trim(s) {
198214
return s.replace(/^\s+/, "").replace(/\s+$/, "");
199215
}
216+
200217
function resolveEntities(s) {
201218
return s.replace(/&([^;]+);/g, function(all, entity) {
202219
if (entity.substring(0, 2) === "#x") {
@@ -212,6 +229,7 @@ module.exports = (function fakeBrowser() {
212229
throw "Unknown entity: " + entity;
213230
});
214231
}
232+
215233
function isWhitespacePreserved() {
216234
for (var j = scopes.length - 1; j >= 0; --j) {
217235
if (scopes[j].space === "preserve") {
@@ -220,13 +238,15 @@ module.exports = (function fakeBrowser() {
220238
}
221239
return false;
222240
}
241+
223242
function lookupDefaultNs() {
224243
for (var j = scopes.length - 1; j >= 0; --j) {
225244
if (scopes[j].hasOwnProperty("xmlns")) {
226245
return scopes[j].xmlns;
227246
}
228247
}
229248
}
249+
230250
function lookupNs(prefix) {
231251
for (var j = scopes.length - 1; j >= 0; --j) {
232252
if (scopes[j].namespaces.hasOwnProperty(prefix)) {
@@ -235,6 +255,7 @@ module.exports = (function fakeBrowser() {
235255
}
236256
throw "Unknow namespace: " + prefix;
237257
}
258+
238259
function getName(name, resolveDefaultNs) {
239260
var j = name.indexOf(":");
240261
if (j >= 0) {
@@ -245,10 +266,12 @@ module.exports = (function fakeBrowser() {
245266
return {name:name, prefix: "", namespace: ""};
246267
}
247268
}
269+
248270
function isWhitespace(s, index) {
249271
var ch = s.charCodeAt(index);
250272
return ch == 10 || ch == 13 || ch == 9 || ch == 32;
251273
}
274+
252275
function parseContent(s, start) {
253276
var pos = start, name, attributes = [];
254277
function skipWs() {
@@ -284,6 +307,12 @@ module.exports = (function fakeBrowser() {
284307
}
285308
return {name: name, attributes: attributes, parsed: pos - start};
286309
}
310+
311+
// ==================================
312+
// The actual XML parsing starts here
313+
// ==================================
314+
315+
s = unSelfClose(s);
287316
while (i < s.length) {
288317
var ch = s.charAt(i);
289318
var j = i;

test/unit/XMLElement/P52.0/XMLtoString.pde

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
String xmlstring = "<root><a></a><b></b></root>";
1+
String xmlstring = "<root><a/><b></b></root>";
22
XMLElement e = XMLElement.parse(xmlstring);
33
String id = e.toString();
44
String check = "<root><a/><b/></root>";

0 commit comments

Comments
 (0)