|
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 /* 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 = Components.interfaces; |
|
8 const C = Components.classes; |
|
9 |
|
10 const nsILocalFile = I.nsILocalFile; |
|
11 const nsIProperties = I.nsIProperties; |
|
12 const nsIFileInputStream = I.nsIFileInputStream; |
|
13 const nsIInputStream = I.nsIInputStream; |
|
14 |
|
15 const nsIDOMParser = I.nsIDOMParser; |
|
16 const nsIDOMSerializer = I.nsIDOMSerializer; |
|
17 const nsIDOMDocument = I.nsIDOMDocument; |
|
18 const nsIDOMElement = I.nsIDOMElement; |
|
19 const nsIDOMNode = I.nsIDOMNode; |
|
20 const nsIDOMCharacterData = I.nsIDOMCharacterData; |
|
21 const nsIDOMAttr = I.nsIDOMAttr; |
|
22 const nsIDOMNodeList = I.nsIDOMNodeList; |
|
23 const nsIDOMXULElement = I.nsIDOMXULElement; |
|
24 const nsIDOMProcessingInstruction = I.nsIDOMProcessingInstruction; |
|
25 |
|
26 function DOMParser() { |
|
27 var parser = C["@mozilla.org/xmlextras/domparser;1"].createInstance(nsIDOMParser); |
|
28 parser.init(); |
|
29 return parser; |
|
30 } |
|
31 |
|
32 var __testsDirectory = null; |
|
33 |
|
34 function ParseFile(file) { |
|
35 if (typeof(file) == "string") { |
|
36 if (!__testsDirectory) { |
|
37 __testsDirectory = do_get_cwd(); |
|
38 } |
|
39 var fileObj = __testsDirectory.clone(); |
|
40 fileObj.append(file); |
|
41 file = fileObj; |
|
42 } |
|
43 |
|
44 do_check_eq(file instanceof nsILocalFile, true); |
|
45 |
|
46 var fileStr = C["@mozilla.org/network/file-input-stream;1"] |
|
47 .createInstance(nsIFileInputStream); |
|
48 // Init for readonly reading |
|
49 fileStr.init(file, 0x01, 0400, nsIFileInputStream.CLOSE_ON_EOF); |
|
50 return ParseXML(fileStr); |
|
51 } |
|
52 |
|
53 function ParseXML(data) { |
|
54 if (typeof(data) == "string") { |
|
55 return DOMParser().parseFromString(data, "application/xml"); |
|
56 } |
|
57 |
|
58 do_check_eq(data instanceof nsIInputStream, true); |
|
59 |
|
60 return DOMParser().parseFromStream(data, "UTF-8", data.available(), |
|
61 "application/xml"); |
|
62 } |
|
63 |
|
64 function DOMSerializer() { |
|
65 return C["@mozilla.org/xmlextras/xmlserializer;1"] |
|
66 .createInstance(nsIDOMSerializer); |
|
67 } |
|
68 |
|
69 function SerializeXML(node) { |
|
70 return DOMSerializer().serializeToString(node); |
|
71 } |
|
72 |
|
73 function roundtrip(obj) { |
|
74 if (typeof(obj) == "string") { |
|
75 return SerializeXML(ParseXML(obj)); |
|
76 } |
|
77 |
|
78 do_check_eq(obj instanceof nsIDOMNode, true); |
|
79 return ParseXML(SerializeXML(obj)); |
|
80 } |
|
81 |
|
82 function do_compare_attrs(e1, e2) { |
|
83 const xmlns = "http://www.w3.org/2000/xmlns/"; |
|
84 |
|
85 var a1 = e1.attributes; |
|
86 var a2 = e2.attributes; |
|
87 for (var i = 0; i < a1.length; ++i) { |
|
88 var att = a1.item(i); |
|
89 // Don't test for namespace decls, since those can just sorta be |
|
90 // scattered about |
|
91 if (att.namespaceURI != xmlns) { |
|
92 var att2 = a2.getNamedItemNS(att.namespaceURI, att.localName); |
|
93 if (!att2) { |
|
94 do_throw("Missing attribute with namespaceURI '" + att.namespaceURI + |
|
95 "' and localName '" + att.localName + "'"); |
|
96 } |
|
97 do_check_eq(att.QueryInterface(nsIDOMAttr).value, |
|
98 att2.QueryInterface(nsIDOMAttr).value); |
|
99 } |
|
100 } |
|
101 } |
|
102 |
|
103 function do_check_equiv(dom1, dom2) { |
|
104 do_check_eq(dom1.nodeType, dom2.nodeType); |
|
105 // There's no classinfo around, so we'll need to do some QIing to |
|
106 // make sure the right interfaces are flattened as needed. |
|
107 switch (dom1.nodeType) { |
|
108 case nsIDOMNode.PROCESSING_INSTRUCTION_NODE: |
|
109 do_check_eq(dom1.QueryInterface(nsIDOMProcessingInstruction).target, |
|
110 dom2.QueryInterface(nsIDOMProcessingInstruction).target); |
|
111 do_check_eq(dom1.data, dom2.data); |
|
112 case nsIDOMNode.TEXT_NODE: |
|
113 case nsIDOMNode.CDATA_SECTION_NODE: |
|
114 case nsIDOMNode.COMMENT_NODE: |
|
115 do_check_eq(dom1.QueryInterface(nsIDOMCharacterData).data, |
|
116 dom2.QueryInterface(nsIDOMCharacterData).data); |
|
117 break; |
|
118 case nsIDOMNode.ELEMENT_NODE: |
|
119 do_check_eq(dom1.namespaceURI, dom2.namespaceURI); |
|
120 do_check_eq(dom1.localName, dom2.localName); |
|
121 // Compare attrs in both directions -- do_compare_attrs does a |
|
122 // subset check. |
|
123 do_compare_attrs(dom1, dom2); |
|
124 do_compare_attrs(dom2, dom1); |
|
125 // Fall through |
|
126 case nsIDOMNode.DOCUMENT_NODE: |
|
127 do_check_eq(dom1.childNodes.length, dom2.childNodes.length); |
|
128 for (var i = 0; i < dom1.childNodes.length; ++i) { |
|
129 do_check_equiv(dom1.childNodes.item(i), dom2.childNodes.item(i)); |
|
130 } |
|
131 break; |
|
132 } |
|
133 } |
|
134 |
|
135 function do_check_serialize(dom) { |
|
136 do_check_equiv(dom, roundtrip(dom)); |
|
137 } |
|
138 |
|
139 function Pipe() { |
|
140 var p = C["@mozilla.org/pipe;1"].createInstance(I.nsIPipe); |
|
141 p.init(false, false, 0, 0xffffffff, null); |
|
142 return p; |
|
143 } |
|
144 |
|
145 function ScriptableInput(arg) { |
|
146 if (arg instanceof I.nsIPipe) { |
|
147 arg = arg.inputStream; |
|
148 } |
|
149 |
|
150 var str = C["@mozilla.org/scriptableinputstream;1"]. |
|
151 createInstance(I.nsIScriptableInputStream); |
|
152 |
|
153 str.init(arg); |
|
154 |
|
155 return str; |
|
156 } |