Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions lib/util/xml.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ const parser = new DOMParser();
export function parseXML(str: string): Promise<any> {
try {
const dom = parser.parseFromString(str, "application/xml");
const errorMessage = getErrorMessage(dom);
if (errorMessage) {
throw new Error(errorMessage);
}
throwIfError(dom);

const obj = domToObject(dom.childNodes[0]);
return Promise.resolve(obj);
Expand All @@ -17,13 +14,19 @@ export function parseXML(str: string): Promise<any> {
}
}

const errorNS = parser.parseFromString("INVALID", "text/xml").getElementsByTagName("parsererror")[0].namespaceURI!;
function getErrorMessage(dom: Document): string | undefined {
const parserErrors = dom.getElementsByTagNameNS(errorNS, "parsererror");
if (parserErrors.length) {
return parserErrors.item(0).innerHTML;
} else {
return undefined;
let errorNS = "";
try {
errorNS = parser.parseFromString("INVALID", "text/xml").getElementsByTagName("parsererror")[0].namespaceURI!;
} catch (ignored) {
// Most browsers will return a document containing <parsererror>, but IE will throw.
}

function throwIfError(dom: Document) {
if (errorNS) {
const parserErrors = dom.getElementsByTagNameNS(errorNS, "parsererror");
if (parserErrors.length) {
throw new Error(parserErrors.item(0).innerHTML);
}
}
}

Expand Down Expand Up @@ -73,7 +76,7 @@ const doc = document.implementation.createDocument(null, null, null);
const serializer = new XMLSerializer();

export function stringifyXML(obj: any, opts?: { rootName?: string }) {
const rootName = (opts || {}).rootName || "root";
const rootName = opts && opts.rootName || "root";
const dom = buildNode(obj, rootName)[0];
return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + serializer.serializeToString(dom);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/xhrHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ export class XhrHttpClient implements HttpClient {
}
}

xhr.open(request.method, request.url);
xhr.timeout = request.timeout;
xhr.withCredentials = request.withCredentials;
xhr.open(request.method, request.url);
for (const header of request.headers.headersArray()) {
xhr.setRequestHeader(header.name, header.value);
}
Expand Down
20 changes: 20 additions & 0 deletions test/shared/xmlTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { parseXML } from "../../lib/util/xml";
import * as assert from "assert";

describe("XML serializer", function () {
it("should parse basic XML", async function() {
const res = await parseXML("<foo>42</foo>");
assert.equal(res, 42);
});

it("should handle errors gracefully", async function () {
try {
await parseXML("INVALID");
throw new Error("did not throw");
} catch (err) {
if (err.message === "did not throw") {
throw err;
}
}
});
});