sane-domparser-error
Provides sane and machine readable parser errors for DOMParser and XHR (with XMLHttpRequest.responseType = 'document'
).
Why?
DOMParser and XHR currently do not throw an exception if a document could not be parsed, but instead return an error document.
Instead of
<parsererror xmlns="http://www.mozilla.org/newlayout/xml/parsererror.xml">
XML Parsing Error: prefix not bound to a namespace\n Location: file:////tmp/index.html
Line Number 1, Column 57:
<sourcetext>
<html xmlns="http://www.w3.org/1999/xhtml"><head/><body><namespace:customtag><!-- namespace:customtag--></namespace:customtag></body></html>
--------------------------------------------------------^
</sourcetext>
</parsererror>
you'll receive
new Error('XML Parsing Error: prefix not bound to a namespace');
Usage
var saneError = require('sane-domparser-error');
var xhr = new XMLHttpRequest();
xhr.onload = function() {
saneError.failOnParseError(xhr.responseXML);
// ... the happy path from here ...
}
xhr.open("GET", "file.html");
xhr.responseType = "document";
xhr.send();
and
var saneError = require('sane-domparser-error');
var p = new DOMParser(),
doc = p.parseFromString(xhtml, "application/xml");
saneError.failOnParseError(doc);
// ... the happy path from here ...
Develop
$ npm run-script watchify
$ xdg-open test/test.html
or just
$ npm test