HTMLCollection-live-mutations.window.js (2386B)
1 function testHTMLCollection(name, hooks) { 2 test(() => { 3 const nodes = { 4 root: document.createElement("div"), 5 div1: document.createElement("div"), 6 div2: document.createElement("div"), 7 p: document.createElement("p") 8 }; 9 10 nodes.div1.id = "div1"; 11 nodes.div2.id = "div2"; 12 13 const list = nodes.root.getElementsByTagName("div"); 14 15 hooks.initial(list, nodes); 16 17 nodes.root.appendChild(nodes.div1); 18 nodes.root.appendChild(nodes.p); 19 nodes.root.appendChild(nodes.div2); 20 21 hooks.afterInsertion(list, nodes); 22 23 nodes.root.removeChild(nodes.div1); 24 25 hooks.afterRemoval(list, nodes); 26 }, `HTMLCollection live mutations: ${name}`); 27 } 28 29 testHTMLCollection("HTMLCollection.length", { 30 initial(list) { 31 assert_equals(list.length, 0); 32 }, 33 afterInsertion(list) { 34 assert_equals(list.length, 2); 35 }, 36 afterRemoval(list) { 37 assert_equals(list.length, 1); 38 } 39 }); 40 41 testHTMLCollection("HTMLCollection.item(index)", { 42 initial(list) { 43 assert_equals(list.item(0), null); 44 }, 45 afterInsertion(list, nodes) { 46 assert_equals(list.item(0), nodes.div1); 47 assert_equals(list.item(1), nodes.div2); 48 }, 49 afterRemoval(list, nodes) { 50 assert_equals(list.item(0), nodes.div2); 51 } 52 }); 53 54 testHTMLCollection("HTMLCollection[index]", { 55 initial(list) { 56 assert_equals(list[0], undefined); 57 }, 58 afterInsertion(list, nodes) { 59 assert_equals(list[0], nodes.div1); 60 assert_equals(list[1], nodes.div2); 61 }, 62 afterRemoval(list, nodes) { 63 assert_equals(list[0], nodes.div2); 64 } 65 }); 66 67 testHTMLCollection("HTMLCollection.namedItem(index)", { 68 initial(list) { 69 assert_equals(list.namedItem("div1"), null); 70 assert_equals(list.namedItem("div2"), null); 71 }, 72 afterInsertion(list, nodes) { 73 assert_equals(list.namedItem("div1"), nodes.div1); 74 assert_equals(list.namedItem("div2"), nodes.div2); 75 }, 76 afterRemoval(list, nodes) { 77 assert_equals(list.namedItem("div1"), null); 78 assert_equals(list.namedItem("div2"), nodes.div2); 79 } 80 }); 81 82 testHTMLCollection("HTMLCollection ownPropertyNames", { 83 initial(list) { 84 assert_object_equals(Object.getOwnPropertyNames(list), []); 85 }, 86 afterInsertion(list) { 87 assert_object_equals(Object.getOwnPropertyNames(list), ["0", "1", "div1", "div2"]); 88 }, 89 afterRemoval(list) { 90 assert_object_equals(Object.getOwnPropertyNames(list), ["0", "div2"]); 91 } 92 });