head_xml.js (3879B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 const I = Ci; 8 const C = Cc; 9 10 const nsIFile = I.nsIFile; 11 const nsIProperties = I.nsIProperties; 12 const nsIFileInputStream = I.nsIFileInputStream; 13 const nsIInputStream = I.nsIInputStream; 14 15 function getParser() { 16 var parser = new DOMParser(); 17 parser.forceEnableXULXBL(); 18 return parser; 19 } 20 21 var __testsDirectory = null; 22 23 function ParseFile(file) { 24 if (typeof file == "string") { 25 if (!__testsDirectory) { 26 __testsDirectory = do_get_cwd(); 27 } 28 var fileObj = __testsDirectory.clone(); 29 fileObj.append(file); 30 file = fileObj; 31 } 32 33 Assert.equal(file instanceof nsIFile, true); 34 35 var fileStr = 36 C["@mozilla.org/network/file-input-stream;1"].createInstance( 37 nsIFileInputStream 38 ); 39 // Init for readonly reading 40 fileStr.init(file, 0x01, 0o400, nsIFileInputStream.CLOSE_ON_EOF); 41 return ParseXML(fileStr); 42 } 43 44 function ParseXML(data) { 45 if (typeof data == "string") { 46 return getParser().parseFromString(data, "application/xml"); 47 } 48 49 Assert.equal(data instanceof nsIInputStream, true); 50 51 return getParser().parseFromStream( 52 data, 53 "UTF-8", 54 data.available(), 55 "application/xml" 56 ); 57 } 58 59 function DOMSerializer() { 60 return new XMLSerializer(); 61 } 62 63 function SerializeXML(node) { 64 return DOMSerializer().serializeToString(node); 65 } 66 67 function roundtrip(obj) { 68 if (typeof obj == "string") { 69 return SerializeXML(ParseXML(obj)); 70 } 71 72 Assert.equal(Node.isInstance(obj), true); 73 return ParseXML(SerializeXML(obj)); 74 } 75 76 function do_compare_attrs(e1, e2) { 77 const xmlns = "http://www.w3.org/2000/xmlns/"; 78 79 var a1 = e1.attributes; 80 var a2 = e2.attributes; 81 for (var i = 0; i < a1.length; ++i) { 82 var att = a1.item(i); 83 // Don't test for namespace decls, since those can just sorta be 84 // scattered about 85 if (att.namespaceURI != xmlns) { 86 var att2 = a2.getNamedItemNS(att.namespaceURI, att.localName); 87 if (!att2) { 88 do_throw( 89 "Missing attribute with namespaceURI '" + 90 att.namespaceURI + 91 "' and localName '" + 92 att.localName + 93 "'" 94 ); 95 } 96 Assert.equal(att.value, att2.value); 97 } 98 } 99 } 100 101 function do_check_equiv(dom1, dom2) { 102 Assert.equal(dom1.nodeType, dom2.nodeType); 103 switch (dom1.nodeType) { 104 case Node.PROCESSING_INSTRUCTION_NODE: 105 Assert.equal(dom1.target, dom2.target); 106 Assert.equal(dom1.data, dom2.data); 107 // fall through 108 case Node.TEXT_NODE: 109 case Node.CDATA_SECTION_NODE: 110 case Node.COMMENT_NODE: 111 Assert.equal(dom1.data, dom2.data); 112 break; 113 case Node.ELEMENT_NODE: 114 Assert.equal(dom1.namespaceURI, dom2.namespaceURI); 115 Assert.equal(dom1.localName, dom2.localName); 116 // Compare attrs in both directions -- do_compare_attrs does a 117 // subset check. 118 do_compare_attrs(dom1, dom2); 119 do_compare_attrs(dom2, dom1); 120 // Fall through 121 case Node.DOCUMENT_NODE: 122 Assert.equal(dom1.childNodes.length, dom2.childNodes.length); 123 for (var i = 0; i < dom1.childNodes.length; ++i) { 124 do_check_equiv(dom1.childNodes.item(i), dom2.childNodes.item(i)); 125 } 126 break; 127 } 128 } 129 130 function do_check_serialize(dom) { 131 do_check_equiv(dom, roundtrip(dom)); 132 } 133 134 function Pipe() { 135 var p = C["@mozilla.org/pipe;1"].createInstance(I.nsIPipe); 136 p.init(false, false, 0, 0xffffffff, null); 137 return p; 138 } 139 140 function ScriptableInput(arg) { 141 if (arg instanceof I.nsIPipe) { 142 arg = arg.inputStream; 143 } 144 145 var str = C["@mozilla.org/scriptableinputstream;1"].createInstance( 146 I.nsIScriptableInputStream 147 ); 148 149 str.init(arg); 150 151 return str; 152 }