Permalink
Browse files

fix to the unit test XML parser, to deal with self-closed elements

  • Loading branch information...
1 parent c22e849 commit 01467b538f32da0163f4f1284b686a6a4c11c0a3 @Pomax Pomax committed Mar 21, 2015
Showing with 31 additions and 2 deletions.
  1. +30 −1 lib/Browser.js
  2. +1 −1 test/unit/XMLElement/P52.0/XMLtoString.pde
View
@@ -192,11 +192,28 @@ module.exports = (function fakeBrowser() {
function DOMParser() {
// parser
function parseXml(s, sink) {
- var i = 0, scopes = [{space:"default", xmlns:"", namespaces: {"xmlns":"http://www.w3.org/2000/xmlns/", "xml":"http://www.w3.org/XML/1998/namespace"}}];
+ var i = 0, scopes = [{
+ space: "default",
+ xmlns: "",
+ namespaces: {
+ "xmlns":"http://www.w3.org/2000/xmlns/",
+ "xml":"http://www.w3.org/XML/1998/namespace"
+ }
+ }];
+
+ function unSelfClose(s) {
+ // This code is actually not good at parsing self-closed elements,
+ // so as a preprocess step we "open" self closed elements.
+ return s.replace(/<([a-zA-Z-_]+)[^\/>]*\/>/g, function(a,b) {
+ if(a==="undefined" && b==="undefined") return '';
+ return a.replace(/\s*\/>/, "></"+b+">");
+ });
+ }
function trim(s) {
return s.replace(/^\s+/, "").replace(/\s+$/, "");
}
+
function resolveEntities(s) {
return s.replace(/&([^;]+);/g, function(all, entity) {
if (entity.substring(0, 2) === "#x") {
@@ -212,6 +229,7 @@ module.exports = (function fakeBrowser() {
throw "Unknown entity: " + entity;
});
}
+
function isWhitespacePreserved() {
for (var j = scopes.length - 1; j >= 0; --j) {
if (scopes[j].space === "preserve") {
@@ -220,13 +238,15 @@ module.exports = (function fakeBrowser() {
}
return false;
}
+
function lookupDefaultNs() {
for (var j = scopes.length - 1; j >= 0; --j) {
if (scopes[j].hasOwnProperty("xmlns")) {
return scopes[j].xmlns;
}
}
}
+
function lookupNs(prefix) {
for (var j = scopes.length - 1; j >= 0; --j) {
if (scopes[j].namespaces.hasOwnProperty(prefix)) {
@@ -235,6 +255,7 @@ module.exports = (function fakeBrowser() {
}
throw "Unknow namespace: " + prefix;
}
+
function getName(name, resolveDefaultNs) {
var j = name.indexOf(":");
if (j >= 0) {
@@ -245,10 +266,12 @@ module.exports = (function fakeBrowser() {
return {name:name, prefix: "", namespace: ""};
}
}
+
function isWhitespace(s, index) {
var ch = s.charCodeAt(index);
return ch == 10 || ch == 13 || ch == 9 || ch == 32;
}
+
function parseContent(s, start) {
var pos = start, name, attributes = [];
function skipWs() {
@@ -284,6 +307,12 @@ module.exports = (function fakeBrowser() {
}
return {name: name, attributes: attributes, parsed: pos - start};
}
+
+ // ==================================
+ // The actual XML parsing starts here
+ // ==================================
+
+ s = unSelfClose(s);
while (i < s.length) {
var ch = s.charAt(i);
var j = i;
@@ -1,4 +1,4 @@
-String xmlstring = "<root><a></a><b></b></root>";
+String xmlstring = "<root><a/><b></b></root>";
XMLElement e = XMLElement.parse(xmlstring);
String id = e.toString();
String check = "<root><a/><b/></root>";

0 comments on commit 01467b5

Please sign in to comment.