summaryrefslogtreecommitdiffstats
path: root/parser/xml/test/unit/test_parser.js
blob: 79c32bae480277aeb1ac570b745e8f2751a0c9f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
function updateDocumentSourceMaps(source) {
  const nsIDOMNode = Components.interfaces.nsIDOMNode;

  const nsISAXXMLReader = Components.interfaces.nsISAXXMLReader;
  const saxReader = Components.classes["@mozilla.org/saxparser/xmlreader;1"]
                              .createInstance(nsISAXXMLReader);
  try {
    saxReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
    saxReader.setFeature("http://xml.org/sax/features/namespace", true);
  }
  catch (e) {
    // do nothing, we'll accept it as it is.
  }
  var parseErrorLog = [];

  /* XXX ajvincent Because throwing an exception doesn't stop parsing, we need
   * to record errors and handle them after the parsing is finished.
   */
  function do_parse_check(aCondition, aMsg) {
    if (!aCondition)
      parseErrorLog[parseErrorLog.length] = aMsg;
  }

  var contentHandler = {
    startDocument: function startDocument() {
    },

    endDocument: function endDocument() {
    },

    handleAttributes: function handleAttributes(aAttributes) {
      for (var i = 0; i < aAttributes.length; i++) {
        var attrNamespaceURI = aAttributes.getURI(i);
        var attrLocalName = aAttributes.getLocalName(i);
        var attrNodeName = aAttributes.getQName(i);
        var value = aAttributes.getValue(i);
        do_parse_check(attrLocalName, "Missing attribute local name");
        do_parse_check(attrNodeName, "Missing attribute node name");
      }
    },

    startElement: function startElement(aNamespaceURI, aLocalName, aNodeName, aAttributes) {
      do_parse_check(aLocalName, "Missing element local name (startElement)");
      do_parse_check(aNodeName, "Missing element node name (startElement)");
      do_parse_check(aAttributes, "Missing element attributes");
      this.handleAttributes(aAttributes);
    },

    endElement: function endElement(aNamespaceURI, aLocalName, aNodeName) {
      do_parse_check(aLocalName, "Missing element local name (endElement)");
      do_parse_check(aNodeName, "Missing element node name (endElement)");
    },

    inCDataSection: false,

    characters: function characters(aData) {
    },

    processingInstruction: function processingInstruction(aTarget, aData) {
      do_parse_check(aTarget, "Missing processing instruction target");
    },

    ignorableWhitespace: function ignorableWhitespace(aWhitespace) {
    },

    startPrefixMapping: function startPrefixMapping(aPrefix, aURI) {
    },

    endPrefixMapping: function endPrefixMapping(aPrefix) {
    }
  };

  var lexicalHandler = {
    comment: function comment(aContents) {
    },

    startDTD: function startDTD(aName, aPublicId, aSystemId) {
      do_parse_check(aName, "Missing DTD name");
    },

    endDTD: function endDTD() {
    },

    startCDATA: function startCDATA() {
    },

    endCDATA: function endCDATA() {
    },

    startEntity: function startEntity(aName) {
      do_parse_check(aName, "Missing entity name (startEntity)");
    },

    endEntity: function endEntity(aName) {
      do_parse_check(aName, "Missing entity name (endEntity)");
    }
  };

  var dtdHandler = {
    notationDecl: function notationDecl(aName, aPublicId, aSystemId) {
      do_parse_check(aName, "Missing notation name");
    },

    unparsedEntityDecl:
    function unparsedEntityDecl(aName, aPublicId, aSystemId, aNotationName) {
      do_parse_check(aName, "Missing entity name (unparsedEntityDecl)");
    }
  };

  var errorHandler = {
    error: function error(aLocator, aError) {
      do_parse_check(!aError, "XML error");
    },

    fatalError: function fatalError(aLocator, aError) {
      do_parse_check(!aError, "XML fatal error");
    },

    ignorableWarning: function ignorableWarning(aLocator, aError) {
      do_parse_check(!aError, "XML ignorable warning");
    }
  };

  saxReader.contentHandler = contentHandler;
  saxReader.lexicalHandler = lexicalHandler;
  saxReader.dtdHandler     = dtdHandler;
  saxReader.errorHandler   = errorHandler;

  saxReader.parseFromString(source, "application/xml");

  // Just in case it leaks.
  saxReader.contentHandler = null;
  saxReader.lexicalHandler = null;
  saxReader.dtdHandler     = null;
  saxReader.errorHandler   = null;

  return parseErrorLog;
}

function do_check_true_with_dump(aCondition, aParseLog) {
  if (!aCondition) {
    dump(aParseLog.join("\n"));
  }
  do_check_true(aCondition);
}

function run_test() {
  var src;
  src = "<!DOCTYPE foo>\n<!-- all your foo are belong to bar -->";
  src += "<foo id='foo'>\n<?foo wooly bully?>\nfoo";
  src += "<![CDATA[foo fighters]]></foo>\n";
  var parseErrorLog = updateDocumentSourceMaps(src);

  if (parseErrorLog.length > 0) {
    dump(parseErrorLog.join("\n"));
  }
  do_check_true_with_dump(parseErrorLog.length == 0, parseErrorLog);

  // End tag isn't well-formed.
  src = "<!DOCTYPE foo>\n<!-- all your foo are belong to bar -->";
  src += "<foo id='foo'>\n<?foo wooly bully?>\nfoo";
  src += "<![CDATA[foo fighters]]></foo\n";

  parseErrorLog = updateDocumentSourceMaps(src);

  do_check_true_with_dump(parseErrorLog.length == 1 && parseErrorLog[0] == "XML fatal error", parseErrorLog);
}