Source code
Revision control
Copy as Markdown
Other Tools
Test Info:
- Manifest: dom/base/test/chrome/chrome.toml
<?xml version="1.0"?>
<window title="Test for the Mozilla extesion of the DOM Parsing and Serialization API"
  <!-- test results are displayed in the html:body -->
  <xul:browser id="test1" type="content" src="data:text/html,content" />
  </body>
  <!-- test code goes here -->
  <script type="application/javascript"><![CDATA[
"use strict";
function throws(fn, type, message) {
  try {
    fn();
    ok(false, message);
  } catch (e) {
    if (type) {
      is(e.name, type, message);
    } else {
      ok(true, message);
    }
  }
}
add_task(function dom_parser_extra_args() {
  // DOMParser constructor should not throw for extra arguments
  new DOMParser(undefined);
  new DOMParser(null);
  new DOMParser(false);
  new DOMParser(0);
  new DOMParser("");
  new DOMParser({});
});
add_task(function xml_serializer_extra_args() {
  // XMLSerializer constructor should not throw for extra arguments
  new XMLSerializer(undefined);
  new XMLSerializer(null);
  new XMLSerializer(false);
  new XMLSerializer(0);
  new XMLSerializer("");
  new XMLSerializer({});
});
add_task(function chrome_window() {
  runTest(window, true);
});
add_task(function content_window() {
  runTest(document.getElementById("test1").contentWindow, false);
});
function runTest(win, expectSystem) {
  let parser = new win.DOMParser();
  let serializer = new win.XMLSerializer();
  let principal = win.document.nodePrincipal;
  is(principal.isSystemPrincipal, expectSystem,
     `expected ${expectSystem ? "system" : "content"} principal`);
  is(typeof parser.parseFromString, "function", "parseFromString should exist");
  is(typeof parser.parseFromBuffer, "function", "parseFromBuffer should exist");
  is(typeof parser.parseFromStream, "function", "parseFromStream should exist");
  is(typeof parser.parseFromSafeString, "function", "parseFromSafeString should exist");
  is(typeof serializer.serializeToString, "function", "serializeToString should exist");
  is(typeof serializer.serializeToStream, "function", "serializeToStream should exist");
  let tests = [
    {input: "<html></html>", type: "text/html",
    {input: "<xml></xml>", type: "text/xml", expected: "<xml/>"},
    {input: "<xml></xml>", type: "application/xml", expected: "<xml/>"},
    {input: "<html></html>", type: "application/xhtml+xml", expected: "<html/>"},
    {input: "<svg></svg>", type: "image/svg+xml", expected: "<svg/>"},
  ];
  for (let t of tests) {
    const fromNormalString = parser.parseFromString(t.input, t.type);
    if (principal.isSystemPrincipal) {
      ok(fromNormalString.nodePrincipal.isNullPrincipal,
         "system principal DOMParser produces a null principal document");
    } else {
      ok(fromNormalString.nodePrincipal === principal,
         "content principal DOMParser produces a document with an object-identical principal");
    }
    const fromSafeString = parser.parseFromSafeString(t.input, t.type);
    ok(fromSafeString.nodePrincipal === principal,
       "DOMParser with parseFromSafeString always produces a document with an object-identical principal");
    is(serializer.serializeToString(parser.parseFromString(t.input, t.type)), t.expected,
       "parseFromString test for " + t.type);
    for (let charset of [null, "UTF-8"]) {
      let pipe = Cc["@mozilla.org/pipe;1"].createInstance(Ci.nsIPipe);
      pipe.init(false, false, 0, 0xffffffff, null);
      let ostream = pipe.outputStream;
      serializer.serializeToStream(parser.parseFromString(t.input, t.type), ostream, charset);
      let istream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(
        Ci.nsIScriptableInputStream
      );
      istream.init(pipe.inputStream);
      let data = istream.read(0xffffffff);
      is(data, t.expected,
         "serializeToStream test for " + t.type + ", charset=" + charset);
    }
    if (t.type === "text/html") {
      // parseFromBuffer and parseFromStream don't support "text/html".
      continue;
    }
    let array = Array.from(t.input, function(c) { return c.charCodeAt(c); });
    let inputs = [
      {array, name: "parseFromBuffer (array)"},
      {array: new Uint8Array(array), name: "parseFromBuffer (Uint8Array)"},
    ];
    for (let input of inputs) {
      let a = input.array;
      is(serializer.serializeToString(parser.parseFromBuffer(a, t.type)), t.expected,
         input.name + " test for " + t.type);
    }
    let istream = Cc["@mozilla.org/io/string-input-stream;1"].
                  createInstance(Ci.nsIStringInputStream);
    for (let charset of [null, "UTF-8"]) {
      istream.setByteStringData(t.input);
      is(serializer.serializeToString(parser.parseFromStream(istream, charset, t.input.length, t.type)),
         t.expected, "parseFromStream test for " + t.type + ", charset=" + charset);
    }
  }
  throws(function() {
    parser.parseFromString("<xml></xml>", "foo/bar");
  }, "TypeError", "parseFromString should throw for the unknown type");
}
  ]]></script>
</window>