Skip to content

Instantly share code, notes, and snippets.

@kyle-ilantzis
Last active January 4, 2016 07:39
Show Gist options
  • Save kyle-ilantzis/8590361 to your computer and use it in GitHub Desktop.
Save kyle-ilantzis/8590361 to your computer and use it in GitHub Desktop.
isaacs / sax-js error events
var sax = require("sax"),
strict = true, // set to false for html-mode
parser = sax.parser(strict);
parser.onerror = function (e) {
// an error happened.
console.log("error");
console.log(e);
parser.resume();
};
parser.ontext = function (t) {
// got some text. t is the string of text.
};
parser.onopentag = function (node) {
// opened a tag. node has "name" and "attributes"
};
parser.onattribute = function (attr) {
// an attribute. attr has "name" and "value"
};
parser.onend = function () {
// parser stream is done, and ready to have more stuff written to it.
console.log("end")
};
var xml =
[ '<?xml version="1.0 encoding="UTF-8"?><root></root>', // PASS: success
'<?xml version="1.0 encoding="UTF-8"?><root>', // PASS: err reports unclosed root tag
'<?xml version="1.0 encoding="UTF-8"?><root></butt>', //FAIL: unexpected close tag
'<?xml version="1.0 encoding="UTF-8"?><root><butt>asd</root>',
'<?xml version="1.0 encoding="UTF-8"?>before<root></root>', //FAIL: text outside of root node
'prefix<?xml version="1.0 encoding="UTF-8"?><root></root>',
'<?xml version="1.0 encoding="UTF-8"?><root></root>after',
'<?xml version="1.0 encoding="UTF-8"?><root attr=></root>', //FAIL: unquoted attribute value
'<?xml version="1.0 encoding="UTF-8"?><root attr></root>']; //FAIL: attribute without value
var i = 5;
try {
parser.write(xml[i]).close();
} catch (err) {
console.log("caught error!");
console.log(err);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment