michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const I = Components.interfaces; michael@0: const C = Components.classes; michael@0: michael@0: const nsILocalFile = I.nsILocalFile; michael@0: const nsIProperties = I.nsIProperties; michael@0: const nsIFileInputStream = I.nsIFileInputStream; michael@0: const nsIInputStream = I.nsIInputStream; michael@0: michael@0: const nsIDOMParser = I.nsIDOMParser; michael@0: const nsIDOMSerializer = I.nsIDOMSerializer; michael@0: const nsIDOMDocument = I.nsIDOMDocument; michael@0: const nsIDOMElement = I.nsIDOMElement; michael@0: const nsIDOMNode = I.nsIDOMNode; michael@0: const nsIDOMCharacterData = I.nsIDOMCharacterData; michael@0: const nsIDOMAttr = I.nsIDOMAttr; michael@0: const nsIDOMNodeList = I.nsIDOMNodeList; michael@0: const nsIDOMXULElement = I.nsIDOMXULElement; michael@0: const nsIDOMProcessingInstruction = I.nsIDOMProcessingInstruction; michael@0: michael@0: function DOMParser() { michael@0: var parser = C["@mozilla.org/xmlextras/domparser;1"].createInstance(nsIDOMParser); michael@0: parser.init(); michael@0: return parser; michael@0: } michael@0: michael@0: var __testsDirectory = null; michael@0: michael@0: function ParseFile(file) { michael@0: if (typeof(file) == "string") { michael@0: if (!__testsDirectory) { michael@0: __testsDirectory = do_get_cwd(); michael@0: } michael@0: var fileObj = __testsDirectory.clone(); michael@0: fileObj.append(file); michael@0: file = fileObj; michael@0: } michael@0: michael@0: do_check_eq(file instanceof nsILocalFile, true); michael@0: michael@0: var fileStr = C["@mozilla.org/network/file-input-stream;1"] michael@0: .createInstance(nsIFileInputStream); michael@0: // Init for readonly reading michael@0: fileStr.init(file, 0x01, 0400, nsIFileInputStream.CLOSE_ON_EOF); michael@0: return ParseXML(fileStr); michael@0: } michael@0: michael@0: function ParseXML(data) { michael@0: if (typeof(data) == "string") { michael@0: return DOMParser().parseFromString(data, "application/xml"); michael@0: } michael@0: michael@0: do_check_eq(data instanceof nsIInputStream, true); michael@0: michael@0: return DOMParser().parseFromStream(data, "UTF-8", data.available(), michael@0: "application/xml"); michael@0: } michael@0: michael@0: function DOMSerializer() { michael@0: return C["@mozilla.org/xmlextras/xmlserializer;1"] michael@0: .createInstance(nsIDOMSerializer); michael@0: } michael@0: michael@0: function SerializeXML(node) { michael@0: return DOMSerializer().serializeToString(node); michael@0: } michael@0: michael@0: function roundtrip(obj) { michael@0: if (typeof(obj) == "string") { michael@0: return SerializeXML(ParseXML(obj)); michael@0: } michael@0: michael@0: do_check_eq(obj instanceof nsIDOMNode, true); michael@0: return ParseXML(SerializeXML(obj)); michael@0: } michael@0: michael@0: function do_compare_attrs(e1, e2) { michael@0: const xmlns = "http://www.w3.org/2000/xmlns/"; michael@0: michael@0: var a1 = e1.attributes; michael@0: var a2 = e2.attributes; michael@0: for (var i = 0; i < a1.length; ++i) { michael@0: var att = a1.item(i); michael@0: // Don't test for namespace decls, since those can just sorta be michael@0: // scattered about michael@0: if (att.namespaceURI != xmlns) { michael@0: var att2 = a2.getNamedItemNS(att.namespaceURI, att.localName); michael@0: if (!att2) { michael@0: do_throw("Missing attribute with namespaceURI '" + att.namespaceURI + michael@0: "' and localName '" + att.localName + "'"); michael@0: } michael@0: do_check_eq(att.QueryInterface(nsIDOMAttr).value, michael@0: att2.QueryInterface(nsIDOMAttr).value); michael@0: } michael@0: } michael@0: } michael@0: michael@0: function do_check_equiv(dom1, dom2) { michael@0: do_check_eq(dom1.nodeType, dom2.nodeType); michael@0: // There's no classinfo around, so we'll need to do some QIing to michael@0: // make sure the right interfaces are flattened as needed. michael@0: switch (dom1.nodeType) { michael@0: case nsIDOMNode.PROCESSING_INSTRUCTION_NODE: michael@0: do_check_eq(dom1.QueryInterface(nsIDOMProcessingInstruction).target, michael@0: dom2.QueryInterface(nsIDOMProcessingInstruction).target); michael@0: do_check_eq(dom1.data, dom2.data); michael@0: case nsIDOMNode.TEXT_NODE: michael@0: case nsIDOMNode.CDATA_SECTION_NODE: michael@0: case nsIDOMNode.COMMENT_NODE: michael@0: do_check_eq(dom1.QueryInterface(nsIDOMCharacterData).data, michael@0: dom2.QueryInterface(nsIDOMCharacterData).data); michael@0: break; michael@0: case nsIDOMNode.ELEMENT_NODE: michael@0: do_check_eq(dom1.namespaceURI, dom2.namespaceURI); michael@0: do_check_eq(dom1.localName, dom2.localName); michael@0: // Compare attrs in both directions -- do_compare_attrs does a michael@0: // subset check. michael@0: do_compare_attrs(dom1, dom2); michael@0: do_compare_attrs(dom2, dom1); michael@0: // Fall through michael@0: case nsIDOMNode.DOCUMENT_NODE: michael@0: do_check_eq(dom1.childNodes.length, dom2.childNodes.length); michael@0: for (var i = 0; i < dom1.childNodes.length; ++i) { michael@0: do_check_equiv(dom1.childNodes.item(i), dom2.childNodes.item(i)); michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: michael@0: function do_check_serialize(dom) { michael@0: do_check_equiv(dom, roundtrip(dom)); michael@0: } michael@0: michael@0: function Pipe() { michael@0: var p = C["@mozilla.org/pipe;1"].createInstance(I.nsIPipe); michael@0: p.init(false, false, 0, 0xffffffff, null); michael@0: return p; michael@0: } michael@0: michael@0: function ScriptableInput(arg) { michael@0: if (arg instanceof I.nsIPipe) { michael@0: arg = arg.inputStream; michael@0: } michael@0: michael@0: var str = C["@mozilla.org/scriptableinputstream;1"]. michael@0: createInstance(I.nsIScriptableInputStream); michael@0: michael@0: str.init(arg); michael@0: michael@0: return str; michael@0: }