• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src/router/lighttpd-1.4.39/external_file/js/davclient.js/dommer/
1/*
2    tests.js - unit tests for dommer.js
3    Copyright (C) 2004-2005 Guido Wesdorp
4    email johnny@debris.demon.nl
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20    $Id: test_dommer.js,v 1.1.1.1 2011/11/09 06:36:04 sungmin Exp $
21
22*/
23
24load('../minisax.js/minisax.js');
25load('../jsbase/string.js');
26load('../jsbase/array.js');
27load('dommer.js');
28
29var global = this;
30function setup() {
31    global.dom = new dommer.DOM();
32    global.doc = global.dom.createDocument();
33    global.docfrag = global.doc;
34};
35
36function tearDown() {
37    global.doc = null;
38    delete global.dom;
39};
40
41function test_property_access() {
42    var doc = global.doc;
43    var el = doc.createElement('foo');
44    testing.assertThrows(undefined, function() {el.nodeName = 'foo'});
45};
46
47function test_createElement() {
48    var doc = global.doc;
49
50    var el1 = doc.createElement('foo');
51    testing.assertEquals(el1.nodeType, 1);
52    testing.assertEquals(el1.nodeName.toLowerCase(), 'foo');
53    testing.assertEquals(el1.namespaceURI, null);
54    testing.assertEquals(el1.ownerDocument, doc);
55
56    var el2 = doc.createElementNS('foo:', 'bar');
57    testing.assertEquals(el2.nodeType, 1);
58    testing.assertEquals(el2.nodeName.toLowerCase(), 'bar');
59    testing.assertEquals(el2.namespaceURI, 'foo:');
60    testing.assertEquals(el2.ownerDocument, doc);
61
62    var attr1 = doc.createAttribute('foo');
63    testing.assertEquals(attr1.nodeType, 2);
64    testing.assertEquals(attr1.nodeName, 'foo');
65    testing.assertEquals(attr1.namespaceURI, null);
66};
67
68function test_appendChild() {
69    var doc = global.doc;
70    var docfrag = global.docfrag;
71
72    var el = doc.createElement('foo');
73    docfrag.appendChild(el);
74    if (docfrag == doc) {
75        // not for browser dom
76        testing.assertEquals(doc.documentElement, el);
77    };
78    testing.assertEquals(el.ownerDocument, doc);
79    testing.assertEquals(el.parentNode, docfrag);
80    testing.assertEquals(el.firstChild, null);
81    testing.assertEquals(el.lastChild, null);
82    testing.assertEquals(el.previousSibling, null);
83    testing.assertEquals(el.nextSibling, null);
84    testing.assertEquals(docfrag.childNodes[0], el);
85
86    var el2 = doc.createElement('foo');
87    if (doc == docfrag) {
88        testing.assertThrows(undefined, doc.appendChild, doc, el2);
89    };
90    el.appendChild(el2);
91    testing.assertEquals(el2.parentNode, el);
92    testing.assertEquals(el.childNodes[0], el2);
93    testing.assertEquals(el.firstChild, el2);
94    testing.assertEquals(el.lastChild, el2);
95    testing.assertEquals(el2.previousSibling, null);
96    testing.assertEquals(el2.nextSibling, null);
97
98    var el3 = doc.createElement('foo');
99    el.appendChild(el3);
100    testing.assertEquals(el3.parentNode, el);
101    testing.assertEquals(el.childNodes[1], el3);
102    testing.assertEquals(el.firstChild, el2);
103    testing.assertEquals(el.lastChild, el3);
104    testing.assertEquals(el2.previousSibling, null);
105    testing.assertEquals(el2.nextSibling, el3);
106    testing.assertEquals(el3.previousSibling, el2);
107    testing.assertEquals(el3.nextSibling, null);
108};
109
110function test_removeChild() {
111    var doc = global.doc;
112    var docfrag = global.docfrag;
113
114    var el = doc.createElement('foo');
115    docfrag.appendChild(el);
116
117    var el2 = doc.createElement('bar');
118    testing.assertEquals(el2.parentNode, null);
119    testing.assertEquals(el.firstChild, null);
120    testing.assertEquals(el.lastChild, null);
121    if (doc == docfrag) {
122        testing.assertThrows(dommer.DOMException, el.removeChild, el, el2);
123    };
124
125    el.appendChild(el2);
126    testing.assertEquals(el2.parentNode, el);
127    testing.assertEquals(el.firstChild, el2);
128    testing.assertEquals(el.lastChild, el2);
129    testing.assertEquals(el.childNodes[0], el2);
130
131    el3 = doc.createElement('baz');
132    el.appendChild(el3);
133    testing.assertEquals(el3.parentNode, el);
134    testing.assertEquals(el.firstChild, el2);
135    testing.assertEquals(el.lastChild, el3);
136    testing.assertEquals(el2.nextSibling, el3);
137    testing.assertEquals(el3.previousSibling, el2);
138
139    el.removeChild(el2);
140    testing.assertEquals(el2.parentNode, null);
141    testing.assertEquals(el.firstChild, el3);
142    testing.assertEquals(el.lastChild, el3);
143    testing.assertEquals(el.childNodes[0], el3);
144    testing.assertEquals(el2.nextSibling, null);
145    testing.assertEquals(el3.previousSibling, null);
146};
147
148function test_replaceChild() {
149    var doc = global.doc;
150    var docfrag = global.docfrag;
151
152    var el = doc.createElement('foo');
153    docfrag.appendChild(el);
154
155    var child1 = doc.createElement('bar');
156    var child2 = doc.createElement('baz');
157    var child3 = doc.createElement('qux');
158    el.appendChild(child1);
159
160    testing.assertEquals(el.childNodes.length, 1);
161    testing.assertEquals(el.childNodes[0], child1);
162    testing.assertEquals(child1.parentNode, el);
163
164    el.appendChild(child3);
165    testing.assertEquals(el.firstChild, child1);
166    testing.assertEquals(el.lastChild, child3);
167    testing.assertEquals(child1.nextSibling, child3);
168    testing.assertEquals(child3.previousSibling, child1);
169
170    el.replaceChild(child2, child1);
171    testing.assertEquals(child1.parentNode, null);
172    testing.assertEquals(el.childNodes.length, 2);
173    testing.assertEquals(el.firstChild, child2);
174    testing.assertEquals(el.lastChild, child3);
175    testing.assertEquals(child2.parentNode, el);
176    testing.assertEquals(child1.nextSibling, null);
177    testing.assertEquals(child2.nextSibling, child3);
178    testing.assertEquals(child3.previousSibling, child2);
179};
180
181function test_getAttribute() {
182    var doc = global.doc;
183
184    var el = doc.createElement('foo');
185    el.setAttribute('bar', 'baz');
186    testing.assertFalse(el.getAttribute('foo'));
187    var attr = el.getAttribute('bar');
188    testing.assertEquals(el.getAttribute('bar'), 'baz');
189    var attr = el.getAttributeNode('bar');
190    testing.assertEquals(attr.nodeValue, 'baz');
191    testing.assertEquals(attr.nodeType, 2);
192    testing.assertEquals(attr.namespaceURI, null);
193    testing.assertEquals(attr.ownerDocument, doc);
194    testing.assertEquals(attr.parentNode, null);
195    testing.assertEquals(attr.ownerElement, el);
196
197    el.setAttributeNS('foo:', 'bar', 'baz');
198    var attr = el.getAttributeNodeNS('foo:', 'bar');
199    testing.assertEquals(attr.nodeValue, 'baz');
200    testing.assertEquals(attr.namespaceURI, 'foo:');
201};
202
203function test_setAttribute() {
204    var doc = global.doc;
205
206    var el = doc.createElement('foo');
207    testing.assertThrows(undefined, el.setAttribute, el, 'foo&bar');
208};
209
210function test_toXML() {
211    var doc = global.doc;
212
213    var foo = doc.createElement('foo');
214    testing.assertEquals(foo.toXML(), '<foo />');
215
216    foo.setAttribute('bar', 'baz');
217    testing.assertEquals(foo.getAttributeNode('bar').toXML(), 'bar="baz"');
218    testing.assertEquals(foo.toXML(), '<foo bar="baz" />');
219
220    var parent = doc.createElement('parent');
221    parent.appendChild(foo);
222    testing.assertEquals(parent.toXML(), '<parent><foo bar="baz" /></parent>');
223
224    doc.appendChild(parent);
225    testing.assertEquals(doc.toXML(), '<parent><foo bar="baz" /></parent>');
226    // using DOM.toXML() adds an XML declaration
227    testing.assertEquals(global.dom.toXML(global.doc),
228            '<?xml version="1.0"?>\n<parent><foo bar="baz" /></parent>');
229
230    var elwithns = doc.createElementNS('foo:', 'bar');
231    parent.appendChild(elwithns);
232    testing.assertEquals(elwithns.toXML(), '<bar xmlns="foo:" />');
233    testing.assertEquals(doc.toXML(), '<parent><foo bar="baz" />' +
234                                    '<bar xmlns="foo:" /></parent>');
235
236    var el1 = doc.createElementNS('foo:', 'bar');
237    el1.setPrefix('foo');
238    var el2 = doc.createElementNS('foo:', 'baz');
239    el2.setPrefix('foo');
240    el1.appendChild(el2);
241    testing.assertEquals(el1.toXML(),
242        '<foo:bar xmlns:foo="foo:"><foo:baz /></foo:bar>');
243
244    var el1 = doc.createElementNS('foo:', 'bar');
245    el1.setPrefix('foo');
246    var el2 = doc.createElementNS('baz:', 'qux');
247    el2.setPrefix('foo');
248    el1.appendChild(el2);
249    testing.assertEquals(el1.toXML(),
250        '<foo:bar xmlns:foo="foo:"><foo:qux xmlns:foo="baz:" /></foo:bar>');
251
252    var el1 = doc.createElementNS('foo:', 'foo:bar');
253    el1.setAttributeNS('baz:', 'baz:qux', 'quux');
254    testing.assertEquals(el1.toXML(),
255            '<foo:bar baz:qux="quux" xmlns:foo="foo:" xmlns:baz="baz:" />');
256};
257
258function test_namedNodeMap() {
259    var nodemap = new dommer.NamedNodeMap();
260
261    var nodes = {};
262    for (var i=0; i < 10; i++) {
263        var node = global.doc.createElement('node-' + i);
264        nodes[i] = node;
265        nodemap.setNamedItem(node);
266    };
267
268    testing.assertEquals(nodemap.length, 10);
269    testing.assertEquals(nodemap[0].nodeName.toLowerCase(), 'node-0');
270    testing.assertEquals(nodemap[0], nodemap.getNamedItem('node-0'));
271    testing.assertEquals(nodemap[nodemap.length - 1],
272                         nodemap.getNamedItem('node-9'));
273
274    nodemap.removeNamedItem(nodemap[4]);
275    testing.assertEquals(nodemap.length, 9);
276    testing.assertEquals(nodemap[3], nodes[3]);
277    testing.assertEquals(nodemap[4], nodes[5]);
278    testing.assertEquals(nodemap[8], nodes[9]);
279};
280
281function test_cloneNode() {
282    var doc = global.doc;
283
284    var foo = doc.createElement('foo');
285    foo.setAttribute('bar', 'bar');
286    var baz = doc.createElement('baz');
287    baz.appendChild(foo);
288
289    var clone = baz.cloneNode(false);
290    testing.assertEquals(clone.nodeName.toLowerCase(), 'baz');
291    testing.assertFalse(clone.hasChildNodes());
292
293    var clone = baz.cloneNode(true);
294    testing.assertEquals(clone.nodeName.toLowerCase(), 'baz');
295    testing.assert(clone.hasChildNodes());
296    testing.assertEquals(clone.childNodes.length, 1);
297    testing.assertEquals(clone.childNodes[0].nodeName.toLowerCase(), 'foo');
298    testing.assertEquals(clone.childNodes[0].attributes.length, 1);
299    testing.assertEquals(
300        clone.childNodes[0].attributes[0].nodeName.toLowerCase(), 'bar');
301    testing.assertEquals(clone.childNodes[0].getAttribute('bar'), 'bar');
302};
303
304function test_parseXML() {
305    var dom = new dommer.DOM();
306
307    var xml1 = '<foo />';
308    var doc = dom.parseXML(xml1);
309    testing.assertEquals(doc.documentElement.nodeName, 'foo');
310    testing.assertEquals(doc.childNodes.length, 1);
311    testing.assertEquals(doc.documentElement.childNodes.length, 0);
312
313    var xml2 = '<foo><bar /></foo>';
314    var doc = dom.parseXML(xml2);
315    testing.assertEquals(doc.documentElement.nodeName, 'foo');
316    testing.assertEquals(doc.documentElement.childNodes.length, 1);
317    testing.assertEquals(doc.documentElement.childNodes[0].nodeName, 'bar');
318
319    var xml3 = '<foo><bar baz="baz" /></foo>';
320    var doc = dom.parseXML(xml3);
321    var bar = doc.documentElement.childNodes[0];
322    testing.assertEquals(bar.attributes.length, 1);
323    testing.assertEquals(bar.attributes[0].nodeName, 'baz');
324    testing.assertEquals(bar.attributes[0].nodeValue, 'baz');
325
326    var xml4 = '<foo xmlns="bar:" />';
327    var doc = dom.parseXML(xml4);
328    testing.assertEquals(doc.documentElement.namespaceURI, 'bar:');
329    testing.assertEquals(doc.documentElement.prefix, null);
330
331    var xml5 = '<foo:bar xmlns:foo="baz:" />';
332    var doc = dom.parseXML(xml5);
333    testing.assertEquals(doc.documentElement.nodeName, 'foo:bar');
334    testing.assertEquals(doc.documentElement.localName, 'bar');
335    testing.assertEquals(doc.documentElement.prefix, 'foo');
336    testing.assertEquals(doc.documentElement.namespaceURI, 'baz:');
337
338    var xml6 = '<foo>bar</foo>';
339    var doc = dom.parseXML(xml6);
340    testing.assertEquals(doc.documentElement.childNodes.length, 1);
341    testing.assertEquals(doc.documentElement.firstChild.nodeType, 3);
342    testing.assertEquals(doc.documentElement.firstChild.nodeValue, 'bar');
343
344    var xml7 = '<foo xmlns="foo:" xmlns:bar="bar:" bar:baz="qux" />';
345    var doc = dom.parseXML(xml7);
346    testing.assertEquals(doc.documentElement.attributes.length, 1);
347    testing.assertEquals(doc.documentElement.attributes[0].nodeName,
348                         'bar:baz');
349    testing.assertEquals(doc.documentElement.attributes[0].localName, 'baz');
350    testing.assertEquals(doc.documentElement.attributes[0].nodeValue, 'qux');
351    testing.assertEquals(doc.documentElement.attributes[0].namespaceURI,
352                         'bar:');
353    testing.assertEquals(doc.documentElement.attributes[0].prefix, 'bar');
354
355    var xml8 = '<foo:bar xmlns:foo="foo:"><foo:baz xmlns:foo="bar:" />' +
356               '</foo:bar>';
357    var doc = dom.parseXML(xml8);
358    testing.assertEquals(doc.toXML(), xml8);
359
360    var xml9 = '<foo:bar xmlns:foo="foo:"><bar:baz xmlns:bar="foo:" />' +
361               '</foo:bar>';
362    var expected = '<foo:bar xmlns:foo="foo:"><foo:baz /></foo:bar>';
363    var doc = dom.parseXML(xml9);
364    testing.assertEquals(doc.toXML(), expected);
365};
366
367