summaryrefslogtreecommitdiffstats
path: root/parser/xml/test/unit/test_xml_declaration.js
blob: 4cad511fb136b9ed41d160a5d1739217b9685205 (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
function noop() {}

function run_test() {
  var evts;

  var contentHandler = {
    attrs: null,
    startDocument: function() {
      evts.push("startDocument");
    },
    endDocument: noop,

    startElement: function startElement() {
      evts.push("startElement");
    },

    endElement: noop,
    characters: noop,
    processingInstruction: noop,
    ignorableWhitespace: noop,
    startPrefixMapping: noop,
    endPrefixMapping: noop
  };

  function XMLDeclHandler(version, encoding, standalone) {
    evts.splice(evts.length, 0, version, encoding, standalone);
  }

  const nsISAXXMLReader = Components.interfaces.nsISAXXMLReader;
  var saxReader = Components.classes["@mozilla.org/saxparser/xmlreader;1"]
                            .createInstance(nsISAXXMLReader);
  saxReader.contentHandler = contentHandler;
  saxReader.declarationHandler = XMLDeclHandler;

  evts = [];
  saxReader.parseFromString("<root/>", "application/xml");
  do_check_eq(evts.length, 2);
  do_check_eq(evts[0], "startDocument");
  do_check_eq(evts[1], "startElement");

  evts = [];
  saxReader.parseFromString("<?xml version='1.0'?><root/>", "application/xml");
  do_check_eq(evts.length, 5);
  do_check_eq(evts[0], "startDocument");
  do_check_eq(evts[1], "1.0");
  do_check_eq(evts[2], "");
  do_check_false(evts[3]);
  do_check_eq(evts[4], "startElement");

  evts = [];
  saxReader.parseFromString("<?xml version='1.0' encoding='UTF-8'?><root/>", "application/xml");
  do_check_eq(evts.length, 5);
  do_check_eq(evts[0], "startDocument");
  do_check_eq(evts[1], "1.0");
  do_check_eq(evts[2], "UTF-8");
  do_check_false(evts[3]);
  do_check_eq(evts[4], "startElement");

  evts = [];
  saxReader.parseFromString("<?xml version='1.0' standalone='yes'?><root/>", "application/xml");
  do_check_eq(evts.length, 5);
  do_check_eq(evts[0], "startDocument");
  do_check_eq(evts[1], "1.0");
  do_check_eq(evts[2], "");
  do_check_true(evts[3]);
  do_check_eq(evts[4], "startElement");

  evts = [];
  saxReader.parseFromString("<?xml version='1.0' encoding='UTF-8' standalone='yes'?><root/>", "application/xml");
  do_check_eq(evts.length, 5);
  do_check_eq(evts[0], "startDocument");
  do_check_eq(evts[1], "1.0");
  do_check_eq(evts[2], "UTF-8");
  do_check_true(evts[3]);
  do_check_eq(evts[4], "startElement");

  evts = [];
  // Not well-formed
  saxReader.parseFromString("<?xml encoding='UTF-8'?><root/>", "application/xml");
  do_check_eq(evts.length, 1);
  do_check_eq(evts[0], "startDocument");  
}