content/base/test/chrome/test_domparsing.xul

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial