1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is Google Inc.
19 * Portions created by the Initial Developer are Copyright (C) 2005
20 * the Initial Developer. All Rights Reserved.
23 * Darin Fisher <darin@meer.net>
24 * Boris Zbarsky <bzbarsky@mit.edu>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 const I = Components.interfaces;
41 const C = Components.classes;
43 const nsILocalFile = I.nsILocalFile;
44 const nsIProperties = I.nsIProperties;
45 const nsIFileInputStream = I.nsIFileInputStream;
46 const nsIInputStream = I.nsIInputStream;
48 const nsIDOMParser = I.nsIDOMParser;
49 const nsIDOMSerializer = I.nsIDOMSerializer;
50 const nsIDOMDocument = I.nsIDOMDocument;
51 const nsIDOMElement = I.nsIDOMElement;
52 const nsIDOMNode = I.nsIDOMNode;
53 const nsIDOM3Node = I.nsIDOM3Node;
54 const nsIDOMCharacterData = I.nsIDOMCharacterData;
55 const nsIDOMAttr = I.nsIDOMAttr;
56 const nsIDOMNodeList = I.nsIDOMNodeList;
57 const nsIDOMXULElement = I.nsIDOMXULElement;
58 const nsIDOMProcessingInstruction = I.nsIDOMProcessingInstruction;
60 function DOMParser() {
61 return C["@mozilla.org/xmlextras/domparser;1"].createInstance(nsIDOMParser);
64 var __testsDirectory = null;
66 function ParseFile(file) {
67 if (typeof(file) == "string") {
68 if (!__testsDirectory) {
69 __testsDirectory = do_get_cwd();
71 var fileObj = __testsDirectory.clone();
76 do_check_eq(file instanceof nsILocalFile, true);
78 var fileStr = C["@mozilla.org/network/file-input-stream;1"]
79 .createInstance(nsIFileInputStream);
80 // Init for readonly reading
81 fileStr.init(file, 0x01, 0400, nsIFileInputStream.CLOSE_ON_EOF);
82 return ParseXML(fileStr);
85 function ParseXML(data) {
86 if (typeof(data) == "string") {
87 return DOMParser().parseFromString(data, "application/xml");
90 do_check_eq(data instanceof nsIInputStream, true);
92 return DOMParser().parseFromStream(data, "UTF-8", data.available(),
96 function DOMSerializer() {
97 return C["@mozilla.org/xmlextras/xmlserializer;1"]
98 .createInstance(nsIDOMSerializer);
101 function SerializeXML(node) {
102 return DOMSerializer().serializeToString(node);
105 function roundtrip(obj) {
106 if (typeof(obj) == "string") {
107 return SerializeXML(ParseXML(obj));
110 do_check_eq(obj instanceof nsIDOMNode, true);
111 return ParseXML(SerializeXML(obj));
114 function do_compare_attrs(e1, e2) {
115 const xmlns = "http://www.w3.org/2000/xmlns/";
117 var a1 = e1.attributes;
118 var a2 = e2.attributes;
119 for (var i = 0; i < a1.length; ++i) {
120 var att = a1.item(i);
121 // Don't test for namespace decls, since those can just sorta be
123 if (att.namespaceURI != xmlns) {
124 var att2 = a2.getNamedItemNS(att.namespaceURI, att.localName);
126 do_throw("Missing attribute with namespaceURI '" + att.namespaceURI +
127 "' and localName '" + att.localName + "'");
129 do_check_eq(att.QueryInterface(nsIDOMAttr).value,
130 att2.QueryInterface(nsIDOMAttr).value);
135 function do_check_equiv(dom1, dom2) {
136 do_check_eq(dom1.nodeType, dom2.nodeType);
137 // There's no classinfo around, so we'll need to do some QIing to
138 // make sure the right interfaces are flattened as needed.
139 switch (dom1.nodeType) {
140 case nsIDOMNode.PROCESSING_INSTRUCTION_NODE:
141 do_check_eq(dom1.QueryInterface(nsIDOMProcessingInstruction).target,
142 dom2.QueryInterface(nsIDOMProcessingInstruction).target);
143 do_check_eq(dom1.data, dom2.data);
144 case nsIDOMNode.TEXT_NODE:
145 case nsIDOMNode.CDATA_SECTION_NODE:
146 case nsIDOMNode.COMMENT_NODE:
147 do_check_eq(dom1.QueryInterface(nsIDOMCharacterData).data,
148 dom2.QueryInterface(nsIDOMCharacterData).data);
150 case nsIDOMNode.ELEMENT_NODE:
151 do_check_eq(dom1.namespaceURI, dom2.namespaceURI);
152 do_check_eq(dom1.localName, dom2.localName);
153 // Compare attrs in both directions -- do_compare_attrs does a
155 do_compare_attrs(dom1, dom2);
156 do_compare_attrs(dom2, dom1);
158 case nsIDOMNode.DOCUMENT_NODE:
159 do_check_eq(dom1.childNodes.length, dom2.childNodes.length);
160 for (var i = 0; i < dom1.childNodes.length; ++i) {
161 do_check_equiv(dom1.childNodes.item(i), dom2.childNodes.item(i));
167 function do_check_serialize(dom) {
168 do_check_equiv(dom, roundtrip(dom));
172 var p = C["@mozilla.org/pipe;1"].createInstance(I.nsIPipe);
173 p.init(false, false, 0, 0xffffffff, null);
177 function ScriptableInput(arg) {
178 if (arg instanceof I.nsIPipe) {
179 arg = arg.inputStream;
182 var str = C["@mozilla.org/scriptableinputstream;1"].
183 createInstance(I.nsIScriptableInputStream);