parser/xml/test/unit/test_parser.js

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

michael@0 1 function updateDocumentSourceMaps(source) {
michael@0 2 const nsIDOMNode = Components.interfaces.nsIDOMNode;
michael@0 3
michael@0 4 const nsISAXXMLReader = Components.interfaces.nsISAXXMLReader;
michael@0 5 const saxReader = Components.classes["@mozilla.org/saxparser/xmlreader;1"]
michael@0 6 .createInstance(nsISAXXMLReader);
michael@0 7 try {
michael@0 8 saxReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
michael@0 9 saxReader.setFeature("http://xml.org/sax/features/namespace", true);
michael@0 10 }
michael@0 11 catch (e) {
michael@0 12 // do nothing, we'll accept it as it is.
michael@0 13 }
michael@0 14 var parseErrorLog = [];
michael@0 15
michael@0 16 /* XXX ajvincent Because throwing an exception doesn't stop parsing, we need
michael@0 17 * to record errors and handle them after the parsing is finished.
michael@0 18 */
michael@0 19 function do_parse_check(aCondition, aMsg) {
michael@0 20 if (!aCondition)
michael@0 21 parseErrorLog[parseErrorLog.length] = aMsg;
michael@0 22 }
michael@0 23
michael@0 24 var contentHandler = {
michael@0 25 startDocument: function startDocument() {
michael@0 26 },
michael@0 27
michael@0 28 endDocument: function endDocument() {
michael@0 29 },
michael@0 30
michael@0 31 handleAttributes: function handleAttributes(aAttributes) {
michael@0 32 for (var i = 0; i < aAttributes.length; i++) {
michael@0 33 var attrNamespaceURI = aAttributes.getURI(i);
michael@0 34 var attrLocalName = aAttributes.getLocalName(i);
michael@0 35 var attrNodeName = aAttributes.getQName(i);
michael@0 36 var value = aAttributes.getValue(i);
michael@0 37 do_parse_check(attrLocalName, "Missing attribute local name");
michael@0 38 do_parse_check(attrNodeName, "Missing attribute node name");
michael@0 39 }
michael@0 40 },
michael@0 41
michael@0 42 startElement: function startElement(aNamespaceURI, aLocalName, aNodeName, aAttributes) {
michael@0 43 do_parse_check(aLocalName, "Missing element local name (startElement)");
michael@0 44 do_parse_check(aNodeName, "Missing element node name (startElement)");
michael@0 45 do_parse_check(aAttributes, "Missing element attributes");
michael@0 46 this.handleAttributes(aAttributes);
michael@0 47 },
michael@0 48
michael@0 49 endElement: function endElement(aNamespaceURI, aLocalName, aNodeName) {
michael@0 50 do_parse_check(aLocalName, "Missing element local name (endElement)");
michael@0 51 do_parse_check(aNodeName, "Missing element node name (endElement)");
michael@0 52 },
michael@0 53
michael@0 54 inCDataSection: false,
michael@0 55
michael@0 56 characters: function characters(aData) {
michael@0 57 },
michael@0 58
michael@0 59 processingInstruction: function processingInstruction(aTarget, aData) {
michael@0 60 do_parse_check(aTarget, "Missing processing instruction target");
michael@0 61 },
michael@0 62
michael@0 63 ignorableWhitespace: function ignorableWhitespace(aWhitespace) {
michael@0 64 },
michael@0 65
michael@0 66 startPrefixMapping: function startPrefixMapping(aPrefix, aURI) {
michael@0 67 },
michael@0 68
michael@0 69 endPrefixMapping: function endPrefixMapping(aPrefix) {
michael@0 70 }
michael@0 71 };
michael@0 72
michael@0 73 var lexicalHandler = {
michael@0 74 comment: function comment(aContents) {
michael@0 75 },
michael@0 76
michael@0 77 startDTD: function startDTD(aName, aPublicId, aSystemId) {
michael@0 78 do_parse_check(aName, "Missing DTD name");
michael@0 79 },
michael@0 80
michael@0 81 endDTD: function endDTD() {
michael@0 82 },
michael@0 83
michael@0 84 startCDATA: function startCDATA() {
michael@0 85 },
michael@0 86
michael@0 87 endCDATA: function endCDATA() {
michael@0 88 },
michael@0 89
michael@0 90 startEntity: function startEntity(aName) {
michael@0 91 do_parse_check(aName, "Missing entity name (startEntity)");
michael@0 92 },
michael@0 93
michael@0 94 endEntity: function endEntity(aName) {
michael@0 95 do_parse_check(aName, "Missing entity name (endEntity)");
michael@0 96 }
michael@0 97 };
michael@0 98
michael@0 99 var dtdHandler = {
michael@0 100 notationDecl: function notationDecl(aName, aPublicId, aSystemId) {
michael@0 101 do_parse_check(aName, "Missing notation name");
michael@0 102 },
michael@0 103
michael@0 104 unparsedEntityDecl:
michael@0 105 function unparsedEntityDecl(aName, aPublicId, aSystemId, aNotationName) {
michael@0 106 do_parse_check(aName, "Missing entity name (unparsedEntityDecl)");
michael@0 107 }
michael@0 108 };
michael@0 109
michael@0 110 var errorHandler = {
michael@0 111 error: function error(aLocator, aError) {
michael@0 112 do_parse_check(!aError, "XML error");
michael@0 113 },
michael@0 114
michael@0 115 fatalError: function fatalError(aLocator, aError) {
michael@0 116 do_parse_check(!aError, "XML fatal error");
michael@0 117 },
michael@0 118
michael@0 119 ignorableWarning: function ignorableWarning(aLocator, aError) {
michael@0 120 do_parse_check(!aError, "XML ignorable warning");
michael@0 121 }
michael@0 122 };
michael@0 123
michael@0 124 saxReader.contentHandler = contentHandler;
michael@0 125 saxReader.lexicalHandler = lexicalHandler;
michael@0 126 saxReader.dtdHandler = dtdHandler;
michael@0 127 saxReader.errorHandler = errorHandler;
michael@0 128
michael@0 129 saxReader.parseFromString(source, "application/xml");
michael@0 130
michael@0 131 // Just in case it leaks.
michael@0 132 saxReader.contentHandler = null;
michael@0 133 saxReader.lexicalHandler = null;
michael@0 134 saxReader.dtdHandler = null;
michael@0 135 saxReader.errorHandler = null;
michael@0 136
michael@0 137 return parseErrorLog;
michael@0 138 }
michael@0 139
michael@0 140 function do_check_true_with_dump(aCondition, aParseLog) {
michael@0 141 if (!aCondition) {
michael@0 142 dump(aParseLog.join("\n"));
michael@0 143 }
michael@0 144 do_check_true(aCondition);
michael@0 145 }
michael@0 146
michael@0 147 function run_test() {
michael@0 148 var src;
michael@0 149 src = "<!DOCTYPE foo>\n<!-- all your foo are belong to bar -->";
michael@0 150 src += "<foo id='foo'>\n<?foo wooly bully?>\nfoo";
michael@0 151 src += "<![CDATA[foo fighters]]></foo>\n";
michael@0 152 var parseErrorLog = updateDocumentSourceMaps(src);
michael@0 153
michael@0 154 if (parseErrorLog.length > 0) {
michael@0 155 dump(parseErrorLog.join("\n"));
michael@0 156 }
michael@0 157 do_check_true_with_dump(parseErrorLog.length == 0, parseErrorLog);
michael@0 158
michael@0 159 // End tag isn't well-formed.
michael@0 160 src = "<!DOCTYPE foo>\n<!-- all your foo are belong to bar -->";
michael@0 161 src += "<foo id='foo'>\n<?foo wooly bully?>\nfoo";
michael@0 162 src += "<![CDATA[foo fighters]]></foo\n";
michael@0 163
michael@0 164 parseErrorLog = updateDocumentSourceMaps(src);
michael@0 165
michael@0 166 do_check_true_with_dump(parseErrorLog.length == 1 && parseErrorLog[0] == "XML fatal error", parseErrorLog);
michael@0 167 }

mercurial