1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
<!DOCTYPE html>
<meta charset=utf-8>
<title>Node.replaceChild</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body><a><b></b><c></c></a>
<div id="log"></div>
<script>
// IDL.
test(function() {
var a = document.createElement("div");
assert_throws(new TypeError(), function() {
a.replaceChild(null, null);
});
var b = document.createElement("div");
assert_throws(new TypeError(), function() {
a.replaceChild(b, null);
});
assert_throws(new TypeError(), function() {
a.replaceChild(null, b);
});
}, "Passing null to replaceChild should throw a TypeError.")
// Step 1.
test(function() {
var a = document.createElement("div");
var b = document.createElement("div");
var c = document.createElement("div");
assert_throws("NotFoundError", function() {
a.replaceChild(b, c);
});
var d = document.createElement("div");
d.appendChild(b);
assert_throws("NotFoundError", function() {
a.replaceChild(b, c);
});
assert_throws("NotFoundError", function() {
a.replaceChild(b, a);
});
}, "If child's parent is not the context node, a NotFoundError exception should be thrown")
test(function() {
var nodes = [
document.implementation.createDocumentType("html", "", ""),
document.createTextNode("text"),
document.implementation.createDocument(null, "foo", null).createProcessingInstruction("foo", "bar"),
document.createComment("comment")
];
var a = document.createElement("div");
var b = document.createElement("div");
nodes.forEach(function(node) {
assert_throws("HierarchyRequestError", function() {
node.replaceChild(a, b);
});
});
}, "If the context node is not a node that can contain children, a NotFoundError exception should be thrown")
// Step 2.
test(function() {
var a = document.createElement("div");
var b = document.createElement("div");
assert_throws("HierarchyRequestError", function() {
a.replaceChild(a, a);
});
a.appendChild(b);
assert_throws("HierarchyRequestError", function() {
a.replaceChild(a, b);
});
var c = document.createElement("div");
c.appendChild(a);
assert_throws("HierarchyRequestError", function() {
a.replaceChild(c, b);
});
}, "If node is an inclusive ancestor of the context node, a HierarchyRequestError should be thrown.")
// Step 3.1.
test(function() {
var doc = document.implementation.createHTMLDocument("title");
var doc2 = document.implementation.createHTMLDocument("title2");
assert_throws("HierarchyRequestError", function() {
doc.replaceChild(doc2, doc.documentElement);
});
assert_throws("HierarchyRequestError", function() {
doc.replaceChild(doc.createTextNode("text"), doc.documentElement);
});
}, "If the context node is a document, inserting a document or text node should throw a HierarchyRequestError.")
// Step 3.2.1.
test(function() {
var doc = document.implementation.createHTMLDocument("title");
var df = doc.createDocumentFragment();
df.appendChild(doc.createElement("a"));
df.appendChild(doc.createElement("b"));
assert_throws("HierarchyRequestError", function() {
doc.replaceChild(df, doc.documentElement);
});
df = doc.createDocumentFragment();
df.appendChild(doc.createTextNode("text"));
assert_throws("HierarchyRequestError", function() {
doc.replaceChild(df, doc.documentElement);
});
df = doc.createDocumentFragment();
df.appendChild(doc.createComment("comment"));
df.appendChild(doc.createTextNode("text"));
assert_throws("HierarchyRequestError", function() {
doc.replaceChild(df, doc.documentElement);
});
}, "If the context node is a document, inserting a DocumentFragment that contains a text node or too many elements should throw a HierarchyRequestError.")
test(function() {
var doc = document.implementation.createHTMLDocument("title");
doc.removeChild(doc.documentElement);
var df = doc.createDocumentFragment();
df.appendChild(doc.createElement("a"));
df.appendChild(doc.createElement("b"));
assert_throws("HierarchyRequestError", function() {
doc.replaceChild(df, doc.doctype);
});
}, "If the context node is a document (without element children), inserting a DocumentFragment that contains multiple elements should throw a HierarchyRequestError.")
// Step 3.2.2.
test(function() {
// The context node has an element child that is not /child/.
var doc = document.implementation.createHTMLDocument("title");
var comment = doc.appendChild(doc.createComment("foo"));
assert_array_equals(doc.childNodes, [doc.doctype, doc.documentElement, comment]);
var df = doc.createDocumentFragment();
df.appendChild(doc.createElement("a"));
assert_throws("HierarchyRequestError", function() {
doc.replaceChild(df, comment);
});
assert_throws("HierarchyRequestError", function() {
doc.replaceChild(df, doc.doctype);
});
}, "If the context node is a document, inserting a DocumentFragment with an element if there already is an element child should throw a HierarchyRequestError.")
test(function() {
// A doctype is following /child/.
var doc = document.implementation.createHTMLDocument("title");
var comment = doc.insertBefore(doc.createComment("foo"), doc.firstChild);
doc.removeChild(doc.documentElement);
assert_array_equals(doc.childNodes, [comment, doc.doctype]);
var df = doc.createDocumentFragment();
df.appendChild(doc.createElement("a"));
assert_throws("HierarchyRequestError", function() {
doc.replaceChild(df, comment);
});
}, "If the context node is a document, inserting a DocumentFragment with an element before the doctype should throw a HierarchyRequestError.")
// Step 3.3.
test(function() {
var doc = document.implementation.createHTMLDocument("title");
var comment = doc.appendChild(doc.createComment("foo"));
assert_array_equals(doc.childNodes, [doc.doctype, doc.documentElement, comment]);
var a = doc.createElement("a");
assert_throws("HierarchyRequestError", function() {
doc.replaceChild(a, comment);
});
assert_throws("HierarchyRequestError", function() {
doc.replaceChild(a, doc.doctype);
});
}, "If the context node is a document, inserting an element if there already is an element child should throw a HierarchyRequestError.")
test(function() {
var doc = document.implementation.createHTMLDocument("title");
var comment = doc.insertBefore(doc.createComment("foo"), doc.firstChild);
doc.removeChild(doc.documentElement);
assert_array_equals(doc.childNodes, [comment, doc.doctype]);
var a = doc.createElement("a");
assert_throws("HierarchyRequestError", function() {
doc.replaceChild(a, comment);
});
}, "If the context node is a document, inserting an element before the doctype should throw a HierarchyRequestError.")
// Step 3.4.
test(function() {
var doc = document.implementation.createHTMLDocument("title");
var comment = doc.insertBefore(doc.createComment("foo"), doc.firstChild);
assert_array_equals(doc.childNodes, [comment, doc.doctype, doc.documentElement]);
var doctype = document.implementation.createDocumentType("html", "", "");
assert_throws("HierarchyRequestError", function() {
doc.replaceChild(doctype, comment);
});
assert_throws("HierarchyRequestError", function() {
doc.replaceChild(doctype, doc.documentElement);
});
}, "If the context node is a document, inserting a doctype if there already is a doctype child should throw a HierarchyRequestError.")
test(function() {
var doc = document.implementation.createHTMLDocument("title");
var comment = doc.appendChild(doc.createComment("foo"));
doc.removeChild(doc.doctype);
assert_array_equals(doc.childNodes, [doc.documentElement, comment]);
var doctype = document.implementation.createDocumentType("html", "", "");
assert_throws("HierarchyRequestError", function() {
doc.replaceChild(doctype, comment);
});
}, "If the context node is a document, inserting a doctype after the document element should throw a HierarchyRequestError.")
// Step 4.
test(function() {
var df = document.createDocumentFragment();
var a = df.appendChild(document.createElement("a"));
var doc = document.implementation.createHTMLDocument("title");
assert_throws("HierarchyRequestError", function() {
df.replaceChild(doc, a);
});
var doctype = document.implementation.createDocumentType("html", "", "");
assert_throws("HierarchyRequestError", function() {
df.replaceChild(doctype, a);
});
}, "If the context node is a DocumentFragment, inserting a document or a doctype should throw a HierarchyRequestError.")
test(function() {
var el = document.createElement("div");
var a = el.appendChild(document.createElement("a"));
var doc = document.implementation.createHTMLDocument("title");
assert_throws("HierarchyRequestError", function() {
el.replaceChild(doc, a);
});
var doctype = document.implementation.createDocumentType("html", "", "");
assert_throws("HierarchyRequestError", function() {
el.replaceChild(doctype, a);
});
}, "If the context node is an element, inserting a document or a doctype should throw a HierarchyRequestError.")
// Step 6.
test(function() {
var a = document.createElement("div");
var b = document.createElement("div");
var c = document.createElement("div");
a.appendChild(b);
a.appendChild(c);
assert_array_equals(a.childNodes, [b, c]);
assert_equals(a.replaceChild(c, b), b);
assert_array_equals(a.childNodes, [c]);
}, "Replacing a node with its next sibling should work (2 children)");
test(function() {
var a = document.createElement("div");
var b = document.createElement("div");
var c = document.createElement("div");
var d = document.createElement("div");
var e = document.createElement("div");
a.appendChild(b);
a.appendChild(c);
a.appendChild(d);
a.appendChild(e);
assert_array_equals(a.childNodes, [b, c, d, e]);
assert_equals(a.replaceChild(d, c), c);
assert_array_equals(a.childNodes, [b, d, e]);
}, "Replacing a node with its next sibling should work (4 children)");
test(function() {
var a = document.createElement("div");
var b = document.createElement("div");
var c = document.createElement("div");
a.appendChild(b);
a.appendChild(c);
assert_array_equals(a.childNodes, [b, c]);
assert_equals(a.replaceChild(b, b), b);
assert_array_equals(a.childNodes, [b, c]);
assert_equals(a.replaceChild(c, c), c);
assert_array_equals(a.childNodes, [b, c]);
}, "Replacing a node with itself should not move the node");
// Step 7.
test(function() {
var doc = document.implementation.createHTMLDocument("title");
var doctype = doc.doctype;
assert_array_equals(doc.childNodes, [doctype, doc.documentElement]);
var doc2 = document.implementation.createHTMLDocument("title2");
var doctype2 = doc2.doctype;
assert_array_equals(doc2.childNodes, [doctype2, doc2.documentElement]);
doc.replaceChild(doc2.doctype, doc.doctype);
assert_array_equals(doc.childNodes, [doctype2, doc.documentElement]);
assert_array_equals(doc2.childNodes, [doc2.documentElement]);
assert_equals(doctype.parentNode, null);
assert_equals(doctype.ownerDocument, doc);
assert_equals(doctype2.parentNode, doc);
assert_equals(doctype2.ownerDocument, doc);
}, "If the context node is a document, inserting a new doctype should work.")
// Bugs.
test(function() {
var doc = document.implementation.createHTMLDocument("title");
var df = doc.createDocumentFragment();
var a = df.appendChild(doc.createElement("a"));
assert_equals(doc.documentElement, doc.replaceChild(df, doc.documentElement));
assert_array_equals(doc.childNodes, [doc.doctype, a]);
}, "Replacing the document element with a DocumentFragment containing a single element should work.");
test(function() {
var doc = document.implementation.createHTMLDocument("title");
var df = doc.createDocumentFragment();
var a = df.appendChild(doc.createComment("a"));
var b = df.appendChild(doc.createElement("b"));
var c = df.appendChild(doc.createComment("c"));
assert_equals(doc.documentElement, doc.replaceChild(df, doc.documentElement));
assert_array_equals(doc.childNodes, [doc.doctype, a, b, c]);
}, "Replacing the document element with a DocumentFragment containing a single element and comments should work.");
test(function() {
var doc = document.implementation.createHTMLDocument("title");
var a = doc.createElement("a");
assert_equals(doc.documentElement, doc.replaceChild(a, doc.documentElement));
assert_array_equals(doc.childNodes, [doc.doctype, a]);
}, "Replacing the document element with a single element should work.");
test(function() {
document.addEventListener("DOMNodeRemoved", function(e) {
document.body.appendChild(document.createElement("x"));
}, false);
var a = document.body.firstChild, b = a.firstChild, c = b.nextSibling;
assert_equals(a.replaceChild(c, b), b);
assert_equals(b.parentNode, null);
assert_equals(a.firstChild, c);
assert_equals(c.parentNode, a);
}, "replaceChild should work in the presence of mutation events.")
test(function() {
var TEST_ID = "findme";
var gBody = document.getElementsByTagName("body")[0];
var parent = document.createElement("div");
gBody.appendChild(parent);
var child = document.createElement("div");
parent.appendChild(child);
var df = document.createDocumentFragment();
var fragChild = df.appendChild(document.createElement("div"));
fragChild.setAttribute("id", TEST_ID);
parent.replaceChild(df, child);
assert_equals(document.getElementById(TEST_ID), fragChild, "should not be null");
}, "Replacing an element with a DocumentFragment should allow a child of the DocumentFragment to be found by Id.")
</script>
|