adoption.window.js (2052B)
1 // Testing DocumentFragment with host separately as it has a different node document by design 2 test(() => { 3 const df = document.createElement("template").content; 4 const child = df.appendChild(new Text('hi')); 5 assert_not_equals(df.ownerDocument, document); 6 const nodeDocument = df.ownerDocument; 7 document.body.appendChild(df); 8 assert_equals(df.childNodes.length, 0); 9 assert_equals(child.ownerDocument, document); 10 assert_equals(df.ownerDocument, nodeDocument); 11 }, `appendChild() and DocumentFragment with host`); 12 13 test(() => { 14 const df = document.createElement("template").content; 15 const child = df.appendChild(new Text('hi')); 16 const nodeDocument = df.ownerDocument; 17 document.adoptNode(df); 18 assert_equals(df.childNodes.length, 1); 19 assert_equals(child.ownerDocument, nodeDocument); 20 assert_equals(df.ownerDocument, nodeDocument); 21 }, `adoptNode() and DocumentFragment with host`); 22 23 [ 24 { 25 "name": "DocumentFragment", 26 "creator": doc => doc.createDocumentFragment() 27 }, 28 { 29 "name": "ShadowRoot", 30 "creator": doc => doc.createElementNS("http://www.w3.org/1999/xhtml", "div").attachShadow({mode: "closed"}) 31 } 32 ].forEach(dfTest => { 33 test(() => { 34 const doc = new Document(); 35 const df = dfTest.creator(doc); 36 const child = df.appendChild(new Text('hi')); 37 assert_equals(df.ownerDocument, doc); 38 39 document.body.appendChild(df); 40 assert_equals(df.childNodes.length, 0); 41 assert_equals(child.ownerDocument, document); 42 assert_equals(df.ownerDocument, doc); 43 }, `appendChild() and ${dfTest.name}`); 44 45 test(() => { 46 const doc = new Document(); 47 const df = dfTest.creator(doc); 48 const child = df.appendChild(new Text('hi')); 49 if (dfTest.name === "ShadowRoot") { 50 assert_throws_dom("HierarchyRequestError", () => document.adoptNode(df)); 51 } else { 52 document.adoptNode(df); 53 assert_equals(df.childNodes.length, 1); 54 assert_equals(child.ownerDocument, document); 55 assert_equals(df.ownerDocument, document); 56 } 57 }, `adoptNode() and ${dfTest.name}`); 58 });