pre-insertion-validation-notfound.js (3969B)
1 function getNonParentNodes() { 2 return [ 3 document.implementation.createDocumentType("html", "", ""), 4 document.createTextNode("text"), 5 document.implementation.createDocument(null, "foo", null).createProcessingInstruction("foo", "bar"), 6 document.createComment("comment"), 7 document.implementation.createDocument(null, "foo", null).createCDATASection("data"), 8 ]; 9 } 10 11 function getNonInsertableNodes() { 12 return [ 13 document.implementation.createHTMLDocument("title") 14 ]; 15 } 16 17 function getNonDocumentParentNodes() { 18 return [ 19 document.createElement("div"), 20 document.createDocumentFragment(), 21 ]; 22 } 23 24 // Test that the steps happen in the right order, to the extent that it's 25 // observable. The variable names "parent", "child", and "node" match the 26 // corresponding variables in the replaceChild algorithm in these tests. 27 28 // Step 1 happens before step 3. 29 test(function() { 30 var illegalParents = getNonParentNodes(); 31 var child = document.createElement("div"); 32 var node = document.createElement("div"); 33 illegalParents.forEach(function (parent) { 34 assert_throws_dom("HierarchyRequestError", function() { 35 insertFunc.call(parent, node, child); 36 }); 37 }); 38 }, "Should check the 'parent' type before checking whether 'child' is a child of 'parent'"); 39 40 // Step 2 happens before step 3. 41 test(function() { 42 var parent = document.createElement("div"); 43 var child = document.createElement("div"); 44 var node = document.createElement("div"); 45 46 node.appendChild(parent); 47 assert_throws_dom("HierarchyRequestError", function() { 48 insertFunc.call(parent, node, child); 49 }); 50 }, "Should check that 'node' is not an ancestor of 'parent' before checking whether 'child' is a child of 'parent'"); 51 52 // Step 3 happens before step 4. 53 test(function() { 54 var parent = document.createElement("div"); 55 var child = document.createElement("div"); 56 57 var illegalChildren = getNonInsertableNodes(); 58 illegalChildren.forEach(function (node) { 59 assert_throws_dom("NotFoundError", function() { 60 insertFunc.call(parent, node, child); 61 }); 62 }); 63 }, "Should check whether 'child' is a child of 'parent' before checking whether 'node' is of a type that can have a parent."); 64 65 66 // Step 3 happens before step 5. 67 test(function() { 68 var child = document.createElement("div"); 69 70 var node = document.createTextNode(""); 71 var parent = document.implementation.createDocument(null, "foo", null); 72 assert_throws_dom("NotFoundError", function() { 73 insertFunc.call(parent, node, child); 74 }); 75 76 node = document.implementation.createDocumentType("html", "", ""); 77 getNonDocumentParentNodes().forEach(function (parent) { 78 assert_throws_dom("NotFoundError", function() { 79 insertFunc.call(parent, node, child); 80 }); 81 }); 82 }, "Should check whether 'child' is a child of 'parent' before checking whether 'node' is of a type that can have a parent of the type that 'parent' is."); 83 84 // Step 3 happens before step 6. 85 test(function() { 86 var child = document.createElement("div"); 87 var parent = document.implementation.createDocument(null, null, null); 88 89 var node = document.createDocumentFragment(); 90 node.appendChild(document.createElement("div")); 91 node.appendChild(document.createElement("div")); 92 assert_throws_dom("NotFoundError", function() { 93 insertFunc.call(parent, node, child); 94 }); 95 96 node = document.createElement("div"); 97 parent.appendChild(document.createElement("div")); 98 assert_throws_dom("NotFoundError", function() { 99 insertFunc.call(parent, node, child); 100 }); 101 102 parent.firstChild.remove(); 103 parent.appendChild(document.implementation.createDocumentType("html", "", "")); 104 node = document.implementation.createDocumentType("html", "", "") 105 assert_throws_dom("NotFoundError", function() { 106 insertFunc.call(parent, node, child); 107 }); 108 }, "Should check whether 'child' is a child of 'parent' before checking whether 'node' can be inserted into the document given the kids the document has right now.");