Last active
March 13, 2018 12:00
-
-
Save Rast1234/7712555 to your computer and use it in GitHub Desktop.
Sniffing out what different DOMParser-enabled browsers return on invalid XML input.
Launch and compare output across browsers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isValidXML(xmlString) { | |
var parser = new DOMParser(); | |
var parsererrorNS = parser.parseFromString('INVALID', 'text/xml').getElementsByTagName("parsererror")[0].namespaceURI; | |
var dom = parser.parseFromString(xmlString, 'text/xml'); | |
if(dom.getElementsByTagNameNS(parsererrorNS, 'parsererror').length > 0) { | |
return false | |
} | |
return true; | |
} | |
function s(x) {var t = new XMLSerializer(); return x ? t.serializeToString(x): undefined;} | |
var p = new DOMParser(); | |
var arr = [ | |
"fail", | |
"abc", | |
"ab", | |
"a", | |
"", | |
null, | |
undefined, | |
"<ok />", | |
"<notok>", | |
"<notok><fail></notok>", | |
"<notok><a/><b/><c><d/></notok>", | |
'<?xml version="1.0" encoding="windows-1251"?><a><b/><c></a>', | |
'<?xml version="1.0" encoding="windows-1251"?><invalid xmlns="http://www.w3.org/1999/xhtml"><foo></invalid>', | |
'<?xml version="1.0" encoding="windows-1251"?><valid xmlns="http://www.w3.org/1999/xhtml"><foo/></valid>', | |
'<?xml version="1.0" encoding="windows-1251"?><valid><foo/><parsererror xmlns="http://www.w3.org/1999/xhtml"/></valid>', // false detect in Chrome | |
'<?xml version="1.0" encoding="windows-1251"?><valid><foo/><parsererror xmlns="http://www.mozilla.org/newlayout/xml/parsererror.xml"/></valid>', // false detect in Firefox | |
]; | |
for(var i = 0; i<arr.length; i++) { | |
var arg = arr[i]; | |
var doc = p.parseFromString(arg, "text/xml"); | |
var parsererror = doc.getElementsByTagName("parsererror")[0]; | |
var namespace = parsererror ? parsererror.namespaceURI : undefined; | |
console.log("\n###xml string:###\n" + arg + "\n###document###\n" + s(doc) + "\n###parsererror node:###\n" + s(parsererror) + "\n###parsererror namespace###\n" + namespace + "\n###test isValidXML: " + isValidXML(arg) + "\n##########"); | |
} | |
// final heuristic function to parse your XML. Beware of falsy alarms (examples in the tests above) | |
function tryParseXML(xmlString) { | |
var parser = new DOMParser(); | |
var parsererrorNS = parser.parseFromString('INVALID', 'text/xml').getElementsByTagName("parsererror")[0].namespaceURI; | |
var dom = parser.parseFromString(xmlString, 'text/xml'); | |
if(dom.getElementsByTagNameNS(parsererrorNS, 'parsererror').length > 0) { | |
throw new Error('Error parsing XML'); | |
} | |
return dom; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A disadvantage of this approach is that
new DOMParser().parseFromString('INVALID', 'text/xml')
reports the following error in Mozilla Firefox's browser console:The alternative would be to avoid creating an invalid document and use a wildcard for the namespace instead: