Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: third_party/WebKit/LayoutTests/imported/web-platform-tests/dom/nodes/Node-replaceChild.html

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

Powered by Google App Engine
This is Rietveld 408576698