tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_domparsing.xhtml (5620B)


      1 <?xml version="1.0"?>
      2 <?xml-stylesheet href="chrome://global/skin" type="text/css"?>
      3 <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
      4 <window title="Test for the Mozilla extesion of the DOM Parsing and Serialization API"
      5  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
      6  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
      7 
      8  <!-- test results are displayed in the html:body -->
      9  <body xmlns="http://www.w3.org/1999/xhtml"
     10        xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
     11  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=816410"
     12     target="_blank">Mozilla Bug 816410</a>
     13  <xul:browser id="test1" type="content" src="data:text/html,content" />
     14  </body>
     15 
     16  <!-- test code goes here -->
     17  <script type="application/javascript"><![CDATA[
     18 "use strict";
     19 /** Test for Bug 816410 */
     20 
     21 function throws(fn, type, message) {
     22  try {
     23    fn();
     24    ok(false, message);
     25  } catch (e) {
     26    if (type) {
     27      is(e.name, type, message);
     28    } else {
     29      ok(true, message);
     30    }
     31  }
     32 }
     33 
     34 add_task(function dom_parser_extra_args() {
     35  // DOMParser constructor should not throw for extra arguments
     36  new DOMParser(undefined);
     37  new DOMParser(null);
     38  new DOMParser(false);
     39  new DOMParser(0);
     40  new DOMParser("");
     41  new DOMParser({});
     42 });
     43 
     44 add_task(function xml_serializer_extra_args() {
     45  // XMLSerializer constructor should not throw for extra arguments
     46  new XMLSerializer(undefined);
     47  new XMLSerializer(null);
     48  new XMLSerializer(false);
     49  new XMLSerializer(0);
     50  new XMLSerializer("");
     51  new XMLSerializer({});
     52 });
     53 
     54 add_task(function chrome_window() {
     55  runTest(window, true);
     56 });
     57 
     58 add_task(function content_window() {
     59  runTest(document.getElementById("test1").contentWindow, false);
     60 });
     61 
     62 function runTest(win, expectSystem) {
     63  let parser = new win.DOMParser();
     64  let serializer = new win.XMLSerializer();
     65  let principal = win.document.nodePrincipal;
     66  is(principal.isSystemPrincipal, expectSystem,
     67     `expected ${expectSystem ? "system" : "content"} principal`);
     68 
     69  is(typeof parser.parseFromString, "function", "parseFromString should exist");
     70  is(typeof parser.parseFromBuffer, "function", "parseFromBuffer should exist");
     71  is(typeof parser.parseFromStream, "function", "parseFromStream should exist");
     72  is(typeof parser.parseFromSafeString, "function", "parseFromSafeString should exist");
     73 
     74  is(typeof serializer.serializeToString, "function", "serializeToString should exist");
     75  is(typeof serializer.serializeToStream, "function", "serializeToStream should exist");
     76 
     77  let tests = [
     78    {input: "<html></html>", type: "text/html",
     79     expected: '<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>'},
     80    {input: "<xml></xml>", type: "text/xml", expected: "<xml/>"},
     81    {input: "<xml></xml>", type: "application/xml", expected: "<xml/>"},
     82    {input: "<html></html>", type: "application/xhtml+xml", expected: "<html/>"},
     83    {input: "<svg></svg>", type: "image/svg+xml", expected: "<svg/>"},
     84  ];
     85  for (let t of tests) {
     86    const fromNormalString = parser.parseFromString(t.input, t.type);
     87    if (principal.isSystemPrincipal) {
     88      ok(fromNormalString.nodePrincipal.isNullPrincipal,
     89         "system principal DOMParser produces a null principal document");
     90    } else {
     91      ok(fromNormalString.nodePrincipal === principal,
     92         "content principal DOMParser produces a document with an object-identical principal");
     93    }
     94 
     95    const fromSafeString = parser.parseFromSafeString(t.input, t.type);
     96    ok(fromSafeString.nodePrincipal === principal,
     97       "DOMParser with parseFromSafeString always produces a document with an object-identical principal");
     98 
     99    is(serializer.serializeToString(parser.parseFromString(t.input, t.type)), t.expected,
    100       "parseFromString test for " + t.type);
    101 
    102    for (let charset of [null, "UTF-8"]) {
    103      let pipe = Cc["@mozilla.org/pipe;1"].createInstance(Ci.nsIPipe);
    104      pipe.init(false, false, 0, 0xffffffff, null);
    105      let ostream = pipe.outputStream;
    106      serializer.serializeToStream(parser.parseFromString(t.input, t.type), ostream, charset);
    107      let istream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(
    108        Ci.nsIScriptableInputStream
    109      );
    110      istream.init(pipe.inputStream);
    111      let data = istream.read(0xffffffff);
    112      is(data, t.expected,
    113         "serializeToStream test for " + t.type + ", charset=" + charset);
    114    }
    115 
    116    if (t.type === "text/html") {
    117      // parseFromBuffer and parseFromStream don't support "text/html".
    118      continue;
    119    }
    120 
    121    let array = Array.from(t.input, function(c) { return c.charCodeAt(c); });
    122    let inputs = [
    123      {array, name: "parseFromBuffer (array)"},
    124      {array: new Uint8Array(array), name: "parseFromBuffer (Uint8Array)"},
    125    ];
    126    for (let input of inputs) {
    127      let a = input.array;
    128      is(serializer.serializeToString(parser.parseFromBuffer(a, t.type)), t.expected,
    129         input.name + " test for " + t.type);
    130    }
    131 
    132    let istream = Cc["@mozilla.org/io/string-input-stream;1"].
    133                  createInstance(Ci.nsIStringInputStream);
    134    for (let charset of [null, "UTF-8"]) {
    135      istream.setByteStringData(t.input);
    136      is(serializer.serializeToString(parser.parseFromStream(istream, charset, t.input.length, t.type)),
    137         t.expected, "parseFromStream test for " + t.type + ", charset=" + charset);
    138    }
    139  }
    140  throws(function() {
    141    parser.parseFromString("<xml></xml>", "foo/bar");
    142  }, "TypeError", "parseFromString should throw for the unknown type");
    143 }
    144  ]]></script>
    145 </window>