|
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 function run_test() |
|
7 { |
|
8 /* |
|
9 * NOTE: [i] is not allowed in this test, since it's done via classinfo and |
|
10 * we don't have that in xpcshell; the workaround is item(i). Suck. |
|
11 */ |
|
12 init(); |
|
13 |
|
14 test_element(); |
|
15 |
|
16 // more tests would be nice here (such as for documents), but the primary |
|
17 // uses of Node.normalize() are in test_element; stuff beyond this is either |
|
18 // unimplemented or is unlikely to be used all that much within a browser |
|
19 // DOM implementation |
|
20 } |
|
21 |
|
22 // TEST CODE |
|
23 |
|
24 var doc; // cache for use in all tests |
|
25 |
|
26 function init() |
|
27 { |
|
28 doc = ParseFile("empty_document.xml"); |
|
29 } |
|
30 |
|
31 function test_element() |
|
32 { |
|
33 var x = doc.createElement("funk"); |
|
34 |
|
35 // one empty Text node |
|
36 x.appendChild(doc.createTextNode("")); |
|
37 do_check_eq(x.childNodes.length, 1); |
|
38 |
|
39 x.normalize(); |
|
40 do_check_eq(x.childNodes.length, 0); |
|
41 |
|
42 |
|
43 // multiple empty Text nodes |
|
44 x.appendChild(doc.createTextNode("")); |
|
45 x.appendChild(doc.createTextNode("")); |
|
46 do_check_eq(x.childNodes.length, 2); |
|
47 |
|
48 x.normalize(); |
|
49 do_check_eq(x.childNodes.length, 0); |
|
50 |
|
51 |
|
52 // empty Text node followed by other Text node |
|
53 x.appendChild(doc.createTextNode("")); |
|
54 x.appendChild(doc.createTextNode("Guaraldi")); |
|
55 do_check_eq(x.childNodes.length, 2); |
|
56 |
|
57 x.normalize(); |
|
58 do_check_eq(x.childNodes.length, 1); |
|
59 do_check_eq(x.childNodes.item(0).nodeValue, "Guaraldi"); |
|
60 |
|
61 |
|
62 // Text node followed by empty Text node |
|
63 clearKids(x); |
|
64 x.appendChild(doc.createTextNode("Guaraldi")); |
|
65 x.appendChild(doc.createTextNode("")); |
|
66 do_check_eq(x.childNodes.length, 2); |
|
67 |
|
68 x.normalize(); |
|
69 do_check_eq(x.childNodes.item(0).nodeValue, "Guaraldi"); |
|
70 |
|
71 |
|
72 // Text node followed by empty Text node followed by other Node |
|
73 clearKids(x); |
|
74 x.appendChild(doc.createTextNode("Guaraldi")); |
|
75 x.appendChild(doc.createTextNode("")); |
|
76 x.appendChild(doc.createElement("jazzy")); |
|
77 do_check_eq(x.childNodes.length, 3); |
|
78 |
|
79 x.normalize(); |
|
80 do_check_eq(x.childNodes.length, 2); |
|
81 do_check_eq(x.childNodes.item(0).nodeValue, "Guaraldi"); |
|
82 do_check_eq(x.childNodes.item(1).nodeName, "jazzy"); |
|
83 |
|
84 |
|
85 // Nodes are recursively normalized |
|
86 clearKids(x); |
|
87 var kid = doc.createElement("eit"); |
|
88 kid.appendChild(doc.createTextNode("")); |
|
89 |
|
90 x.appendChild(doc.createTextNode("Guaraldi")); |
|
91 x.appendChild(doc.createTextNode("")); |
|
92 x.appendChild(kid); |
|
93 do_check_eq(x.childNodes.length, 3); |
|
94 do_check_eq(x.childNodes.item(2).childNodes.length, 1); |
|
95 |
|
96 x.normalize(); |
|
97 do_check_eq(x.childNodes.length, 2); |
|
98 do_check_eq(x.childNodes.item(0).nodeValue, "Guaraldi"); |
|
99 do_check_eq(x.childNodes.item(1).childNodes.length, 0); |
|
100 } |
|
101 |
|
102 |
|
103 // UTILITY FUNCTIONS |
|
104 |
|
105 function clearKids(node) |
|
106 { |
|
107 while (node.hasChildNodes()) |
|
108 node.removeChild(node.childNodes.item(0)); |
|
109 } |